Compute the (multiplicative) inverse of a matrix. Given a square matrix `a`, return the matrix `ainv` satisfying ``dot(a, ainv) = dot(ainv, a) = eye(a.shape[0])``. Parameters ---------- a : (..., M, M) array_like Matrix to be inverted. Returns -------
(a)
| 491 | |
| 492 | @array_function_dispatch(_unary_dispatcher) |
| 493 | def inv(a): |
| 494 | """ |
| 495 | Compute the (multiplicative) inverse of a matrix. |
| 496 | |
| 497 | Given a square matrix `a`, return the matrix `ainv` satisfying |
| 498 | ``dot(a, ainv) = dot(ainv, a) = eye(a.shape[0])``. |
| 499 | |
| 500 | Parameters |
| 501 | ---------- |
| 502 | a : (..., M, M) array_like |
| 503 | Matrix to be inverted. |
| 504 | |
| 505 | Returns |
| 506 | ------- |
| 507 | ainv : (..., M, M) ndarray or matrix |
| 508 | (Multiplicative) inverse of the matrix `a`. |
| 509 | |
| 510 | Raises |
| 511 | ------ |
| 512 | LinAlgError |
| 513 | If `a` is not square or inversion fails. |
| 514 | |
| 515 | See Also |
| 516 | -------- |
| 517 | scipy.linalg.inv : Similar function in SciPy. |
| 518 | |
| 519 | Notes |
| 520 | ----- |
| 521 | |
| 522 | .. versionadded:: 1.8.0 |
| 523 | |
| 524 | Broadcasting rules apply, see the `numpy.linalg` documentation for |
| 525 | details. |
| 526 | |
| 527 | Examples |
| 528 | -------- |
| 529 | >>> from numpy.linalg import inv |
| 530 | >>> a = np.array([[1., 2.], [3., 4.]]) |
| 531 | >>> ainv = inv(a) |
| 532 | >>> np.allclose(np.dot(a, ainv), np.eye(2)) |
| 533 | True |
| 534 | >>> np.allclose(np.dot(ainv, a), np.eye(2)) |
| 535 | True |
| 536 | |
| 537 | If a is a matrix object, then the return value is a matrix as well: |
| 538 | |
| 539 | >>> ainv = inv(np.matrix(a)) |
| 540 | >>> ainv |
| 541 | matrix([[-2. , 1. ], |
| 542 | [ 1.5, -0.5]]) |
| 543 | |
| 544 | Inverses of several matrices can be computed at once: |
| 545 | |
| 546 | >>> a = np.array([[[1., 2.], [3., 4.]], [[1, 3], [3, 5]]]) |
| 547 | >>> inv(a) |
| 548 | array([[[-2. , 1. ], |
| 549 | [ 1.5 , -0.5 ]], |
| 550 | [[-1.25, 0.75], |
no test coverage detected