⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vtr.cpp

📁 自己写的矩阵向量库
💻 CPP
字号:
#include <iostream.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>

//#ifndef __VTR_h__
//#define __VTR_h__


class Vtr
{
private:
	int lenth;
	double* ets;
public:
	Vtr(int, double*);
	Vtr(int =0, double d=0);
	Vtr(const Vtr&);
	~Vtr(){delete[] ets;}

	int size() const {return lenth;}
	Vtr& operator=(const Vtr&);
	Vtr& operator+=(const Vtr&);
	Vtr& operator-=(const Vtr&);
	double onenorm() const;
	double maxnorm() const;
	double twonorm() const;
	double& operator[](int i) const {return ets[i];}

    friend Vtr operator+(const Vtr&);
	friend Vtr operator-(const Vtr&);
	friend Vtr operator+(const Vtr&, const Vtr&);
	friend Vtr operator-(const Vtr&, const Vtr&);
	friend Vtr operator*(double, const Vtr&);
	friend Vtr operator*(const Vtr&, double);
	friend Vtr operator/(const Vtr&, const Vtr&);
	friend Vtr operator*(const Vtr&, const Vtr&);
	
	friend double dot(const Vtr&, const Vtr&);
//    friend double dot(double* a, double* b, int n);
//	friend double dot(complex<double>* a, complex<double>* b, int n);
	
		
	friend ostream& operator<<(ostream&, const Vtr&);

};
/*
double Vtr::onenorm(const double* const v, int size)
{
	if(size>maxsize) cout << "vector size too large. \n";
	double norm=fabs(v[0]);
	for(int i=1; i<size; i++) norm += fabs(v[i]);
	return norm;
}

double Vtr::maxnorm(const double* const v, int size)
{
	if(size>maxsize) cout << "vector size too large. \n";
    double norm=fabs(v[0]);
    for(int i=1; i<size; i++) norm =max(norm,fabs(v[i]));
	return norm;
}

double Vtr::twonorm(const double* const v, int size)
{
	if(size>maxsize) cout << "vector size too large. \n";
	double norm=v[0]*v[0];
	for(int i=1; i<size; i++) norm += v[i]*v[i];
	return sqrt(norm);
}
*/
inline void error(char* v)
{
	cout << v << ".program exited \n";
	exit(1);
}

Vtr::Vtr(int n, double* abd)
{
	ets=new double [lenth=n];
	for(int i=0; i<lenth; i++) ets[i]=*(abd+i);
}

Vtr::Vtr(int n, double a)
{
	ets=new double [lenth=n];
	for(int i=0; i<lenth; i++) ets[i]=a;
}

Vtr::Vtr(const Vtr& v)
{
	ets=new double [lenth=v.lenth];
	for(int i=0; i<lenth; i++) ets[i]=v[i];
}

Vtr& Vtr::operator =(const Vtr& v)
{
	if(this != &v)
	{
		if(lenth != v.lenth) error("bad vector sizes");
		for(int i=0; i<lenth; i++) ets[i]-=v[i];
	}
	return *this;
}

Vtr& Vtr::operator +=(const Vtr& v)
{
	if(this != &v)
	{
		if(lenth != v.lenth) error("bad vector sizes");
		for(int i=0; i<lenth; i++) ets[i]+=v[i];
	}
	return *this;
}

Vtr& Vtr::operator -=(const Vtr& v)
{
	if(this != &v)
	{
		if(lenth != v.lenth) error("bad vector sizes");
		for(int i=0; i<lenth; i++) ets[i]-=v[i];
	}
	return *this;
}

inline Vtr operator+(const Vtr& v)
{
	return v;
}

inline Vtr operator-(const Vtr& v)
{
	return Vtr(v.lenth-v);
}

Vtr operator+(const Vtr& v1, const Vtr& v2)
{
	if(v1.lenth != v2.lenth) error("bad vector sizes");
	Vtr sum = v1;
	sum+=v2;
	return sum;
}

Vtr operator-(const Vtr& v1, const Vtr& v2)
{
	if(v1.lenth != v2.lenth) error("bad vector sizes");
	Vtr sum = v1;
	sum -= v2;
	return sum;
}

Vtr operator*(double scalar, const Vtr& v)
{
	Vtr tm(v.lenth);
	for(int i=0; i<v.lenth;i++) tm[i]=scalar*v[i];
	return tm;
}

inline Vtr operator*(const Vtr& v, double scalar)
{
	return scalar*v;
}

Vtr operator*(const Vtr& v1, const Vtr& v2)
{
	if(v1.lenth != v2.lenth) error("bad vector sizes");
    int n=v1.lenth;
	Vtr tm(n);
	for(int i=0; i,n; i++) tm[i]=v1[i]*v2[i];
	return tm;
}

Vtr operator/(const Vtr& v, double scalar)
{
	if(!scalar)
		error("disvision by zero in vector-scalar division");
	return (1.0/scalar)*v;
}

double Vtr::twonorm() const 
{
	double norm=ets[0]*ets[0];
	for(int i=1; i<lenth; i++) norm += ets[i]*ets[i];
	return sqrt(norm);
}

double Vtr::maxnorm() const 
{
	double norm=fabs(ets[0]);
	for(int i=1; i<lenth; i++) 
		if(norm<fabs(ets[i])) norm = fabs(ets[i]);
//			norm = max(norm,fabs(ets[i]));
	return norm;
}

double Vtr::onenorm() const 
{
	double norm=fabs(ets[0]);
	for(int i=1; i<lenth; i++) 
		 norm += fabs(ets[i]);
	return norm;
}

double dot(const Vtr& v1, const Vtr& v2)
{
	if(v1.lenth != v2.lenth) error("bad vector sizes");
	double tm = v1[0]*v2[0];
	for(int i=1; i<=v1.lenth; i++) tm+=v1[i]*v2[i];
	return tm;
}
/*
double dot(double* a, double* b, int n) {
	double init = 0;
	for(int i=0; i<n; i++) init += *a++ * *b++;
	return init;
}

double dot(complex<double>* a, complex<double>* b, int n) {
	complex<double>* init=0;
	for(int i=0; i<n; i++) init += *a++ * *b++;
	return init;
}
*/
ostream& operator<<(ostream& s, const Vtr& v)
{
	for(int i=0; i<v.lenth;i++)
	{
		s << v[i] << " ";
		if(i%10 == 9) s << "\n";
	}
	return s;
}

//#endif

void main()
{
//	using namespace std;
	double* x=new double [10];
	for(int i=0;i<10;i++)
		x[i]=rand()%100;
	Vtr V(10,x);
	Vtr V1(V);
	Vtr C=V1+V;
	

	cout<<C<<endl;
	cout << V1<<endl;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -