在 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]]
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP