-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
BUG: numpy gradient yields incorrect results #28999
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
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Could you try again with the latest |
I just did, same result |
I think this is a misunderstanding of how
The "gradient in rows" is the y gradient and the "gradient in columns" is the x gradient in the way you've plotted things. If you make the following modification:
Then you'll get a plot that looks like what you expect. |
Okay, that is rather confusing, but yes, it indeed solves it. Thanks |
The row major/column major thing isn't really something we can do a whole lot about. It's a pretty fundamental part of how NumPy stores data, in C order. You can tell NumPy to store the data in Fortran order by specifying This is one way in which Matlab and Fortran are less confusing than Python and C. A lot of people also like 1-indexing too! |
Describe the issue:
I thought i was going mad about a 2D gradient calculation, so I made a simple check: a 2D gaussian should have all arrows pointing towards the peak. But that is not what I observe when plotting a quiver diagram, it looks more like the center being a hyperbolic point.
Reproduce the code example:
Error message:
Python and NumPy Versions:
Python 3.9.21
Numpy 2.0.2
Runtime Environment:
[{'numpy_version': '2.0.2',
'python': '3.9.21 (main, Feb 10 2025, 00:00:00) \n'
'[GCC 11.5.0 20240719 (Red Hat 11.5.0-5)]',
'uname': uname_result(system='Linux', node='XXX', release='5.14.0-578.el9.x86_64', version='#1 SMP PREEMPT_DYNAMIC Mon Apr 7 19:22:46 UTC 2025', machine='x86_64')},
{'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'],
'found': ['SSSE3',
'SSE41',
'POPCNT',
'SSE42',
'AVX',
'F16C',
'FMA3',
'AVX2',
'AVX512F',
'AVX512CD',
'AVX512_SKX',
'AVX512_CLX'],
'not_found': ['AVX512_KNL',
'AVX512_KNM',
'AVX512_CNL',
'AVX512_ICL']}},
{'architecture': 'SkylakeX',
'filepath': '/home/XXX/myenv/lib/python3.9/site-packages/numpy.libs/libscipy_openblas64_-99b71e71.so',
'internal_api': 'openblas',
'num_threads': 64,
'prefix': 'libscipy_openblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.27'},
{'architecture': 'SkylakeX',
'filepath': '/home/XXX/myenv/lib/python3.9/site-packages/scipy.libs/libopenblasp-r0-01191904.3.27.so',
'internal_api': 'openblas',
'num_threads': 64,
'prefix': 'libopenblas',
'threading_layer': 'pthreads',
'user_api': 'blas',
'version': '0.3.27'}]
Context for the issue:
If this turns out to be a bug (and given how simple the reference code is i doubt I made a mistake) this is quite important, since gradients are all over the place.
The text was updated successfully, but these errors were encountered: