国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#計(jì)算矩陣的逆矩陣方法實(shí)例分析

2020-01-24 01:32:53
字體:
供稿:網(wǎng)友

本文實(shí)例講述了C#計(jì)算矩陣的逆矩陣方法。分享給大家供大家參考。具體如下:

1.代碼思路

1)對矩陣進(jìn)行合法性檢查:矩陣必須為方陣
2)計(jì)算矩陣行列式的值(Determinant函數(shù))
3)只有滿秩矩陣才有逆矩陣,因此如果行列式的值為0(在代碼中以絕對值小于1E-6做判斷),則終止函數(shù),報(bào)出異常
4)求出伴隨矩陣(AdjointMatrix函數(shù))
5)逆矩陣各元素即其伴隨矩陣各元素除以矩陣行列式的商

2.函數(shù)代碼

(注:本段代碼只實(shí)現(xiàn)了一個(gè)思路,可能并不是該問題的最優(yōu)解)

/// <summary>/// 求矩陣的逆矩陣/// </summary>/// <param name="matrix"></param>/// <returns></returns>public static double[][] InverseMatrix(double[][] matrix){ //matrix必須為非空 if (matrix == null || matrix.Length == 0) {  return new double[][] { }; } //matrix 必須為方陣 int len = matrix.Length; for (int counter = 0; counter < matrix.Length; counter++) {  if (matrix[counter].Length != len)  {   throw new Exception("matrix 必須為方陣");  } } //計(jì)算矩陣行列式的值 double dDeterminant = Determinant(matrix); if (Math.Abs(dDeterminant) <= 1E-6) {  throw new Exception("矩陣不可逆"); } //制作一個(gè)伴隨矩陣大小的矩陣 double[][] result = AdjointMatrix(matrix); //矩陣的每項(xiàng)除以矩陣行列式的值,即為所求 for (int i = 0; i < matrix.Length; i++) {  for (int j = 0; j < matrix.Length; j++)  {   result[i][j] = result[i][j] / dDeterminant;  } } return result;}/// <summary>/// 遞歸計(jì)算行列式的值/// </summary>/// <param name="matrix">矩陣</param>/// <returns></returns>public static double Determinant(double[][] matrix){ //二階及以下行列式直接計(jì)算 if (matrix.Length == 0) return 0; else if (matrix.Length == 1) return matrix[0][0]; else if (matrix.Length == 2) {  return matrix[0][0] * matrix[1][1] - matrix[0][1] * matrix[1][0]; } //對第一行使用“加邊法”遞歸計(jì)算行列式的值 double dSum = 0, dSign = 1; for (int i = 0; i < matrix.Length; i++) {  double[][] matrixTemp = new double[matrix.Length - 1][];  for (int count = 0; count < matrix.Length - 1; count++)  {   matrixTemp[count] = new double[matrix.Length - 1];  }  for (int j = 0; j < matrixTemp.Length; j++)  {   for (int k = 0; k < matrixTemp.Length; k++)   {    matrixTemp[j][k] = matrix[j + 1][k >= i ? k + 1 : k];   }  }  dSum += (matrix[0][i] * dSign * Determinant(matrixTemp));  dSign = dSign * -1; } return dSum;}/// <summary>/// 計(jì)算方陣的伴隨矩陣/// </summary>/// <param name="matrix">方陣</param>/// <returns></returns>public static double[][] AdjointMatrix(double [][] matrix){ //制作一個(gè)伴隨矩陣大小的矩陣 double[][] result = new double[matrix.Length][]; for (int i = 0; i < result.Length; i++) {  result[i] = new double[matrix[i].Length]; } //生成伴隨矩陣 for (int i = 0; i < result.Length; i++) {  for (int j = 0; j < result.Length; j++)  {   //存儲代數(shù)余子式的矩陣(行、列數(shù)都比原矩陣少1)   double[][] temp = new double[result.Length - 1][];   for (int k = 0; k < result.Length - 1; k++)   {    temp[k] = new double[result[k].Length - 1];   }   //生成代數(shù)余子式   for (int x = 0; x < temp.Length; x++)   {    for (int y = 0; y < temp.Length; y++)    {     temp[x][y] = matrix[x < i ? x : x + 1][y < j ? y : y + 1];    }   }   //Console.WriteLine("代數(shù)余子式:");   //PrintMatrix(temp);   result[j][i] = ((i + j) % 2 == 0 ? 1 : -1) * Determinant(temp);  } } //Console.WriteLine("伴隨矩陣:"); //PrintMatrix(result); return result;}/// <summary>/// 打印矩陣/// </summary>/// <param name="matrix">待打印矩陣</param>private static void PrintMatrix(double[][] matrix, string title = ""){ //1.標(biāo)題值為空則不顯示標(biāo)題 if (!String.IsNullOrWhiteSpace(title)) {  Console.WriteLine(title); } //2.打印矩陣 for (int i = 0; i < matrix.Length; i++) {  for (int j = 0; j < matrix[i].Length; j++)  {   Console.Write(matrix[i][j] + "/t");   //注意不能寫為:Console.Write(matrix[i][j] + '/t');  }  Console.WriteLine(); } //3.空行 Console.WriteLine();}

3.Main函數(shù)調(diào)用

static void Main(string[] args){ double[][] matrix = new double[][]  {  new double[] { 1, 2, 3 },   new double[] { 2, 2, 1 },  new double[] { 3, 4, 3 }  }; PrintMatrix(matrix, "原矩陣"); PrintMatrix(AdjointMatrix(matrix), "伴隨矩陣"); Console.WriteLine("行列式的值為:" + Determinant(matrix) + '/n'); PrintMatrix(InverseMatrix(matrix), "逆矩陣"); Console.ReadLine();}

4.執(zhí)行結(jié)果

希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 江西省| 宝山区| 安福县| 孟州市| 长丰县| 陵水| 麦盖提县| 台北县| 彭阳县| 商洛市| 根河市| 南部县| 中宁县| 定南县| 舞钢市| 元阳县| 尖扎县| 饶河县| 昌宁县| 高唐县| 平度市| 依安县| 黎川县| 德格县| 拉萨市| 丹巴县| 尼玛县| 苍南县| 措勤县| 云安县| 连平县| 八宿县| 张掖市| 济阳县| 白银市| 罗平县| 天峨县| 兴义市| 佛冈县| 木兰县| 梧州市|