MyMathLib series (corrected two functions) and mymathlib correction

Source: Internet
Author: User

MyMathLib series (corrected two functions) and mymathlib correction

There is a problem with the judgment condition in the determining element. The corrected code is provided here:

1) MyMathLib. LinearAlgebra. CalcDeterminant Method

 
/// <Summary> // calculate the determinant of the triangle method, /// </summary> /// <param name = "Determinants"> rank N </param> /// <returns> calculation result </returns> public static double calcDeterminant (double [,] 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 One line if (Determinants [j, I] = 0) {continue;} // revised part: // if the top left neighbor element [J-1, the I-1] And the elements on its left are both 0 to be exchanged // since all the elements on the left of the current element are zero, so if you want to swap it, it cannot generate a non-zero number on the left of the row, // The upper left neighbor and all its elements must be 0. var theCanDo = true; for (int s = I-1; s> = 0; s --) {if (Determinants [j-1, s]! = 0) {theCanDo = false; break;} if (theCanDo) {// if the previous line of [j, I] [J-1, if the value of I] is 0, if (Determinants [j-1, I] = 0) is exchanged {// each time the value of the determinant remains the same, the opposite sign is 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 {// subtract the product of the previous row and theRate from the current row. 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) ;}}}}// the result is the product of the elements on the diagonal line. Pay attention to symbol processing. Double theRetDec = theSign; for (int I = 0; I <theN; I ++) {theRetDec * = Determinants [I, I];} return theRetDec ;}


2. MyMathLib. LinearAlgebra. EquationsElimination

/// <Summary> // the element of the equations. The final column is the coefficient, and the result is in CoefficientDeterminant. /// this algorithm can also be used to calculate the rank of the matrix. /// </summary> /// <param name = "CoefficientDeterminant"> array of equations coefficient </param> public static void EquationsElimination (double [,] CoefficientDeterminant) {var theRowCount = CoefficientDeterminant. getLength (0); var theColCount = CoefficientDeterminant. getLength (1); int theN = theRowCount; int theE = theColCount-1; // from column 1st TO E-1 column, the last column does not need to be processed. for (int I = 0; I <theE; I ++) {// from row theN-1 to row 1st, change D [j, I] to 0 in sequence, it should be noted that: // if the first J-1 line, all the left elements are 0 to continue switching. for (int j = theN-1; j> 0; j --) {// if the current value is 0, it is not processed. Continue to process the previous line if (CoefficientDeterminant [j, i] = 0) {continue;} // ***** added here the condition for determining whether to continue switching the consumer element: // if the last left neighbor element [J-1, the I-1] And the elements on its left are both 0 to be exchanged // since all the elements on the left of the current element are zero, so if you want to swap it, it cannot generate a non-zero number on the left of the row, // The upper left neighbor and all its elements must be 0. var theCanDo = true; for (int s = I-1; s> = 0; S --) {if (CoefficientDeterminant [j-1, s]! = 0) {theCanDo = false; break;} if (theCanDo) {// if the previous line of [j, I] [J-1, if the value of I] is 0, if (CoefficientDeterminant [j-1, I] = 0) {for (int k = 0; k <= theE; k ++) // here the constant is exchanged, So k <= theE {double 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 <= theE; k ++) // here the constant term is calculated, so k <= theE // {// CoefficientDeterminant [j, k] = CoefficientDeterminant [j, k]-CoefficientDeterminant [j-1, k] * theRate; //} // improvement: Multiplication can avoid the error var theRate2 = CoefficientDeterminant [j, I] caused by decimal conversion. var theRate1 = CoefficientDeterminant [j-1, I]; for (int k = 0; k <= theE; k ++) // The constant term is calculated here, so k <= theE {CoefficientDeterminant [j, k] = CoefficientDeterminant [j, k] * theRate1-CoefficientDeterminant [j-1, k] * theRate2 ;}}}}}}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.