在 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