C++实现数学向量计算

这个代码实现了数学向量的一些基本运算,多维向量正在研发中,请期待一下(^_-)

这是头文件:

#pragma once
#ifndef _mathvector
#define _mathvector
#include<cmath>
#include<tuple>
#include<iostream>
using namespace std;
template<class type>
class mathvector{
public:
	type begin,end;
	typedef unsigned long long size_type;
	mathvector(){
	}
	mathvector(type b,type e){
		begin=b;
		end=e;
	}
	mathvector(type a[2]){
		begin=a[0];
		end=a[1];
	}
	mathvector(type a[],size_type begin_plase,size_type end_plase){
		begin=a[begin_plase];
		end=a[end_plase];
	}
	friend istream& operator>>(istream& in,mathvector &n){
		in>>n.begin>>n.end;
		return in;
	}
	friend ostream& operator<<(ostream& out,const mathvector n){
		out<<'['<<n.begin<<','<<n.end<<']';
		return out;
	}
	mathvector operator+(const mathvector b)const{
		return mathvector({begin+b.begin,end+b.end});
	}
	mathvector operator-(const mathvector b)const{
		return mathvector({begin-b.begin,end-b.end});
	}
	template<class t>
	mathvector operator*(const t number)const{
		return mathvector(this->begin*number,this->end*number);
	}
	template<class t>
	friend const mathvector operator*(const t number,const mathvector b){
		return mathvector(b.begin*number,b.end*number);
	}
	tuple<type,type,type,char> operator^(const mathvector b)const{
		return tuple<type,type,type,char>(0,0,begin*b.end-end*b.begin,'v');
	}
	type operator*(const mathvector b)const{
		return begin*b.begin+end*b.end;
	}
	template<class t>
	mathvector operator/(const t number)const{
		return mathvector(this->begin/number,this->end/number);
	}
};
ostream& operator<<(ostream& out,const tuple<int,int,int,char> n){
	if(get<3>(n)=='v'){
		out<<'['<<0<<','<<0<<','<<get<2>(n)<<']';
	}
	return out;
}
template<class type>
long double abs(mathvector<type> a){
	return sqrt((long double)(a.begin*a.begin)+(long double)(a.end*a.end));
}
template<class type>
long double angle(mathvector<type> a,mathvector<type> b){
	long double x=(a*b)/(abs(a)*abs(b));
	auto radian_to_angle=[&](double rad){
	    double flag = (rad < 0)? -1.0 : 1.0;
	    if(rad<0)
	    {
			rad = rad * (-1.0);
	    }
		double result = (rad*180)/3.1415926535;		
		return flag * result;
	};
	return radian_to_angle(__builtin_acos(x));
}
template<class type>
mathvector<long double> unitvector(mathvector<type> v){
	long double number=1.0/abs(v);
	return mathvector<long double>(v.begin*number,v.end*number);
}
#endif

测试样例:

#include<bits/stdc++.h>
#include"mathvector.h" //刚编好的头文件
using namespace std;
int main(){
	mathvector<int> a,b;
	cin>>a>>b;
	cout<<a<<endl<<b<<endl<<a+b/*向量和*/
<<endl<<a-b/*向量差*/
<<endl<<2*a/*向量数乘*/
<<endl<<(a^b)/*向量叉乘*/
<<endl<<a*b/*向量内乘*/
<<endl<<a/2/*向量数除*/
<<endl<<angle(a,b)/*向量夹角*/
<<endl<<abs(a)/*向量模*/
<<endl<<unitvector(a)/*单位向量*/;
	return 0;
}

输入:

1 3
3 1

输出:

[1,3]
[3,1]
[4,4]
[-2,2]
[2,6]
[0,0,-8]
6
[0,1]
53.1301
3.16228
[0.316228,0.948683]

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值