在 Python 中計算矩陣堆疊的對數行列式


要計算矩陣堆疊的對數行列式,請在 Python 中使用 numpy.linalg.slogdet() 方法。第一個引數 s 是一個輸入陣列,必須是方形的二維陣列。該方法,帶符號返回一個表示行列式符號的數字。對於實數矩陣,此值為 1、0 或 -1。對於複數矩陣,此值為絕對值為 1 的複數,否則為 0。

該方法,帶 logdet 返回行列式絕對值的自然對數。如果行列式為零,則符號將為 0,logdet 將為 -Inf。在所有情況下,行列式都等於 sign * np.exp(logdet)。

步驟

首先,匯入所需的庫 -

import numpy as np

建立一個數組 -

arr = np.array([ [[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]] ])

顯示陣列 -

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)

線性代數中陣列的行列式 -

print("\nDeterminant...\n",np.linalg.det(arr))

要計算矩陣堆疊的對數行列式,請使用 numpy.linalg.slogdet() 方法。如果行列式為零,則符號將為 0,logdet 將為 -Inf。在所有情況下,行列式都等於 sign * np.exp(logdet) -

(sign, logdet) = np.linalg.slogdet(arr)
print("\nResult....\n",(sign, logdet))

示例

import numpy as np

# Create an array
arr = np.array([ [[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]])

# 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)

# The determinant of an array in linear algebra
print("\nDeterminant...\n",np.linalg.det(arr))

# To compute log-determinants for a stack of matrices, use the numpy.linalg.slogdet() method in Python
(sign, logdet) = np.linalg.slogdet(arr)
print("\nResult....\n",(sign, logdet))

輸出

Our Array...
[[[1 2]
[3 4]]

[[1 2]
[2 1]]

[[1 3]
[3 1]]]

Dimensions of our Array...
3

Datatype of our Array object...
int64

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

Determinant...
[-2. -3. -8.]

Result....
(array([-1., -1., -1.]), array([0.69314718, 1.09861229, 2.07944154]))

更新於: 2022年2月25日

143 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.