在 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]])

顯示陣列 -

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

要計算陣列行列式的符號和自然對數,請在 Python 中使用 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]])

# 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 the sign and natural logarithm of the determinant of an array, 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]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

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

Determinant...
-2.0000000000000004

Result....
(-1.0, 0.6931471805599455)

更新於: 2022年3月2日

152 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告