Compute the inverse of a matrix. Given a square matrix `a`, return the matrix `ainv` satisfying ``a @ ainv = ainv @ a = eye(a.shape[0])``. Parameters ---------- a : (..., M, M) array_like Matrix to be inverted. Returns ------- ainv : (..., M, M) ndarra
(a)
| 546 | |
| 547 | @array_function_dispatch(_unary_dispatcher) |
| 548 | def inv(a): |
| 549 | """ |
| 550 | Compute the inverse of a matrix. |
| 551 | |
| 552 | Given a square matrix `a`, return the matrix `ainv` satisfying |
| 553 | ``a @ ainv = ainv @ a = eye(a.shape[0])``. |
| 554 | |
| 555 | Parameters |
| 556 | ---------- |
| 557 | a : (..., M, M) array_like |
| 558 | Matrix to be inverted. |
| 559 | |
| 560 | Returns |
| 561 | ------- |
| 562 | ainv : (..., M, M) ndarray or matrix |
| 563 | Inverse of the matrix `a`. |
| 564 | |
| 565 | Raises |
| 566 | ------ |
| 567 | LinAlgError |
| 568 | If `a` is not square or inversion fails. |
| 569 | |
| 570 | See Also |
| 571 | -------- |
| 572 | scipy.linalg.inv : Similar function in SciPy. |
| 573 | numpy.linalg.cond : Compute the condition number of a matrix. |
| 574 | numpy.linalg.svd : Compute the singular value decomposition of a matrix. |
| 575 | |
| 576 | Notes |
| 577 | ----- |
| 578 | Broadcasting rules apply, see the `numpy.linalg` documentation for |
| 579 | details. |
| 580 | |
| 581 | If `a` is detected to be singular, a `LinAlgError` is raised. If `a` is |
| 582 | ill-conditioned, a `LinAlgError` may or may not be raised, and results may |
| 583 | be inaccurate due to floating-point errors. |
| 584 | |
| 585 | References |
| 586 | ---------- |
| 587 | .. [1] Wikipedia, "Condition number", |
| 588 | https://en.wikipedia.org/wiki/Condition_number |
| 589 | |
| 590 | Examples |
| 591 | -------- |
| 592 | >>> import numpy as np |
| 593 | >>> from numpy.linalg import inv |
| 594 | >>> a = np.array([[1., 2.], [3., 4.]]) |
| 595 | >>> ainv = inv(a) |
| 596 | >>> np.allclose(a @ ainv, np.eye(2)) |
| 597 | True |
| 598 | >>> np.allclose(ainv @ a, np.eye(2)) |
| 599 | True |
| 600 | |
| 601 | If a is a matrix object, then the return value is a matrix as well: |
| 602 | |
| 603 | >>> ainv = inv(np.matrix(a)) |
| 604 | >>> ainv |
| 605 | matrix([[-2. , 1. ], |
no test coverage detected