Matrix inversion algorithm and program implementation (c + +)

Source: Internet
Author: User

In the course of doing the project, we encountered the problem of finding polynomial, and used the method of seeking inverse. Matrix inversion generally uses a simple algorithm, there are fast algorithms such as the full selection of the main element Gauss-Jordan elimination method, but this program mainly wrote a simple matrix inverse algorithm definition method of the adjoint matrix inverse formula is as follows, where a reversible:

, where the adjoint matrix is:

1. Given a square, non-singular (not also can, the procedure is considered);

2. The matrix gets its determinant, the value such as | a|;

3. To seek its adjoint matrix;

4. Get its inverse matrix.

The main functions are as follows:

1 //the inverse matrix of the given matrix SRC is saved to Des. 2 BOOLGetmatrixinverse (DoubleSrc[n][n],intNDoubleDes[n][n])3 {4     Doubleflag=Geta (src,n);5     DoubleT[n][n];6     if(flag==0)7     {8         return false;9     }Ten     Else One     { A Getastart (src,n,t); -          for(intI=0; i<n;i++) -         { the              for(intj=0; j<n;j++) -             { -des[i][j]=t[i][j]/Flag; -             } +  -         } +     } A  at  -     return true; -  -}

Calculation | a|:

1 //expand calculation by First line | a|2 DoubleGeta (DoubleArcs[n][n],intN)3 {4     if(n==1)5     {6         returnarcs[0][0];7     }8     DoubleAns =0;9     Doubletemp[n][n]={0.0};Ten     inti,j,k; One      for(i=0; i<n;i++) A     { -          for(j=0; j<n-1; j + +) -         { the              for(k=0; k<n-1; k++) -             { -Temp[j][k] = arcs[j+1[(k>=i) K +1: K]; -  +             } -         } +         Doublet = Geta (temp,n-1); A         if(i%2==0) at         { -Ans + = arcs[0][i]*T; -         } -         Else -         { -Ans-= arcs[0][i]*T; in         } -     } to     returnans; +}

To compute the adjoint matrix:

1 //calculates the cofactor for each element of each column of each row, forming a *2 voidGetastart (DoubleArcs[n][n],intNDoubleAns[n][n])3 {4     if(n==1)5     {6ans[0][0] =1;7         return;8     }9     inti,j,k,t;Ten     DoubleTemp[n][n]; One      for(i=0; i<n;i++) A     { -          for(j=0; j<n;j++) -         { the              for(k=0; k<n-1; k++) -             { -                  for(t=0; t<n-1; t++) -                 { +Temp[k][t] = arcs[k>=i?k+1: k][t>=j?t+1: t]; -                 } +             } A  at  -Ans[j][i] = Geta (temp,n-1); -             if((i+j)%2==1) -             { -Ans[j][i] =-Ans[j][i]; -             } in         } -     } to}

These three functions constitute the inverse matrix of the steps, the author calls and successful use, no fault, in this mutual encouragement. If you have any questions, please leave a message. Thank you

Other matrix inverse good algorithm and code please share.

Matrix inversion algorithm and program implementation (c + +)

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.