C Online Compiler
Example: Matrix Inverse using Determinant and Adjoint in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Matrix Inverse using Determinant and Adjoint #include <stdio.h> #define N 3 // Define the size of the matrix (N x N) // Function to get cofactor of matrix[p][q] in temp[][] // n is current dimension of matrix void getCofactor(double mat[N][N], double temp[N][N], 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 elements which are not in given row and column if (row != p && col != q) { temp[i][j++] = mat[row][col]; // Row is full, so increment row index and reset col index if (j == n - 1) { j = 0; i++; } } } } } // Recursive function for finding determinant of matrix. // n is current dimension of mat[][]. double determinant(double mat[N][N], int n) { double D = 0; // Initialize result // Base case : if matrix contains single element if (n == 1) return mat[0][0]; double temp[N][N]; // To store cofactors int sign = 1; // To store sign multiplier // Iterate for each element of the first row for (int f = 0; f < n; f++) { // Getting Cofactor of mat[0][f] getCofactor(mat, temp, 0, f, n); D += sign * mat[0][f] * determinant(temp, n - 1); // Terms are to be added with alternate signs sign = -sign; } return D; } // Function to calculate and store adjoint of matrix[N][N] in adj[N][N] void adjoint(double mat[N][N], double adj[N][N]) { if (N == 1) { adj[0][0] = 1; return; } int sign = 1; double temp[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { // Get cofactor of mat[i][j] getCofactor(mat, temp, i, j, N); // sign is (-1)^(i+j) sign = ((i + j) % 2 == 0) ? 1 : -1; // Transpose of cofactor matrix is adjoint adj[j][i] = (sign) * (determinant(temp, N - 1)); } } } // Function to calculate and store inverse, returns false if // matrix is singular int inverse(double mat[N][N], double inv[N][N]) { // Find determinant of matrix double det = determinant(mat, N); if (det == 0) { printf("Singular matrix, cannot find its inverse.\n"); return 0; } // Find adjoint double adj[N][N]; adjoint(mat, adj); // Find inverse using formula "inverse(A) = adj(A)/det(A)" for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { inv[i][j] = adj[i][j] / det; } } return 1; } // Generic function to display a matrix void display(double mat[N][N]) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("%8.4f ", mat[i][j]); } printf("\n"); } } int main() { // Step 1: Define the matrix for which to find the inverse double mat[N][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; // This matrix is singular (determinant is 0) double mat2[N][N] = {{1, 2, 3}, {0, 1, 4}, {5, 6, 0}}; // This matrix is non-singular double inverseMat[N][N]; printf("Original Matrix 1:\n"); display(mat); // Step 2: Attempt to find the inverse of the first matrix if (inverse(mat, inverseMat)) { printf("\nInverse of Matrix 1:\n"); display(inverseMat); } else { printf("\nMatrix 1 is singular, inverse does not exist.\n"); } printf("\n-------------------------\n"); printf("\nOriginal Matrix 2:\n"); display(mat2); // Step 3: Attempt to find the inverse of the second matrix if (inverse(mat2, inverseMat)) { printf("\nInverse of Matrix 2:\n"); display(inverseMat); } else { printf("\nMatrix 2 is singular, inverse does not exist.\n"); } return 0; }
Output
Clear
ADVERTISEMENTS