Compute the 'inverse' of an N-dimensional array. 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. P
(a, ind=2)
| 471 | |
| 472 | @array_function_dispatch(_tensorinv_dispatcher) |
| 473 | def tensorinv(a, ind=2): |
| 474 | """ |
| 475 | Compute the 'inverse' of an N-dimensional array. |
| 476 | |
| 477 | The result is an inverse for `a` relative to the tensordot operation |
| 478 | ``tensordot(a, b, ind)``, i. e., up to floating-point accuracy, |
| 479 | ``tensordot(tensorinv(a), a, ind)`` is the "identity" tensor for the |
| 480 | tensordot operation. |
| 481 | |
| 482 | Parameters |
| 483 | ---------- |
| 484 | a : array_like |
| 485 | Tensor to 'invert'. Its shape must be 'square', i. e., |
| 486 | ``prod(a.shape[:ind]) == prod(a.shape[ind:])``. |
| 487 | ind : int, optional |
| 488 | Number of first indices that are involved in the inverse sum. |
| 489 | Must be a positive integer, default is 2. |
| 490 | |
| 491 | Returns |
| 492 | ------- |
| 493 | b : ndarray |
| 494 | `a`'s tensordot inverse, shape ``a.shape[ind:] + a.shape[:ind]``. |
| 495 | |
| 496 | Raises |
| 497 | ------ |
| 498 | LinAlgError |
| 499 | If `a` is singular or not 'square' (in the above sense). |
| 500 | |
| 501 | See Also |
| 502 | -------- |
| 503 | numpy.tensordot, tensorsolve |
| 504 | |
| 505 | Examples |
| 506 | -------- |
| 507 | >>> import numpy as np |
| 508 | >>> a = np.eye(4*6).reshape((4, 6, 8, 3)) |
| 509 | >>> ainv = np.linalg.tensorinv(a, ind=2) |
| 510 | >>> ainv.shape |
| 511 | (8, 3, 4, 6) |
| 512 | >>> rng = np.random.default_rng() |
| 513 | >>> b = rng.normal(size=(4, 6)) |
| 514 | >>> np.allclose(np.tensordot(ainv, b), np.linalg.tensorsolve(a, b)) |
| 515 | True |
| 516 | |
| 517 | >>> a = np.eye(4*6).reshape((24, 8, 3)) |
| 518 | >>> ainv = np.linalg.tensorinv(a, ind=1) |
| 519 | >>> ainv.shape |
| 520 | (8, 3, 24) |
| 521 | >>> rng = np.random.default_rng() |
| 522 | >>> b = rng.normal(size=24) |
| 523 | >>> np.allclose(np.tensordot(ainv, b, 1), np.linalg.tensorsolve(a, b)) |
| 524 | True |
| 525 | |
| 526 | """ |
| 527 | a = asarray(a) |
| 528 | oldshape = a.shape |
| 529 | prod = 1 |
| 530 | if ind > 0: |