hi, i hv to do the inverse of a matrix by row elimination
plz
help
Row-elimination (Gauss–Jordan) is the straightforward way to get A^{-1}: augment A with the identity and reduce the left block to I so the right block becomes the inverse. 's 3x3 code is a useful concrete example; correctly warned about rounding, pivoting and singular examples (for instance the common 3x3 with rows [1 2 3], [4 5 6], [7 8 9] is singular). Treat this as an algorithm exercise, but be aware of numerical traps.
Practical checklist before coding: use double (or long double) not float; implement partial pivoting (pick the row with largest absolute entry in the pivot column and swap); scale rows or use equilibration if sizes vary widely; treat a very small pivot (|pivot| < epsilon, eg 1e-12) as singular/ill-conditioned; avoid computing the inverse for solving Ax=b repeatedly — compute an LU decomposition and solve for each right-hand side instead. Always verify results by computing the residual R = A_orig * X - I and checking its norm.
A compact Gauss–Jordan sketch (adapt as needed):
bool invertGaussJordan(vector<vector<double>>& A, vector<vector<double>>& inv) {
int n = A.size();
inv = identity(n);
for (int col = 0; col < n; ++col) {
int p = col;
for (int r = col+1; r < n; ++r) if (fabs(A[r][col]) > fabs(A[p][col])) p = r;
if (fabs(A[p][col]) < 1e-12) return false; // singular or ill-conditioned
swap(A[col], A[p]); swap(inv[col], inv[p]);
double pv = A[col][col];
for (int c=0;c<n;++c){ A[col][c]/=pv; inv[col][c]/=pv; }
for (int r=0;r<n;++r) if (r!=col) {
double f = A[r][col];
for (int c=0;c<n;++c){ A[r][c]-=f*A[col][c]; inv[r][c]-=f*inv[col][c]; }
}
}
return true;
} Test with known cases and compute the residual norm to detect problems. For production or larger sizes, prefer a vetted linear-algebra library or LU/SVD routines for stability and performance.
Jump to Post— jonsca 1,059This has a step by step method:
Jump to Post— rahul8590 71This has a step by step method:
That was a nice link enlightening the procedure.
I would like to add more on it by saying that u could use matrix template library
It might be helpful …
This has a step by step method:
This has a step by step method:
That was a nice link enlightening the procedure.
I would like to add more on it by saying that u could use matrix template library
It might be helpful to you.
I'm going to go out on a limb and say that it's probably a school assignment so the OP probably can't incorporate it but he/she can get some good ideas from it.
If you do write your own Gauss-Jorden elimination procedure, be very careful to deal with the huge numerical rounding errors. Re- stabalization of matrix after most steps is essential. Also please not
the obvious test matrix [1,2,3][4,5,6][7,8,9] has zero determinate so has no inverse.
Simple approaches in books like numerical recipes, e.g. pivoting are a start but much better methods exist (e.g. partial row summation).
Note the NR book is freely viewable.
//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;
}
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.