MyMathLib系列(訂正兩個函數),mymathlib訂正
在行列式消元中的判斷條件有問題,這裡給出訂正後的代碼:
1)MyMathLib.LinearAlgebra.CalcDeterminant方法
/// <summary> /// 化三角法行列式計算, /// </summary> /// <param name="Determinants">N階行列式</param> /// <returns>計算結果</returns> public static double CalcDeterminant(double[,] Determinants) { int theSign = 1;//正負符號,如果需要變換,將記錄變換後的符號. int theN = Determinants.GetLength(0); //從第1列到第theN-1列 for (int i = 0; i < theN - 1; i++) { //從第theN-1行到第i+1行,將D[j,i]依次變為0 for (int j = theN - 1; j > i; j--) { //如果為當前值為0,則不處理,繼續處理上一行 if (Determinants[j, i] == 0) { continue; } //修訂部分: //如果左上鄰元素[j-1, i-1]以及其左邊的元素都為0方可交換 //因為當前元素的左邊元素已經全部是零,因此如果要交換不能使本行左邊產生非零數, //則需要左上鄰及其所有元素皆為0. var theCanDo = true; for (int s = i - 1; s >= 0; s--) { if (Determinants[j - 1, s] != 0) { theCanDo = false; break; } } if (theCanDo) { //如果[j,i]的上一行[j-1, i]的值為0則交換 if (Determinants[j - 1, i] == 0) { //每次交換,行列式的值大小不變,符號取反 theSign = 0 - theSign; for (int k = 0; k < theN; k++) { double theTmpDec = Determinants[j, k]; Determinants[j, k] = Determinants[j - 1, k]; Determinants[j - 1, k] = theTmpDec; } } else { //將當前行減去上一行與theRate的積。 double theRate = Math.Round(Determinants[j, i] / Determinants[j - 1, i], ConstDef.Decimals); for (int k = 0; k < theN; k++) { Determinants[j, k] = Math.Round(Determinants[j, k] - Determinants[j - 1, k] * theRate, ConstDef.Decimals); } } } } } //結果為對角線上的元素的乘積,注意符號的處理。 double theRetDec = theSign; for (int i = 0; i < theN; i++) { theRetDec *= Determinants[i, i]; } return theRetDec; }
2.MyMathLib.LinearAlgebra.EquationsElimination
/// <summary> /// 方程組消元,最後一列為係數,結果就在CoefficientDeterminant裡. /// 本演算法也可以用來求矩陣的秩. /// </summary> /// <param name="CoefficientDeterminant">方程組係數數組</param> public static void EquationsElimination(double[,] CoefficientDeterminant) { var theRowCount = CoefficientDeterminant.GetLength(0); var theColCount = CoefficientDeterminant.GetLength(1); int theN = theRowCount; int theE = theColCount - 1; //從第1列到第theE-1列,最後一列不用處理. for (int i = 0; i < theE; i++) { //從第theN-1行到第1行,將D[j,i]依次變為0,需要注意的是: //如果第j-1行,的左元素全部為0,才能繼續交換. for (int j = theN - 1; j > 0; j--) { //如果為當前值為0,則不處理,繼續處理上一行 if (CoefficientDeterminant[j, i] == 0) { continue; } //******這裡增加修改了判斷是否繼續交換消元的條件: //如果左上鄰元素[j-1, i-1]以及其左邊的元素都為0方可交換 //因為當前元素的左邊元素已經全部是零,因此如果要交換不能使本行左邊產生非零數, //則需要左上鄰及其所有元素皆為0. var theCanDo = true; for (int s = i - 1; s >= 0; s--) { if (CoefficientDeterminant[j - 1, s] != 0) { theCanDo = false; break; } } if (theCanDo) { //如果[j,i]的上一行[j-1, i]的值為0則交換 if (CoefficientDeterminant[j - 1, i] == 0) { for (int k = 0; k <= theE; k++)//這裡要交換常數項,所以是k <= theE { double theTmpDec = CoefficientDeterminant[j, k]; CoefficientDeterminant[j, k] = CoefficientDeterminant[j - 1, k]; CoefficientDeterminant[j - 1, k] = theTmpDec; } } else { //將當前行減去上一行與theRate的積。 //var theRate = CoefficientDeterminant[j, i] / CoefficientDeterminant[j - 1, i]; //for (int k = 0; k <= theE; k++)//這裡要計算常數項,所以是k <= theE //{ // CoefficientDeterminant[j, k] = CoefficientDeterminant[j, k] - CoefficientDeterminant[j - 1, k] * theRate; //} //改進:做乘法,可以避免小數換算帶來的誤差 var theRate2 = CoefficientDeterminant[j, i]; var theRate1 = CoefficientDeterminant[j - 1, i]; for (int k = 0; k <= theE; k++)//這裡要計算常數項,所以是k <= theE { CoefficientDeterminant[j, k] = CoefficientDeterminant[j, k] * theRate1 - CoefficientDeterminant[j - 1, k] * theRate2; } } } } } }