c++矩阵类

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;
} 
  • 19
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的 C++ 矩阵实现矩阵相乘的示例代码: ```c++ #include <iostream> #include <vector> using namespace std; class Matrix { private: vector<vector<int>> data; // 存储矩阵的二维向量 int m, n; // 矩阵的行数和列数 public: // 构造函数 Matrix(int rows, int cols) { m = rows; n = cols; data.resize(m, vector<int>(n, 0)); // 初始化矩阵为全零矩阵 } // 矩阵相乘运算符重载 Matrix operator*(const Matrix& other) { if (n != other.m) { cout << "矩阵维度不匹配,无法相乘" << endl; return Matrix(0, 0); } Matrix result(m, other.n); for (int i = 0; i < m; i++) { for (int j = 0; j < other.n; j++) { int temp = 0; for (int k = 0; k < n; k++) { temp += data[i][k] * other.data[k][j]; } result.data[i][j] = temp; } } return result; } // 矩阵赋值运算符重载 Matrix& operator=(const Matrix& other) { if (this == &other) { return *this; } m = other.m; n = other.n; data = other.data; return *this; } // 打印矩阵 void print() { for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { cout << data[i][j] << " "; } cout << endl; } } }; int main() { Matrix A(2, 3); Matrix B(3, 2); A.print(); B.print(); A = {{1, 2, 3}, {4, 5, 6}}; B = {{7, 8}, {9, 10}, {11, 12}}; A.print(); B.print(); Matrix C = A * B; C.print(); return 0; } ``` 这个示例代码中,我们定义了一个 `Matrix` 来表示矩阵,并实现了矩阵相乘的运算符重载。在 `main()` 函数中,我们创建了两个矩阵 `A` 和 `B`,并将它们赋值为我们需要相乘的矩阵。然后,我们通过 `A * B` 的方式来计算它们的乘积,并将结果存储在 `C` 中。最后,我们打印了 `C` 的值,即矩阵相乘的结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值