使用 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]]
广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP