Certainly! To calculate the inverse of a 3x3 matrix, we can use the formula that involves the determinant of the matrix and the adjugate (or adjoint) of the matrix. The formula for the inverse of a matrix A is A^-1 = 1/det(A) * adj(A), where det(A) is the determinant of A, and adj(A) is the adjugate of A.
Before implementing MatrixInverse, let's outline what needs to be done:
- Calculate the determinant of the input matrix.
- Compute the adjugate of the matrix.
- Divide each element of the adjugate by the determinant to get the inverse.
Given this outline, here's how the function can be implemented in C:
#include <stdio.h>
void CalculateCofactor(float mat[3][3], float temp[3][3], int p, int q, int n) {
int i = 0, j = 0;
// Looping for each element of the matrix
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
// Copying into temporary matrix only those element which are not in given row and column
if (row != p && col != q) {
temp[i][j++] = mat[row][col];
// Row is filled, so increase row index and reset col index
if (j == n - 1) {
j = 0;
i++;
}
}
}
}
}
float Determinant(float mat[3][3], int n) {
float D = 0; // Initialize result
// Base case : if matrix contains single element
if (n == 1)
return mat[0][0];
float temp[3][3]; // To store cofactors
int sign = 1; // To store sign multiplier
// Iterate for each element of first row
for (int f = 0; f < n; f++) {
// Getting Cofactor of mat[0][f]
CalculateCofactor(mat, temp, 0, f, n);
D += sign * mat[0][f] * Determinant(temp, n - 1);
// terms are to be added with alternate sign
sign = -sign;
}
return D;
}
void Adjoint(float mat[3][3], float adj[3][3]) {
if (3 == 1) {
adj[0][0] = 1;
return;
}
int sign = 1;
float temp[3][3];
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// Get cofactor of A[i][j]
CalculateCofactor(mat, temp, i, j, 3);
// sign of adj[j][i] positive if sum of row and column indexes is even.
sign = ((i + j) % 2 == 0)? 1: -1;
// Interchanging rows and columns to get the transpose of the cofactor matrix
adj[j][i] = (sign)*(Determinant(temp, 2));
}
}
}
void MatrixInverse(float mat[3][3], float result[3][3]) {
float det = Determinant(mat, 3);
if (det == 0) {
printf("Singular matrix, can't find its inverse\n");
return;
}
float adj[3][3]; // To store adjoint of A[][]
Adjoint(mat, adj);
// Find Inverse using formula "inverse(A) = adj(A)/det(A)"
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
result[i][j] = adj[i][j] / det;
}
int main() {
float mat[3][3] = {{5, -3, 2},
{1, 0, -1},
{3, 1, 5}};
float inv[3][3]; // To store inverse of matrix
MatrixInverse(mat, inv);
printf("Inverse of the matrix is:\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf("%f ", inv[i][j]);
}
printf("\n");
}
return 0;
}
This code includes:
- A function to calculate the determinant of a matrix.
- A function to calculate the adjoint of a matrix.
- The
MatrixInverse function itself, which calculates the inverse by utilizing the determinant and adjoint functions.
This implementation provides a straightforward demonstration of computing a matrix inverse for a 3x3 matrix, but note the efficiency and numerical stability concerns in practical applications, particularly for larger matrices or those with a near-zero determinant.