MyMathLib series (determining calculation)
It is better to rely on others to prepare their own MathLib:
////// Determine the number of results calculated. 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 .///Public static partial class LinearAlgebra {////// Calculate the number of reverse orders //////Number Sequence///
Public static int CalcInverseNumber (string Numbers) {int theRet = 0; if (string. isNullOrWhiteSpace (Numbers) {return theRet;} else {string [] theNumbers = Numbers. split (new string [] {",", ""}, StringSplitOptions. removeEmptyEntries); return CalcInverseNumber (theNumbers );}}////// Calculate the number of reverse orders //////Number Sequence///
Public static int CalcInverseNumber (string [] Numbers) {var theRet = 0; if (Numbers. count () <= 0) {return theRet;} else {string [] theNumbers = Numbers. toArray (); int [] theNums = new int [theNumbers. count ()]; for (int I = 0; I <theNumbers. count (); I ++) {theNums [I] = Convert. toInt32 (theNumbers [I]);} for (int theI = 0; theI <theNums. count ()-1; theI ++) {for (int theJ = theI + 1; theJ <theNums. count (); theJ ++) {if (theNums [theI]> theNums [theJ]) {theRet ++ ;}}} return theRet ;} private static string HandlingMultiExp (string Exp, int Sign) {decimal theRetNum = Sign; var theRetLine = ""; var theDigits = Exp. split ('*'); foreach (var theD in theDigits) {decimal theDec = 0; if (decimal. tryParse (theD, out theDec) {theRetNum * = theDec;} else {if (theRetLine = "") {theRetLine = theD ;} else {theRetLine + = "*" + theD ;}}if (theRetNum = 0) {return ";} else {if (theRetNum = 1) {return theRetLine;} if (theRetNum =-1) {return "-" + theRetLine;} return theRetNum. toString () + theRetLine ;}}////// Determine the extension of the expression //////Order N Determinant///
Public static string DeterminantToExpression (string [,] Determinants) {int theR = Determinants. GetLength (0); int theC = Determinants. GetLength (1); if (theR! = TheC) {throw new Exception ("it is not a rank-N criterion! ");} Var theResults = new List
(); List
TheNs = new List
(); For (int I = 1; I <= theC; I ++) {theNs. add (I. toString ();} FullPermutation (theNs, 0, theC-1, theResults); var theNExp = ""; var thePExp = ""; foreach (var theAL in theResults) {var theInverseNum = Convert. toInt32 (Math. pow (-1, CalcInverseNumber (theAL); var theLine = ""; string [] theNums = theAL. split (new string [] {","}, StringSplitOptions. removeEmptyEntries); for (int I = 1; I <= theC; I ++) {var theV = Determinants [I-1, int. parse (theNums [I-1])-1]; theLine + = "*" + theV;} theLine = HandlingMultiExp (theLine. substring (1), theInverseNum); if (! String. isNullOrEmpty (theLine) {if (theLine [0] = '-') {theNExp + = "" + theLine ;} else {thePExp + = "+" + theLine ;}} return thePExp. substring (1) + theNExp ;}///
/// Arrange the array in full //////
Array to be fully arranged///
Start subscript for full sorting///
End subscript for full sortingPublic static void FullPermutation (List
LsArray, int begin, int end, List
Result) {if (begin = end) {string theLine = ""; for (int I = 0; I <= end; I ++) {theLine + = ", "+ lsArray [I];} Result. add (theLine. substring (1) ;}for (int I = begin; I <= end; I ++) {Swap (lsArray, begin, I); FullPermutation (lsArray, begin + 1, end, Result); Swap (lsArray, begin, I );}}///
/// Exchange the subscript of x and y in the array //////
This array///
///
Private static void Swap (List
LsArray, int x, int y) {string t = lsArray [x]; lsArray [x] = lsArray [y]; lsArray [y] = t ;}///
// Calculate the determinant of the triangle method ,//////
Order N Determinant///
Calculation Result
Public static decimal CalcDeterminant (decimal [,] Determinants) {int theSign = 1; // Positive and Negative symbols. If you need to change, the converted symbols are recorded. int theN = Determinants. getLength (0); // 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 (Determinants [j, I] = 0) {continue ;} // if the value of [J-1, I] In the last line of [j, I] is 0, if (Determinants [j-1, I] = 0) {// submit each request In other words, the value of the determining factor remains unchanged, and the opposite value of the symbol is theSign = 0-theSign; for (int k = 0; k <theN; k ++) {decimal theTmpDec = Determinants [j, k]; Determinants [j, k] = Determinants [j-1, k]; Determinants [j-1, k] = theTmpDec ;}} else {// subtract the product of the previous row and theRate from the current row. Var theRate = Determinants [j, I]/Determinants [j-1, I]; for (int k = 0; k <theN; k ++) {Determinants [j, k] = Determinants [j, k]-Determinants [j-1, k] * theRate ;}}// the result is the product of the elements on the diagonal line, note the symbol processing. Decimal theRetDec = theSign; for (int I = 0; I <theN; I ++) {theRetDec * = Determinants [I, I];} return theRetDec ;}}