Finding Inverse Matrix Source Code 1
Finding Inverse Matrix Source Code 1
Finding Inverse Matrix Source Code 1
Source code 1
/* Inverse of a n by n matrix */
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
void arg(int *,int *, int *,int ,int );
int det(int *,int *);
int a[10][10],b[10][10],c[10][10],n,i,j,m,d;
clrscr();
printf("Enter the order of the matrix");
scanf("%d",&n);
printf("Enter the matrix");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
if(n==2)
{
c[0][0]=a[1][1];
c[1][1]=a[0][0];
c[0][1]=-a[0][1];
c[1][0]=-a[1][0];
d=a[0][0]*a[1][1]-a[0][1]*a[1][0];
printf("Determinant is:%d\n",d);
if(d==0)
{
getch();
exit(d-'0');
}
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf(" %f",c[i][j]/(float)d);
}
}
else
{
m=n;
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
n=m;
arg(&a[0][0],&b[0][0],&n,i,j);
c[j][i]=pow(-1,(i+j))*det(&b[0][0],&n);
}
}
n=m;
d=det(&a[0][0],&n);
printf("Determinant is :%d\n",d);
if(d==0)
{
printf("INVERSE DOES NOT EXIST");
getch();
exit(d-'0');
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<m;j++)
printf(" %f",c[i][j]/(float)d);
}
} getch();
}
}
}
*n=*n-1;
}
return(sum);
}
#include<stdio.h>
#include<math.h>
float detrm( float[][], float );
void cofact( float[][], float );
void trans( float[][], float[][], float );
main()
{
float a[ 25 ][ 25 ], k, d;
int i, j;
printf( "ENTER THE ORDER OF THE MATRIX:\n" );
scanf( "%f", &k );
printf( "ENTER THE ELEMENTS OF THE MATRIX:\n" );
for ( i = 0;i < k;i++ )
{
for ( j = 0;j < k;j++ )
{
scanf( "%f", &a[ i ][ j ] );
}
}
d = detrm( a, k );
printf( "THE DETERMINANT IS=%f", d );
if ( d == 0 )
printf( "\nMATRIX IS NOT INVERSIBLE\n" );
else
cofact( a, k );
}
/******************FUNCTION TO FIND THE DETERMINANT OF THE
MATRIX************************/
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++ )
{
m = 0;
n = 0;
for ( i = 0;i < k;i++ )
{
for ( j = 0;j < k;j++ )
{
b[ i ][ j ] = 0;
if ( i != 0 && j != c )
{
b[ m ][ n ] = a[ i ][ j ];
if ( n < ( k – 2 ) )
n++;
else
{
n = 0;
m++;
}
}
}
}
det = det + s * ( a[ 0 ][ c ] * detrm( b, k – 1 ) );
s = -1 * s;
}
}
return ( det );
}
/*******************FUNCTION TO FIND
COFACTOR*********************************/
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 );
}
/*************FUNCTION TO FIND TRANSPOSE AND INVERSE OF A
MATRIX**************************/
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" );
}
}
Source Code 3
1. double CDblMatrix::Det()
2. {
3. double detValue = 0;
4. int newRows = this->m_nRows - 1;
5. int newCols = this->m_nCols - 1;
6. int incRows = 0;
7. int incCols = 0;
8. double incRowCopy;
9. double sign;
10. double tempDet;
11. double tempDetSign;
12. double origValue;
13.
14. try{
15.
16. // If the matrix is a 2 x 2 matrix - Simply Determinant
calculation
17. if((this->m_nRows == 2) && (this->m_nCols == 2)){
18. detValue = (this->m_ppData[0][0] * this->m_ppData[1][1]) -
(this->m_ppData[1][0] * this->m_ppData[0][1]);
19. }
20. // If not a square matrix
21. if(this->m_nRows != this->m_nCols){
22. throw 100;
23. }
24.
25. else{
26. for(int i = 0; i < this->m_nRows; i++){
27.
28. CDblMatrix temp(newRows, newCols);
29.
30. for(int i1 = 0; i1 < this->m_nRows; i1++){
31. for(int j2 = 0; j2 < this->m_nCols; j2++){
32.
33. if((i1 != i) && (j2 != 0) && (incRows <= (newRows-
1)) && (incCols <= (newCols-1))){
34. temp.m_ppData[incRows][incCols] = this-
>m_ppData[i1][j2];
35.
36. if((incCols <= (newCols-1)) && (incRows <=
(temp.m_nRows - 1))){
37. // Last element of row, not last row
38. if((incCols == (newCols - 1)) && (incRows <
(temp.m_nRows-1))){
39. incCols = 0;
40. incRows++;
41. }
42. // If last element
43. else if((incCols == (newCols - 1)) && (incRows
== (newRows - 1))){
44. break;
45. }
46. // Normal increment of column
47. else if((incRows <= (temp.m_nRows-1)) &&
(incCols < (temp.m_nCols - 1))){
48. incCols++;
49. }
50. }
51. }
52. }
53. }
54.
55. // Reset values
56. incRows = 0;
57. incCols = 0;
58.
59. // Find determinant of the new reduced matrix
60. tempDet = temp.Det();
61.
62. // Copy int value to double - required for pow(double,
double)
63. incRowCopy = i;
64.
65. // Determine whether 1 or -1 to multipy by the
determinant
66. sign = pow(-1, incRowCopy + 2);
67.
68. // The correctly signed determinant
69. tempDetSign = sign * tempDet;
70.
71. // Orignal value of the coordinte to multipy the
determinant by
72. origValue = this->m_ppData[i][0];
73. // Finally total the values of determinants
74. detValue = detValue + (tempDetSign * origValue);
75.
76. }
77. }
78. }
79.
80. catch(...){
81. cout << "The matrix is not a square matrix! Can't find
determinant of a non-square matrix." << endl;
82. return 0;
83. //throw;
84. }
85.
86.
87. return detValue;
88.
89. }