收藏 分享(赏)

C#矩阵类(实现转置,相乘,相加,求逆).doc

上传人:精品资料 文档编号:10477876 上传时间:2019-11-21 格式:DOC 页数:7 大小:34KB
下载 相关 举报
C#矩阵类(实现转置,相乘,相加,求逆).doc_第1页
第1页 / 共7页
C#矩阵类(实现转置,相乘,相加,求逆).doc_第2页
第2页 / 共7页
C#矩阵类(实现转置,相乘,相加,求逆).doc_第3页
第3页 / 共7页
C#矩阵类(实现转置,相乘,相加,求逆).doc_第4页
第4页 / 共7页
C#矩阵类(实现转置,相乘,相加,求逆).doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

1、using System;class Matrixdouble , matrix; public int row=0, col=0;/int i=0,j=0;/int count=1;/定义三个不同情况下的构造函数public Matrix()public Matrix(int row)matrix = new doublerow, row;public Matrix(int row, int col)this.row = row;this.col = col;matrix = new doublerow, col;/复制构造函数public Matrix(Matrix m)int row =

2、 m.row;int col = m.col;matrix = new doublerow, col;for (int i = 0; i row; i+)for (int j = 0; j col; j+)matrixi, j = m.getNum(i, j);/输入相应的值,对矩阵进行设置public void SetNum(int i,int j, double num)matrixi, j = num;/得到相应的矩阵某个数public double getNum(int i,int j)return matrixi, j;/输出矩阵public void OutputM()Consol

3、e.WriteLine(“矩阵为:“);for (int p = 0; p row; p+)for (int q = 0; q col; q+)Console.Write(“t“+matrixp,q);Console.Write(“n“);/输入矩阵具体数字实现public void InputM(int Row, int Col)for (int a = 0; a Col; a+)for (int b = 0; b Col; b+)Console.WriteLine(“第0行,第1列“, a + 1, b + 1);double value = Convert.ToDouble(Consol

4、e.ReadLine();this.SetNum(a, b, value);/得到 matrixpublic double, Detailget return matrix; set matrix = value; /矩阵转置实现public Matrix Transpose()Matrix another = new Matrix(row, col);for (int i = 0; i row; i+)for (int j = 0; j col; j+)another.SetNum(j, i, matrixi, j);return another;/矩阵相加实现public static M

5、atrix Add(Matrix lm,Matrix rm)/行出错if (lm.row != rm.row) System.Exception e = new Exception(“相加的两个矩阵的行数不等“);throw e;/列出错if (lm.col != rm.col) System.Exception e = new Exception(“相加的两个矩阵的列数不等“);throw e;Matrix another=new Matrix(lm.row,lm.col);for(int i=0;ilm.row;i+)for(int j=0;jlm.col;j+)double temp=l

6、m.getNum(i,j)+rm.getNum(i,j);another.SetNum(i, j, temp);return another;/矩阵相乘实现public static Matrix Mutiply(Matrix m1, Matrix m2)double temp=0;Matrix ret;if (m1.col != m2.row)System.Exception e = new Exception(“前者列数不等于后者行数,无法相乘“);throw e;ret = new Matrix(m1.row, m1.col);for (int i = 0; i m1.row; i+)f

7、or (int j = 0; j m1.col; j+)for (int p = 0; p m1.col; p+)temp += m1.getNum(i, p) + m2.getNum(p, i);ret.SetNum(i, j, temp);return ret;/矩阵求逆实现public static Matrix Inverse(Matrix M)int m = M.row;int n = M.col;if (m != n)Exception myException = new Exception(“求逆的矩阵不是方阵 “);throw myException;Matrix ret =

8、new Matrix(m, n);double, a0 = M.Detail;double, a = (double,)a0.Clone();double, b = ret.Detail;int i, j, row, k;double max, temp;/单位矩阵for (i = 0; i n; i+)bi, i = 1;for (k = 0; k n; k+)max = 0; row = k;/找最大元,其所在行为 rowfor (i = k; i n; i+)temp = Math.Abs(ai, k);if (max temp)max = temp;row = i;if (max =

9、0)Exception myException = new Exception(“该矩阵无逆矩阵“);throw myException;/交换 k 与 row 行if (row != k)for (j = 0; j n; j+)temp = arow, j;arow, j = ak, j;ak, j = temp;temp = brow, j;brow, j = bk, j;bk, j = temp;/首元化为 1for (j = k + 1; j n; j+) ak, j /= ak, k;for (j = 0; j n; j+) bk, j /= ak, k;ak, k = 1;/k 列

10、化为 0/对 afor (j = k + 1; j n; j+)for (i = 0; i k; i+) ai, j -= ai, k * ak, j;for (i = k + 1; i n; i+) ai, j -= ai, k * ak, j;/对 bfor (j = 0; j n; j+)for (i = 0; i k; i+) bi, j -= ai, k * bk, j;for (i = k + 1; i n; i+) bi, j -= ai, k * bk, j;for (i = 0; i n; i+) ai, k = 0;ak, k = 1;return ret;/主函数publ

11、ic static void Main()int Row, Col, choice;Console.WriteLine(“请输入想要的矩阵行数与列数 “);Row = Convert.ToInt32(Console.ReadLine();Col = Convert.ToInt32(Console.ReadLine();Matrix m = new Matrix(Row, Col);Console.WriteLine(“输入矩阵数据“);m.InputM(Row, Col);m.OutputM();doConsole.WriteLine(“请选择你想要进行的运算: n(1)转置;n(2)输入另一

12、矩阵并相加;n(3)求逆; n(4)输入另一矩阵并相乘; n(0)停止;“);choice = Convert.ToInt32(Console.ReadLine();switch (choice)/转置case 1:Matrix n = m.Transpose();n.OutputM();break;/相加case 2:Console.WriteLine(“请输入第二个矩阵行数与列数 “);Row = Convert.ToInt32(Console.ReadLine();Col = Convert.ToInt32(Console.ReadLine();Matrix m2 = new Matri

13、x(Row, Col);Console.WriteLine(“输入矩阵数据“);m2.InputM(Row, Col);Console.WriteLine(“第二个矩阵为: “);m2.OutputM();Matrix result = Add(m, m2);result.OutputM();break;/求逆case 3:Matrix m3 = Inverse(m);m3.OutputM();break;/相乘case 4:Console.WriteLine(“请输入第二个矩阵行数与列数 “);Row = Convert.ToInt32(Console.ReadLine();Col = Convert.ToInt32(Console.ReadLine();Matrix m4 = new Matrix(Row, Col);Console.WriteLine(“输入矩阵数据“);m4.InputM(Row, Col);Console.WriteLine(“第二个矩阵为: “);m4.OutputM();Matrix ret = Mutiply(m, m4);ret.OutputM();break;/跳出case 0:break;while (choice != 0);/暂停Console.Write(“请按任意键继续 “);Console.ReadLine();

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报