MyMathLib series (determining factor calculation 3), mymathlib determining factor
Today, the determining factor and linear equations are complete.
Using System; using System. collections. generic; using System. linq; using System. text; namespace MyMathLib {// <summary> // calculates the determinant. This program is part of MyMathLib. You are welcome to use it for reference and comments. /// If you have time to rewrite it in the function language, you have to perform your own MathLib. The algorithm in it has been verified, but it has not passed the // strict test. If you need to refer to it, please be careful. /// </summary> public static partial class LinearAlgebra {# region linear equations /// <summary> /// calculate the maximum value based on the Laplace theorem. /// </Summary> /// <param name = "Determinants"> rank N </param> /// <returns> calculation result </returns> public static decimal calcDeterByLaplaceLaw (decimal [,] determinants) {var n = Determinants. getLength (0); // if the order is smaller than 3, it is unnecessary to use Laplace to expand if (n <= 3) {return CalcDeterminantAij (Determinants, false );} var theRows = GetLaplaceRowsOdd (n); return CalcDeterByLaplaceLaw (Determinants, theRows); }/// <summary> // solve the linear equations. Here Require N /// </summary> /// <param name = "CoefficientDeterminant"> coefficient determining factor of linear equations </param> /// <param name = "ConstantTerms"> constant item </param> /// <returns> </returns> public static decimal [] LinearEquations (int UnknownElements, decimal [,] CoefficientDeterminant, decimal [] ConstantTerms) {var theRowCount = CoefficientDeterminant. getLength (0); var theColCount = CoefficientDeterminant. getLength (1); if (UnknownElements = = TheRowCount & theColCount = UnknownElements) {var theD = CalcDeterByLaplaceLaw (CoefficientDeterminant); if (theD = 0) {return null ;} decimal [] theResults = new decimal [UnknownElements]; for (int I = 1; I <= UnknownElements; I ++) {// replace column I, note that the original value is saved. var theTemp = new decimal [UnknownElements]; for (int j = 1; j <= UnknownElements; j ++) will be restored in the next calculation) {theTemp [J-1] = CoefficientDeterminant [J-1, I-1]; C OefficientDeterminant [j-1, I-1] = ConstantTerms [j-1];} var theDi = CalcDeterByLaplaceLaw (CoefficientDeterminant)/theD; theResults [I-1] = theDi; // determine the coefficient of recovery. for (int j = 1; j <= UnknownElements; j ++) {CoefficientDeterminant [j-1, I-1] = theTemp [j-1];} return theResults;} else {throw new Exception ("the parameter format is incorrect! ") ;}/// <Summary> // solves the linear equations (elimination method). This method is similar to the method used to calculate the determinant of a triangle. Here, the number of equations and the number of elements must be the same. /// If the number of equations is smaller than the number of elements, the elimination of elements is okay, but it involves general solutions and symbol operations. This is not considered here. /// </summary> /// <param name = "CoefficientDeterminant"> coefficient determining factor of linear equations, the rightmost N + 1 column is a constant </param> // <returns> </returns> public static decimal [] LinearEquationsEM (int UnknownElements, decimal [,] CoefficientDeterminant) {var theRowCount = CoefficientDeterminant. getLength (0); var theColCount = CoefficientDeterminant. getLength (1); if (UnknownElements = theRowCou Nt & theColCount = UnknownElements + 1) {decimal [] theResults = new decimal [UnknownElements]; int theN = UnknownElements; // from column 1st to column theN-1 for (int I = 0; I <theN-1; I ++) {// from row theN-1 to row I + 1, change D [j, I] to 0 for (int j = theN-1; j> I; j --) {// if the current value is 0, it will not be processed. continue to process the previous line if (CoefficientDeterminant [j, I] = 0) {continue ;} // if the value of [J-1, I] in the previous line of [j, I] is 0, then if (CoefficientDeterminant [j-1, I] = 0) {(Int k = 0; k <= theN; k ++) // here the constant is exchanged, So k <= theN {decimal theTmpDec = CoefficientDeterminant [j, k]; coefficientDeterminant [j, k] = CoefficientDeterminant [j-1, k]; CoefficientDeterminant [j-1, k] = theTmpDec ;}} else {// subtract the product of the previous row and theRate from the current row. Var theRate = CoefficientDeterminant [j, I]/CoefficientDeterminant [j-1, I]; for (int k = 0; k <= theN; k ++) // here the constant term is calculated, so k <= theN {CoefficientDeterminant [j, k] = CoefficientDeterminant [j, k]-CoefficientDeterminant [j-1, k] * theRate ;}}}// processing result if (CoefficientDeterminant [UnknownElements-1, UnknownElements-1] = 0) {if (CoefficientDeterminant [UnknownElements-1, unknownElements] = = 0) {throw new Exception ("invalid equation, infinite solutions! ");} Else {throw new Exception (" no solution to the equation! ") ;}}// Result processing, return to for (int I = UnknownElements-1; I> = 0; I --) {// calculate the evaluated item decimal theTempDec = 0; for (int j = I + 1; j <theN; j ++) {theTempDec + = CoefficientDeterminant [I, j] * theResults [j];} // calculation result, if the coefficient is 0, it is invalid equation if (CoefficientDeterminant [I, I] = 0) {throw new Exception ("invalid equation ");} theResults [I] = (CoefficientDeterminant [I, UnknownElements]-theTempDec)/CoefficientDeterminant [I, I];} re Turn theResults;} else {throw new Exception ("the parameter format is incorrect! "); }}# Endregion }}