-
-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Closed
Closed
Copy link
Labels
Description
Describe the issue:
__array_ufunc__
catches array ** 2
as power
and not square
for structured arrays. The following works well -
import numpy
class DummySubclass(numpy.ndarray):
def __array_ufunc__(
self,
ufunc,
method,
*inputs,
**kwargs,
):
print(ufunc)
if ufunc is numpy.power:
print("numpy.power called")
elif ufunc is numpy.square:
print("numpy.square called")
d = DummySubclass(shape=(1,1), dtype=float)
d**2
# output:
# <ufunc 'square'>
# numpy.square called
but changing dtype
to that of a structured array (see below) sets ufunc
as power
. The behavior for structured and non-structures arrays was identical before NumPy 2.3.0
.
Reproduce the code example:
import numpy
class DummySubclass(numpy.ndarray):
def __array_ufunc__(
self,
ufunc,
method,
*inputs,
**kwargs,
):
print(ufunc)
if ufunc is numpy.power:
print("numpy.power called")
elif ufunc is numpy.square:
print("numpy.square called")
d = DummySubclass(shape=(1,1), dtype=[('x', '<f8'), ('y', '<f8'), ('z', '<f8'), ('t', '<f8')])
d**2
Error message:
<ufunc 'power'>
numpy.power called
Python and NumPy Versions:
2.3.1 (can confirm that the bug was introduced in 2.3.0)
3.13.5 (main, Jun 11 2025, 15:36:57) [Clang 17.0.0 (clang-1700.0.13.3)]
Runtime Environment:
[{'numpy_version': '2.3.1',
'python': '3.13.5 (main, Jun 11 2025, 15:36:57) [Clang 17.0.0 '
'(clang-1700.0.13.3)]',
'uname': uname_result(system='Darwin', node='Saranshs-MacBook-Pro-3.local', release='24.5.0', version='Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:29 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6030', machine='arm64')},
{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP'],
'not_found': ['ASIMDFHM']}}]
Context for the issue:
This is a silent bug which is messing up a lot of calculations in our work (and I am assuming that it is doing the exact same in other codebases without people actually noticing it).
I will be more than happy to fix it in NumPy if someone could point me to the right direction. Thanks!