在Python中计算线性代数中矩阵的条件数
要计算线性代数中矩阵的条件数,在Python中使用numpy.linalg.cond()方法。根据p的值,该方法能够使用七种不同的范数返回条件数。
返回矩阵的条件数。可能是无穷大。矩阵x的条件数定义为x的范数乘以x的逆的范数;范数可以是通常的L2范数或其他一些矩阵范数中的某个。第一个参数是x,要求其条件数的矩阵。第二个参数是p,用于计算条件数的范数的阶数。
步骤
首先,导入所需的库 –
import numpy as np
from numpy import linalg as LA
创建一个数组 –
arr = np.array([[ 1, 1, 0],
[1, 0, 1],
[1, 0, 0]])
显示数组 –
print("Our Array...\n",arr)
检查尺寸 –
print("\nDimensions of our Array...\n",arr.ndim)
获取数据类型 –
print("\nDatatype of our Array object...\n",arr.dtype)
获取形状 –
print("\nShape of our Array object...\n",arr.shape)
在线性代数中,要计算一个矩阵的条件数,可以使用Python的numpy.linalg.cond()方法。
print("\nResult...\n",LA.cond(arr))
示例
import numpy as np
from numpy import linalg as LA
# Create an array
arr = np.array([[ 1, 1, 0],
[1, 0, 1],
[1, 0, 0]])
# Display the array
print("Our Array...\n",arr)
# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)
# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)
# Get the Shape
print("\nShape of our Array object...\n",arr.shape)
# To compute the condition number of a matrix in linear algebra, use the numpy.linalg.cond() method in Python
print("\nResult...\n",LA.cond(arr))
输出
Our Array...
[[1 1 0]
[1 0 1]
[1 0 0]]
Dimensions of our Array...
2
Datatype of our Array object...
int64
Shape of our Array object...
(3, 3)
Result...
3.7320508075688776