在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.