返回Python中線性代數的Cholesky分解


要返回Cholesky分解,請使用numpy.linalg.cholesky()方法。返回方陣a的Cholesky分解L * L.H,其中L是下三角矩陣,.H是共軛轉置運算子。a必須是Hermitian(埃爾米特)且正定的。不會執行任何檢查以驗證a是否為Hermitian矩陣。此外,只使用a的下三角和對角元素。實際上只返回L。

引數a是Hermitian(如果所有元素都是實數,則為對稱)正定輸入矩陣。該方法返回a的上三角或下三角Cholesky因子。如果a是矩陣物件,則返回矩陣物件。

步驟

首先,匯入所需的庫:

import numpy as np

使用numpy.array()方法建立一個二維numpy陣列:

arr = np.array([[1,-2j],[2j,5]])

顯示陣列:

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)

要返回Cholesky分解,請使用numpy.linalg.cholesky()方法:

print("\nCholesky decomposition in Linear Algebra...\n",np.linalg.cholesky(arr))

示例

import numpy as np

# Creating a 2D numpy array using the numpy.array() method
arr = np.array([[1,-2j],[2j,5]])

# 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 return the Cholesky decomposition, use the numpy.linalg.cholesky() method.
print("\nCholesky decomposition in Linear Algebra...\n",np.linalg.cholesky(arr))

輸出

Our Array...
[[ 1.+0.j -0.-2.j]
[ 0.+2.j 5.+0.j]]

Dimensions of our Array...
2

Datatype of our Array object...
complex128

Shape of our Array object...
(2, 2)

Cholesky decomposition in Linear Algebra...
[[1.+0.j 0.+0.j]
[0.+2.j 1.+0.j]]

更新於:2022年2月24日

406 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告