Skip to content

ENH: Create helper for conversion to arrays - #24214

Merged
ngoldbaum merged 4 commits into
numpy:mainfrom
seberg:creation-helper
Jan 22, 2024
Merged

ngoldbaum merged 4 commits into
numpy:mainfrom
seberg:creation-helper

Conversation

@seberg

@seberg seberg commented Jul 19, 2023

Copy link
Copy Markdown
Member

Had to give this a start to be able to experiment. It does solve at least some problems (e.g. np.select() use-case and it looks like it would fix and/or simplify linalg code).

The main thing is that we often convert multiple arrays, and then want something like:

  • a, b, c = np.asarray(a), np.asarray(b), np.asarray(c) and then dtype = np.result_type(a, b, c) often with an additional 0.0. That is a pretty common pattern, NEP 50 makes it tricky if you wish to support weak scalars, because you need to call result_type with the scalar information intact (before the asarray or manually undo). (For example, this happens in np.select())
    • Maybe libraries can disregard that, but for NumPy it's hard, since you want many functions to be ufunc-like, so if ufuncs do it, others probably should be able to/try to.
  • Maybe mainly internally we sometimes need the __array_wrap__ for subclass support, although it seems consistently used for single input functions (outside ufuncs).
    • Additional motif: A clean wrapping pattern seems required for making scalar logic sane.
  • It may be useful to know if an input was a scalar or not (not a 0-D array). You need to ask NumPy about it, but it discovered at conversion time. So it is an information that is weird to tag onto an array (probably?) but figuring it out later would require duplicating work.

First, I wanted to just make a single function. But with multiple things above that may be interesting to return, and e.g. result_type() might error and I am not sure we always need it.

So, is it insane to create a rich object (rather than a complicated return value and many kwargs):

# better names of course, currently in `np.core._multiarray_umath.ArrayCreationHelper`
arrays = creation_helper(*inputs)

# e.g. a linspace could do something like this:
dtype = arrays.common_dtype(ensure_inexact=True)

arrays.arrays  # arrays as tuple (probably allow arrays[0])

# Apply the typical wrapping if needed
# (should cache wrap, or fetch it early on rather, maybe just
# make it a method which applies the wrapping `arrays.wrap(result)`).
if arrays.wrap is not None:
    return arrays.wrap(result)
return result 

(I can see a, b, info = helper(a, b) as a nice pattern; OTOH it makes including casting kwarg heavy since you somewhat need to include it in the first step.)

Further things might include:

  • Casting to a given or the common dtype. (maybe arrays.to_common_dtype(ensure_inexact=True))
  • I think @mdhaber was hoping to include broadcasting. Not sure, since it is one call away, but also seems fine to add.

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants