检查对合矩阵的Python程序
给定一个矩阵,任务是检查矩阵是否是对合矩阵。
对合矩阵:如果矩阵乘以自身返回单位矩阵,则称矩阵为对合矩阵。对合矩阵是自身逆矩阵。如果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
Python3
# Program to implement involutory matrix.
N = 3;
# Function for matrix multiplication.
def multiply(mat, res):
for i in range(N):
for j in range(N):
res[i][j] = 0;
for k in range(N):
res[i][j] += mat[i][k] * mat[k][j];
return res;
# Function to check involutory matrix.
def InvolutoryMatrix(mat):
res=[[0 for i in range(N)]
for j in range(N)];
# multiply function call.
res = multiply(mat, res);
for i in range(N):
for j in range(N):
if (i == j and res[i][j] != 1):
return False;
if (i != j and res[i][j] != 0):
return False;
return True;
# Driver Code
mat = [[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)):
print("Involutory Matrix");
else:
print("Not Involutory Matrix");
# This code is contributed by mits
输出 :
Involutory Matrix
请参阅程序上的完整文章以检查对开矩阵以获取更多详细信息!