flopscope.numpy.linalg.tensorinv
fnp.linalg.tensorinv(a, ind=2)[flopscope source][numpy source]
Compute the 'inverse' of an N-dimensional array.
Adapted from NumPy docs np.linalg.tensorinv
Cost
4×
Flopscope Context
Tensor inverse. Cost: $n^3$ after reshape (delegates to inv).
The result is an inverse for a relative to the tensordot operation
tensordot(a, b, ind), i. e., up to floating-point accuracy,
tensordot(tensorinv(a), a, ind) is the "identity" tensor for the
tensordot operation.
Parameters
- a:array_like
Tensor to 'invert'. Its shape must be 'square', i. e.,
prod(a.shape[:ind]) == prod(a.shape[ind:]).- ind:int, optional
Number of first indices that are involved in the inverse sum. Must be a positive integer, default is 2.
Returns
- b:ndarray
a's tensordot inverse, shapea.shape[ind:] + a.shape[:ind].
Raises
- :LinAlgError
If
ais singular or not 'square' (in the above sense).
See also
Examples
>>> import flopscope.numpy as fnp
>>> a = flops.eye(4*6)
>>> a.shape = (4, 6, 8, 3)
>>> ainv = flops.linalg.tensorinv(a, ind=2)
>>> ainv.shape
(8, 3, 4, 6)
>>> rng = flops.random.default_rng()
>>> b = rng.normal(size=(4, 6))
>>> flops.allclose(flops.tensordot(ainv, b), flops.linalg.tensorsolve(a, b))
True>>> a = flops.eye(4*6)
>>> a.shape = (24, 8, 3)
>>> ainv = flops.linalg.tensorinv(a, ind=1)
>>> ainv.shape
(8, 3, 24)
>>> rng = flops.random.default_rng()
>>> b = rng.normal(size=24)
>>> flops.allclose(flops.tensordot(ainv, b, 1), flops.linalg.tensorsolve(a, b))
True