Find X in AX=B. Calculate the rank of the matrix, use the Gauss elimination method to find the inverse of A, x = the inverse of A * B


This problem can be solved by adjoint matrix, Gauss elimination and QR decomposition.
I think the online tycoons write very complex and can't understand it. Some tycoons think it's hard to give the Gauss elimination method to the computer
Using QR decomposition method
I prefer to use Gauss elimination method, because I think the adjoint matrix is particularly annoying and QR decomposition method is difficult.
On my Gauss elimination method
First put it in a matrix all (the variable names may be different later, changed)
Notes:

  1. In this case, a and p represent the number in the array, not the variable name
  2. The whole row of the circled number representation matrix all
  3. Under the condition of 0 < = k < 2n, a [j][k] = a[j][k] - (a[j[i]/a[i][i])*a[i][k];
  4. This draft is just a draft. It's bound to be wrong. That's what it means
  5. When calculating, you can only assign the values of array elements to other variables, and use other variables to calculate. You can try, anyway, code blocks can't
  6. If there is a big guy who can use dynamic array, he can input matrix A and matrix B directly without defining n and n1 in macro, and judge the size of n and n1 to calculate. I don't know. I will. Can you teach me
  7. I wanted to do it with a function, but it was difficult to return it, so I did it directly in the main function

Direct mapping

Personally, I think my notes are very clear and detailed, just a little Chinglish.

Look at code

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define n 4
#define n1 2
int main()
{
    double A[n][n] = {{1,3,2,13},{7,2,1,-3},{9,15,3,-2},{-2,-2,11,5}};//you can change  matrix A and B ,number n and n1 to do other matrix
    double B[n][n1] = {{9,0},{6,4},{11,7},{-2,-1}};
    double X[n][n1];
    double a[n][2*n]={{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0}};
    int i,j,k;
    double t,t1,t2;
    //calculate rank first
    //but I think maybe we dont have to do that,because when the rank doesnt comfort to the question, we get answer like    -1.#IND00
    //we can still calculate the rank as we wish
    for(i=0;i<n;i++)
    {
        t=A[i][i];//get the numbers from diagonal line
        for(j=i+1;j<n;j++)
        {
             t1=A[j][i];//get the each number below A[i][i]
                    for(k=0;k<n;k++)//do this for every number in each row
                    {
                        t2=A[i][k];//get each number in the array
                        A[j][k] = A[j][k] - (t1/t)*t2;//let numbers below the diagonal line to be 0
                    }
        }
    }
    int count=0;//to figure out how many zeros are there in the diagonal line
    for(i=0;i<n;i++)
    {
        if(A[i][i]==0) count++;//This place can be changed. If you change it to if(A[i][i]!=0) count + +, then delete the following line, and print count after the for loop ends to know the rank of the matrix
        if(count==1) {printf("this A The rank of is not satisfied with the condition of inverse matrix");return 0;}
    }

    for(i=0;i<n;i++)//this loop get the whole matrix to do the Gaussian elimination
        {
            for(j=0;j<n;j++)
            {
                a[i][j]=A[i][j];
            }
            a[i][n+i]=1;
        }// now the all matrix is ready to figure out the reverse matrix of A matrix
    for(i=0;i<n;i++)
    {
        t=a[i][i];//get the number in the diagonal line
        for(j=0;j<n;j++)
        {
            if(j!=i)//every row that is not row i
            {
                t1=a[j][i];//get the numbers in the column except those belong to the diagonal line
                for(k=0;k<2*n;k++)//do the math for the whole row
                {
                    t2=a[i][k];
                    a[j][k] = a[j][k] - (t1/t)*t2;// you can only do math when you have variables, you cannot use the array elements directly to do the math
                }
            }
        }
    }
    //now we have the diagonal matrix
    for(i=0;i<n;i++)//For each number in a row, divide by the value on the diagonal of each row of the diagonal matrix on the left
    {
        t=a[i][i];
        for(k=0;k<2*n;k++)
        {
            a[i][k] = a[i][k]/t;
        }
    }
    for(i=0;i<n;i++)
    {
        for(j=0;j<n;j++)
        {
            A[i][j] = a[i][j+n];
        }
    }
    //Now we already get the reverse matrix A
    //use A to left multiply B to get X
    double sum;
    for(k=0;k<n1;k++)
    {
        for(i=0;i<n;i++)
        {
            sum=0;
            for(j=0;j<n;j++)
            {
                t1=A[i][j];
                t2=B[j][k];
                sum=sum+t1*t2;
            }
            X[i][k]=sum;
        }
    }
    for(i=0;i<n;i++)
    {
        for(k=0;k<n1;k++)
        {
            t=X[i][k];
            printf("%lf\t",t);
        }
        printf("\n");
    }
    return 0;
}

Published 2 original articles, won praise 0, visited 4
Private letter follow

Posted on Thu, 16 Jan 2020 03:39:25 -0800 by snowplank