在 Python 中一次計算多個矩陣的乘法逆
要在 Python 中計算矩陣的(乘法)逆矩陣,請使用 numpy.linalg.inv() 方法。給定一個方陣 a,返回滿足 dot(a, ainv) = dot(ainv, a) = eye(a.shape[0]) 的矩陣 ainv。該方法返回矩陣 a 的(乘法)逆矩陣。第一個引數 a 是要進行逆運算的矩陣。
步驟
首先,匯入必需的庫 -
import numpy as np from numpy.linalg import inv
使用 array() 建立多個矩陣 -
arr = np.array([[[1., 2.], [3., 4.]], [[1, 3], [3, 5]]])
顯示陣列 -
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 several matrices using array()
arr = np.array([[[1., 2.], [3., 4.]], [[1, 3], [3, 5]]])
# 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... [[[1. 2.] [3. 4.]] [[1. 3.] [3. 5.]]] Dimensions of our Array... 3 Datatype of our Array object... float64 Shape of our Array object... (2, 2, 2) Result... [[[-2. 1. ] [ 1.5 -0.5 ]] [[-1.25 0.75] [ 0.75 -0.25]]]
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP