在 Python 中计算 N 维数组的逆矩阵


要计算 N 维数组的逆矩阵,可以使用 Python 中的 numpy.linalg.tensorinv() 方法。结果是相对于 tensordot 操作 tensordot(a, b, ind) 的 a 的逆矩阵,即,在浮点精度范围内,tensordot(tensorinv(a), a, ind) 是 tensordot 操作的“单位”张量。

该方法返回 a 的 tensordot 逆矩阵,形状为 a.shape[ind:] + a.shape[:ind]。第一个参数是 a,要“反转”的张量。它的形状必须是“方形”,即 prod(a.shape[:ind]) == prod(a.shape[ind:])。第二个参数是 ind,参与逆和运算的前几个索引的数量。必须是正整数,默认为 2。

步骤

首先,导入所需的库 -

import numpy as np
from numpy.linalg import inv

创建一个数组。numpy.eye() 返回一个二维数组,对角线上的元素为 1,其他位置的元素为 0 -

arr = np.eye(4*6)

更改上面创建的数组的形状 -

arr.shape = (4, 6, 8, 3)

显示数组 -

print("Our Array...
",arr)

检查维度 -

print("
Dimensions of our Array...
",arr.ndim)

获取数据类型 -

print("
Datatype of our Array object...
",arr.dtype)

获取形状 -

print("
Shape of our Array object...
",arr.shape)

要计算 N 维数组的逆矩阵,可以使用 Python 中的 numpy.linalg.tensorinv() 方法 -

print("
Result...
",np.linalg.tensorinv(arr))

示例

import numpy as np
from numpy.linalg import inv

# Create an array

# The numpy.eye() returns a 2-D array with ones on the diagonal and zeros elsewhere
arr = np.eye(4*6)

# Changing the shape of the array created above
arr.shape = (4, 6, 8, 3)

# Display the array
print("Our Array...
",arr) # Check the Dimensions print("
Dimensions of our Array...
",arr.ndim) # Get the Datatype print("
Datatype of our Array object...
",arr.dtype) # Get the Shape print("
Shape of our Array object...
",arr.shape) # To compute the inverse of an N-dimensional array, use the numpy.linalg.tensorinv() method in Python. print("
Result...
",np.linalg.tensorinv(arr))

输出

Our Array...
  [[[[1. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]]

  [[0. 1. 0.]
  [0. 0. 0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0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 1. 0.]]

  [[0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 0.]
  [0. 0. 1.]]]]

Dimensions of our Array...
4

Datatype of our Array object...
float64

Shape of our Array object...
(4, 6, 8, 3)

Result...
  [[[[1. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]]

  [[0. 1. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]]

  [[0. 0. 1. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]]]


  [[[0. 0. 0. 1. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]]

  [[0. 0. 0. 0. 1. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]]

  [[0. 0. 0. 0. 0. 1.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]]]


  [[[0. 0. 0. 0. 0. 0.]
  [1. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]]

  [[0. 0. 0. 0. 0. 0.]
  [0. 1. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]]

  [[0. 0. 0. 0. 0. 0.]
  [0. 0. 1. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]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 0. 1. 0. 0. 0.]]]


  [[[0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 1. 0. 0.]]

  [[0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 1. 0.]]

  [[0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0. 1.]]]]

更新于: 2022-02-25

182 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告