Skip to content

BUG: Any dtype should call square on arr ** 2 #29392

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

Merged
merged 5 commits into from
Jul 22, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
BUG: replace new variable is_square with direct op comparison in `f…
…ast_scalar_power` function
  • Loading branch information
MaanasArora committed Jul 17, 2025
commit ddaef11f219990372f1c10bca2d3d3f0bb57a2e9
4 changes: 1 addition & 3 deletions numpy/_core/src/multiarray/number.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ static int
fast_scalar_power(PyObject *o1, PyObject *o2, int inplace, PyObject **result)
{
PyObject *fastop = NULL;
int is_square = 0;

if (PyLong_CheckExact(o2)) {
int overflow = 0;
Expand All @@ -346,7 +345,6 @@ fast_scalar_power(PyObject *o1, PyObject *o2, int inplace, PyObject **result)
}
else if (exp == 2) {
fastop = n_ops.square;
is_square = 1;
}
else {
return 1;
Expand All @@ -369,7 +367,7 @@ fast_scalar_power(PyObject *o1, PyObject *o2, int inplace, PyObject **result)
if (PyArray_ISOBJECT(a1)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, yeah this seems right/safe to me. I am not sure I see why **2 confuses a custom __array_ufunc__, but I don't think this is an issue (and probably restores old behavior). (If we had a bigger reason to keep it as is, I would be tempted to ask downstream to fix it there.)

Curious, does **0.5 always end up with sqrt()?


(Maybe the nicer solution for at least **2 would actually be to make the object square implementation use **2 internally then this special case wouldn't even be needed. But no priority so if, let's do that as a follow-up.)

return 1;
}
if (!is_square && !PyArray_ISFLOAT(a1) && !PyArray_ISCOMPLEX(a1)) {
if (fastop != n_ops.square && !PyArray_ISFLOAT(a1) && !PyArray_ISCOMPLEX(a1)) {
// we special-case squaring for any array type
// gh-29388
return 1;
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy