Python 检查逆正交矩阵
在本文中,我们将学习一个用于检查逆正交矩阵的Python程序。
假设我们有一个 输入矩阵 。我们现在将检查输入矩阵是否是一个 逆正交矩阵 ,使用以下方法。
使用的方法
以下是完成此任务的各种方法:
- 使用嵌套的for循环
-
使用NumPy模块
什么是逆正交矩阵
如果矩阵乘以自身并给出单位矩阵,那么它被称为逆正交矩阵。其逆矩阵就是 逆正交矩阵 。
如果 A * A = I ,矩阵A被称为逆正交矩阵。这里的I表示 单位矩阵 。
方法1:使用嵌套的for循环
步骤
以下是执行所需任务的算法/步骤:
- 创建一个变量来存储矩阵的行数。
-
创建一个函数 multiplyMatrix() 来执行矩阵乘法,接受输入矩阵作为参数。
-
执行矩阵乘法。
-
使用 return 语句返回矩阵乘法的结果。
-
创建一个函数 checkInvolutoryMat() 来检查输入矩阵是否为不变矩阵,接受输入矩阵作为参数。
-
创建一个 rows*rows 大小的矩阵,所有元素都为0,并将其存储为 outputMatrix 。
-
调用上述的 multiplyMatrix() 函数来执行矩阵乘法,并将结果保存在上述的outputMatrix变量中。
-
遍历outputMatrix的每个元素。
-
使用条件语句检查对角线元素是否不为1,如果是,则返回False,即不是不变矩阵。
-
使用条件语句检查非对角线元素是否不为0,如果是,则返回False,即不是不变矩阵。
-
创建一个变量来存储输入矩阵。
-
使用条件语句检查上述定义的 checkInvolutoryMat() 函数是否返回true,通过将输入矩阵作为参数。
-
如果条件为真,则打印一个不变矩阵。
-
否则打印”NOT involuntary matrix”。
示例
以下程序使用嵌套循环检查输入矩阵是否为不变矩阵 –
# input no of rows
rows = 3
# creating a function to perform matrix multiplication
def multiplyMatrix(inputMatrix, output):
# traversing through the rows of a matrix
for p in range(rows):
# traversing through all the columns of a current row
for q in range(rows):
# assuming the current element is 0
output[p][q] = 0
for x in range(rows):
# performing matrix multiplication
output[p][q] += inputMatrix[p][x] * inputMatrix[x][q]
# returning the resultant matrix multiplication output
return output
# creating a function to check whether the input matrix
# is an involuntary matrix by accepting matrix as an argument
def checkInvolutoryMat(inputMatrix):
# Creating a rows*rows size matrix with all elements as 0
output = [[0 for p in range(rows)]
for q in range(rows)]
# calling the above multiplyMatrix() function
output = multiplyMatrix(inputMatrix, output)
# Iterating in the rows of the output matrix
for p in range(rows):
# Iterating in the diagonals of the output matrix
for q in range(rows):
# If it is a diagonal element and if it is not 1 then return False(Not Involuntary)
if (p == q and output[p][q] != 1):
return False
# If it is a non-diagonal element and if it is not 1 then return False(Not Involuntary)
if (p != q and output[p][q] != 0):
return False
# It is an involuntary matrix so return True
return True
# input matrix
inputMatrix = [[1, 0, 0], [0, -1, 0], [0, 0, -1]]
# checking whether the above checkInvolutoryMat() function returns true
# by passing the input matrix as an argument
if (checkInvolutoryMat(inputMatrix)):
# printing involuntary matrix if the condition is true
print("The input matrix is an Involutory matrix")
else:
# else printing NOT involuntary matrix
print("The input matrix is NOT an Involutory matrix")
输出
The input matrix is an Involutory matrix
时间复杂度 - O(N^3)
辅助空间 - O(N^2)
方法2:使用NumPy模块
利用numpy库是判断矩阵是否是自反矩阵的另一种方法。通过使用numpy.allclose()函数将矩阵与其逆矩阵进行比较实现。
步骤
以下是执行所需任务的算法/步骤。
- 使用import关键字导入numpy模块。
-
创建一个函数checkInvolutoryMat(),以输入矩阵作为参数,检查输入矩阵是否是自反矩阵。
-
使用numpy.linalg.inv()函数(计算矩阵的逆)获取输入矩阵的逆矩阵。
-
使用numpy.allclose()函数通过将输入矩阵和其逆矩阵作为参数来检查输入矩阵是否等于其逆矩阵,并返回结果。
-
如果两个数组在容忍度内逐元素相等,则allclose()函数将返回True。
-
容忍度值为正,并且通常具有非常小的值。
示例
以下程序使用numpy.linalg.inv()和numpy.allclose()函数检查输入矩阵是否是自反矩阵。
# using the numpy module
import numpy as np
# creating a function to check whether the input matrix
# is an involuntary matrix by accepting matrix as an argument
def checkInvolutoryMat(inputMatrix):
# Getting the inverse of an input matrix using the numpy linalg module
inverseMat = np.linalg.inv(inputMatrix)
# checking whether the input matrix is equal to its inverse matrix
# using the numpy.allclose() function and returning the result
return np.allclose(inputMatrix, inverseMat)
# input matrix
inputMatrix = [[1, 0, 0], [0, -1, 0], [0, 0, -1]]
# calling the checkInvolutoryMat() function by passing the input matrix as an argument
# The output True indicates an INVOLUNTARY matrix
print(checkInvolutoryMat(inputMatrix))
输出
True
在上面的示例中,输出 True 表示给定的输入矩阵是不可逆矩阵。
该方法利用了 numpy 的优化线性代数函数,并且具有更简短和更简单理解的优势。这种方法的时间复杂度取决于矩阵求逆计算的复杂度,对于稠密矩阵通常为 O(N^3)。空间复杂度为 O(N^2)。
时间复杂度 - O(N^3)
空间复杂度 - O(N^2)
结论
在本文中,我们学习了两种不同的方法来确定给定矩阵是否是不可逆矩阵。我们学习了如何利用 NumPy 模块的 linalg.inv() 函数来获取给定矩阵的逆。