numpy.linalg.tensorinv#

linalg.tensorinv(a, ind=2)[原始碼]#

計算 N 維陣列的「反矩陣」。

結果是相對於 tensordot 運算的 a 的反矩陣 tensordot(a, b, ind),也就是說,在浮點精確度範圍內,tensordot(tensorinv(a), a, ind) 是 tensordot 運算的「單位」張量。

參數:
aarray_like

要「反轉」的張量。其形狀必須是「方形」,即 prod(a.shape[:ind]) == prod(a.shape[ind:])

indint,可選

參與反向和的第一個索引的數量。必須是正整數,預設值為 2。

回傳:
bndarray

a 的 tensordot 反矩陣,形狀為 a.shape[ind:] + a.shape[:ind]

引發:
LinAlgError

如果 a 是奇異的或不是「方形」(在上述意義上)。

範例

>>> import numpy as np
>>> a = np.eye(4*6)
>>> a.shape = (4, 6, 8, 3)
>>> ainv = np.linalg.tensorinv(a, ind=2)
>>> ainv.shape
(8, 3, 4, 6)
>>> rng = np.random.default_rng()
>>> b = rng.normal(size=(4, 6))
>>> np.allclose(np.tensordot(ainv, b), np.linalg.tensorsolve(a, b))
True
>>> a = np.eye(4*6)
>>> a.shape = (24, 8, 3)
>>> ainv = np.linalg.tensorinv(a, ind=1)
>>> ainv.shape
(8, 3, 24)
>>> rng = np.random.default_rng()
>>> b = rng.normal(size=24)
>>> np.allclose(np.tensordot(ainv, b, 1), np.linalg.tensorsolve(a, b))
True