在 Python 中計算矩陣乘法逆


要計算矩陣的(乘法)逆運算,可在 Python 中使用 numpy.linalg.inv() 方法。給定方陣 a,它將返回矩陣 ainv,滿足 dot(a, ainv) = dot(ainv, a) = eye(a.shape[0])。該方法返回矩陣 a 的(乘法)逆運算。第一個引數 a 是要進行逆運算的矩陣。

步驟

首先,匯入所需的庫 -

import numpy as np
from numpy.linalg import inv

建立一個數組 -

arr = np.array([[ 5, 10], [ 15, 20 ]])

顯示陣列 -

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.inv() 方法 -

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

示例

import numpy as np
from numpy.linalg import inv

# Create an array
arr = np.array([[ 5, 10], [ 15, 20 ]])

# 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 (multiplicative) inverse of a matrix, use the numpy.linalg.inv() method in Python.
print("\nResult...\n",np.linalg.inv(arr))

輸出

Our Array...
[[ 5 10]
[15 20]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

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

Result...
[[-0.4 0.2]
[ 0.3 -0.1]]

更新於: 2022 年 2 月 25 日

1 千多人瀏覽

開啟你的 職業

完成課程獲得認證

開始吧
廣告
© . All rights reserved.