Python 计算矩阵对角线的总和
在本文中,我们将学习使用Python编写一个高效计算矩阵对角线总和的程序。
使用的方法
以下是实现此任务的各种方法:
- 使用嵌套循环
-
仅使用单个循环
在一个矩阵中,我们有两个对角线:
- 主对角线
-
次对角线
示例
让我们以一个3×3的矩阵为例,如下所示:
A00 A01 A02
A10 A11 A12
A20 A21 A22
主对角线条件 - 行列条件是行=列。主对角线由3×3矩阵中的A00、A11和A22元素组成。
次对角线条件 - 行列条件是行 = 总行数 – 列 – 1。次对角线由3×3矩阵中的A02、A11和A22元素组成。
方法1:使用嵌套循环
步骤
以下是要执行所需任务的算法/步骤:
- 创建一个函数 sumOfDiagonals() ,通过接受输入的矩阵和行数作为参数,打印矩阵的对角线和。
-
初始化一个变量为0,用于存储主对角线的和。
-
初始化另一个变量为0,用于存储次对角线的和。
-
使用 for循环 遍历矩阵的行。
-
使用另一个 嵌套循环 遍历当前行的所有列。
-
使用 if条件语句 检查行号是否等于列号(主对角线条件),如果是,则将矩阵元素的值加到主对角线的和中。
-
类似地,使用 if条件语句 检查行号和列号之和是否等于行数(次对角线条件),如果是,则将矩阵元素的值加到次对角线的和中。
-
打印输入矩阵的 主 对角线元素的和。
-
打印输入矩阵的 次 对角线元素的和。
-
创建一个变量来存储 输入矩阵。
-
通过将输入矩阵和行数(维度)作为参数传递给上述定义的 sumOfDiagonals() 函数,来调用该函数以打印对角线和。
示例
以下程序使用嵌套循环返回输入矩阵的对角线和。
# creating a function to print the sum of diagonals
# of a matrix by accepting input matrix, rows as arguments
def sumOfDiagonals(inputMatrix, rows):
# Initializing with 0 to store the principal diagonal sum
principal_diag = 0
# Initializing with 0 to store the secondary diagonal sum
secondary_diag = 0
# Traversing through the rows of a matrix
for p in range(0, rows):
# Traversing through the columns of the current row
for q in range(0, rows):
# Principal diagonal condition
if (p == q):
principal_diag += inputMatrix[p][q]
# Secondary diagonal condition(row -1 because index starts from 0)
if ((p + q) == (rows - 1)):
secondary_diag += inputMatrix[p][q]
# Printing the sum of principal diagonal elements
print("Sum of principal diagonal elements:", principal_diag)
# Printing the sum of secondary diagonal elements
print("Sum of secondary diagonal elements:", secondary_diag)
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3],
[9, 6, 8],
[4, 2, 7]]
rows = 3
print("Given Matrix is:")
# traversing through the rows of a matrix
for p in range(rows):
# Traversing through the columns of a current row
for q in range(rows):
# printing the corresponding element at the current row and column of the matrix
print(inputMatrix[p][q], end=" ")
# Printing a new line
print()
# calling sumOfDiagonals() function by passing input matrix
# and no of rows(dimensions) to it
sumOfDiagonals(inputMatrix, rows)
输出
执行上述程序后,将生成以下输出结果:
Given Matrix is:
5 1 3
9 6 8
4 2 7
Sum of principal diagonal elements: 18
Sum of secondary diagonal elements: 13
时间复杂度 − O(NN),因为我们使用嵌套循环遍历了NN次。
辅助空间 − O(1)。因为我们没有使用额外的空间。
方法2:只使用一个循环
示例
以下程序使用只有一个循环(单个循环)返回输入矩阵对角线的和−
# Creating a function to print the sum of diagonals
# of a matrix by accepting input matrix, rows as arguments
def sumOfDiagonals(inputMatrix, rows):
# Initializing with 0 to store the principal diagonal sum
principal_diag = 0
# Initializing with 0 to store the secondary diagonal sum
secondary_diag = 0
# Traversing the rows of a matrix
for p in range(0, rows):
# Adding the principal Diagonal element of the current row
principal_diag += inputMatrix[p][p]
# Adding the secondary Diagonal element of the current row
secondary_diag += inputMatrix[p][rows - p - 1]
# printing the sum of principal diagonal elements
print("Sum of principal diagonal elements:", principal_diag)
# printing the sum of secondary diagonal elements
print("Sum of secondary diagonal elements:", secondary_diag)
# input matrix(3x3 matrix)
inputMatrix = [[5, 1, 3],
[9, 6, 8],
[4, 2, 7]]
rows = 3
print("The Given Matrix is:")
# traversing through the rows of a matrix
for p in range(rows):
# Traversing through the columns of a current row
for q in range(rows):
# printing the corresponding element at the current row and column of the matrix
print(inputMatrix[p][q], end=" ")
# Printing a new line
print()
# calling sumOfDiagonals() function by passing input matrix
# and no of rows(dimensions) to it
sumOfDiagonals(inputMatrix, rows)
输出
执行上述程序后,将生成以下输出结果
Given Matrix is:
5 1 3
9 6 8
4 2 7
Sum of principal diagonal elements: 18
Sum of secondary diagonal elements: 13
时间复杂度 - O(N)。由于我们使用循环遍历了N次。
辅助空间 - O(1)。由于我们没有使用任何额外的空间。
结论
在这篇文章中,我们学习了关于矩阵对角线的知识以及计算矩阵对角线和(主对角线和副对角线和)的两种不同方法。我们学习了一种高效的方法,只需对矩阵进行一次遍历(O(N)时间复杂度)即可计算出矩阵对角线和。