Advertisement
bero_0401

adjoint and inverse of matrix

Jul 11th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. const int N = 4;
  6.  
  7. // Function to get cofactor of A[p][q] in temp[][]. n is current dimension of A[][]
  8. void getCofactor(int A[N][N], int temp[N][N], int p, int q, int n)
  9. {
  10.     int i = 0, j = 0;
  11.  
  12.     // Looping for each element of the matrix
  13.     for (int row = 0; row < n; row++)
  14.     {
  15.         for (int col = 0; col < n; col++)
  16.         {
  17.             // Copying into temporary matrix only those element which are not in given row and column
  18.             if (row != p && col != q)
  19.             {
  20.                 temp[i][j++] = A[row][col];
  21.  
  22.                 // Row is filled, so increase row index and reset col index
  23.                 if (j == n - 1)
  24.                 {
  25.                     j = 0;
  26.                     i++;
  27.                 }
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33. /* Recursive function for finding determinant of matrix. n is current dimension of A[][]. */
  34. int determinant(int A[N][N], int n)
  35. {
  36.     int D = 0; // Initialize result
  37.  
  38.     // Base case : if matrix contains single element
  39.     if (n == 1)
  40.         return A[0][0];
  41.  
  42.     int temp[N][N]; // To store cofactors
  43.     int sign = 1; // To store sign multiplier
  44.  
  45.     // Iterate for each element of first row
  46.     for (int f = 0; f < n; f++)
  47.     {
  48.         // Getting Cofactor of A[0][f]
  49.         getCofactor(A, temp, 0, f, n);
  50.         D += sign * A[0][f] * determinant(temp, n - 1);
  51.  
  52.         // terms are to be added with alternate sign
  53.         sign = -sign;
  54.     }
  55.  
  56.     return D;
  57. }
  58.  
  59. // Function to get adjoint of A[N][N] in adj[N][N].
  60. void adjoint(int A[N][N], int adj[N][N])
  61. {
  62.     if (N == 1)
  63.     {
  64.         adj[0][0] = 1;
  65.         return;
  66.     }
  67.  
  68.     // temp is used to store cofactors of A[][]
  69.     int sign = 1;
  70.     int temp[N][N];
  71.  
  72.     for (int i = 0; i < N; i++)
  73.     {
  74.         for (int j = 0; j < N; j++)
  75.         {
  76.             // Get cofactor of A[i][j]
  77.             getCofactor(A, temp, i, j, N);
  78.  
  79.             // sign of adj[j][i] positive if sum of row and column indexes is even.
  80.             sign = ((i + j) % 2 == 0) ? 1 : -1;
  81.  
  82.             // Interchanging rows and columns to get the transpose of the cofactor matrix
  83.             adj[j][i] = (sign) * (determinant(temp, N - 1));
  84.         }
  85.     }
  86. }
  87.  
  88. // Function to calculate and store inverse, returns false if matrix is singular
  89. bool inverse(int A[N][N], float inv[N][N])
  90. {
  91.     // Find determinant of A[][]
  92.     int det = determinant(A, N);
  93.     if (det == 0)
  94.     {
  95.         cout << "Singular matrix, can't find its inverse";
  96.         return false;
  97.     }
  98.  
  99.     // Find adjoint
  100.     int adj[N][N];
  101.     adjoint(A, adj);
  102.  
  103.     // Find Inverse using formula "inverse(A) = adj(A)/det(A)"
  104.     for (int i = 0; i < N; i++)
  105.         for (int j = 0; j < N; j++)
  106.             inv[i][j] = adj[i][j] / float(det);
  107.  
  108.     return true;
  109. }
  110. // Generic function to display the matrix. We use it to display both adjoint and inverse. Adjoint is integer matrix and inverse is a float.
  111. void display(int A[N][N])
  112. {
  113.     for (int i = 0; i < N; i++)
  114.     {
  115.         for (int j = 0; j < N; j++)
  116.             cout << A[i][j] << " ";
  117.         cout << endl;
  118.     }
  119. }
  120.  
  121. void display(float A[N][N])
  122. {
  123.     for (int i = 0; i < N; i++)
  124.     {
  125.         for (int j = 0; j < N; j++)
  126.             cout << fixed << setprecision(6) << A[i][j] << " ";
  127.         cout << endl;
  128.     }
  129. }
  130.  
  131.  
  132. int main()
  133. {
  134.     int A[N][N] = { {5, -2, 2, 7},
  135.                     {1, 0, 0, 3},
  136.                     {-3, 1, 5, 0},
  137.                     {3, -1, -9, 4}};
  138.  
  139.     int adj[N][N]; // To store adjoint of A[][]
  140.     float inv[N][N]; // To store inverse of A[][]
  141.  
  142.     cout << "Input matrix is :\n";
  143.     display(A);
  144.  
  145.     cout << "\nThe Adjoint is :\n";
  146.     adjoint(A, adj);
  147.     display(adj);
  148.  
  149.     cout << "\nThe Inverse is :\n";
  150.     if (inverse(A, inv))
  151.         display(inv);
  152.  
  153.     return 0;
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement