使用 Python 中的 matrix() 計算矩陣物件的乘法逆
若要使用 matrix() 計算矩陣物件的乘法逆,請在 Python 中使用 numpy.linalg.inv() 方法。給定方陣 a,返回滿足 dot(a, ainv) = dot(ainv, a) = eye(a.shape[0]) 的矩陣 ainv。
該方法返回矩陣 a 的(乘法)逆。第 1 個引數 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)
若要使用 matrix() 計算矩陣物件的乘法逆,請在 Python 中使用 numpy.linalg.inv() 方法 -
print("\nResult...\n",np.linalg.inv(np.matrix(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 object with matrx(), use the numpy.linalg.inv() method in Python. print("\nResult...\n",np.linalg.inv(np.matrix(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]]
廣告