求矩阵的逆

  1 //***************************
  2 //求任何一个矩阵的逆矩阵
  3 //***************************
  4 #include <stdio.h>
  5 #include <malloc.h>
  6 
  7 void main( void )
  8 {
  9      float *buffer,*p;   //定义数组首地址指针变量
 10      short int row,num; //定义矩阵行数row及矩阵元素个数
 11      short int i,j;
 12      float determ;      //定义矩阵的行列式
 13 
 14      float comput_D(float *p,short int n);      //求矩阵的行列式
 15      float Creat_M(float *p, short int m,short int n,short int k); //求代数余子式
 16      void Print( float *p,short int n);     //打印n×n的矩阵
 17 
 18      printf("
Please input the number of rows: ");
 19      scanf("%d",&row);
 20     
 21      num=2 * row * row;
 22      buffer = (float *)calloc(num, sizeof(float));     //分配内存单元
 23 
 24      p=buffer;
 25      if(p != NULL)
 26      {
 27          for(i=0;i<row;i++)                   //输入各单元值
 28          {
 29              printf("Input the number of %d row ",i+1);
 30              for(j=0;j<row;j++)
 31              {
 32                  scanf("%f",p++);
 33              }
 34          }  
 35      }
 36      else
 37          printf( "Can't allocate memory
" );
 38 
 39      printf("
The original matrix is:
");
 40      Print(buffer,row);     //打印该矩阵
 41 
 42      determ=comput_D(buffer,row);     //求整个矩阵的行列式
 43      p=buffer + row * row;
 44      if (determ != 0)
 45      {
 46          for (i=0;i<row; i++)       //求逆矩阵
 47              for (j=0; j<row; j++)
 48                     *(p+j*row+i)=   Creat_M(buffer,i,j,row)/determ;    
 49             
 50          printf("The determinant is %G
",determ);
 51 
 52          p=buffer + row * row;
 53          printf("
The inverse matrix is:
"); 
 54          Print(p,row);     //打印该矩阵
 55      }
 56      else
 57          printf("The determnant is 0, and there is no inverse matrix !
");
 58      free( buffer );
 59 }
 60 //--------------------------------------------------------
 61 //功能:求矩阵 n X n 的行列式
 62 //入口参数:矩阵首地址 p;矩阵行数 n
 63 //返回值:矩阵的行列式值
 64 //--------------------------------------------------------
 65 float comput_D(float *p,short int n)  
 66 {
 67      short int i,j,m;         //i--row; j--column
 68      short int lop=0;
 69      float result=0;
 70      float mid=1;
 71     
 72      if (n!=1)
 73      {
 74          lop=(n==2)?1:n;     //控制求和循环次数,若为2阶,则循环1次,否则为n次
 75 
 76          for(m=0;m<lop;m++)
 77          {
 78              mid=1;          //顺序求和
 79              for(i=0,j=m;i<n;i++,j++)
 80                  mid = mid * ( *(p+i*n+j%n) );
 81              result+=mid;
 82          }
 83 
 84          for(m=0;m<lop;m++)
 85          {                       
 86              mid=1;          //逆序相减
 87              for(i=0,j=n-1-m+n; i<n; i++,j--)
 88                  mid=mid * ( *(p+i*n+j%n));
 89              result-=mid;
 90          }
 91         }
 92      else result=*p;
 93      return(result);
 94 }
 95 //----------------------------------------------------
 96 //功能:求k×k矩阵中元素A(mn)的代数余子式
 97 //入口参数:k×k矩阵首地址;元素A的下标m,n; 矩阵行数 k
 98 //返回值: k×k矩阵中元素A(mn)的代数余子式
 99 //----------------------------------------------------
100 float Creat_M(float *p, short int m,short int n,short int k)
101 {
102      short int len;
103      short int i,j;
104      float mid_result=0;
105      short int quo=1;
106      float *p_creat,*p_mid;
107 
108      len=(k-1)*(k-1);
109      p_creat = (float *)calloc(len, sizeof(float));     //分配内存单元
110      p_mid=p_creat;
111      for(i=0;i<k;i++)
112          for(j=0;j<k;j++)
113          {
114              if (i!=m && j!=n)
115                  *p_mid++ =* (p+i*k+j);            
116          } 
117      //     Print(p_creat,k-1);
118      quo = (m + n) %2==0 ? 1:-1; 
119      mid_result = (float ) quo * comput_D(p_creat,k-1);
120      free(p_creat);
121      return(mid_result);
122 }
123 //-------------------------------------------
124 //功能:打印n×n的矩阵
125 //入口参数:n×n矩阵的首地址;该矩阵的行数 n
126 //返回值: 无
127 //-------------------------------------------
128 void Print( float *p,short int n)    
129 {
130      int i,j;
131      for (i=0;i<n;i++)
132      {
133          for (j=0; j<n;j++)
134              printf("%10G ",*p++);
135          printf("
");
136      }
137      printf("--------------
");
138 }
原文地址:https://www.cnblogs.com/Zblogs/p/3403041.html