scipy.linalg.

inv#

scipy.linalg.inv(a, overwrite_a=False, check_finite=True, assume_a=None)[source]#

Compute the inverse of a matrix.

If the data matrix is known to be a particular type then supplying the corresponding string to assume_a key chooses the dedicated solver. The available options are

general

‘general’ (or ‘gen’)

upper triangular

‘upper triangular’

lower triangular

‘lower triangular’

symmetric positive definite

‘pos’, ‘pos upper’, ‘pos lower’

For the ‘pos upper’ and ‘pos lower’ options, only the specified triangle of the input matrix is used, and the other triangle is not referenced.

Parameters:
aarray_like, shape (…, M, M)

Square matrix (or a batch of matrices) to be inverted.

overwrite_abool, optional

Discard data in a (may improve performance). Default is False.

check_finitebool, optional

Whether to check that the input matrix contains only finite numbers. Disabling may give a performance gain, but may result in problems (crashes, non-termination) if the inputs do contain infinities or NaNs.

assume_astr, optional

Valid entries are described above. If omitted or None, checks are performed to identify structure so the appropriate solver can be called.

Returns:
ainvndarray

Inverse of the matrix a.

Raises:
LinAlgError

If a is singular.

ValueError

If a is not square, or not 2D.

Notes

The input array a may represent a single matrix or a collection (a.k.a. a “batch”) of square matrices. For example, if a.shape == (4, 3, 2, 2), it is interpreted as a (4, 3)-shaped batch of \(2\times 2\) matrices.

This routine checks the condition number of the a matrix and emits a LinAlgWarning for ill-conditioned inputs.

Examples

>>> import numpy as np
>>> from scipy import linalg
>>> a = np.array([[1., 2.], [3., 4.]])
>>> linalg.inv(a)
array([[-2. ,  1. ],
       [ 1.5, -0.5]])
>>> np.dot(a, linalg.inv(a))
array([[ 1.,  0.],
       [ 0.,  1.]])