numpy icon indicating copy to clipboard operation
numpy copied to clipboard

BUG: meshgrid() for 3 or 4 arrays of input is giving wrong results

Open R0ckySu opened this issue 1 year ago • 1 comments

Describe the issue:

meshgrid() works fine with just 2 arrays as input. When working with higher dimension(greater than 3) it fails to construct the meshgrid in the right order.

Reproduce the code example:

A = [1,2,3]
B = [311,422]
C = [0.11,0.22,0.33]
Am, Bm, Cm = np.meshgrid(A,B,C)

Error message:

Expect Bm to be:
array([[[311, 311, 311],
        [422, 422, 422]],

       [[311, 311, 311],
        [422, 422, 422]],

       [[311, 311, 311],
        [422, 422, 422]]])
But get:
array([[[311, 311, 311],
        [311, 311, 311],
        [311, 311, 311]],

       [[422, 422, 422],
        [422, 422, 422],
        [422, 422, 422]]])

Python and NumPy Versions:

1.26.4 3.9.19 | packaged by conda-forge | (main, Mar 20 2024, 12:38:46) [MSC v.1929 64 bit (AMD64)]

Runtime Environment:

[{'numpy_version': '1.26.4', 'python': '3.9.19 | packaged by conda-forge | (main, Mar 20 2024, 12:38:46) ' '[MSC v.1929 64 bit (AMD64)]', 'uname': uname_result(system='Windows', node='DESKTOP-5LVQAIH', release='10', version='10.0.22621', machine='AMD64')}, {'simd_extensions': {'baseline': ['SSE', 'SSE2', 'SSE3'], 'found': ['SSSE3', 'SSE41', 'POPCNT', 'SSE42', 'AVX', 'F16C', 'FMA3', 'AVX2', 'AVX512F', 'AVX512CD', 'AVX512_SKX', 'AVX512_CLX', 'AVX512_CNL', 'AVX512_ICL'], 'not_found': []}}, {'architecture': 'SkylakeX', 'filepath': 'C:\Users\LabUser ' 'OPX\AppData\Roaming\Python\Python39\site-packages\numpy.libs\libopenblas64__v0.3.23-293-gc2f4bdbb-gcc_10_3_0-2bde3a66a51006b2b53eb373ff767a3f.dll', 'internal_api': 'openblas', 'num_threads': 16, 'prefix': 'libopenblas', 'threading_layer': 'pthreads', 'user_api': 'blas', 'version': '0.3.23.dev'}, {'architecture': 'SkylakeX', 'filepath': 'C:\Users\LabUser ' 'OPX\AppData\Roaming\Python\Python39\site-packages\scipy.libs\libopenblas_v0.3.27--3aa239bc726cfb0bd8e5330d8d4c15c6.dll', 'internal_api': 'openblas', 'num_threads': 16, 'prefix': 'libopenblas', 'threading_layer': 'pthreads', 'user_api': 'blas', 'version': '0.3.27'}]

Context for the issue:

No response

R0ckySu avatar Jun 25 '24 04:06 R0ckySu

Bug confirmed. I've attached a reproducible notebook with the relevant code. I am working on this bug. link: https://colab.research.google.com/drive/1lBdxPRiU-mgf_Rx7vvZKNqnO8Fa_BdZ-?usp=sharing

Jaimin020 avatar Jun 25 '24 14:06 Jaimin020

This behavior is explained in the documentation notes. You probably want to use indexing="ij" which is more regular/obvious in its behavior. (Is that the better default? Maybe not, the history of it maybe comes from some image tooling where this default is probably more often what you want.)

seberg avatar Jul 11 '24 09:07 seberg

It still seems like a bug to me. Even if you use indexing='ij' you need to transpose the arrays to get what you would expect based on the results for using two arrays. The results using the default are completely unexpected.

jslavin avatar Nov 21 '24 19:11 jslavin