首頁 > 後端開發 > C++ > 檢查矩陣是否奇異的C程序

檢查矩陣是否奇異的C程序

WBOY
發布: 2023-08-25 23:09:06
轉載
1472 人瀏覽過

檢查矩陣是否奇異的C程序

給定一個矩陣 mat[row][column],我們的任務是透過函數檢查給定矩陣是否奇異並顯示結果。

奇異矩陣是其行列式的矩陣為零,如果行列式不為零,則該矩陣是非奇異的。

因此,要確定該矩陣是奇異的還是非奇異的,我們需要先計算行列式。矩陣的行列式可以計算為-

$$M1[3][3]\:=\:\begin{bmatrix}a & b & c \d & e & f \g & h & i \end{bmatrix}$$

|m1| = a(e*i - f*h) - b(d*i - f*g) c(d*h - e*g)

範例

Input-: mat[3][3]= { 4, 10, 1 },
   { 0, 2, 3 },
   { 1, 4, -3 }
Output-: matrix is non-singular
Input-: mat[3][3]= { 0, 0, 0 },
   { 10, 20, 30 },
   { 1, 4, -3 }
Output-: matrix is singular
Since the entire first row is 0 the determinant will be zero only
登入後複製

演算法

Start
In function cofactor(int matrix[N][N], int matrix2[N][N], int p, int q, int n)
{
   Step 1-> Declare and initialize i = 0, j = 0, row, col
   Step 2-> Loop For row = 0 and row < n and row++
   Loop For col = 0 and col < n and col++
      If row != p && col != q then,
      Set  matrix2[i][j++] as matrix[row][col]
         If j == n &ndash; 1 then,
            Set j = 0
            Increment i by 1
         End for
      End for
In function int check_singular(int matrix[N][N], int n)
   Step 1-> Declare and initialize int D = 0;
   Step 2-> If n == 1 then,
      Return matrix[0][0]
   Step 3-> Declare matrix2[N][N], sign = 1
   Step 4-> Loop For f = 0 and f < n and f++
      Call function cofactor(matrix, matrix2, 0, f, n)
         Set D += sign * matrix[0][f] * check_singular(matrix2, n - 1)
         Set sign = -sign
      End loop
   Step 5-> Return D
In main()
   Step 1-> Declare and initialize a matrix[N][N]
   Step 2-> If call check_singular(matrix, N) returns non 0 value then,
      Print "Matrix is Singular "
   Step 3-> Else
      Print "Matrix is non-Singular "
Stop
登入後複製

範例

 即時示範

#include <stdio.h>
#define N 4
//to find the cofactors
int cofactor(int matrix[N][N], int matrix2[N][N], int p, int q, int n) {
   int i = 0, j = 0;
   int row, col;
   // Looping for each element of the matrix
   for (row = 0; row < n; row++) {
      for (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) {
            matrix2[i][j++] = matrix[row][col];
            // Row is filled, so increase row
            // index and reset col index
            if (j == n - 1) {
               j = 0;
               i++;
            }
         }
      }
   }
   return 0;
}
/* Recursive function to check if matrix[][] is singular or not. */
int check_singular(int matrix[N][N], int n) {
   int D = 0; // Initialize result
   // Base case : if matrix contains single element
   if (n == 1)
   return matrix[0][0];
   int matrix2[N][N]; // 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 matrix[0][f]
      cofactor(matrix, matrix2, 0, f, n);
      D += sign * matrix[0][f] * check_singular(matrix2, n - 1);
      // terms are to be added with alternate sign
      sign = -sign;
   }
   return D;
}
// Driver program to test above functions
int main() {
   int matrix[N][N] = { { 4, 10, 1 },
   { 0, 2, 3 },
   { 1, 4, -3 } };
   if (check_singular(matrix, N))
      printf("Matrix is Singular</p><p>");
   else
      printf("Matrix is non-Singular</p><p>");
   return 0;
}
登入後複製

輸出

如果執行上面的程式碼,它將產生以下輸出-

Matrix is non-Singular
登入後複製

以上是檢查矩陣是否奇異的C程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板