question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: nan returned by np.linalg.det while it should be 0 on arm64 mac

See original GitHub issue

Describe the issue:

np.linalg.det on a 4x4 real matrix returns nan, while it should be 0.

If the same code example is run on an intel64 linux machine, the determinant is zero for both matrices.

This happens both with conda-forge and pypi numpy, on both python 3.9 and 3.10.

Reproduce the code example:

import numpy as np
np.linalg.det(np.array([[1.0000000000000000e+000, 0.0000000000000000e+000,
         0.0000000000000000e+000, 0.0000000000000000e+000],
        [0.0000000000000000e+000, 1.0000000000000000e+000,
         3.8307904270117927e-146, 1.4674955295685193e-291],
        [0.0000000000000000e+000, 3.8307904270117927e-146,
         1.4674955295685193e-291, 0.0000000000000000e+000],
        [0.0000000000000000e+000, 1.4674955295685193e-291,
         0.0000000000000000e+000, 0.0000000000000000e+000]]))
/opt/homebrew/Caskroom/miniforge/base/envs/numpy-test/lib/python3.10/site-packages/numpy/linalg/linalg.py:2154: RuntimeWarning: overflow encountered in det
  r = _umath_linalg.det(a, signature=signature)
/opt/homebrew/Caskroom/miniforge/base/envs/numpy-test/lib/python3.10/site-packages/numpy/linalg/linalg.py:2154: RuntimeWarning: invalid value encountered in det
  r = _umath_linalg.det(a, signature=signature)
nan

This one instead works fine:

np.linalg.det(np.array([[1.0000000000000000e+000, 0.0000000000000000e+000,
         0.0000000000000000e+000, 0.0000000000000000e+000],
        [0.0000000000000000e+000, 1.0000000000000000e+000,
         3.8307904315347111e-146, 1.4674955330337903e-291],
        [0.0000000000000000e+000, 3.8307904315347111e-146,
         1.4674955330337899e-291, 0.0000000000000000e+000],
        [0.0000000000000000e+000, 1.4674955330337903e-291,
         0.0000000000000000e+000, 0.0000000000000000e+000]]))

I’m also attaching a .zip file with the matrices saved as .npy files in case there are numerical precision problems: numpy_nan.zip.

test_nan.npy gives the nan while for test_ok.npy the determinant is correct.

Error message:

No response

NumPy/Python version information:

1.23.1 3.10.0 | packaged by conda-forge | (default, Nov 20 2021, 02:27:15) [Clang 11.1.0 ]

Output of np.show_config():

>>> np.show_config()
openblas64__info:
    libraries = ['openblas64_', 'openblas64_']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None)]
    runtime_library_dirs = ['/usr/local/lib']
blas_ilp64_opt_info:
    libraries = ['openblas64_', 'openblas64_']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None)]
    runtime_library_dirs = ['/usr/local/lib']
openblas64__lapack_info:
    libraries = ['openblas64_', 'openblas64_']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None), ('HAVE_LAPACKE', None)]
    runtime_library_dirs = ['/usr/local/lib']
lapack_ilp64_opt_info:
    libraries = ['openblas64_', 'openblas64_']
    library_dirs = ['/usr/local/lib']
    language = c
    define_macros = [('HAVE_CBLAS', None), ('BLAS_SYMBOL_SUFFIX', '64_'), ('HAVE_BLAS_ILP64', None), ('HAVE_LAPACKE', None)]
    runtime_library_dirs = ['/usr/local/lib']
Supported SIMD extensions in this NumPy install:
    baseline = NEON,NEON_FP16,NEON_VFPV4,ASIMD
    found = ASIMDHP
    not found = ASIMDDP,ASIMDFHM

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:20 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
mattipcommented, Aug 8, 2022

How do I get the latest openblas?

You can build from source (not recommended) or wait a few weeks until the conda forge feedstock is updated, and then install that. Note that it seems you are only using the pip installed numpy, to install from conda forge you would have to do

pip uninstall numpy
conda install openblas==0.3.21
conda install --force-reinstall numpy
1reaction
mattipcommented, Aug 8, 2022

From the output above, you are running the arm64 variant and using the homebrew-provided OpenBLAS 0.3.20. It is strange that the pip-installed numpy does not show anything, I think you need to import numpy first:

python -c "import numpy, threadpoolctl, pprint; \
pprint.pprint(threadpoolctl.threadpool_info())"

OpenBLAS released 0.3.21 yesterday, I wonder if that will change anything.

Read more comments on GitHub >

github_iconTop Results From Across the Web

np.linalg.inv() leads to array full of np.nan - Stack Overflow
A matrix has and inverse if its determinant is non-zero. Check first whether np.linalg.det(I-A) ~= 0. If it's non-zero, then you should be ......
Read more >
SciPy 1.9.0 Release Notes
SciPy 1.9.0 is the culmination of 6 months of hard work. It contains many new features, numerous bug-fixes, improved test coverage and better...
Read more >
Release Notes — NumPy v1.15 Manual
This is a bugfix release for bugs and regressions reported following the 1.15.0 release. The annoying but harmless RuntimeWarning that “numpy.
Read more >
Release Notes — NumPy v1.17 Manual
#14436: BUG: Update 1.17.x with 1.18.0-dev pocketfft.py. ... In some cases where interp would previously return nan , it now returns an appropriate...
Read more >
Release Notes — Numba 0.56.4+0.g288a38bbd.dirty-py3.7 ...
This is a bugfix release that supports NumPy 1.23 and fixes CUDA function caching. ... 3.7 as the next release series (Numba 0.57)...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found