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

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

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

更新於: 2022-02-25

182 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.