C Program to Find Inverse of a Matrix
This is the C Program to find the determinant of a matrix
In linear algebra, the determinant is a value associated with a square matrix. It can be computed from the entries of the matrix by a specific arithmetic expression, while other ways to determine its value exist as well. The determinant provides important information about a matrix of coefficients of a system of linear equations, or about a matrix that corresponds to a linear transformation of a vector space. In the first case the system has a unique solution exactly when the determinant is nonzero; when the determinant is zero there are either no solutions or many solutions. In the second case the transformation has an inverse operation exactly when the determinant is nonzero. A geometric interpretation can be given to the value of the determinant of a square matrix with real entries: the absolute value of the determinant gives the scale factor by which area or volume (or a higher-dimensional analogue) is multiplied under the associated linear transformation, while its sign indicates whether the transformation preserves orientation. Thus a 2 × 2 matrix with determinant −2, when applied to a region of the plane with finite area, will transform that region into one with twice the area, while reversing its orientation.
Determinants occur throughout mathematics. The use of determinants in calculus includes the Jacobian determinant in the substitution rule for integrals of functions of several variables. They are used to define the characteristic polynomial of a matrix that is an essential tool in eigenvalue problems in linear algebra. In some cases they are used just as a compact notation for expressions that would otherwise be unwieldy to write down.
The determinant of a matrix A is denoted det(A), det A, or |A|.In the case where the matrix entries are written out in full, the determinant is denoted by surrounding the matrix entries by vertical bars instead of the brackets or parentheses of the matrix.
#include<stdio.h>
#include<math.h>
float detrm(float[25][25],float);
void cofact(float[25][25],float);
void trans(float[25][25],float[25][25],float);
main()
{
float a[25][25],k,d;
int i,j;
printf("Enter the order of the matrixn");
scanf("%f",&k);
printf("Enter the elements of the matrixn");
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
scanf("%f",&a[i][j]);
}
}
d=detrm(a,k);
printf("Deteminant = %f",d);
if(d==0)
printf("nMatrix is not inversiblen");
else
cofact(a,k);
}
float detrm(float a[25][25],float k)
{
float s=1,det=0,b[25][25];
int i,j,m,n,c;
if(k==1)
{
return(a[0][0]);
}
else
{
det=0;
for(c=0;c<k;c++)
{
printf("nThe value of c is %d n",c);
m=0;
n=0;
for(i=0;i<k;i++)
{
printf("The value of i is %dn",i);
for(j=0;j<k;j++)
{
printf("The value of j is %d i-0 j+cn",j);
b[i][j]=0;
if(i!=0&&j!=c)
{
b[m][n]=a[i][j];
if(n<(k-2))
printf("The value of k-2 is %d %d n",k,n);
n++;
else
{
n=0;
m++;
}
}
}
}
det=det+s*(a[0][c]*detrm(b,k-1));
s=-1*s;
printf("value of s= %lfn",s);
}
}
}
return(det);
}
void cofact(float num[25][25],float f)
{
float b[25][25],fac[25][25];
int p,q,m,n,i,j;
for(q=0;q<f;q++)
{
for(p=0;p<f;p++)
{
m=0;
n=0;
for(i=0;i<f;i++)
{
for(j=0;j<f;j++)
{
b[i][j]=0;
if(i!=q&&j!=p)
{
b[m][n]=num[i][j];
if(n<(f-2))
n++;
else
{
n=0;
m++;
}
}
}
}
fac[q][p]=pow(-1,q+p)*detrm(b,f-1);
}
}
trans(num,fac,f);
}
void trans(float num[25][25],float fac[25][25],float r)
{
int i,j;
float b[25][25],inv[25][25],d;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
b[i][j]=fac[j][i];
}
}
d=detrm(num,r);
inv[i][j]=0;
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
inv[i][j]=b[i][j]/d;
}
}
printf("nThe inverse of the matrix :n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
printf("t%f",inv[i][j]);
}
printf("n");
}
}
Recent Comments