计算 NumPy 数组的对角线元素之和
有时我们需要求右上、左上、右下或左下对角元素的总和。 Numpy 为我们提供了使用 numpy.trace() 和numpy.diagonal()方法计算不同对角线元素之和的工具。
方法一:使用numpy.trace()求对角线元素之和
Syntax : numpy.trace(a, offset=0, axis1=0, axis2=1, dtype=None, out=None)
示例 1:对于3X3 Numpy 矩阵
Python3
# importing Numpy package
import numpy as np
# creating a 3X3 Numpy matrix
n_array = np.array([[55, 25, 15],
[30, 44, 2],
[11, 45, 77]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the Trace of a matrix
trace = np.trace(n_array)
print("\nTrace of given 3X3 matrix:")
print(trace)
Python3
# importing Numpy package
import numpy as np
# creating a 4X4 Numpy matrix
n_array = np.array([[55, 25, 15, 41],
[30, 44, 2, 54],
[11, 45, 77, 11],
[11, 212, 4, 20]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the Trace of a matrix
trace = np.trace(n_array)
print("\nTrace of given 4X4 matrix:")
print(trace)
Python3
# importing Numpy package
import numpy as np
# creating a 3X3 Numpy matrix
n_array = np.array([[55, 25, 15],
[30, 44, 2],
[11, 45, 77]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# Finding the diagonal elements of a matrix
diag = np.diagonal(n_array)
print("\nDiagonal elements are:")
print(diag)
print("\nSum of Diagonal elements is:")
print(sum(diag))
Python3
# importing Numpy package
import numpy as np
# creating a 5X5 Numpy matrix
n_array = np.array([[5, 2, 1, 4, 6],
[9, 4, 2, 5, 2],
[11, 5, 7, 3, 9],
[5, 6, 6, 7, 2],
[7, 5, 9, 3, 3]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# Finding the diagonal elements of a matrix
diag = np.diagonal(n_array)
print("\nDiagonal elements are:")
print(diag)
print("\nSum of Diagonal elements is:")
print(sum(diag))
输出:
示例 2:对于4X4 Numpy 矩阵
Python3
# importing Numpy package
import numpy as np
# creating a 4X4 Numpy matrix
n_array = np.array([[55, 25, 15, 41],
[30, 44, 2, 54],
[11, 45, 77, 11],
[11, 212, 4, 20]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# calculating the Trace of a matrix
trace = np.trace(n_array)
print("\nTrace of given 4X4 matrix:")
print(trace)
输出:
方法 2:使用numpy.diagonal()求对角线元素的总和
句法 :
numpy.diagonal(a, offset=0, axis1=0, axis2=1
示例 1:对于3X3 Numpy 矩阵
Python3
# importing Numpy package
import numpy as np
# creating a 3X3 Numpy matrix
n_array = np.array([[55, 25, 15],
[30, 44, 2],
[11, 45, 77]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# Finding the diagonal elements of a matrix
diag = np.diagonal(n_array)
print("\nDiagonal elements are:")
print(diag)
print("\nSum of Diagonal elements is:")
print(sum(diag))
输出:
示例 2:对于5X5 Numpy 矩阵
Python3
# importing Numpy package
import numpy as np
# creating a 5X5 Numpy matrix
n_array = np.array([[5, 2, 1, 4, 6],
[9, 4, 2, 5, 2],
[11, 5, 7, 3, 9],
[5, 6, 6, 7, 2],
[7, 5, 9, 3, 3]])
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
# Finding the diagonal elements of a matrix
diag = np.diagonal(n_array)
print("\nDiagonal elements are:")
print(diag)
print("\nSum of Diagonal elements is:")
print(sum(diag))
输出: