Use Adjoint and Determinant Method
In this step, we will implement the matrix inversion method using the adjoint and determinant approach. We'll extend the previous matrix input program to calculate the matrix inverse.
First, let's add functions to calculate the determinant and adjoint of a matrix:
#include <stdio.h>
#define MAX_SIZE 10
// Function to get cofactor of matrix
void getCofactor(int matrix[MAX_SIZE][MAX_SIZE], int temp[MAX_SIZE][MAX_SIZE],
int p, int q, int n) {
int i = 0, j = 0;
for (int row = 0; row < n; row++) {
for (int col = 0; col < n; col++) {
if (row != p && col != q) {
temp[i][j++] = matrix[row][col];
if (j == n - 1) {
j = 0;
i++;
}
}
}
}
}
// Recursive function to calculate determinant
int determinant(int matrix[MAX_SIZE][MAX_SIZE], int n) {
if (n == 1)
return matrix[0][0];
int det = 0;
int sign = 1;
int temp[MAX_SIZE][MAX_SIZE];
for (int f = 0; f < n; f++) {
getCofactor(matrix, temp, 0, f, n);
det += sign * matrix[0][f] * determinant(temp, n - 1);
sign = -sign;
}
return det;
}
// Function to calculate adjoint matrix
void adjoint(int matrix[MAX_SIZE][MAX_SIZE], int adj[MAX_SIZE][MAX_SIZE], int n) {
if (n == 1) {
adj[0][0] = 1;
return;
}
int sign = 1;
int temp[MAX_SIZE][MAX_SIZE];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
getCofactor(matrix, temp, i, j, n);
sign = ((i + j) % 2 == 0) ? 1 : -1;
adj[j][i] = sign * determinant(temp, n - 1);
}
}
}
// Function to calculate inverse matrix
int inverse(int matrix[MAX_SIZE][MAX_SIZE], float inverse[MAX_SIZE][MAX_SIZE], int n) {
int det = determinant(matrix, n);
if (det == 0) {
printf("Inverse does not exist (determinant is zero).\n");
return 0;
}
int adj[MAX_SIZE][MAX_SIZE];
adjoint(matrix, adj, n);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
inverse[i][j] = (float)adj[i][j] / det;
}
}
return 1;
}
// Previous readMatrix function from the first step
void readMatrix(int matrix[MAX_SIZE][MAX_SIZE], int size) {
printf("Enter the elements of the %dx%d matrix:\n", size, size);
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("Enter element [%d][%d]: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
}
int main() {
int matrix[MAX_SIZE][MAX_SIZE];
float inv[MAX_SIZE][MAX_SIZE];
int size;
printf("Enter the size of the square matrix (max %d): ", MAX_SIZE);
scanf("%d", &size);
if (size <= 0 || size > MAX_SIZE) {
printf("Invalid matrix size!\n");
return 1;
}
readMatrix(matrix, size);
// Calculate and print inverse matrix
if (inverse(matrix, inv, size)) {
printf("\nInverse Matrix:\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
printf("%.2f ", inv[i][j]);
}
printf("\n");
}
}
return 0;
}
Example output:
Enter the size of the square matrix (max 10): 3
Enter the elements of the 3x3 matrix:
Enter element [0][0]: 1
Enter element [0][1]: 2
Enter element [0][2]: 3
Enter element [1][0]: 0
Enter element [1][1]: 1
Enter element [1][2]: 4
Enter element [2][0]: 1
Enter element [2][1]: 0
Enter element [2][2]: 1
Inverse Matrix:
-2.00 1.00 1.00
2.00 -1.00 -1.00
0.00 1.00 0.00
Let's compile and run the program:
cd ~/project
gcc matrix_inverse.c -o matrix_inverse
./matrix_inverse
Key points about the matrix inversion method:
- We use the adjoint method to calculate the matrix inverse.
- The determinant is calculated recursively.
- The inverse is calculated by dividing the adjoint matrix by the determinant.
- If the determinant is zero, the matrix is not invertible.