重构-C++实现矩阵的简单实例
重构-C++实现矩阵的简单实例
#include<iostream> #include<cmath> usingnamespacestd; doublecofactor(double*detPtr,intrank,intt);//代数余子式 doublevalDet(double*detPtr,intrank);//行列式 template<classT> voidexchange(T&t1,T&t2){Ttemp;temp=t1;t1=t2;t2=temp;}//交换 classSquareMatrix; classMatrix{ public: friendclassSquareMatrix;//配合转换函数食用 Matrix(){m=n=mn=0;}//默认构造函数 Matrix(intmt,intnt);//构造矩阵 Matrix(constMatrix&mtrx);//复制构造函数 Matrix(intmt,intnt,double*a);//数组初始化矩阵 MatrixtransposeMtrx();//转置矩阵 //初等变换 voidexchangeRow(intr1,intr2,intc=0);//交换行 voidmultiRow(intr,intk,intc=0);//数乘行 voidaddMultiRow(intr1,intr2,intk=1,intc=0);//r1+=k*r2 voidexchangeColumn(intc1,intc2,intr=0);//交换列 voidmultiColumn(intc,intk,intr=0);//数乘列 voidaddMultiColumn(intc1,intc2,intk=1,intr=0);//c1+=k*c2 Matrix&operator=(constMatrix&mtrx);//赋值构造函数 friendistream&operator>>(istream&input,Matrix&mtrx); friendostream&operator<<(ostream&output,Matrix&mtrx);//输出矩阵 friendMatrixoperator*(Matrix&m1,Matrix&m2);//矩阵乘法 protected: intm; intn; intmn; double*matrixPtr; }; classSquareMatrix:publicMatrix{ public: SquareMatrix():Matrix(){}//默认构造函数 SquareMatrix(intmt):Matrix(mt,mt){};//构造函数 SquareMatrix(intmt,double*a):Matrix(mt,mt,a){};//数组初始化方阵 SquareMatrix(constMatrix&mtrx);//矩阵到方阵转换 SquareMatrixtransposeSqrMtrx();//转置方阵 SquareMatrixadjugateSqrMatrix();//伴随矩阵 SquareMatrixinverseSqrMatrix();//逆矩阵 friendistream&operator>>(istream&input,SquareMatrix&mtrx); //输入方阵 friendSquareMatrixoperator*(SquareMatrix&sm1,SquareMatrix&sm2); //方阵乘法 doublegetDet();//行列式的值 private: }; Matrix::Matrix(intmt,intnt){//初始化m*n矩阵 m=mt;n=nt;mn=m*n; matrixPtr=newdouble[mn]; } Matrix::Matrix(constMatrix&mtrx){//复制构造函数 m=mtrx.m;n=mtrx.n;mn=mtrx.mn; matrixPtr=newdouble[mn]; for(inti=0;i<mn;i++)matrixPtr[i]=mtrx.matrixPtr[i]; } Matrix::Matrix(intmt,intnt,double*a){//数组初始化m*n矩阵 m=mt;n=nt;mn=m*n; matrixPtr=newdouble[mn]; for(inti=0;i<mn;i++) matrixPtr[i]=a[i]; } istream&operator>>(istream&input,Matrix&mtrx){//重载>> if(!mtrx.m){ cout<<"enterthem,nofmatrix:"; input>>mtrx.m>>mtrx.n; mtrx.mn=mtrx.m*mtrx.n; mtrx.matrixPtr=newdouble[mtrx.mn]; cout<<"enterthematrix:"<<endl; } elsecout<<"entera"<<mtrx.m<<'*'<<mtrx.n<<"matrix:"<<endl; for(inti=0;i<mtrx.mn;i++)input>>mtrx.matrixPtr[i]; returninput; } MatrixMatrix::transposeMtrx(){//转置矩阵 Matrixmtrx(n,m); for(inti=0;i<n;i++) for(intj=0;j<m;j++) mtrx.matrixPtr[m*i+j]=matrixPtr[n*j+i]; returnmtrx; } voidMatrix::exchangeRow(intr1,intr2,intc){//交换行,默认c=0 for(inti=c;i<n;i++) exchange(matrixPtr[n*r1+i],matrixPtr[n*r2+i]); } voidMatrix::multiRow(intr,intk,intc){//数乘行,默认c=0 for(inti=c;i<n;i++) matrixPtr[n*r+i]*=k; } voidMatrix::addMultiRow(intr1,intr2,intk,intc){//r1+=k*r2,默认k=1,c=0 for(inti=c;i<n;i++) matrixPtr[n*r1+i]+=matrixPtr[n*r2+i]*k; } voidMatrix::exchangeColumn(intc1,intc2,intr){//交换列,默认r=0 for(inti=r;i<m;i++) exchange(matrixPtr[n*i+c1],matrixPtr[n*i+c2]); } voidMatrix::multiColumn(intc,intk,intr){//数乘列,默认k=1,r=0 for(inti=r;i<m;i++) matrixPtr[n*i+c]*=k; } voidMatrix::addMultiColumn(intc1,intc2,intk,intr){//c1+=k*c2,默认r=0 for(inti=r;i<m;i++) matrixPtr[n*i+c1]+=matrixPtr[n*i+c2]*k; } Matrix&Matrix::operator=(constMatrix&mtrx){//重载= m=mtrx.m;n=mtrx.n;mn=m*n; matrixPtr=newdouble[mn]; for(inti=0;i<mn;i++)matrixPtr[i]=mtrx.matrixPtr[i]; return*this; } ostream&operator<<(ostream&output,Matrix&mtrx){//重载<< output<<endl; for(inti=0;i<mtrx.m;i++){ for(intj=0;j<mtrx.n;j++) output<<mtrx.matrixPtr[mtrx.n*i+j]<<''; output<<endl; } output<<endl; returnoutput; } Matrixoperator*(Matrix&m1,Matrix&m2){//重载* Matrixm3(m1.m,m2.n); for(inti=0;i<m3.m;i++) for(intj=0;j<m3.n;j++){ doubleval=0; for(intk=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; } returnm3; } //我是萌萌哒分割线------------------------------------------------------- SquareMatrix::SquareMatrix(constMatrix&mtrx){//构造函数 m=n=mtrx.m;mn=m*n;matrixPtr=newdouble[mn]; for(inti=0;i<mn;i++)matrixPtr[i]=mtrx.matrixPtr[i]; } istream&operator>>(istream&input,SquareMatrix&mtrx){//重载>> if(!mtrx.m){ cout<<"enterthemofsquareMatrix:"; input>>mtrx.m; mtrx.n=mtrx.m;mtrx.mn=mtrx.m*mtrx.n; mtrx.matrixPtr=newdouble[mtrx.mn]; cout<<"enterthesquareMatrix:"<<endl; } elsecout<<"entera"<<mtrx.m<<"ordersquareMatrix:"<<endl; for(inti=0;i<mtrx.mn;i++)input>>mtrx.matrixPtr[i]; returninput; } SquareMatrixSquareMatrix::transposeSqrMtrx(){//转置方阵 returnSquareMatrix((*this).transposeMtrx()); } SquareMatrixSquareMatrix::adjugateSqrMatrix(){//伴随矩阵 SquareMatrixaSM(m); for(inti=0;i<mn;i++) aSM.matrixPtr[i]=cofactor(matrixPtr,m,i); aSM=aSM.transposeSqrMtrx(); returnaSM; } SquareMatrixSquareMatrix::inverseSqrMatrix(){//逆矩阵 doubledet=getDet(); if(det==0){ cerr<<"thisisasingularmatrix!"<<endl;//判断奇异矩阵 return0; } SquareMatrixaSM(m),iSM(m); aSM=adjugateSqrMatrix(); for(inti=0;i<mn;i++) iSM.matrixPtr[i]=aSM.matrixPtr[i]/det; returniSM; } SquareMatrixoperator*(SquareMatrix&sm1,SquareMatrix&sm2){//重载* SquareMatrixsm3(sm1.m); for(inti=0;i<sm3.m;i++) for(intj=0;j<sm3.n;j++){ doubleval=0; for(intk=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; } returnsm3; } doubleSquareMatrix::getDet(){//行列式 returnvalDet(matrixPtr,m); } //又是一条萌萌哒分割线------------------------------------------ doublevalDet(double*detPtr,intrank) { doubleval=0; if(rank==1)returndetPtr[0]; for(inti=0;i<rank;i++)//计算余子式保存在nextDetPtr[]中 { double*nextDetPtr=newdouble[(rank-1)*(rank-1)]; for(intj=0;j<rank-1;j++) for(intk=0;k<i;k++) nextDetPtr[j*(rank-1)+k]=detPtr[(j+1)*rank+k]; for(intj=0;j<rank-1;j++) for(intk=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); } returnval; } doublecofactor(double*detPtr,intrank,intt){//计算代数余子式 double*nextDetPtr=newdouble[(rank-1)*(rank-1)]; for(inti=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++; } returnvalDet(nextDetPtr,rank-1)*pow(-1.0,t/rank+t%rank); } intmain(){ cout<<endl<<"测试驱动程序-------------------"<<endl; /* cout<<endl<<"输入任意矩阵-------------------"<<endl; Matrixm1;cin>>m1;cout<<m1; cout<<endl<<"输入任意方阵-------------------"<<endl; SquareMatrixsm1;cin>>sm1;cout<<sm1; cout<<endl<<"输入3*2矩阵--------------------"<<endl; Matrixm2(3,2);cin>>m2;cout<<m2; cout<<endl<<"输入2阶方阵--------------------"<<endl; SquareMatrixsm2(2);cin>>sm2;cout<<sm2; */ cout<<endl<<"数组初始化矩阵-----------------"<<endl; doublea1[6]={1,2,3,7,8,9}; Matrixm3(2,3,a1);cout<<m3; cout<<endl<<"数组初始化方阵-----------------"<<endl; doublea2[4]={3,4,5,6}; SquareMatrixsm3(2,a2);cout<<sm3; cout<<endl<<"复制构造方阵/矩阵--------------"<<endl; Matrixm4;m4=m3;Matrixm5(m3); cout<<m4<<m5; SquareMatrixsm4;sm4=sm3;SquareMatrixsm5(sm3); cout<<sm4<<sm5; cout<<endl<<"矩阵/方阵乘法------------------"<<endl; doublea3[6]={1,0,3,2,1,0},a4[9]={4,1,0,-1,1,3,2,0,1}; Matrixm6(2,3,a3),m7(3,3,a4); Matrixm8=m6*m7;cout<<m8; doublea5[4]={1,2,2,3},a6[4]={2,3,4,1}; SquareMatrixsm6(2,a5),sm7(2,a6); SquareMatrixsm8(sm6*sm7);cout<<sm8; cout<<endl<<"矩阵转换为方阵-----------------"<<endl; SquareMatrixsm9(m7);cout<<m7<<sm9; cout<<endl<<"转置矩阵/方阵------------------"<<endl; Matrixm9(m6.transposeMtrx()); cout<<m6<<m9; SquareMatrixsm10=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; SquareMatrixsm11=sm3.inverseSqrMatrix();cout<<sm11; SquareMatrixsm12=sm3*sm11;cout<<sm12; return0; }
以上这篇重构-C++实现矩阵的简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。