Python程序检查对合矩阵


在本文中,我们将学习一个Python程序来检查对合矩阵。

假设我们已经输入了一个矩阵。我们现在将使用以下方法检查输入矩阵是否为对合矩阵

使用的方法

以下是用以完成此任务的各种方法:

  • 使用嵌套循环

  • 使用NumPy模块

什么是对合矩阵?

如果一个矩阵与其自身相乘得到单位矩阵,则称其为对合矩阵。其逆矩阵就是对合矩阵

如果A * A = I,则矩阵A被称为对合矩阵。此处I表示单位矩阵

方法1:使用嵌套循环

算法(步骤)

以下是执行所需任务的算法/步骤:

  • 创建一个变量来存储矩阵的行数。

  • 创建一个函数multiplyMatrix(),通过接受输入矩阵来执行矩阵乘法。

  • 执行矩阵乘法。

  • 使用return语句返回矩阵乘法的结果输出。

  • 创建一个函数checkInvolutoryMat(),通过接受输入矩阵作为参数来检查输入矩阵是否为对合矩阵。

  • 创建一个大小为rows*rows的矩阵,所有元素都为0,并将其存储为outputMatrix

  • 调用上述multiplyMatrix()函数执行矩阵乘法,并将结果保存在上述outputMatrix变量中。

  • 遍历outputMatrix的每个元素。

  • 使用if条件语句检查对角线元素是否不为1,如果是,则返回False,即不是对合矩阵。

  • 使用if条件语句检查非对角线元素是否不为0,如果是,则返回False,即不是对合矩阵。

  • 创建一个变量来存储输入矩阵

  • 使用if条件语句检查上述定义的checkInvolutoryMat()函数是否返回True,并将输入矩阵作为参数传递。

  • 如果条件为真,则打印对合矩阵

  • 否则打印非对合矩阵。

示例

以下程序使用嵌套循环检查输入矩阵是否为对合矩阵:

Open Compiler
# 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)

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

方法2:使用NumPy模块

利用numpy库是确定矩阵是否为对合矩阵的另一种方法。这可以通过使用numpy.allclose()函数比较矩阵与其逆矩阵来实现。

算法(步骤)

以下是执行所需任务的算法/步骤:

  • 使用import关键字导入numpy模块。

  • 创建一个函数checkInvolutoryMat(),通过接受输入矩阵作为参数来检查输入矩阵是否为对合矩阵。

  • 使用numpy.linalg.inv()函数(计算矩阵的逆)获取输入矩阵的逆。

  • 使用numpy.allclose()函数检查输入矩阵是否等于其逆矩阵,将输入矩阵及其逆矩阵作为参数传递并返回结果。

  • 如果两个数组在容差范围内元素级相等,则allclose()函数将返回True。

  • 容差值是正数,通常具有非常小的值。

示例

以下程序使用numpy.linalg.inv()、numpy.allclose()函数检查输入矩阵是否为对合矩阵:

Open Compiler
# 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()函数获取给定矩阵的逆。

更新于: 2023年1月31日

103 次查看

开启你的职业生涯

通过完成课程获得认证

开始学习
广告