MyMathLib series (determining factor calculation 4 -- Vector part)

Source: Internet
Author: User

MyMathLib series (determining factor calculation 4 -- Vector part)

1) Remove a vector group and convert it into a step matrix. This is the basic algorithm for finding a large linear irrelevant group of a vector group. This method has been provided before, but it has been improved here to determine whether linear correlation is possible:

////// 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 .//////Equations coefficient ArrayPublic static void EquationsElimination (decimal [,] CoefficientDeterminant) {var theRowCount = CoefficientDeterminant. getLength (0); var theColCount = CoefficientDeterminant. getLength (1); int theN = theRowCount; int theE = theColCount-1; // from column 1st to column theE-1, 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 If the current value is 0, do not process, continue to process the previous line if (CoefficientDeterminant [j, I] = 0) {continue;} // if the upper left adjacent 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. for (int s = I-1; s> = 0; s --) {if (CoefficientDeterminant [j-1, s]! = 0) {break;} // if the value of [J-1, I] in the previous line of [j, I] is 0, if (CoefficientDeterminant [j-1, i] = 0) {for (int k = 0; k <= theE; k ++) // here the constant is exchanged, so k <= theE {decimal theTmpDec = CoefficientDeterminant [j, k]; CoefficientDeterminant [j, k] = Hangzhou [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 ;}}}}}

This elimination method is also used in the process of rank calculation of the matrix. However, the rank calculation of the matrix is slightly modified on this algorithm, and another method is adopted. The following is a vector-related algorithm:

/* Vector. cs * Albert. Tian on 20141225 */using System; using System. Collections. Generic; using System. Linq; using System. Text; namespace MyMathLib {////// Vector correlation operation, which does not give a basic transformation to the vector. Because // vector groups can be expressed in a matrix, many vector operations are ultimately attributed to matrix operations. /// This algorithm is based on the textbook algorithm to find the maximum linear irrelevant group. It is not the simplest method. /// in matrix expression, based on the elementary transformation of the matrix, it is easy to obtain a maximum number of linear independent groups. ///Public class Vectors {////// Determine whether all elements from 0 to the specified delimiter are 0 .//////Current Vector///End Index///
 Public static bool IsZeroVector (decimal [] CurrVector, int EndIndex = 0) {int theEndIndex = EndIndex; if (theEndIndex = 0) {theEndIndex = CurrVector. length;} var theIsZeroVector = true; for (int I = 0; I <theEndIndex; I ++) {if (curisrvector [I]! = 0) {theIsZeroVector = false; break ;}} return theIsZeroVector ;}////// Determine whether the current vector is linearly related to the previous vector, using deelemental form //////Current Vector///Existing linear independent vector groups///
 Private static bool IsLinearCorrelation (decimal [] CurrVector, List
 
  
PrevMaxLIVectors) {// The zero vector must be linearly related var theIsZeroVector = IsZeroVector (CurrVector); if (theIsZeroVector) {return true;} // if there is no vector above, the current non-zero vector must be linearly independent. if (PrevMaxLIVectors. count <= 0) {return false;} // construct the equation and determine whether linear correlation var theECount = CurrVector. length; var theVCount = PrevMaxLIVectors. count; // Add the current vector as a constant vector, so the column of the equations is theVCount + 1. var theEquealGroup = new decimal [theECount, theVCount + 1]; // import PrevMaxLIVectors for (int I = 0; I <theVCount; I ++) {for (int j = 0; j <theECount; j ++) {theEquealGroup [j, I] = PrevMaxLIVectors [I] [j] ;}} // Add the current vector as the constant vector for (int j = 0; j <theECount; j ++) {theEquealGroup [j, theVCount] = CurrVector [j];} // demeta. The elimination method here is the basic algorithm used to calculate the Matrix Rank. linearAlgebra. equationsElimination (theEquealGroup); // If theRA
  
   
= TheRA1, there is at least one non-zero solution. // The judgment here is mainly due to the method of RMB elimination. Of course, this is evidenced in the matrix. Var theRA = 0; var theRA1 = 0; for (int I = 0; I <theECount; I ++) {if (! IsZeroVector (theEquealGroup. GetVector (I), theVCount) {theRA ++;} if (! IsZeroVector (theEquealGroup. GetVector (I) {theRA1 ++ ;}} return (theRA >= theRA1 );}///
   /// This function is useless for the moment. Determine the equivalence of the two vectors //////
   ///
   ///
   Private static bool IsEquivalent (decimal [] V1, decimal [] V2) {var theV1IsZeroVector = IsZeroVector (V1); var theV2IsZeroVector = IsZeroVector (V2 ); // both are 0 vectors. if (theV1IsZeroVector & theV2IsZeroVector) {return true ;}// one of the zero vectors and the other is a non-zero vector if (theV1IsZeroVector | theV2IsZeroVector) {return ;} // a component must be proportional to if (V1.Length <= 1) {return true;} decimal thePreRate = 0; var theCount = V1.Length; var TheIndex = 0; for (int I = 0; I <theCount; I ++) {// if both are equal to 0, if (V1 [I] = V2 [I] & V2 [I] = 0) {continue;} // if one of them is 0, it is not proportional. It is not equivalent to if (V1 [I] = 0 | V2 [I] = 0) {return false;} if (theIndex = 0) {thePreRate = V1 [I]/V2 [I]; theIndex ++;} else {if (thePreRate! = V1 [I]/V2 [I]) {return false ;}} return true ;}///
   /// Obtain the maximum linear independent group of the vector group. The method is written according to the textbook. //////
   Vector Group///
   Public static List
   
    
GetMaxLinearIndependentGroup (decimal [,] Vectors) {// number of Vectors var theVCount = Vectors. GetLength (0); // number of elements var theECount = Vectors. GetLength (1); List
    
     
TheTempVectors = new List
     
      
(); For (int I = 0; I <theVCount; I ++) {var theTempVector = Vectors. getVector (I); // if the current vector cannot be listed in the linear form of the preceding vector, it is added to the large group. // can it be listed in a linear form, in fact, after the current vector is added to the preceding vector group, // or is it not linearly related if (! IsLinearCorrelation (theTempVector, theTempVectors) {theTempVectors. Add (theTempVector) ;}} return theTempVectors ;}}}
     
    
   
  
 


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.