📜  C++ 程序来检查对合矩阵

📅  最后修改于: 2022-05-13 01:55:25.814000             🧑  作者: Mango

C++ 程序来检查对合矩阵

给定一个矩阵,任务是检查矩阵是否是对合矩阵。
对合矩阵如果矩阵乘以自身返回单位矩阵,则称矩阵为对合矩阵。对合矩阵是自身逆矩阵。如果A * A = I ,则称矩阵A是对合矩阵。其中 I 是单位矩阵。

对合矩阵

例子:

Input : mat[N][N] = {{1, 0, 0},
                     {0, -1, 0},
                     {0, 0, -1}}
Output : Involutory Matrix

Input : mat[N][N] = {{1, 0, 0},
                     {0, 1, 0},
                     {0, 0, 1}} 
Output : Involutory Matrix
C++
// Program to implement involutory matrix.
#include 
#define N 3
using namespace std;
  
// Function for matrix multiplication.
void multiply(int mat[][N], int res[][N])
{
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            res[i][j] = 0;
            for (int k = 0; k < N; k++)
                res[i][j] += mat[i][k] * mat[k][j];
        }
    }
}
  
// Function to check involutory matrix.
bool InvolutoryMatrix(int mat[N][N])
{
    int res[N][N];
  
    // multiply function call.
    multiply(mat, res);
  
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (i == j && res[i][j] != 1)
                return false;
            if (i != j && res[i][j] != 0)
                return false;
        }
    }
    return true;
}
  
// Driver function.
int main()
{
    int mat[N][N] = { { 1, 0, 0 },
                      { 0, -1, 0 },
                      { 0, 0, -1 } };
  
    // Function call. If function return
    // true then if part will execute otherwise
    // else part will execute.
    if (InvolutoryMatrix(mat))
        cout << "Involutory Matrix";
    else
        cout << "Not Involutory Matrix";
  
    return 0;
}


输出 :

Involutory Matrix

请参阅程序上的完整文章以检查对开矩阵以获取更多详细信息!