在 Python 中計算線性代數中矩陣的行列式


如需計算線性代數中矩陣的行列式,請在 Python Numpy 中使用 np.linalg.det()。第一個引數 a 是要計算行列式的輸入矩陣。該方法返回行列式。

步驟

首先,匯入必需的模組 -

import numpy as np

建立一個矩陣 −

arr = np.array([[ 5, 10], [12, 18]])

顯示矩陣 −

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 中使用 np.linalg.det() −

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

示例

import numpy as np

# Create an array
arr = np.array([[ 5, 10], [12, 18]])

# 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 determinant of an array in linear algebra, use the np.linalg.det() in Python Numpy.
print("\nResult (determinant)...\n",np.linalg.det(arr))

輸出

Our Array...
[[ 5 10]
[12 18]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

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

Result (determinant)...
-30.000000000000014

更新時間: 25-Feb-2022

182 次瀏覽

開啟您的職業生涯

完成課程,獲得認證

立即開始
廣告
© . All rights reserved.