欢迎来到天天文库
浏览记录
ID:47846186
大小:34.00 KB
页数:7页
时间:2019-11-26
《C#矩阵类(实现转置,相乘,相加,求逆)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、usingSystem;classMatrix{double[,]matrix;publicintrow=0,col=0;//inti=0,j=0;//intcount=1;//定义三个不同情况下的构造函数publicMatrix(){}publicMatrix(introw){matrix=newdouble[row,row];}publicMatrix(introw,intcol){this.row=row;this.col=col;matrix=newdouble[row,col];}//复制构造函
2、数publicMatrix(Matrixm){introw=m.row;intcol=m.col;matrix=newdouble[row,col];for(inti=0;i3、){returnmatrix[i,j];}//输出矩阵publicvoidOutputM(){Console.WriteLine("矩阵为:");for(intp=0;p4、b++){Console.WriteLine("第{0}行,第{1}列",a+1,b+1);doublevalue=Convert.ToDouble(Console.ReadLine());this.SetNum(a,b,value);}}}//得到matrixpublicdouble[,]Detail{get{returnmatrix;}set{matrix=value;}}//矩阵转置实现publicMatrixTranspose(){Matrixanother=newMatrix(row,col);5、for(inti=0;i6、eptione=newException("相加的两个矩阵的列数不等");throwe;}Matrixanother=newMatrix(lm.row,lm.col);for(inti=0;i7、ixm1,Matrixm2){doubletemp=0;Matrixret;if(m1.col!=m2.row){System.Exceptione=newException("前者列数不等于后者行数,无法相乘");throwe;}ret=newMatrix(m1.row,m1.col);for(inti=0;i8、;}ret.SetNum(i,j,temp);}}returnret;}//矩阵求逆实现publicstaticMatrixInverse(MatrixM){intm=M.row;intn=M.col;if(m!=n){ExceptionmyException=newException("求逆的矩阵不是方阵");throwmyException;}Matrixret=newMatrix(m,n);double[,]a0=M.D
3、){returnmatrix[i,j];}//输出矩阵publicvoidOutputM(){Console.WriteLine("矩阵为:");for(intp=0;p4、b++){Console.WriteLine("第{0}行,第{1}列",a+1,b+1);doublevalue=Convert.ToDouble(Console.ReadLine());this.SetNum(a,b,value);}}}//得到matrixpublicdouble[,]Detail{get{returnmatrix;}set{matrix=value;}}//矩阵转置实现publicMatrixTranspose(){Matrixanother=newMatrix(row,col);5、for(inti=0;i6、eptione=newException("相加的两个矩阵的列数不等");throwe;}Matrixanother=newMatrix(lm.row,lm.col);for(inti=0;i7、ixm1,Matrixm2){doubletemp=0;Matrixret;if(m1.col!=m2.row){System.Exceptione=newException("前者列数不等于后者行数,无法相乘");throwe;}ret=newMatrix(m1.row,m1.col);for(inti=0;i8、;}ret.SetNum(i,j,temp);}}returnret;}//矩阵求逆实现publicstaticMatrixInverse(MatrixM){intm=M.row;intn=M.col;if(m!=n){ExceptionmyException=newException("求逆的矩阵不是方阵");throwmyException;}Matrixret=newMatrix(m,n);double[,]a0=M.D
4、b++){Console.WriteLine("第{0}行,第{1}列",a+1,b+1);doublevalue=Convert.ToDouble(Console.ReadLine());this.SetNum(a,b,value);}}}//得到matrixpublicdouble[,]Detail{get{returnmatrix;}set{matrix=value;}}//矩阵转置实现publicMatrixTranspose(){Matrixanother=newMatrix(row,col);
5、for(inti=0;i6、eptione=newException("相加的两个矩阵的列数不等");throwe;}Matrixanother=newMatrix(lm.row,lm.col);for(inti=0;i7、ixm1,Matrixm2){doubletemp=0;Matrixret;if(m1.col!=m2.row){System.Exceptione=newException("前者列数不等于后者行数,无法相乘");throwe;}ret=newMatrix(m1.row,m1.col);for(inti=0;i8、;}ret.SetNum(i,j,temp);}}returnret;}//矩阵求逆实现publicstaticMatrixInverse(MatrixM){intm=M.row;intn=M.col;if(m!=n){ExceptionmyException=newException("求逆的矩阵不是方阵");throwmyException;}Matrixret=newMatrix(m,n);double[,]a0=M.D
6、eptione=newException("相加的两个矩阵的列数不等");throwe;}Matrixanother=newMatrix(lm.row,lm.col);for(inti=0;i7、ixm1,Matrixm2){doubletemp=0;Matrixret;if(m1.col!=m2.row){System.Exceptione=newException("前者列数不等于后者行数,无法相乘");throwe;}ret=newMatrix(m1.row,m1.col);for(inti=0;i8、;}ret.SetNum(i,j,temp);}}returnret;}//矩阵求逆实现publicstaticMatrixInverse(MatrixM){intm=M.row;intn=M.col;if(m!=n){ExceptionmyException=newException("求逆的矩阵不是方阵");throwmyException;}Matrixret=newMatrix(m,n);double[,]a0=M.D
7、ixm1,Matrixm2){doubletemp=0;Matrixret;if(m1.col!=m2.row){System.Exceptione=newException("前者列数不等于后者行数,无法相乘");throwe;}ret=newMatrix(m1.row,m1.col);for(inti=0;i8、;}ret.SetNum(i,j,temp);}}returnret;}//矩阵求逆实现publicstaticMatrixInverse(MatrixM){intm=M.row;intn=M.col;if(m!=n){ExceptionmyException=newException("求逆的矩阵不是方阵");throwmyException;}Matrixret=newMatrix(m,n);double[,]a0=M.D
8、;}ret.SetNum(i,j,temp);}}returnret;}//矩阵求逆实现publicstaticMatrixInverse(MatrixM){intm=M.row;intn=M.col;if(m!=n){ExceptionmyException=newException("求逆的矩阵不是方阵");throwmyException;}Matrixret=newMatrix(m,n);double[,]a0=M.D
此文档下载收益归作者所有