Skip to content

BUG: Assign scalars into new arrays with subarray dims fails for higher dimensions #23083

@eriknw

Description

@eriknw

Describe the issue:

Scalar values don't broadcast correctly when creating an array with a sub-array dtype.

For example, this works as expected:

>>> import numpy as np
>>> np.__version__
'1.24.1'
>>> np.array([0, 1, 2], dtype=('<i8', (3,)))
array([[0, 0, 0],
       [1, 1, 1],
       [2, 2, 2]])

But these do not:

>>> np.array([[0, 1, 2]], dtype=('<i8', (3,)))
array([[[                   0,                    0,
                            0],
        [                   1,  4672486039879745776,
         -1080498730957541302],
        [                   2,  5549843219359026209,
                   4311806977]]])
>>> np.array([[0], [1], [2]], dtype=('<i8', (3,)))
array([[[                   0,                    0,
                            0]],

       [[                   1,  4672486039879745776,
         -1080498730957541302]],

       [[                   2,  5549843219359026209,
                   4311806977]]])

Reproduce the code example:

import numpy as np

a = np.array([[0, 1, 2]], dtype=('<i8', (3,)))
expected = np.array([0, 1, 2], dtype=('<i8', (3,)))[np.newaxis, :, :]
np.testing.assert_array_equal(a, expected)

b = np.array([[0], [1], [2]], dtype=('<i8', (3,)))
expected = np.array([0, 1, 2], dtype=('<i8', (3,)))[:, np.newaxis, :]
np.testing.assert_array_equal(a, expected)

Error message:

>>> np.testing.assert_array_equal(a, expected)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/erik/miniconda3/envs/np/lib/python3.11/site-packages/numpy/testing/_private/utils.py", line 983, in assert_array_equal
    assert_array_compare(operator.__eq__, x, y, err_msg=err_msg,
  File "/home/erik/miniconda3/envs/np/lib/python3.11/contextlib.py", line 81, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/erik/miniconda3/envs/np/lib/python3.11/site-packages/numpy/testing/_private/utils.py", line 862, in assert_array_compare
    raise AssertionError(msg)
AssertionError:
Arrays are not equal

Mismatched elements: 6 / 9 (66.7%)
Max absolute difference: 4905783429429202131
Max relative difference: 4.90578343e+18
 x: array([[[                  0,     140460087864352,    5330496813138160],
        [                  1, 4905783429429202132,   58565490347611344],
        [                  2,                   0,                  80]]])
 y: array([[[0, 0, 0],
        [1, 1, 1],
        [2, 2, 2]]])

Runtime information:

>>> import sys, numpy; print(numpy.__version__); print(sys.version)
1.24.1
3.11.0 | packaged by conda-forge | (main, Jan 14 2023, 12:27:40) [GCC 11.3.0]
>>> print(numpy.show_runtime())
WARNING: `threadpoolctl` not found in system! Install it by `pip install threadpoolctl`. Once installed, try `np.show_runtime` again for more detailed build information
[{'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': ['AVX512_KNL', 'AVX512_KNM']}}]
None

Context for the issue:

It's an interesting edgecase🙃

I expect sub-array dtypes to be important "user-defined types" for some users of python-graphblas, and we defer to numpy when creating arrays of these types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions