Inverse C-language algorithm for matrices

Source: Internet
Author: User

Inverse C-language algorithm for matrices

I. Solution steps

1. The matrix must be a square (that is, the ranks of the matrix are equal).

2. The sufficient and necessary condition for the invertible matrix is that the value of its determinant is not zero.

3. Find the adjoint matrix of the matrix.

4. Inverse matrix = determinant value of the reciprocal * adjoint matrix.

Two. Algorithms

  

#include <stdio.h>#include<stdlib.h>voidMain () {inti,j; intdimension; DoubleDetervalue=1; Double**array,**deterarray, **companionmatrix, *temp; //declaring functions    voidPrintfdouble2dimension (intSintNDouble**array); DoubleDeter (intDimensionDouble**array); voidCopydouble2dimension (intSintNDouble**source,Double**dest); voidGetcompanionmatrix (intDimensionDouble**array,Double**Companionmatrix); printf ("Please enter the order of the Matrix N:"); scanf ("%d",&dimension); Array=(Double**)malloc(dimension*sizeof(Double*)); Deterarray=(Double**)malloc(dimension*sizeof(Double*)); Companionmatrix=(Double**)malloc(dimension*sizeof(Double*)); //Loop input Phalanx     for(i=0; i<dimension;i++) {Temp=(Double*)malloc(dimension*sizeof(Double)); Deterarray[i]=(Double*)malloc(dimension*sizeof(Double)); Companionmatrix[i]=(Double*)malloc(dimension*sizeof(Double)); printf ("Please enter line%d of the square:", i+1);  for(j=0; j<dimension;j++) scanf ("%LF", temp+j); Array[i]=temp; }    //Copy Arraycopydouble2dimension (Dimension,dimension,array,deterarray); //Print Squareprintf"before the Matrix Elementary transformation is as follows: \ n");        Printfdouble2dimension (Dimension,dimension,array); Detervalue=deter (Dimension,deterarray); printf ("after the elementary transformation of the Phalanx is as follows: \ n");    Printfdouble2dimension (Dimension,dimension,deterarray); if(detervalue==0) {printf ("The square determinant value is 0. \ n"); System ("Pause"); return ; } printf ("determinant value:%.2lf\n", Detervalue); //seeking adjoint matricesGetcompanionmatrix (Dimension,array,companionmatrix); //Print adjoint matrixprintf"The accompanying matrix is as follows: \ n");    Printfdouble2dimension (Dimension, Dimension, Companionmatrix); System ("Pause");}//seeking adjoint matricesvoidGetcompanionmatrix (intDimensionDouble**array,Double**Companionmatrix) {    intI,j,k,l,m,n,o; intFlag//symbols of the symbolic algebraic cofactor type    Double**companiontemp; DoubleDeter (intDimensionDouble**array); Companiontemp=(Double**)malloc((dimension-1)*sizeof(Double*));  for(i=0; i<dimension-1; i++) Companiontemp[i]=(Double*)malloc((dimension-1)*sizeof(Double));  for(i=0; i<dimension;i++)    {         for(j=0; j<dimension;j++) {flag= (i+j)%2==0?1:-1;  for(k=0, m=0; k<dimension;k++)            {                if(k==i)Continue;  for(l=0, n=0; l<dimension;l++)                {                    if(L==J)Continue; * (* (companiontemp+m) +n) = * (* (ARRAY+K) +l); N++; } m++; }            //line I, the algebraic cofactor of column J is assigned to line J, column I* (* (companionmatrix+j) +i) = flag * Deter (dimension-1, companiontemp); }    }}/** Calculate the determinant value*/DoubleDeter (intDimensionDouble**Array) {    inti,j,k,l,b; intFlag =1; Doublesum=1; Doubletemp;  for(i=0, j;i<dimension-1; i++) {J=i; if(* (* (array+i) +j) = =0) {b=0;  for(k=i+1; k<dimension;k++)            {                if(* (* (ARRAY+K) +j)! =0)//find a row that is not 0 and then wrap                {                     for(l=j;l<dimension;l++) {Temp=* (* (array+k) +l); * (* (array+k) +l) = * (* (array+i) +l); * (* (array+i) +l) =temp; } Flag*=-1; b=1;  Break; }            }            if(!b) {return 0; } I--; Continue; }         for(; j<dimension-1; j + +)        {            if(* (* (* (array+j+1) +i) = =0)                 Continue; Temp=-* (* (array+j+1) +i)/* (* (array+i) +i);  for(k=i;k<dimension;k++)                * (* (array+j+1+K) + = * (* (array+i) +k) *temp; }    }         for(i=0; i<dimension;i++) Sum*= * (* (array+i) +i); returnsum*Flag;}//Print ArrayvoidPrintfdouble2dimension (intSintNDouble**Array) {    //printf ("%d,%d", s,n);    inti,j;  for(i=0; i<s;i++)    {         for(j=0; j<n;j++) {printf ("%6.2LF", * (* (array+i) +j)); } printf ("\ n"); }}//Copy ArrayvoidCopydouble2dimension (intSintNDouble**source,Double**dest) {    inti,j;  for(i=0; i<s;i++)    {         for(j=0; j<n;j++)        {            * (* (dest+i) +j) =* (* (source+i) +j); }    }}

Three. Procedures

1> Exercise 3.3-2 (2)

2> Exercise 3.3-2 (3)

3> Exercise 3.3-2 (4)

4> Exercise 3.3-2 (5)

5> Exercise 3.3-7 (2). 1

6> Exercise 3.3-7 (2). 2

Inverse C-language algorithm for matrices

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.