Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: np.linalg.inv: Allow disabling error when one matrix is singular in a stack #28782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions numpy/linalg/_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,12 @@ def tensorinv(a, ind=2):

# Matrix inversion

def _unary_dispatcher(a):
def _unary_dispatcher(a, *, noerr=None):
return (a,)


@array_function_dispatch(_unary_dispatcher)
def inv(a):
def inv(a, *, noerr=False):
"""
Compute the inverse of a matrix.

Expand All @@ -507,6 +507,10 @@ def inv(a):
----------
a : (..., M, M) array_like
Matrix to be inverted.
noerr : bool, optional
If True, do not raise a LinAlgError when a matrix is singular.
Instead, return a matrix with NaN values for the singular matrices.
Default is False.

Returns
-------
Expand Down Expand Up @@ -579,6 +583,25 @@ def inv(a):
[-0.5 , 0.625, 0.25 ],
[ 0. , 0. , 1. ]])

Using the `noerr` parameter to handle singular matrices in a stack:

>>> a = np.array([
... [[1.0, 0.0], [0.0, 1.0]], # invertible
... [[1.0, 1.0], [1.0, 1.0]], # singular
... [[2.0, 1.0], [1.0, 2.0]] # invertible
... ])
>>> # Without noerr, a LinAlgError is raised
>>> try:
... inv(a)
... except np.linalg.LinAlgError:
... print("LinAlgError raised")
LinAlgError raised
>>> # With noerr=True, NaN values are returned for singular matrices
>>> result = inv(a, noerr=True)
>>> # Check which matrices were singular
>>> np.isnan(result).any(axis=(1, 2))
array([False, True, False])

To detect ill-conditioned matrices, you can use `numpy.linalg.cond` to
compute its *condition number* [1]_. The larger the condition number, the
more ill-conditioned the matrix is. As a rule of thumb, if the condition
Expand All @@ -605,9 +628,13 @@ def inv(a):
t, result_t = _commonType(a)

signature = 'D->D' if isComplexType(t) else 'd->d'
with errstate(call=_raise_linalgerror_singular, invalid='call',
over='ignore', divide='ignore', under='ignore'):
ainv = _umath_linalg.inv(a, signature=signature)
if noerr:
with errstate(all='ignore'):
ainv = _umath_linalg.inv(a, signature=signature)
else:
with errstate(call=_raise_linalgerror_singular, invalid='call',
over='ignore', divide='ignore', under='ignore'):
ainv = _umath_linalg.inv(a, signature=signature)
return wrap(ainv.astype(result_t, copy=False))


Expand Down
23 changes: 23 additions & 0 deletions numpy/linalg/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,29 @@ class ArraySubclass(np.ndarray):
assert_equal(a.shape, res.shape)
assert_(isinstance(res, ArraySubclass))

def test_noerr(self):
# test noerr=True case
a = np.array([
[[1.0, 0.0], [0.0, 1.0]], # invertible
[[1.0, 1.0], [1.0, 1.0]], # singular
[[2.0, 1.0], [1.0, 2.0]] # invertible
])

result = linalg.inv(a, noerr=True)

assert_allclose(
result,
[
[[1.0, 0.0], [0.0, 1.0]],
[[np.nan, np.nan], [np.nan, np.nan]],
[[2.0 / 3, -1.0 / 3], [-1.0 / 3, 2.0 / 3]],
]
)

# test noerr=False case
with assert_raises(np.linalg.LinAlgError):
linalg.inv(a, noerr=False)


class EigvalsCases(LinalgSquareTestCase, LinalgGeneralizedSquareTestCase):

Expand Down
Loading