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

Skip to content

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

@matteoacrossi

Description

@matteoacrossi

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions