?
Solved

Program to Inverse a Matrix

Posted on 2009-02-15
4
Medium Priority
?
2,721 Views
Last Modified: 2012-05-06
which is the easiest way to invert a matrix in  C?
0
Comment
Question by:bbk_doll
  • 2
4 Comments
 
LVL 9

Assisted Solution

by:unassassinable
unassassinable earned 60 total points
ID: 23644084
found this see if it works for you:


//PennyBoki @ </dream.in.code>
#include <stdio.h>
 
int main()
{
     float A[3][3];// the matrix that is entered by user
     float B[3][3];//the transpose of a matrix A 
     float C[3][3];//the adjunct matrix of transpose of a matrix A not adjunct of A
     double X[3][3];//the inverse
     int i,j;
     float x,n=0;//n is the determinant of A
     
     printf("========== Enter matrix A =============================================\n");
     for(i=0;i<3;i++)
     {     printf("\n");
          for(j=0;j<3;j++)
          {     
               printf(" A[%d][%d]= ",i,j);
               scanf("%f", &A[i][j]);
               B[i][j]=0;
               C[i][j]=0;
          }
     }
     
     
          for(i=0,j=0;j<3;j++)
          {     
               if(j==2) 
               n+=A[i][j]*A[i+1][0]*A[i+2][1];
               else if(j==1)
               n+=A[i][j]*A[i+1][j+1]*A[i+2][0];
               else 
               n+=A[i][j]*A[i+1][j+1]*A[i+2][j+2];
          }
          for(i=2,j=0;j<3;j++)
          {     
               if(j==2) 
               n-=A[i][j]*A[i-1][0]*A[i-2][1];
               else if(j==1)
               n-=A[i][j]*A[i-1][j+1]*A[i-2][0];
               else
               n-=A[i][j]*A[i-1][j+1]*A[i-2][j+2];
          }
 
 
     printf("\n========== The matrix A is ==========================================\n");     
     for(i=0;i<3;i++)
     {
          printf("\n");
          for(j=0;j<3;j++)
          {     
               printf(" A[%d][%d]= %.2f  ",i,j,A[i][j]);
          }
     }
     printf("\n \n");
     
     printf("=====================================================================\n\n");
     printf("The determinant of matrix A is %.2f ",n);
     printf("\n\n=====================================================================\n");
     
     if(n!=0) x=1.0/n;
     else 
     {
          printf("Division by 0, not good!\n");
          printf("=====================================================================\n\n");
          return 0;
     }
     printf("\n========== The transpose of a matrix A ==============================\n");     
     for(i=0;i<3;i++)
     {
          printf("\n");
          for(j=0;j<3;j++)
          {     
                    
               B[i][j]=A[j][i];
               printf(" B[%d][%d]= %.2f  ",i,j,B[i][j]);
               
          }
     }
     printf("\n\n");
 
 
     C[0][0]=B[1][1]*B[2][2]-(B[2][1]*B[1][2]);
     C[0][1]=(-1)*(B[1][0]*B[2][2]-(B[2][0]*B[1][2]));
     C[0][2]=B[1][0]*B[2][1]-(B[2][0]*B[1][1]);
     
     C[1][0]=(-1)*(B[0][1]*B[2][2]-B[2][1]*B[0][2]);
     C[1][1]=B[0][0]*B[2][2]-B[2][0]*B[0][2];
     C[1][2]=(-1)*(B[0][0]*B[2][1]-B[2][0]*B[0][1]);
 
     C[2][0]=B[0][1]*B[1][2]-B[1][1]*B[0][2];
     C[2][1]=(-1)*(B[0][0]*B[1][2]-B[1][0]*B[0][2]);
     C[2][2]=B[0][0]*B[1][1]-B[1][0]*B[0][1];
 
 
     printf("\n========== The adjunct matrix of transpose of the matrix A ==========\n");     
     for(i=0;i<3;i++)
     {
          printf("\n");
          for(j=0;j<3;j++)
          {     
               printf("C[%d][%d]= %.2f",i,j,C[i][j]);
               
          }
     }
     printf("\n\n");
 
     
     for(i=0;i<3;i++)
     {
          for(j=0;j<3;j++)
          {     
               X[i][j]=C[i][j]*x;
               
          }
     }
 
 
 
     printf("\n========== The inverse matrix of the matrix you entered!!! ==========\n");
     for(i=0;i<3;i++)
     {     printf("\n");
          for(j=0;j<3;j++)
          {     
               printf(" X[%d][%d]= %.2f",i,j,X[i][j]);
          
          }
     }
     printf("\n\n");
     
 
return 0;
} 

Open in new window

0
 
LVL 53

Expert Comment

by:Infinity08
ID: 23644103
For smaller matrices (2x2, 3x3), doing it directly (by calculating a lot of determinants) is ok. For larger matrices, Gauss-Jordan elimination will be faster.

For matrices with special properties, more specific algorithms (faster) exist.
0
 

Author Comment

by:bbk_doll
ID: 23644172
Thanks unassassinable !
but its really very large code. and, which method it is based on ?

Thanks Infinity08 !
yes, i read Guass Jordon method, long ago. may i have any lnformation / link on it.

Thanks again.
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 90 total points
ID: 23644187
Gauss-Jordan elimination is explained here :

        http://mathworld.wolfram.com/Gauss-JordanElimination.html

There are several code samples floating around the net. You could use one of those or use it as a basis for implementing your own. Alternatively you can use a full-blown matrix library.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question