Open source math.net basic Math Class Library use C # to calculate matrix rank

Source: Internet
Author: User
Tags rowcount

Original: "Original" open source math.net basic Math class library using C # computing matrix rank

Total contents of this blog article category: http://www.cnblogs.com/asxinyu/p/4288836.html

Open source math.net basic Math Class library use total directory: http://www.cnblogs.com/asxinyu/p/4329737.html

The basic usage of math.net was introduced last month, which includes matrix, vector related operation, analytic data format, numerical integration, data statistic, correlation function, solving linear equation Group and random number generator. This month then dig deep into the various functions of the math.net, and the source code analysis, so that you can use the math.net in the. NET platform to easily develop mathematical calculations related, or can be quickly transplanted into their own system of the source (sometimes do not need all the functions, Only some of these feature codes are needed, today is the ability to compute the rank of matrices using C # in Math.net.

This text address: http://www.cnblogs.com/asxinyu/p/4304304.html

1. What is matrix rank

The rank of matrices is an important concept to reflect the intrinsic characteristics of matrices. In linear algebra, the column rank of a matrix A is the maximum number of linearly unrelated columns of a. Similarly, the row rank is the maximum number of linearly unrelated rows of a. The column rank and row rank of a matrix are always equal, so they can be simply referred to as the rank of matrix A. Usually expressed as r (a), RK (a) or rank A. The line rank of matrix is equal to the rank of column, which is an important part of the basic theorem of linear algebra. The basic proof of the idea is that the matrix can be regarded as the transformation matrix of linear mapping, the column rank is a dimension of the image space, the row rank is a dimension of the non-0 image space, so the column rank is equal to the row rank, that is, the dimension of the space is equal to the dimension of the non-0 image space (the non-0 image space here refers to the quotient space after about 0 space: the image space). This can be seen from the singular value decomposition of the matrix. The easiest way to calculate the rank of a matrix is Gaussian elimination, which refers to Wikipedia's content:

The easiest way to calculate the rank of matrix A is Gaussian elimination, that is, using the elementary transformation of the matrix to generate a row ladder matrix, because the elementary transformation of the matrix does not change the rank of the matrix, so A's row ladder array form has the same rank as a. The number of non-0 rows of the matrix after the elementary transformation is the rank of the original matrix. Consider, for example, a 4x4 matrix:

We see that the 2nd column is twice times the 1th column, and the 4th column equals the sum of the 1th and 3rd columns. The 1th and 3rd columns are linearly independent, so the rank of a is 2. This can be verified with a Gaussian algorithm. It produces the following form of a row ladder array:

It has two non-zero rows. When applying floating-point numbers on a computer, the basic Gaussian elimination (LU decomposition) may be unstable and should be decomposed using rank revelation (revealing). An effective alternative is singular value decomposition (SVD), but there are less costly options, such as QR decomposition with fulcrum (pivoting), which is also stronger than Gaussian elimination in numerical values. The numerical determination of rank requires a value such as whether a singular value from SVD is zero, and the actual selection depends on the matrix and the application.

http://zh.wikipedia.org/wiki/Rank _ (linear algebra)

The application of matrix rank in linear algebra is still very wide, such as calculating the number of solutions of linear equations, and the following is a look at the implementation of this process in math.net and how to invoke the example.

The realization of 2.math.net matrix rank calculation

Math.net in the calculation of the rank of the matrix, and the determinant of the implementation of a very similar, but also as a matrix calculation of a small part of the function, as a property added in the various matrix decomposition algorithm abstraction and implementation class, look at one of the SVD decomposition algorithm abstract, because the calculation is simple, has directly realized the rank of the calculation, inheriting class can directly Use, that's enough, the other uses are similar to the determinant below.

1 Internal Abstract classsvd:svd<float>2 {3     protectedSVD (vector<float> S, matrix<float> U, matrix<float> VT,BOOLvectorscomputed)4:Base(S, U, VT, vectorscomputed) {}5 6     /// <summary>Calculate matrix Rank</summary>7     /// <value>The number of non-negligible singular values.</value>8      Public Override intRank9     {Ten         Get One         { A             returnS.count (t =!) Math.Abs (t). Almostequal (0.0f)); -         } -     } the      Public Override DoubleL2norm -     { -         Get{returnMath.Abs (s[0]);} -     } +  -      Public Override floatConditionnumber +     { A         Get at         { -             varTMP = Math.min (U.rowcount, VT. ColumnCount)-1; -             returnMath.Abs (s[0]) /Math.Abs (s[tmp]); -         } -     } -     /// <summary>Calculate determinant</summary> in      Public Override floatdeterminant -     { to         Get +         { -             if(U.rowcount! =VT. ColumnCount) the             { *                 Throw NewArgumentException (resources.argumentmatrixsquare); $             }Panax Notoginseng  -             varDet =1.0; the             foreach(varValueinchS) +             { ADet *=value; the                 if(Math.Abs (value). Almostequal (0.0f)) +                 { -                     return 0; $                 } $             } -             returnConvert.tosingle (Math.Abs (DET)); -         } the     } -}
3.math.net code to calculate the rank of a matrix

The above process and principle is just easy to understand its implementation process, the following is a simple demonstration of the calculation of the matrix rank in the math.net process, is directly called calculation.

1 //format Settings2 varFormatProvider =(CultureInfo) CultureInfo.InvariantCulture.Clone ();3FormatProvider.TextInfo.ListSeparator =" ";4 5 //Create a random matrix6 varMatrix =NewDensematrix (5);7 varRnd =NewRandom (1); 8  for(vari =0; I < Matrix. RowCount; i++)9 {Ten      for(varj =0; J < Matrix. ColumnCount; J + +) One     { AMatrix[i, J] =Rnd. Nextdouble (); -     } - } the  -Console.WriteLine (@"Initial Matrix"); -Console.WriteLine (Matrix. ToString ("#0.00\t", formatprovider)); - Console.WriteLine (); + //1. Rank -Console.WriteLine (@"the result of the matrix rank calculation is:"); + Console.WriteLine (Matrix. Rank ()); AConsole.WriteLine ();

The results are as follows:

1 Initial Matrix2Densematrix 5x5-Double3 0.25      0.11    0.47    0.77    0.664 0.43      0.35    0.94    0.10    0.645 0.03      0.25    0.32    0.99    0.686 0.65      0.28    0.62    0.70    0.707 0.95      0.09    0.16    0.38    0.808 9 Ten the result of the matrix rank calculation is: One 5
4. Resources

Including source code and case can go to the official website to download, this series of articles in the directory of the first article: Http://www.cnblogs.com/asxinyu/p/4264638.html, there is an introduction. Because the source code is very big, if cannot find the corresponding case, can carry on the search, can find the corresponding code relatively quickly.

Open source math.net basic Math Class Library use C # to calculate matrix rank

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.