MyMathLib series (determining factor calculation), mymathlib determining factor

Source: Internet
Author: User

MyMathLib series (determining factor calculation), mymathlib determining factor

It's better to rely on others to prepare their own MathLib. When I was at school, I thought about computer-based mathematical things, but I never had time to do this, now I feel that I am idle in my spare time. I will do this first, starting with linear algebra. After all, many of the algorithms here are useful in practice. In the process of making these algorithms, I also realized that the things in mathematics are not useless, but you are useless. The following algorithms (except for full sorting) are all originally created and inefficient. Please share some of them with us. Well, let's talk about the Code as follows:

/// <Summary> /// calculate the determinant. This program is part of MyMathLib. You are welcome to use it. For more information, see. /// 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 {// <summary> /// calculate the number of reverse orders /// </summary> // <param name =" numbers "> Number Sequence </param> // <returns> </returns> public static int CalcInverseNumber (string Numbers) {int theRet = 0; if (string. isNullOrWhiteSpace (Numbers) {return theRet;} else {string [] theNumbers = Numbers. split (new str Ing [] {",", ""}, StringSplitOptions. removeEmptyEntries); return CalcInverseNumber (theNumbers );}} /// <summary> /// calculate the number of reverse orders /// </summary> /// <param name = "Numbers"> Number Sequence </param> /// <returns> </returns> public static int CalcInverseNumber (string [] Numbers) {var theRet = 0; if (Numbers. count () <= 0) {return theRet;} else {string [] theNumbers = Numbers. toArray (); int [] theNums = new int [theNumbe Rs. 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 ;}} /// <summary> /// expand the expression of the determining factor /// </summary> /// <param name = "Determinants"> level N determining factor </param> /// <returns> </returns> 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 <string> (); List <string> theNs = new List <string> (); 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 strin G [] {","}, 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 ;} /// <summary> /// arrange the array in full /// </summary> /// <param name = "lsArray"> array to be fully arranged </ param> /// <param name = "begin"> Start subscript for full sorting </param> // <param name = "end"> end subscript for full sorting </param> public static void FullPermutation (List <string> lsArray, int begin, Int end, List <string> 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) ;}/// <summary> // The subscript in the exchange array is x, y value // </summary> /// <param name = "lsArray"> This array </param> /// <Param name = "x"> </param> // <param name = "y"> </param> private static void Swap (List <string> lsArray, int x, int y) {string t = lsArray [x]; lsArray [x] = lsArray [y]; lsArray [y] = t ;} /// <summary> // calculate the determinant of the triangle method, /// </summary> /// <param name = "Determinants"> rank N </param> /// <returns> calculation result </returns> public static decimal calcDeterminant (decimal [,] determinants) {int theSign = 1; // Positive and Negative symbols, if you need to change To record the converted symbols. 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) {// during each exchange, the value of the determinant remains the same, and the symbol is reversed to 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 ;}}

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.