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 – 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\n");
else
printf("Matrix is non-Singular\n");
return 0;
} 出力
上記のコードを実行すると、次の出力が生成されます-
Matrix is non-Singular
-
C++で対合行列をチェックするプログラム
行列M[r][c]が与えられた場合、「r」は行数を示し、「c」はr=cが正方行列を形成するような列数を示します。与えられた正方行列が対合行列であるかどうかを確認する必要があります かどうか。 対合行列 行列は非自発的と呼ばれます 行列がそれ自体と乗算され、その結果が単位行列である場合に限り、行列。行列Iは、その主対角線が1であり、主対角線以外の要素がゼロである場合にのみ、単位行列です。したがって、行列は対合行列であると言えます。 M * M =Iの場合のみ 、ここで M はいくつかの行列であり、私は単位行列です。 以下の例のように- ここで、行列にそれ自体を乗算すると、結果は単
-
C++でべき等行列をチェックするプログラム
行列M[r][c]が与えられた場合、「r」は行数を示し、「c」はr=cが正方行列を形成するような列数を示します。与えられた正方行列がべき等行列であるかどうかを確認する必要があります かどうか。 べき等行列 行列「M」はべき等行列と呼ばれます 行列「M」にそれ自体を掛けたものだけが同じ行列「M」を返す場合、つまり M * M=M。 以下の例のように- 上記の行列はそれ自体で乗算され、同じ行列を返すと言えます。したがって、マトリックスはIデポテンツマトリックスです。 。 例 Input: m[3][3] = { {2, -2, -4}, {-1, 3,