在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

更新于:2022年3月2日

1K+ 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.