使用NumPy计算一个给定矩阵的条件数
在这篇文章中,我们将使用NumPy包的cond()函数来计算给定矩阵的条件数。 cond()是NumPy包中线性代数模块的一个函数。
语法:
numpy.linalg.cond(x, p=None)
例子1:条件2X2矩阵的数量
# Importing library
import numpy as np
# Creating a 2X2 matrix
matrix = np.array([[4, 2], [3, 1]])
print("Original matrix:")
print(matrix)
# Output
result = np.linalg.cond(matrix)
print("Condition number of the matrix:")
print(result)
输出:
Original matrix:
[[4 2]
[3 1]]
Condition number of the matrix:
14.933034373659256
例子2:条件 3X3矩阵的数量
# Importing library
import numpy as np
# Creating a 3X3 matrix
matrix = np.array([[4, 2, 0], [3, 1, 2], [1, 6, 4]])
print("Original matrix:")
print(matrix)
# Output
result = np.linalg.cond(matrix)
print("Condition number of the matrix:")
print(result)
输出:
Original matrix:
[[4 2 0]
[3 1 2]
[1 6 4]]
Condition number of the matrix:
5.347703616656448
例子3:条件 4X4矩阵的数量
# Importing library
import numpy as np
# Creating a 4X4 matrix
matrix = np.array([[4, 1, 4, 2], [3, 1, 2, 0],
[3, 5, 7 ,1], [0, 6, 8, 4]])
print("Original matrix:")
print(matrix)
# Output
result = np.linalg.cond(matrix)
print("Condition number of the matrix:")
print(result)
输出:
Original matrix:
[[4 1 4 2]
[3 1 2 0]
[3 5 7 1]
[0 6 8 4]]
Condition number of the matrix:
57.34043866386226