C++矩阵运算的实现简单

利用C++实现矩阵的构造,通过运算符的重载实现矩阵的乘法、加法等。并且实现矩阵形状的打印,矩阵的打印。

#include#include#include#includeusing namespace std; class Matrix{public:Matrix(int row, int col); //构造函数Matrix(int row, int col, int num); //构造函数重载~Matrix(); //析构函数Matrix(const Matrix & other); //赋值函数Matrix operator*(const Matrix& other); //矩阵相乘Matrix operator+(const Matrix& other); //矩阵相加Matrix operator-(const Matrix& other); //矩阵相减int **a = nullptr; //初始化一共空指针int row, col; void shape(); //打印矩阵形状void Ma_pri(); //打印矩阵}; int main(){Matrix a(2,1); //构造一个(2,1)矩阵Matrix b(1,2); //构造一个(1,2)矩阵a.a[0][0] = 4; //初始化矩阵a.a[1][0] = 2; b.a[0][0] = 3; b.a[0][1] = 5; a.shape(); //矩阵形状打印b.shape(); Matrix c = a*b; //矩阵相乘c.shape(); c.Ma_pri(); //矩阵打印Matrix d(3,3,1); d.Ma_pri(); system("pause"); return 0; }Matrix::Matrix(int row, int col){this->row = row; this->col = col; this->a = new int*[row]; for(int i=0; irow; i++){a[i] = new int[this->col]; }}Matrix::Matrix(int row, int col, int num){this->row = row; this->col = col; this->a = new int*[row]; for(int i=0; irow; i++){a[i] = new int[this->col]; }for(int i = 0; i < this->row; i++){for(int j =0; j row; j++){this->a[i][j] = num; }}}Matrix::~Matrix(){for(int i=0; irow; i++){if(a[i] != nullptr){delete[] a[i]; a[i] = nullptr; }}if(a != nullptr){delete[] a; a = nullptr; }}Matrix::Matrix(const Matrix& other){row = other.row; col = other.col; a = new int*[row]; for(int i=0; icol != other.row){cout<<"shape error"<row,other.col); for(int i=0; irow; i++){for(int j=0; jcol; k++){sum += this->a[i][k] * other.a[k][j]; }m.a[i][j] = sum; }}return m; }Matrix Matrix::operator+(const Matrix& other){if(this->col != other.col or this->row != other.row){cout<<"shape error"<row,this->col); for(int i = 0; i < this->row; i++){for(int j = 0; j < this-> col; j++){m.a[i][j] = this->a[i][j] + other.a[i][j]; }}return m; }Matrix Matrix::operator-(const Matrix& other){if(this->col != other.col or this->row != other.row){cout<<"shape error"<row,this->col); for(int i = 0; i < this->row; i++){for(int j = 0; j < this-> col; j++){m.a[i][j] = this->a[i][j] - other.a[i][j]; }}return m; }void Matrix::shape(){cout<<"("<row<<","<col<<")"<row; i++){for(int j =0; j row; j++){cout<a[i][j]<<" "; }cout<
矩阵求逆算法及程序实现  在做课题时,遇到了求多项式问题,利用了求逆方法。矩阵求逆一般使用简单的算法,还有快速算法 如全选主元高斯-约旦消元法,但本文程序主要写了简单的矩阵求逆算法定义法之伴随矩阵求逆公式如下,其中A可逆:A^{-1}=\frac{A^*}{|A|},其中A^*是A的伴随矩阵。。
1.给定一个方阵,非奇异(不是也可,程序有考虑);
2.由矩阵得到其行列式,求其值如|A|;
【C++矩阵运算的实现简单】3.求其伴随矩阵A^*;
4.得到其逆矩阵。
主要函数如下:
//得到给定矩阵src的逆矩阵保存到des中。bool GetMatrixInverse(double src[N][N],int n,double des[N][N]){double flag=getA(src,n); double t[N][N]; if(flag==0){return false; }else{getAStart(src,n,t); for(int i=0; i
计算|A|:
//按第一行展开计算|A|double getA(double arcs[N][N],int n){if(n==1){return arcs[0][0]; }double ans = 0; double temp[N][N]={0.0}; int i,j,k; for(i=0; i=i)?k+1:k]; }}double t = getA(temp,n-1); if(i%2==0){ans += arcs[0][i]*t; }else{ans -=arcs[0][i]*t; }}return ans; }

计算伴随矩阵:
//计算每一行每一列的每个元素所对应的余子式,组成A*voidgetAStart(double arcs[N][N],int n,double ans[N][N]){if(n==1){ans[0][0] = 1; return; }int i,j,k,t; double temp[N][N]; for(i=0; i=i?k+1:k][t>=j?t+1:t]; }}ans[j][i]=getA(temp,n-1); if((i+j)%2 == 1){ans[j][i] = - ans[j][i]; }}}}

到此这篇关于C++矩阵运算的实现简单的文章就介绍到这了,更多相关C++ 矩阵运算内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读