Skip to content

PoC: Try 0d preservation#52

Draft
seberg wants to merge 8 commits intomainfrom
try-0d-preservation-rebase
Draft

PoC: Try 0d preservation#52
seberg wants to merge 8 commits intomainfrom
try-0d-preservation-rebase

Conversation

@seberg
Copy link
Owner

@seberg seberg commented May 12, 2025

Implements an older PoC to try preserving scalars in NumPy. It doesn't pass all tests, but it passes most (the test failures remaining are mostly bug-fixes around object arrays, but...).

This is in response to scientific-python/summit-2025#38. Since the change to NumPy (for the first part here!) isn't the blocker for a while now IMO, the blocker is to properly assess downstream impact.
(That was of course not always the case, there are many fixes in the past 2 years that enabled this.)


The simple rule is, if any input is already an array, the output should also be an array.
The one extension is that axis=None for reductions should return scalars. Some may argue that last rule, but None is special enough, and it doesn't even matter if you like it or not, because the main point is that arr.sum() won't change behavior.
(Gufuncs that remove axes will effectively always return arrays here, which may not always be desired.)


It also adds a last commit to enable NUMPY_DISLIKE_SCALARS=1, if set, NumPy will never return scalars (this is probably even true for a lot of scalar math, so that is a problem).
To be clear, while I am happy to agree that anything makes sense to try. I don't believe in this being feasible.

Since that doesn't work at all, it adds an arr.to_scalar() method, that does the same as arr[()] did previously.

seberg added 8 commits May 11, 2025 15:51
Signed-off-by: Sebastian Berg <sebastianb@nvidia.com>
Many remaining ones are around object arrays returning arguably
better (less "NumPy") results, but probably not all :)
I don't believe in this (I mean as an option, I suppose who cares,
but that isn't actually useful if you can never transition, IMO).

But, it should be at least tried a bit, so...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant