在 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]]

更新于: 2022 年 2 月 25 日

1 千多人浏览

开启你的 职业

完成课程获得认证

开始吧
广告
© . All rights reserved.