c - C 中的方阵求逆

标签 c matrix-inverse

我为 n*n 方阵编写了一个求逆函数。

void inverseMatrix(int n, float **matrix)
{
    float ratio,a;
    int i, j, k;

   for(i = 0; i < n; i++)
   {
      for(j = n; j < 2*n; j++)
      {
         if(i==(j-n))
            matrix[i][j] = 1.0;
         else
            matrix[i][j] = 0.0;
      }
  }

  for(i = 0; i < n; i++)
  {
      for(j = 0; j < n; j++)
      {
          if(i!=j)
          {
              ratio = matrix[j][i]/matrix[i][i];
              for(k = 0; k < 2*n; k++)
              {
                  matrix[j][k] -= ratio * matrix[i][k];
              }
          }
      }
  }

  for(i = 0; i < n; i++)
  {
      a = matrix[i][i];
      for(j = 0; j < 2*n; j++)
      {
          matrix[i][j] /= a;
      }
  }

//return matrix;
}

这在几乎所有情况下都可以正常工作,但在某些情况下会失败,如下所示:

1 1 1 0       1 1 1 0
1 1 2 0       1 1 2 0  
1 2 0 1       1 2 1 0
1 2 0 2       1 2 0 2

我忽略的情况可能是什么?

谢谢!

最佳答案

参见http://www.sourcecodesworld.com/source/show.asp?ScriptID=1086 .

使用高斯乔丹算法

#include<stdio.h>
#include<stdlib.h>

int main()
{
    float **A,**I,temp;
    int i,j,k,matsize;

    printf("Enter the size of the matrix(i.e. value of 'n' as size is
nXn):");
    scanf("%d",&matsize);

    A=(float **)malloc(matsize*sizeof(float *));            //allocate memory
dynamically for matrix A(matsize X matsize)
    for(i=0;i<matsize;i++)
        A[i]=(float *)malloc(matsize*sizeof(float));

    I=(float **)malloc(matsize*sizeof(float *));            //memory allocation for
indentity matrix I(matsize X matsize)
    for(i=0;i<matsize;i++)
        I[i]=(float *)malloc(matsize*sizeof(float));

    printf("Enter the matrix: ");                           // ask the user for matrix A
    for(i=0;i<matsize;i++)
        for(j=0;j<matsize;j++)
            scanf("%f",&A[i][j]);

    for(i=0;i<matsize;i++)                                  //automatically initialize the unit matrix, e.g.
        for(j=0;j<matsize;j++)                              //  -       -
            if(i==j)                                        // | 1  0  0 |
                I[i][j]=1;                                  // | 0  1  0 |
            else                                            // | 0  0  1 |
                I[i][j]=0;                                  //  -       -
/*---------------LoGiC starts here------------------*/      //procedure // to make the matrix A to unit matrix

    for(k=0;k<matsize;k++)                                  //by some row operations,and the same row operations of
    {                                                       //Unit mat. I gives the inverse of matrix A
        temp=A[k][k];                   //'temp'  
        // stores the A[k][k] value so that A[k][k]  will not change
        for(j=0;j<matsize;j++)      //during the operation //A[i] //[j]/=A[k][k]  when i=j=k
        {
            A[k][j]/=temp;                                  //it performs // the following row operations to make A to unit matrix
            I[k][j]/=temp;                                  //R0=R0/A[0][0],similarly for I also
R0=R0/A[0][0]
        }                                                   //R1=R1-R0*A[1][0] similarly for I
        for(i=0;i<matsize;i++)                              //R2=R2-R0*A[2][0]      ,,
        {
            temp=A[i][k];                       //R1=R1/A[1][1]
            for(j=0;j<matsize;j++)             //R0=R0-R1*A[0][1]
            {                                   //R2=R2-R1*A[2][1]
                if(i==k)
                    break;                      //R2=R2/A[2][2]
                A[i][j]-=A[k][j]*temp;          //R0=R0-R2*A[0][2]
                I[i][j]-=I[k][j]*temp;          //R1=R1-R2*A[1][2]
            }
        }
    }
/*---------------LoGiC ends here--------------------*/
    printf("The inverse of the matrix is: ");               //Print the //matrix I that now contains the inverse of mat. A
    for(i=0;i<matsize;i++)
    {
        for(j=0;j<matsize;j++)
            printf("%f  ",I[i][j]);
        printf(" ");
    }
    return 0;
}

关于c - C 中的方阵求逆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32043346/

相关文章:

c - 取消引用返回指向 C 中结构的指针的函数

c - 编写基于 Unix 的操作系统

c - 运行时错误,C 中的 NxN 矩阵求逆

c++ - 反转 4x4 矩阵

matlab - MatLab:胆碱必须是正定的

matlab - 矩阵的 det 在 matlab 中返回 0

c - 为什么这个 static const char* 初始化架构特定?

c - 一个线程多次调用一个函数

自定义 shell 仅采用一个参数

matlab - 在 Matlab 中计算逆矩阵