Python 检查矩阵是否对称

Python 检查矩阵是否对称

在这篇文章中,我们将学习一个Python程序来检查矩阵是否对称。

什么是对称矩阵

如果一个方阵的转置与原矩阵相同,则该矩阵被称为 对称 矩阵。通过将行变成列,列变成行,可以得到对称矩阵。

示例

2 6 8
6 1 3
8 3 9

假设我们有一个 N x N 的输入矩阵。现在我们将使用下面的方法来检查矩阵是否对称。

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

  • 使用嵌套的 for 循环

  • 高效的解决方案,无需转置矩阵

  • 使用列表推导

方法 1:使用嵌套的 for 循环

步骤

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

  • 创建一个函数 transposeMatrix() 来获取矩阵的转置。

  • 使用 for 循环 遍历矩阵的行。

  • 使用另一个 嵌套的 for 循环 遍历当前行的列。

  • 获取输入矩阵的转置,即交换行和列。

  • 创建一个函数 checkingSymmetric() ,通过接受输入矩阵和行数的参数,如果矩阵是 对称的 则返回 true,否则返回 false。

  • 调用上述 transposeMatrix() 函数来获取矩阵的转置。

  • 遍历矩阵。

  • 使用 if 条件语句 检查当前元素是否等于转置矩阵的元素。

  • 如果上述条件为真,则返回 False

  • 如果在上述嵌套循环中没有返回 False,则说明矩阵是对称的,因此返回 True

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

  • 调用上述 checkingSymmetric() 函数,并使用 if 条件语句检查函数是否返回 true,通过传递输入矩阵和行数作为参数。

  • 如果条件为真,即函数返回 true,则打印 “Symmetric matrix”

  • 否则打印“ NOT a Symmetric matrix”。

示例

以下程序使用嵌套的 for 循环来检查输入矩阵是否是对称的。

# creating a function for getting the transpose of a matrix
def transposeMatrix(inputMatrix, t, rows):
   # traversing through the rows of a matrix
      for p in range(rows):
         # traversing the columns of a current row
         for q in range(rows):
            # transposing the matrix i.e, exchange the rows and cols
               t[p][q] = inputMatrix[q][p]
# creating a function that returns true if the matrix is symmetric
# else false by accepting the input matrix, no of rows as arguments
def checkingSymmetric(inputMatrix, rows):
   # Creating the new matrix with all 0 values
      t = [[0 for q in range(len(inputMatrix[0]))]
         for p in range(len(inputMatrix))]
      # calling the above transposeMatrix() function to transpose the given matrix
      transposeMatrix(inputMatrix, t, rows)
   # traversing through the rows of a matrix
      for p in range(rows):
         # traversing the columns of a current row
         for q in range(rows):
            # checking whether the current element is not equal transpose matrix element
               if (inputMatrix[p][q] != t[p][q]):
                  # returning False if the condition is true
                  return False
   # else returning True
      return True
# input matrix
inputMatrix = [[6, 3, 5], [3, 2, 4], [5, 4, 6]]
# checking whether above defined checkingSymmetric() function returns true
# by calling it by passing input matrix and no of rows as arguments
if (checkingSymmetric(inputMatrix, 3)):
   # printing "Symmetric matrix" if the function returns true
   print("Input matrix is a Symmetric matrix")
else:
   # else printing NOT a Symmetric matrix
   print("Input matrix is NOT a Symmetric matrix")

输出

在执行时,上述程序将生成以下输出结果 –

Input matrix is a Symmetric matrix

时间复杂度 - O(N x N)

辅助空间 - O(N x N)

方法2:不使用转置矩阵的高效解决方案

为了快速确定一个矩阵是否对称,我们可以比较它的元素而不进行转置。在这种方法中,我们将比较matrix[i][j]matrix[j][i]

示例

以下程序使用比较来检查输入矩阵是否对称 –

# creating a function that returns true if the matrix is symmetric
# else false by accepting the input matrix, no of rows as arguments
def checkingSymmetric(inputMatrix, rows):
   # traversing through the rows of a matrix
      for p in range(rows):
         # traversing the columns of a current row
            for q in range(rows):
               # checking whether the current element is not equal to its transpose
                  if (inputMatrix[p][q] != inputMatrix[q][p]):
                     # returning False if the condition is true
                     return False
   # else returning True
      return True
# input matrix
inputMatrix = [[6, 3, 5], [3, 2, 4], [5, 4, 6]]
if (checkingSymmetric(inputMatrix, 3)):
   print("Input matrix is a Symmetric matrix")
else:
   print("Input matrix is NOT a Symmetric matrix")

输出

Input matrix is a Symmetric matrix

时间复杂度 − O(N x N)

辅助空间 − O(1)

方法3:使用列表推导式

示例

下面的程序使用列表推导式检查输入矩阵是否对称 –

# creating a function that returns true if the matrix is symmetric
# else false by accepting the input matrix, no of rows as arguments
def checkingSymmetric(inputMatrix, rows):
   # getting transpose of a matrix
   transpose_matrix = [[inputMatrix[q][p]
      for q in range(rows)] for p in range(rows)]
   # checking whether the input matrix is not equal to the transpose matrix
   if(inputMatrix == transpose_matrix):
      return True
   return False
# input matrix
inputMatrix = [[6, 3, 5], [3, 2, 4], [5, 4, 6]]
if (checkingSymmetric(inputMatrix, 3)):
   print("Input matrix is a Symmetric matrix")
else:
   print("Input matrix is NOT a Symmetric matrix")

输出

Input matrix is a Symmetric matrix

时间复杂度 − O(N*N)

辅助空间 − O(N*N)

结论

在本文中,我们首先学习了对称矩阵的定义,接着学习了使用三种不同的方法来实现一个程序,判断给定矩阵是否对称。此外,我们还学习了一种高效的方法来判断给定矩阵是否对称,而无需进行转置,这样能够节省空间并将空间复杂度降低到O(1)。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程