重构-C++实现矩阵的简单实例
投稿:jingxian
下面小编就为大家带来一篇重构-C++实现矩阵的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
重构-C++实现矩阵的简单实例
#include <iostream> #include <cmath> using namespace std; double cofactor(double* detPtr,int rank,int t); //代数余子式 double valDet( double *detPtr, int rank); //行列式 template <class T> void exchange(T& t1,T& t2){T temp;temp=t1;t1=t2;t2=temp;} //交换 class SquareMatrix; class Matrix{ public: friend class SquareMatrix; //配合转换函数食用 Matrix(){m=n=mn=0;} //默认构造函数 Matrix(int mt,int nt); //构造矩阵 Matrix(const Matrix& mtrx); //复制构造函数 Matrix(int mt,int nt,double* a); //数组初始化矩阵 Matrix transposeMtrx(); //转置矩阵 //初等变换 void exchangeRow(int r1,int r2,int c=0); //交换行 void multiRow(int r,int k,int c=0); //数乘行 void addMultiRow(int r1,int r2,int k=1,int c=0); //r1+=k*r2 void exchangeColumn(int c1,int c2,int r=0); //交换列 void multiColumn(int c,int k,int r=0); //数乘列 void addMultiColumn(int c1,int c2,int k=1,int r=0); //c1+=k*c2 Matrix& operator =(const Matrix& mtrx); //赋值构造函数 friend istream& operator>>(istream& input,Matrix& mtrx); friend ostream& operator<<(ostream& output,Matrix& mtrx); //输出矩阵 friend Matrix operator*(Matrix& m1,Matrix& m2); //矩阵乘法 protected: int m; int n; int mn; double* matrixPtr; }; class SquareMatrix:public Matrix{ public: SquareMatrix():Matrix(){} //默认构造函数 SquareMatrix(int mt):Matrix(mt,mt){}; //构造函数 SquareMatrix(int mt,double* a):Matrix(mt,mt,a){}; //数组初始化方阵 SquareMatrix(const Matrix& mtrx); //矩阵到方阵转换 SquareMatrix transposeSqrMtrx(); //转置方阵 SquareMatrix adjugateSqrMatrix(); //伴随矩阵 SquareMatrix inverseSqrMatrix(); //逆矩阵 friend istream& operator>>(istream& input,SquareMatrix& mtrx); //输入方阵 friend SquareMatrix operator *(SquareMatrix& sm1,SquareMatrix& sm2); //方阵乘法 double getDet(); //行列式的值 private: }; Matrix::Matrix(int mt,int nt){ //初始化m*n矩阵 m=mt;n=nt;mn=m*n; matrixPtr=new double[mn]; } Matrix::Matrix(const Matrix& mtrx){ //复制构造函数 m=mtrx.m;n=mtrx.n;mn=mtrx.mn; matrixPtr=new double[mn]; for(int i=0;i<mn;i++) matrixPtr[i]=mtrx.matrixPtr[i]; } Matrix::Matrix(int mt,int nt,double* a){ //数组初始化m*n矩阵 m=mt;n=nt;mn=m*n; matrixPtr=new double[mn]; for(int i=0;i<mn;i++) matrixPtr[i]=a[i]; } istream& operator>>(istream& input,Matrix& mtrx){ //重载>> if(!mtrx.m){ cout<<"enter the m,n of matrix:"; input>>mtrx.m>>mtrx.n; mtrx.mn=mtrx.m*mtrx.n; mtrx.matrixPtr=new double[mtrx.mn]; cout<<"enter the matrix:"<<endl; } else cout<<"enter a "<<mtrx.m<<'*'<<mtrx.n<<" matrix:"<<endl; for(int i=0;i<mtrx.mn;i++) input>>mtrx.matrixPtr[i]; return input; } Matrix Matrix::transposeMtrx(){ //转置矩阵 Matrix mtrx(n,m); for(int i=0;i<n;i++) for(int j=0;j<m;j++) mtrx.matrixPtr[m*i+j]=matrixPtr[n*j+i]; return mtrx; } void Matrix::exchangeRow(int r1,int r2,int c){ //交换行,默认c=0 for(int i=c;i<n;i++) exchange(matrixPtr[n*r1+i],matrixPtr[n*r2+i]); } void Matrix::multiRow(int r,int k,int c){ //数乘行,默认c=0 for(int i=c;i<n;i++) matrixPtr[n*r+i]*=k; } void Matrix::addMultiRow(int r1,int r2,int k,int c){ //r1+=k*r2,默认k=1,c=0 for(int i=c;i<n;i++) matrixPtr[n*r1+i]+=matrixPtr[n*r2+i]*k; } void Matrix::exchangeColumn(int c1,int c2,int r){ //交换列,默认r=0 for(int i=r;i<m;i++) exchange(matrixPtr[n*i+c1],matrixPtr[n*i+c2]); } void Matrix::multiColumn(int c,int k,int r){ //数乘列,默认k=1,r=0 for(int i=r;i<m;i++) matrixPtr[n*i+c]*=k; } void Matrix::addMultiColumn(int c1,int c2,int k,int r){ //c1+=k*c2,默认r=0 for(int i=r;i<m;i++) matrixPtr[n*i+c1]+=matrixPtr[n*i+c2]*k; } Matrix& Matrix::operator=(const Matrix& mtrx){ //重载= m=mtrx.m;n=mtrx.n;mn=m*n; matrixPtr=new double[mn]; for(int i=0;i<mn;i++) matrixPtr[i]=mtrx.matrixPtr[i]; return *this; } ostream& operator<<(ostream& output,Matrix& mtrx){ //重载<< output<<endl; for(int i=0;i<mtrx.m;i++){ for(int j=0;j<mtrx.n;j++) output<<mtrx.matrixPtr[mtrx.n*i+j]<<' '; output<<endl; } output<<endl; return output; } Matrix operator *(Matrix& m1,Matrix& m2){ //重载* Matrix m3(m1.m,m2.n); for(int i=0;i<m3.m;i++) for(int j=0;j<m3.n;j++){ double val=0; for(int k=0;k<m2.m;k++) val+=m1.matrixPtr[m1.n*i+k]*m2.matrixPtr[m2.n*k+j]; m3.matrixPtr[m3.n*i+j]=val; } return m3; } //我是萌萌哒分割线------------------------------------------------------- SquareMatrix::SquareMatrix(const Matrix& mtrx){ //构造函数 m=n=mtrx.m;mn=m*n;matrixPtr=new double[mn]; for(int i=0;i<mn;i++) matrixPtr[i]=mtrx.matrixPtr[i]; } istream& operator>>(istream& input,SquareMatrix& mtrx){ //重载>> if(!mtrx.m){ cout<<"enter the m of squareMatrix:"; input>>mtrx.m; mtrx.n=mtrx.m;mtrx.mn=mtrx.m*mtrx.n; mtrx.matrixPtr=new double[mtrx.mn]; cout<<"enter the squareMatrix:"<<endl; } else cout<<"enter a "<<mtrx.m<<" order squareMatrix:"<<endl; for(int i=0;i<mtrx.mn;i++) input>>mtrx.matrixPtr[i]; return input; } SquareMatrix SquareMatrix::transposeSqrMtrx(){ //转置方阵 return SquareMatrix((*this).transposeMtrx()); } SquareMatrix SquareMatrix::adjugateSqrMatrix(){ //伴随矩阵 SquareMatrix aSM(m); for(int i=0;i<mn;i++) aSM.matrixPtr[i]=cofactor(matrixPtr,m,i); aSM=aSM.transposeSqrMtrx(); return aSM; } SquareMatrix SquareMatrix::inverseSqrMatrix(){ //逆矩阵 double det=getDet(); if(det==0){ cerr<<"this is a singular matrix!"<<endl; //判断奇异矩阵 return 0; } SquareMatrix aSM(m),iSM(m); aSM=adjugateSqrMatrix(); for(int i=0;i<mn;i++) iSM.matrixPtr[i]=aSM.matrixPtr[i]/det; return iSM; } SquareMatrix operator *(SquareMatrix& sm1,SquareMatrix& sm2){ //重载* SquareMatrix sm3(sm1.m); for(int i=0;i<sm3.m;i++) for(int j=0;j<sm3.n;j++){ double val=0; for(int k=0;k<sm2.m;k++) val+=sm1.matrixPtr[sm1.n*i+k]*sm2.matrixPtr[sm2.n*k+j]; sm3.matrixPtr[sm3.n*i+j]=val; } return sm3; } double SquareMatrix::getDet(){ //行列式 return valDet(matrixPtr,m); } //又是一条萌萌哒分割线------------------------------------------ double valDet( double *detPtr, int rank) { double val=0; if(rank==1) return detPtr[0]; for(int i=0;i<rank;i++) //计算余子式保存在nextDetPtr[]中 { double *nextDetPtr=new double[(rank-1)*(rank-1)]; for(int j=0;j<rank-1;j++) for(int k=0;k<i;k++) nextDetPtr[j*(rank-1)+k]=detPtr[(j+1)*rank+k]; for(int j=0;j<rank-1;j++) for(int k=i;k<rank-1;k++) nextDetPtr[j*(rank-1)+k]=detPtr[(j+1)*rank+k+1]; val+=detPtr[i]*valDet(nextDetPtr,rank-1)*pow(-1.0,i); } return val; } double cofactor(double* detPtr,int rank,int t){ //计算代数余子式 double *nextDetPtr=new double[(rank-1)*(rank-1)]; for(int i=0,j=0;i<rank*rank;i++) if(i>=(t/rank)*rank&&i<(t/rank)*rank+rank||!((t-i)%rank)); //如果i和t同行或同列 else{ nextDetPtr[j]=detPtr[i]; j++; } return valDet(nextDetPtr,rank-1)*pow(-1.0,t/rank+t%rank); } int main(){ cout<<endl<<"测试驱动程序-------------------"<<endl; /* cout<<endl<<"输入任意矩阵-------------------"<<endl; Matrix m1;cin>>m1;cout<<m1; cout<<endl<<"输入任意方阵-------------------"<<endl; SquareMatrix sm1;cin>>sm1;cout<<sm1; cout<<endl<<"输入3*2矩阵--------------------"<<endl; Matrix m2(3,2);cin>>m2;cout<<m2; cout<<endl<<"输入2阶方阵--------------------"<<endl; SquareMatrix sm2(2);cin>>sm2;cout<<sm2; */ cout<<endl<<"数组初始化矩阵-----------------"<<endl; double a1[6]={1,2,3,7,8,9}; Matrix m3(2,3,a1);cout<<m3; cout<<endl<<"数组初始化方阵-----------------"<<endl; double a2[4]={3,4,5,6}; SquareMatrix sm3(2,a2);cout<<sm3; cout<<endl<<"复制构造方阵/矩阵--------------"<<endl; Matrix m4;m4=m3;Matrix m5(m3); cout<<m4<<m5; SquareMatrix sm4;sm4=sm3;SquareMatrix sm5(sm3); cout<<sm4<<sm5; cout<<endl<<"矩阵/方阵乘法------------------"<<endl; double a3[6]={1,0,3,2,1,0},a4[9]={4,1,0,-1,1,3,2,0,1}; Matrix m6(2,3,a3),m7(3,3,a4); Matrix m8=m6*m7;cout<<m8; double a5[4]={1,2,2,3},a6[4]={2,3,4,1}; SquareMatrix sm6(2,a5),sm7(2,a6); SquareMatrix sm8(sm6*sm7);cout<<sm8; cout<<endl<<"矩阵转换为方阵-----------------"<<endl; SquareMatrix sm9(m7);cout<<m7<<sm9; cout<<endl<<"转置矩阵/方阵------------------"<<endl; Matrix m9(m6.transposeMtrx()); cout<<m6<<m9; SquareMatrix sm10=sm9.transposeSqrMtrx(); cout<<sm9<<sm10; cout<<endl<<"初等变换-----------------------"<<endl; cout<<m3<<m4; m4.exchangeRow(0,1,2);cout<<m3<<m4; m4.exchangeRow(0,1);cout<<m4; m4.exchangeColumn(0,2);cout<<m4; m4.multiRow(1,2);cout<<m4; m4.multiColumn(1,2,1);cout<<m4; m4.addMultiRow(0,1);cout<<m4; m4.addMultiColumn(0,2,2,1);cout<<m4; cout<<sm3<<sm4; sm4.exchangeRow(0,1);cout<<sm3<<sm4; cout<<endl<<"方阵的行列式值-----------------"<<endl; cout<<sm3<<sm3.getDet()<<endl; cout<<endl<<"逆矩阵-------------------------"<<endl; SquareMatrix sm11=sm3.inverseSqrMatrix();cout<<sm11; SquareMatrix sm12=sm3*sm11;cout<<sm12; return 0; }
以上这篇重构-C++实现矩阵的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。