c++矩阵类

#include<iostream>
using namespace std;
//定义矩阵类
class Matrix
{
public:
	float Mat[20][20]={0};
	void get_mat();//矩阵输入函数
	void put_mat();//矩阵输出函数
	Matrix operator+(Matrix &B);//运算符重载,矩阵加法 
	Matrix operator-(Matrix &B);//运算符重载,矩阵减法 
	Matrix operator*(Matrix &B);//运算符重载,矩阵乘法 
	//Matrix mat_eye(int n);
	Matrix mat_and(Matrix &B);//矩阵合并 
	Matrix mat_div(Matrix &B);//Ax=B的解,矩阵左除 
	Matrix mat_inv();//矩阵的逆,Ax=E的解,通过左除实现 
	Matrix mat_tra();//矩阵转置 
	Matrix operator/(Matrix &B);//运算符重载,矩阵右除。xA=B的解 
	int row=0,col=0;//行列
};
//矩阵输入函数
void Matrix::get_mat()
{
	cout<<"please enter the matrix:"<<endl;
	char c;
	cin>>Mat[row][col];
	while((c=getchar())!='\n')
	{	
		if(c==';'){
			row++;
			col=-1;
		}
		cin>>Mat[row][++col];
		
	}
	row++;
	col++;
}
//矩阵输出函数
void Matrix::put_mat()
{
	int i,j;
	//cout<<"call put"<<endl;
	for(i=0;i<row;i++){
		for(j=0;j<col;j++){
			cout<<Mat[i][j]<<' ';
		}
		cout<<endl;
	}
}
//定义矩阵相加函数
Matrix Matrix::operator+(Matrix &B)
{
	Matrix C;
	if(col!=B.col||row!=B.row)
	{
		cout<<"error: The number of rows or columns of two matrices is not equal"<<endl;
		return C;
	}
	int i,j;
	for(i=0;i<row;i++){
		for(j=0;j<col;j++){
			C.Mat[i][j]=Mat[i][j]+B.Mat[i][j];
		}
	}
	C.col=col;
	C.row=row;
	return C;
} 
//定义矩阵相减函数 
Matrix Matrix::operator-(Matrix &B)
{
	Matrix C;
	if(col!=B.col||row!=B.row)
	{
		cout<<"error: The number of rows or columns of two matrices is not equal"<<endl;
		return C;
	}
	int i,j;
	for(i=0;i<row;i++){
		for(j=0;j<col;j++){
			C.Mat[i][j]=Mat[i][j]-B.Mat[i][j];
		}
	}
	C.col=col;
	C.row=row;
	return C;
}
//定义矩阵相乘函数,返回结果矩阵
Matrix Matrix::operator*(Matrix &B)
{
	Matrix C;
	if(col!=B.row){
		cout<<"error: The number of rows and columns doesn't correspond"<<endl;
		return C;
	}
	int i,j,a;
	for(i=0;i<row;i++){
		for(j=0;j<B.col;j++){
			for(a=0;a<col;a++){
				C.Mat[i][j]+=Mat[i][a]*B.Mat[a][j];
			}
		}
	}
	C.row=row;
	C.col=B.col;
	return C;
}
//生成n阶单位矩阵 
Matrix mat_eye(int n)
{
	Matrix C;
	int i,j;
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			if(i==j){
				C.Mat[i][i]=1;
			}
			else{
				C.Mat[i][j]=0;
			}
		}
	}
	C.row=n;
	C.col=n;
	return C;
}
//A与B矩阵的合并(A,B)
Matrix Matrix::mat_and(Matrix &B)
{
	//cout<<"call and"<<endl; 
	Matrix C;
	int i,j,r;
	//cout<<A.row<<" "<<A.col<<endl;
	//cout<<B.row<<" "<<B.col<<endl;
	for(i=0;i<row;i++)
	{
		for(j=0;j<row+B.col;j++)
		{
			if(j<row)
			{
				//cout<<"call"<<endl;
				C.Mat[i][j]=Mat[i][j];
				if(j>=col&&i==j)
				{
					C.Mat[i][j]=1;
				}
			}
			else
			{
				C.Mat[i][j]=B.Mat[i][j-row];
			}
		}
	}
	C.row=row;
	C.col=row+B.col;
	//C.put_mat();
	return C;
}

