#include<stdio.h>
#include<math.h>
//function declaration for holding determinant of A
float detrminant(float[25][25], float);
//function declaration for holding cofactors of A
void cofactors(float[25][25], float);
//function declaration for holding transpose of A
void trans(float[25][25], float[25][25], float);
main()
{
float a[25][25], n, d;
int i, j;
//Enter the order & element of matrix A i.e. 5x5
printf("Enter the order of the matrix A:\n");
scanf("%f", &n);
printf("Enter the elements of the matrix A:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%f", &a[i][j]);
}
}
d = detrminant(a, n);
printf("\n The Determinant of Matrix =%2f", d);
if (d == 0)
//property: a matrix is not inversible if its det=0
printf("\nMATRIX IS NOT INVERSIBLE\n");
else
cofactors(a, n);
}
float detrminant(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] * detrminant(b, k - 1));
s = -1 * s;
}
}
return (det);
}
void cofactors(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) * detrminant(b, f - 1);
}
}
trans(num, fac, f);
}
void trans(float num[25][25], float fac[25][25], float r)
{
int i, j,k;
float b[25][25], inv[25][25],d,A[25][25];
for (i = 0; i < r; i++)
{
for (j = 0; j < r; j++)
{
b[i][j] = fac[j][i];
}
}
d = detrminant(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 A: \n");
for (i = 0; i < r; i++)
{
for (j = 0; j < r; j++)
{
printf("\t%2f", inv[i][j]);
}
printf("\n");
}
//now we got the inverse of A
getch();
return 0;
}
//now we got the inverse of A
/* 5X5 Inverse Matrix Program in C, n x n Inverse of Matrix, Transpose Of Matrix in C, Cofactor Of Matrix in C, Determinant Of Matrix in C */
#include<math.h>
//function declaration for holding determinant of A
float detrminant(float[25][25], float);
//function declaration for holding cofactors of A
void cofactors(float[25][25], float);
//function declaration for holding transpose of A
void trans(float[25][25], float[25][25], float);
main()
{
float a[25][25], n, d;
int i, j;
//Enter the order & element of matrix A i.e. 5x5
printf("Enter the order of the matrix A:\n");
scanf("%f", &n);
printf("Enter the elements of the matrix A:\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%f", &a[i][j]);
}
}
d = detrminant(a, n);
printf("\n The Determinant of Matrix =%2f", d);
if (d == 0)
//property: a matrix is not inversible if its det=0
printf("\nMATRIX IS NOT INVERSIBLE\n");
else
cofactors(a, n);
}
float detrminant(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] * detrminant(b, k - 1));
s = -1 * s;
}
}
return (det);
}
void cofactors(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) * detrminant(b, f - 1);
}
}
trans(num, fac, f);
}
void trans(float num[25][25], float fac[25][25], float r)
{
int i, j,k;
float b[25][25], inv[25][25],d,A[25][25];
for (i = 0; i < r; i++)
{
for (j = 0; j < r; j++)
{
b[i][j] = fac[j][i];
}
}
d = detrminant(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 A: \n");
for (i = 0; i < r; i++)
{
for (j = 0; j < r; j++)
{
printf("\t%2f", inv[i][j]);
}
printf("\n");
}
//now we got the inverse of A
getch();
return 0;
}
//now we got the inverse of A
/* 5X5 Inverse Matrix Program in C, n x n Inverse of Matrix, Transpose Of Matrix in C, Cofactor Of Matrix in C, Determinant Of Matrix in C */
No comments:
Post a Comment