C#矩陣類(實(shí)現(xiàn)轉(zhuǎn)置,相乘,相加,求逆)_第1頁
C#矩陣類(實(shí)現(xiàn)轉(zhuǎn)置,相乘,相加,求逆)_第2頁
C#矩陣類(實(shí)現(xiàn)轉(zhuǎn)置,相乘,相加,求逆)_第3頁
C#矩陣類(實(shí)現(xiàn)轉(zhuǎn)置,相乘,相加,求逆)_第4頁
C#矩陣類(實(shí)現(xiàn)轉(zhuǎn)置,相乘,相加,求逆)_第5頁
已閱讀5頁,還剩2頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、using System;class Matrix double , matrix; public int row=0, col=0; /int i=0,j=0; /int count=1; /定義三個不同情況下的構(gòu)造函數(shù) 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; /復(fù)制構(gòu)造函數(shù) public Matrix(Matri

2、x m) int row = 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); /輸入相應(yīng)的值,對矩陣進(jìn)行設(shè)置 public void SetNum(int i,int j, double num) matrixi, j = num; /得到相應(yīng)的矩陣某個數(shù) public double getNum(int i,int j) return matrixi,

3、 j; /輸出矩陣 public void OutputM() Console.WriteLine("矩陣為:"); for (int p = 0; p < row; p+) for (int q = 0; q < col; q+) Console.Write("t"+matrixp,q); Console.Write("n"); /輸入矩陣具體數(shù)字實(shí)現(xiàn) public void InputM(int Row, int Col) for (int a = 0; a < Col; a+) for (int b = 0;

4、b < Col; b+) Console.WriteLine("第0行,第1列", a + 1, b + 1); double value = Convert.ToDouble(Console.ReadLine(); this.SetNum(a, b, value); /得到matrix public double, Detail get return matrix; set matrix = value; /矩陣轉(zhuǎn)置實(shí)現(xiàn) public Matrix Transpose() Matrix another = new Matrix(row, col); for (int

5、 i = 0; i < row; i+) for (int j = 0; j < col; j+) another.SetNum(j, i, matrixi, j); return another; /矩陣相加實(shí)現(xiàn) public static Matrix Add(Matrix lm,Matrix rm) /行出錯 if (lm.row != rm.row) System.Exception e = new Exception("相加的兩個矩陣的行數(shù)不等"); throw e; /列出錯 if (lm.col != rm.col) System.Exceptio

6、n e = new Exception("相加的兩個矩陣的列數(shù)不等"); throw e; Matrix another=new Matrix(lm.row,lm.col); for(int i=0;i<lm.row;i+) for(int j=0;j<lm.col;j+) double temp=lm.getNum(i,j)+rm.getNum(i,j); another.SetNum(i, j, temp); return another; /矩陣相乘實(shí)現(xiàn) public static Matrix Mutiply(Matrix m1, Matrix m2)

7、double temp=0; Matrix ret; if (m1.col != m2.row) System.Exception e = new Exception("前者列數(shù)不等于后者行數(shù),無法相乘"); throw e; ret = new Matrix(m1.row, m1.col); for (int i = 0; i < m1.row; i+) for (int j = 0; j < m1.col; j+) for (int p = 0; p < m1.col; p+) temp += m1.getNum(i, p) + m2.getNum(p

8、, i); ret.SetNum(i, j, temp); return ret; /矩陣求逆實(shí)現(xiàn) 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 = new Matrix(m, n); double, a0 = M.Detail; double, a = (double,)a0.Clone(); d

9、ouble, 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; /找最大元,其所在行為row for (i = k; i < n; i+) temp = Math.Abs(ai, k); if (max < temp) max = temp; row = i; if (max = 0) Exception myException = new Exceptio

10、n("該矩陣無逆矩陣"); 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; /首元化為1 for (j = k + 1; j < n; j+) ak, j /= ak, k; for (j = 0; j < n; j+) bk, j /= ak, k; ak, k = 1; /k列化為0

11、/對a for (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; /對b for (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

12、, k = 0; ak, k = 1; return ret; /主函數(shù) public static void Main() int Row, Col, choice; Console.WriteLine("請輸入想要的矩陣行數(shù)與列數(shù)"); Row = Convert.ToInt32(Console.ReadLine(); Col = Convert.ToInt32(Console.ReadLine(); Matrix m = new Matrix(Row, Col); Console.WriteLine("輸入矩陣數(shù)據(jù)"); m.InputM(Row,

13、 Col); m.OutputM(); do Console.WriteLine("請選擇你想要進(jìn)行的運(yùn)算:n(1)轉(zhuǎn)置;n(2)輸入另一矩陣并相加;n(3)求逆;n(4)輸入另一矩陣并相乘;n(0)停止;"); choice = Convert.ToInt32(Console.ReadLine(); switch (choice) /轉(zhuǎn)置 case 1: Matrix n = m.Transpose(); n.OutputM(); break; /相加 case 2: Console.WriteLine("請輸入第二個矩陣行數(shù)與列數(shù)"); Row =

14、Convert.ToInt32(Console.ReadLine(); Col = Convert.ToInt32(Console.ReadLine(); Matrix m2 = new Matrix(Row, Col); Console.WriteLine("輸入矩陣數(shù)據(jù)"); 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("請輸入第二個矩陣行數(shù)與列數(shù)"); Row = Convert.ToInt32(Console.ReadLine(); Col = Convert.ToInt32(Console.ReadLine(); Matrix m4 = new Matrix(Row, Col); Console.WriteLine("輸入矩陣數(shù)據(jù)&q

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論