//定义矩阵除法,使用LU分解
Matrix Matrix::mat_div(Matrix &B)
{
	Matrix C,X;
	if(row!=B.row||row<col)
	{
		cout<<"error: The number of rows and columns doesn't correspond"<<endl;
		return X;
	}
	C=Matrix::mat_and(B);
	int r,i,k,n,m,j;
	n=C.row;
	m=C.col;
	//cout<<m<<endl;
	//cout<<n<<endl;
	float eps=0.0001,par;
	//LU分解
	for(r=0;r<n;r++)
	{
		//计算U
		for(i=r;i<m;i++)
		{
			for(k=0;k<=r-1;k++)
			{
				C.Mat[r][i]-=C.Mat[r][k]*C.Mat[k][i];
			}
		}
		if((C.Mat[r][r]>0?C.Mat[r][r]:-C.Mat[r][r])<eps)
		{
			cout<<"error:Doolittle decomposition failed"<<endl;
			return X;
		}
		//计算L
		if(r<n-1)
		{
			for(i=r+1;i<n;i++)
			{
				par=0;
				for(k=0;k<=r-1;k++)
				{
					par+=C.Mat[i][k]*C.Mat[k][r];
				}
				C.Mat[i][r]=(C.Mat[i][r]-par)/C.Mat[r][r];
			}
		}
	}
	//cout<<"***"<<endl;
	//C.put_mat();
	//解方程Ux=y:
	for(j=0;j<B.col;j++)
	{
		X.Mat[n-1][j]=C.Mat[n-1][n+j]/C.Mat[n-1][n-1];
		for(k=n-2;k>=0;k--)
		{
			par=0;
			for(i=k+1;i<n;i++)
			{
				par+=C.Mat[k][i]*X.Mat[i][j];
			}
			X.Mat[k][j]=(C.Mat[k][n+j]-par)/C.Mat[k][k];
		}
	}
	X.row=col;
	X.col=B.col;
	return X;
}
//定义求逆矩阵函数
#if 1
Matrix Matrix::mat_inv()
{
	Matrix B,C;
	C=mat_eye(row);
	B=mat_div(C);
	B.row=row;
	return B;
}
#endif 
//定义转置矩阵函数
Matrix Matrix::mat_tra()
{
	Matrix C;
	int i,j;
	for(i=0;i<row;i++){
		for(j=0;j<col;j++){
			C.Mat[j][i]=Mat[i][j];
		}
	}
	C.row=col;
	C.col=row;
	return C;
} 
//定义矩阵除法(矩阵右除)
#if 1
Matrix Matrix::operator/(Matrix &B)
{
	Matrix C,A,D;
	int i,j;
	for(i=0;i<row;i++){
		for(j=0;j<col;j++){
			A.Mat[i][j]=Mat[i][j];
		}
	}
	A.col=col;
	A.row=row;
	D=B.mat_inv();
	C=A*D;
	return C;
} 
#endif
int main()
{
	Matrix a,b,c,d,e,f,g,h,x,m;
	cout<<"get a:"<<endl;
	a.get_mat();
	cout<<"get b:"<<endl;
	b.get_mat();
	cout<<"put a:"<<endl;
	a.put_mat();
	cout<<"put b:"<<endl;
	b.put_mat();
	cout<<"a.row,a.col"<<endl;
	cout<<a.row<<" "<<a.col<<endl;
	cout<<"b.row,b.col"<<endl;
	cout<<b.row<<" "<<b.col<<endl;
	system("pause");
	cout<<"c=a*b="<<endl;
	c=a*b;
	c.put_mat();
	system("pause");
	cout<<"d=a+b="<<endl;
	d=a+b;
	d.put_mat();
	system("pause");
	cout<<"e=a-b="<<endl;
	e=a-b;
	e.put_mat();
	cout<<"----"<<endl;
	system("pause");
	cout<<"x=a.mat_div(b)="<<endl;
	x=a.mat_div(b);
	x.put_mat();
	system("pause");
	cout<<"f=mat_eye(10)="<<endl;
	f=mat_eye(10);
	f.put_mat();
	system("pause");
	cout<<"a.mat_inv()="<<endl;
	g=a.mat_inv();
	g.put_mat();
	system("pause");
	cout<<"a.mat_tra()"<<endl;
	h=a.mat_tra();
	h.put_mat();
	system("pause");
	cout<<"m=c/b="<<endl;
	m=c/b;
	m.put_mat();
	system("pause"); 
	return 0;
} 

更多推荐