Skip to content

NumPy: numpy.linalg.LinAlgError not thrown #4859

Open
numpy/numpy
#32036

Description

@henryiii

🐛 Bug

The following code:

import numpy as np

A = np.array([[1,-2j],[2j,5]])
print(np.linalg.cholesky(A))

B = np.array([[1,3],[2,4]])
np.linalg.cholesky(B)

Produces an error on normal CPython, just like the docs state it should.

[[1.+0.j 0.+0.j]
 [0.+2.j 1.+0.j]]
Traceback (most recent call last):
  File "/Users/henryschreiner/git/webpages/iscinumpy.gitlab.io/np.py", line 7, in <module>
    np.linalg.cholesky(B)
  File "/usr/local/lib/python3.12/site-packages/numpy/linalg/linalg.py", line 779, in cholesky
    r = gufunc(a, signature=signature, extobj=extobj)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/numpy/linalg/linalg.py", line 115, in _raise_linalgerror_nonposdef
    raise LinAlgError("Matrix is not positive definite")
numpy.linalg.LinAlgError: Matrix is not positive definite

Pyodide does not get the exception, though, and instead just sets NaN's.

To Reproduce

On https://pyodide.org/en/stable/console.html

>>> import numpy as np
>>> A = np.array([[1,-2j],[2j,5]])
>>> print(np.linalg.cholesky(A))
[[1.+0.j 0.+0.j]
 [0.+2.j 1.+0.j]]
>>> B = np.array([[1,3],[2,4]])
>>> np.linalg.cholesky(B)
array([[nan, nan],
       [nan, nan]])

The same code produces an error outside a pyodide.

Expected behavior

It should throw numpy.linalg.LinAlgError.

Additional context

See scikit-hep/iminuit#995, this is breaking iminuit's test suite.

Activity

  1. hoodmane commented on Jun 10, 2024

    @hoodmane
    Member

    Unfortunately, I think this is very hard for us to fix without webassembly getting support for floating point exception modes. This would require a whole process with the wasm committees. Probably someone should inquire whether it's a change they'd consider...

  2. henryiii commented on Jun 11, 2024

    @henryiii
    ContributorAuthor

    Maybe NumPy should check for NaN in situations like this if running on WASM?

  3. lesteve commented on Jun 23, 2024

    @lesteve
    Contributor

    Just for explicitness:

  4. lesteve commented on Jun 23, 2024

    @lesteve
    Contributor

    As a potential work-around (likely not applicable in iminuit but maybe useful in other cases) scipy.linalg.cholesky does raise an exception on the same data inside Pyodide:

    import numpy as np
    import scipy.linalg
    
    B = np.array([[1,3],[2,4]])
    scipy.linalg.cholesky(B)
    Traceback (most recent call last):
      File "<console>", line 1, in <module>
      File "/lib/python3.12/site-packages/scipy/linalg/_decomp_cholesky.py", line 88, in cholesky
        c, lower = _cholesky(a, lower=lower, overwrite_a=overwrite_a, clean=True,
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "/lib/python3.12/site-packages/scipy/linalg/_decomp_cholesky.py", line 36, in _cholesky
        raise LinAlgError("%d-th leading minor of the array is not positive "
    numpy.linalg.LinAlgError: 2-th leading minor of the array is not positive definite
    
  5. agriyakhetarpal commented on Jun 23, 2024

    @agriyakhetarpal
    Member

    I have noticed the same problem when running the test suite for statsmodels and xfailed for now: statsmodels/statsmodels#9270

  6. agriyakhetarpal commented on Aug 6, 2026

    @agriyakhetarpal
    Member

    This will be fixed in NumPy 2.6, via numpy/numpy#32036.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions