C++でべき等行列をチェックするプログラム
行列M[r][c]が与えられた場合、「r」は行数を示し、「c」はr=cが正方行列を形成するような列数を示します。与えられた正方行列がべき等行列であるかどうかを確認する必要があります かどうか。
べき等行列
行列「M」はべき等行列と呼ばれます 行列「M」にそれ自体を掛けたものだけが同じ行列「M」を返す場合、つまり M * M=M。
以下の例のように-
上記の行列はそれ自体で乗算され、同じ行列を返すと言えます。したがって、マトリックスはIデポテンツマトリックスです。 。
例
Input: m[3][3] = { {2, -2, -4},
{-1, 3, 4},
{1, -2, -3}}
Output: Idempotent
Input: m[3][3] == { {3, 0, 0},
{0, 2, 0},
{0, 0, 3} }
Output: Not Idempotent アルゴリズム
Start
Step 1 -> define macro as #define size 3
Step 2 -> declare function for matrix multiplication
void multiply(int arr[][size], int res[][size])
Loop For int i = 0 and i < size and i++
Loop For int j = 0 and j < size and j++
Set res[i][j] = 0
Loop for int k = 0 and k < size and k++
Set res[i][j] += arr[i][k] * arr[k][j]
End
End
End
Step 3 -> declare function to check idempotent or not
bool check(int arr[][size])
declare int res[size][size]
call multiply(arr, res)
Loop For int i = 0 and i < size and i++
Loop For int j = 0 and j < size and j++
IF (arr[i][j] != res[i][j])
return false
End
End
End
return true
step 4 -> In main()
declare int arr[size][size] = {{1, -1, -1},
{-1, 1, 1},
{1, -1, -1}}
IF (check(arr))
Print its an idempotent matrix
Else
Print its not an idempotent matrix
Stop 例
#include<bits/stdc++.h>
#define size 3
using namespace std;
//matrix multiplication.
void multiply(int arr[][size], int res[][size]){
for (int i = 0; i < size; i++){
for (int j = 0; j < size; j++){
res[i][j] = 0;
for (int k = 0; k < size; k++)
res[i][j] += arr[i][k] * arr[k][j];
}
}
}
//check idempotent or not
bool check(int arr[][size]){
int res[size][size];
multiply(arr, res);
for (int i = 0; i < size; i++)
for (int j = 0; j < size; j++)
if (arr[i][j] != res[i][j])
return false;
return true;
}
int main(){
int arr[size][size] = {{1, -1, -1},
{-1, 1, 1},
{1, -1, -1}};
if (check(arr))
cout << "its an idempotent matrix";
else
cout << "its not an idempotent matrix";
return 0;
} 出力
its an idempotent matrix
-
C++で対合行列をチェックするプログラム
行列M[r][c]が与えられた場合、「r」は行数を示し、「c」はr=cが正方行列を形成するような列数を示します。与えられた正方行列が対合行列であるかどうかを確認する必要があります かどうか。 対合行列 行列は非自発的と呼ばれます 行列がそれ自体と乗算され、その結果が単位行列である場合に限り、行列。行列Iは、その主対角線が1であり、主対角線以外の要素がゼロである場合にのみ、単位行列です。したがって、行列は対合行列であると言えます。 M * M =Iの場合のみ 、ここで M はいくつかの行列であり、私は単位行列です。 以下の例のように- ここで、行列にそれ自体を乗算すると、結果は単
-
C++で対角行列とスカラー行列をチェックするプログラム
行列M[r][c]が与えられた場合、「r」は行数を示し、「c」はr=cが正方行列を形成するような列数を示します。与えられた正方行列が対角であるかどうかを確認する必要があります およびスカラー 対角の場合、行列かどうか およびスカラー マトリックスを作成し、結果にyesを出力します。 対角行列 正方行列m[][]は、主対角を除く要素がゼロの場合にのみ対角行列になります。 下の図のように- ここで、赤の要素は主対角線であり、主対角線がゼロであることを除いてゼロ以外の残りの要素であり、対角行列になっています。 。 例 Input: m[3][3] = { {7, 0, 0},