1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
ofstream g ("inv.txt");
float determinant(float matrix[30][30],float size)
{
float s=1,det=0,m_minor[30][30];
int i,j,m,n,c;
if (size==1)
{
return (matrix[0][0]);
}
else
{
det=0;
for (c=0;c<size;c++)
{
m=0;
n=0;
for (i=0;i<size;i++)
{
for (j=0;j<size;j++)
{
m_minor[i][j]=0;
if (i != 0 && j != c)
{
m_minor[m][n]=matrix[i][j];
if (n<(size-2))
n++;
else
{
n=0;
m++;
}
}
}
}
det=det + s * (matrix[0][c] * determinant(m_minor,size-1));
s=-1 * s;
}
}
return (det);
}
void transpose(float matrix[30][30],float matrix_cofactor[30][30],float size)
{
int i,j;
float m_transpose[30][30],m_inverse[30][30],d;
for (i=0;i<size;i++)
{
for (j=0;j<size;j++)
{
m_transpose[i][j]=matrix_cofactor[j][i];
}
}
d=determinant(matrix,size);
for (i=0;i<size;i++)
{
for (j=0;j<size;j++)
{
m_inverse[i][j]=m_transpose[i][j] / d;
}
}
cout<<"\nThe inverse matrix is : \n";
for (i=0;i<size;i++)
{
for (j=0;j<size;j++)
{
cout<<m_inverse[i][j]<<" ";
g<<m_inverse[i][j]<<" ";
}
cout<<endl;
}
g.close(); cout<<endl;
}
void cofactor(float matrix[30][30],float size)
{
float m_cofactor[30][30],matrix_cofactor[30][30];
int p,q,m,n,i,j;
for (q=0;q<size;q++)
{
for (p=0;p<size;p++)
{
m=0;
n=0;
for (i=0;i<size;i++)
{
for (j=0;j<size;j++)
{
if (i != q && j != p)
{
m_cofactor[m][n]=matrix[i][j];
if (n<(size-2))
n++;
else
{
n=0;
m++;
}
}
}
}
matrix_cofactor[q][p]=pow(-1,q + p) * determinant(m_cofactor,size-1);
}
}
transpose(matrix,matrix_cofactor,size);
}
|