scipy: BUG: `scipy.linalg.expm` generates inconsistent results between runs

Describe your issue.

For a custom (400, 400) matrix , scipy.linalg.expm generates inconsistent results between runs. I understand that the documentation mentions for n>=400, some approximation will be applied. However, there are just too much differences between runs, potentially causing backward incompatibility. Is there a way to make it deterministic like scipy.sparse.linalg.expm and matlab?

The matrix I used is uploaded here: removed for security reasons.

Reproducing Code Example

import numpy as np
import scipy
data = np.load("laplacian.npy")
scipy.linalg.expm(data)

Error message

N/A

SciPy/NumPy/Python version and system information

1.10.0 1.23.4 sys.version_info(major=3, minor=10, micro=9, releaselevel='final', serial=0)
lapack_mkl_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/path/to/miniconda3/envs/dlrl/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/path/to/miniconda3/envs/dlrl/include']
lapack_opt_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/path/to/miniconda3/envs/dlrl/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/path/to/miniconda3/envs/dlrl/include']
blas_mkl_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/path/to/miniconda3/envs/dlrl/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/path/to/miniconda3/envs/dlrl/include']
blas_opt_info:
    libraries = ['mkl_rt', 'pthread']
    library_dirs = ['/path/to/miniconda3/envs/dlrl/lib']
    define_macros = [('SCIPY_MKL_H', None), ('HAVE_CBLAS', None)]
    include_dirs = ['/path/to/miniconda3/envs/dlrl/include']
Supported SIMD extensions in this NumPy install:
    baseline = SSE,SSE2,SSE3
    found = SSSE3,SSE41,POPCNT,SSE42,AVX,F16C,FMA3,AVX2
    not found = AVX512F,AVX512CD,AVX512_KNL,AVX512_KNM,AVX512_SKX,AVX512_CLX,AVX512_CNL,AVX512_ICL

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 33 (29 by maintainers)

Commits related to this issue

Most upvoted comments

I can reproduce the problem with the main development branch on a Linux machine with OpenBLAS.

If I change this line https://github.com/scipy/scipy/blob/ae7355270b81efd643e9c30822843c81e0972bc0/scipy/linalg/_matfuncs.py#L331

to

    Am = np.zeros((5, n, n), dtype=a.dtype)

the problem goes away. Presumably something in the code that follows assumes that Am is initialized with zeros.

@ilayn, ping me if there’s anything else I can help with here. I have 12 hours per week allocated for identifying if linalg bug reports are due to something in SciPy or OpenBLAS, submitting C only reproducers on the OpenBLAS side or helping fix bugs on the SciPy side

Dangit, I completely forgot this. Let me also have a look.

What is the inconsistency nature you mention? The entries or the eigenvalues or something else?