Skip to content

BUG: linalg.solve brings different results on numpy 1.x and 2.x #28709

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

Closed
0x1abcd opened this issue Apr 15, 2025 · 1 comment
Closed

BUG: linalg.solve brings different results on numpy 1.x and 2.x #28709

0x1abcd opened this issue Apr 15, 2025 · 1 comment
Labels

Comments

@0x1abcd
Copy link

0x1abcd commented Apr 15, 2025

Describe the issue:

linalg.solve brings different results on numpy 1.26.4 and 2.2.3.
Is the version 2.2.3 result correct?

Reproduce the code example:

import numpy as np

print(np.linalg.solve(np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]]), np.array([[1, 2], [3, 4]])))

Error message:

numpy version 1.26.4:
[[0.  0.5]
 [0.  0.5]]

numpy version 2.2.3:
[[[ 1.  0.]
  [ 0.  1.]]

 [[ 5.  4.]
  [-4. -3.]]]

Python and NumPy Versions:

1.26.4
3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, 13:27:36) [GCC 11.2.0]

2.2.3
3.10.0 (default, Mar 3 2022, 09:58:08) [GCC 7.5.0]

Runtime Environment:

[{'numpy_version': '1.26.4',
'python': '3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, '
'13:27:36) [GCC 11.2.0]',
'uname': uname_result(system='Linux', node='zzz', release='5.15.167.4-microsoft-standard-WSL2', version='#1 SMP Tue Nov 5 00:21:55 UTC 2024', machine='x86_64')},
{'simd_extensions': {'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']}},
{'architecture': 'Haswell',
'filepath': '/home/jacin/anaconda3/lib/python3.12/site-packages/numpy.libs/libopenblas64_p-r0-0cf96a72.3.23.dev.so',
'internal_api': 'openblas',
'num_threads': 18,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.23.dev'}]

[{'numpy_version': '2.2.3',
'python': '3.10.0 (default, Mar 3 2022, 09:58:08) [GCC 7.5.0]',
'uname': uname_result(system='Linux', node='zzz', release='5.15.167.4-microsoft-standard-WSL2', version='#1 SMP Tue Nov 5 00:21:55 UTC 2024', machine='x86_64')},
{'simd_extensions': {'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']}},
{'architecture': 'Haswell',
'filepath': '/home/jacin/anaconda3/envs/llm4api/lib/python3.10/site-packages/numpy.libs/libscipy_openblas64_-6bb31eeb.so',
'internal_api': 'openblas',
'num_threads': 18,
'prefix': 'libscipy_openblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.28'}]

Context for the issue:

No response

@jakevdp
Copy link
Contributor

jakevdp commented Apr 15, 2025

Thanks for the report! This is expected due to the changing of batching convention for np.linalg.solve in NumPy 2.0; see https://numpy.org/devdocs/release/2.0.0-notes.html#removed-ambiguity-when-broadcasting-in-np-solve for details.

To recover the 1.x output in 2.x for this case, you can replace np.linalg.solve(x, y) with np.linalg.solve(x, y[..., None])[..., 0].

@jakevdp jakevdp closed this as completed Apr 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants