行列が可逆かどうかをチェックするC++プログラム
行列式は、それが可逆であるかどうかを見つけるために使用できます。行列式がゼロ以外の場合、行列は可逆です。したがって、行列式がゼロであることが判明した場合、行列は可逆ではありません。例-
The given matrix is: 4 2 1 2 1 1 9 3 2 The determinant of the above matrix is: 3 So the matrix is invertible.
行列が可逆かどうかをチェックするプログラムは次のとおりです。
例
#include<iostream>
#include<math.h>
using namespace std;
int determinant( int matrix[10][10], int n) {
int det = 0;
int submatrix[10][10];
if (n == 2)
return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
else {
for (int x = 0; x < n; x++) {
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x)
continue;
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
det = det + (pow(-1, x) * matrix[0][x] * determinant( submatrix, n - 1 ));
}
}
return det;
}
int main() {
int n, d, i, j;
int matrix[10][10];
cout << "Enter the size of the matrix:\n";
cin >> n;
cout << "Enter the elements of the matrix:\n";
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> matrix[i][j];
cout<<"The entered matrix is:"<<endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
cout << matrix[i][j] <<" ";
cout<<endl;
}
d = determinant(matrix, n);
cout<<"Determinant of the matrix is "<< d <<endl;
if( d == 0 )
cout<<"This matrix is not invertible as the determinant is zero";
else
cout<<"This matrix is invertible as the determinant is not zero";
return 0;
} 出力
Enter the size of the matrix: 3 Enter the elements of the matrix: 1 2 3 2 1 2 1 1 4 The entered matrix is: 1 2 3 2 1 2 1 1 4 Determinant of the matrix is -7 This matrix is invertible as the determinant is not zero
上記のプログラムでは、行列のサイズと要素がmain()関数で提供されています。次に、関数determinant()が呼び出されます。 dに格納されている行列式を返します。行列式が0の場合、行列は可逆ではなく、行列式が0でない場合、行列は可逆です。これは、次のコードスニペットで示されています。
cout << "Enter the size of the matrix:\n";
cin >> n;
cout << "Enter the elements of the matrix:\n";
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
cin >> matrix[i][j];
cout<<"The entered matrix is:"<<endl;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
cout << matrix[i][j] <<" ";
cout<<endl;
}
d = determinant(matrix, n);
cout<<"Determinant of the matrix is "<< d <<endl;
if( d == 0 )
cout<<"This matrix is not invertible as the determinant is zero";
else
cout<<"This matrix is invertible as the determinant is not zero"; 関数determinant()で、行列のサイズが2の場合、行列式が直接計算され、値が返されます。これは次のように表示されます。
if (n == 2) return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
行列のサイズが2でない場合、行列式は再帰的に計算されます。ループ変数x、i、およびjで使用される3つのネストされたforループがあります。これらのループは行列式を計算するために使用され、関数determinant()は再帰的に呼び出されて内部行列式を計算し、それを外部値と乗算します。これは、次のコードスニペットによって示されます。
for (int x = 0; x < n; x++) {
int subi = 0;
for (int i = 1; i < n; i++) {
int subj = 0;
for (int j = 0; j < n; j++) {
if (j == x)
continue;
submatrix[subi][subj] = matrix[i][j];
subj++;
}
subi++;
}
det = det + (pow(-1, x) * matrix[0][x] * determinant( submatrix, n - 1 ))
} -
C++でべき等行列をチェックするプログラム
行列M[r][c]が与えられた場合、「r」は行数を示し、「c」はr=cが正方行列を形成するような列数を示します。与えられた正方行列がべき等行列であるかどうかを確認する必要があります かどうか。 べき等行列 行列「M」はべき等行列と呼ばれます 行列「M」にそれ自体を掛けたものだけが同じ行列「M」を返す場合、つまり M * M=M。 以下の例のように- 上記の行列はそれ自体で乗算され、同じ行列を返すと言えます。したがって、マトリックスはIデポテンツマトリックスです。 。 例 Input: m[3][3] = { {2, -2, -4}, {-1, 3,
-
隣接行列を実装するためのC++プログラム
グラフの隣接行列は、サイズV x Vの正方行列です。VはグラフGの頂点の数です。この行列では、各辺にV個の頂点がマークされています。グラフにiからjの頂点までのエッジがある場合、i thの隣接行列に 行とjth 列は1(または加重グラフの場合はゼロ以外の値)になります。それ以外の場合、その場所は0を保持します。 隣接行列表現の複雑さ: 隣接行列表現は、計算中にO(V2)のスペースを取ります。グラフに最大数のエッジと最小数のエッジがある場合、どちらの場合も必要なスペースは同じになります。 入力: 出力: 0 1 2 3 4