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)
| 417 | |
| 418 | @array_function_dispatch(_tensorinv_dispatcher) |
| 419 | def tensorinv(a, ind=2): |
| 420 | """ |
| 421 | Compute the 'inverse' of an N-dimensional array. |
| 422 | |
| 423 | The result is an inverse for `a` relative to the tensordot operation |
| 424 | ``tensordot(a, b, ind)``, i. e., up to floating-point accuracy, |
| 425 | ``tensordot(tensorinv(a), a, ind)`` is the "identity" tensor for the |
| 426 | tensordot operation. |
| 427 | |
| 428 | Parameters |
| 429 | ---------- |
| 430 | a : array_like |
| 431 | Tensor to 'invert'. Its shape must be 'square', i. e., |
| 432 | ``prod(a.shape[:ind]) == prod(a.shape[ind:])``. |
| 433 | ind : int, optional |
| 434 | Number of first indices that are involved in the inverse sum. |
| 435 | Must be a positive integer, default is 2. |
| 436 | |
| 437 | Returns |
| 438 | ------- |
| 439 | b : ndarray |
| 440 | `a`'s tensordot inverse, shape ``a.shape[ind:] + a.shape[:ind]``. |
| 441 | |
| 442 | Raises |
| 443 | ------ |
| 444 | LinAlgError |
| 445 | If `a` is singular or not 'square' (in the above sense). |
| 446 | |
| 447 | See Also |
| 448 | -------- |
| 449 | numpy.tensordot, tensorsolve |
| 450 | |
| 451 | Examples |
| 452 | -------- |
| 453 | >>> a = np.eye(4*6) |
| 454 | >>> a.shape = (4, 6, 8, 3) |
| 455 | >>> ainv = np.linalg.tensorinv(a, ind=2) |
| 456 | >>> ainv.shape |
| 457 | (8, 3, 4, 6) |
| 458 | >>> b = np.random.randn(4, 6) |
| 459 | >>> np.allclose(np.tensordot(ainv, b), np.linalg.tensorsolve(a, b)) |
| 460 | True |
| 461 | |
| 462 | >>> a = np.eye(4*6) |
| 463 | >>> a.shape = (24, 8, 3) |
| 464 | >>> ainv = np.linalg.tensorinv(a, ind=1) |
| 465 | >>> ainv.shape |
| 466 | (8, 3, 24) |
| 467 | >>> b = np.random.randn(24) |
| 468 | >>> np.allclose(np.tensordot(ainv, b, 1), np.linalg.tensorsolve(a, b)) |
| 469 | True |
| 470 | |
| 471 | """ |
| 472 | a = asarray(a) |
| 473 | oldshape = a.shape |
| 474 | prod = 1 |
| 475 | if ind > 0: |
| 476 | invshape = oldshape[ind:] + oldshape[:ind] |