8000 BUG: `linalg.pinv` behaves differently on NumPy version 1.x and 2.x · Issue #28707 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: linalg.pinv behaves differently on NumPy version 1.x and 2.x #28707

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

Open
apiqwe opened this issue Apr 14, 2025 · 4 comments
Open

BUG: linalg.pinv behaves differently on NumPy version 1.x and 2.x #28707

apiqwe opened this issue Apr 14, 2025 · 4 comments
Labels

Comments

@apiqwe
Copy link
apiqwe commented Apr 14, 2025

Describe the issue:

linalg.pinv behaves differently on NumPy version 1.26.4 and 2.1.3.
I would like to know which version results in the correct one? Or are they all wrong?

Reproduce the code example:

import numpy as np

print(np.linalg.pinv(np.array([[1,1],[1,1]]),rcond=0.0))

Error message:

NumPy version 1.26.4:

[[-1.49044426e+16  1.49044426e+16]
 [ 1.49044426e+16 -1.49044426e+16]]


NumPy version 2.1.3:

[[0.25 0.25]
 [0.25 0.25]]

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.1.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='xxx', 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']}},
{'filepath': '/home/jacin/anaconda3/lib/libmkl_rt.so.2',
'internal_api': 'mkl',
'num_threads': 9,
'prefix': 'libmkl_rt',
'threading_layer': 'intel',
'user_api': 'blas',
'version': '2023.1-Product'},
{'filepath': '/home/jacin/anaconda3/lib/libiomp5.so',
'internal_api': 'openmp',
'num_threads': 18,
'prefix': 'libiomp',
'user_api': 'openmp',
'version': None}]

[{'numpy_version': '2.1.3',
'python': '3.10.0 (default, Mar 3 2022, 09:58:08) [GCC 7.5.0]',
'uname': uname_result(system='Linux', node='xxx', 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']}}]

Context for the issue:

No response

@charris
Copy link
Member
charris commented Apr 14, 2025

Hmm, on 2.2.3 I get

array([[-1.49044426e+16,  1.49044426e+16],
       [ 1.49044426e+16, -1.49044426e+16]])

In [2]: np.__version__
Out[2]: '2.2.3'

Seems to depend on rcond

In [3]: np.linalg.pinv(np.array([[1,1],[1,1]]),rcond=.1)
Out[3]: 
array([[0.25, 0.25],
       [0.25, 0.25]])

That might put the problem in the BLAS version.

@charris
Copy link
Member
charris commented Apr 14, 2025

The second one is more correct IMHO, as it gives the shortest solution to A@x = array([[1], [1]])

@apiqwe
Copy link
Author
apiqwe commented Apr 14, 2025

Hmm, on 2.2.3 I get

array([[-1.49044426e+16,  1.49044426e+16],
       [ 1.49044426e+16, -1.49044426e+16]])

In [2]: np.__version__
Out[2]: '2.2.3'

Seems to depend on rcond

In [3]: np.linalg.pinv(np.array([[1,1],[1,1]]),rcond=.1)
Out[3]: 
array([[0.25, 0.25],
       [0.25, 0.25]])

That might put the problem in the BLAS version.

on NumPy version 2.2.3, my result is also:

[[0.25 0.25]
[0.25 0.25]]

@charris
Copy link
Member
charris commented Apr 14, 2025

Note that rcond=0 is going to be numerically delicate when the matrix is singular.

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
0