data-apis / array-api-compat

Compatibility layer for common array libraries to support the Array API
https://data-apis.org/array-api-compat/
MIT License
69 stars 22 forks source link

Allow Python scalars in array_namespace #147

Open asmeurer opened 2 months ago

asmeurer commented 2 months ago

They are just ignored. This makes array_namespace easier to use for functions that accept either arrays or scalars.

I'm not sure if I should have this behavior by default, or if it should be enabled by a flag.

asmeurer commented 2 months ago

We can also automatically ignore None inputs. The scipy helper does this so it would clearly be useful.

lucascolley commented 2 months ago

I would say this should be opt-in, given the standard really focusses on just arrays. In practice, it will probably be used more often than not (at least for now), but given that (for now) most projects will wrap array_namespace, it isn't a problem to have the extra argument.

In SciPy, it doesn't really matter either way as we coerce scalars to NumPy arrays before passing them to array_namespace.

asmeurer commented 2 months ago

There are some active discussions on allowing scalars in more places in the array API. See https://github.com/data-apis/array-api/issues/805 and https://github.com/data-apis/array-api/issues/807.

but given that (for now) most projects will wrap array_namespace, it isn't a problem to have the extra argument.

I'm trying to make that not the case. Some projects like SciPy will have specific reasons they'll need to wrap things, but ideally whatever most projects need should be implemented already in the wrappers here.

lucascolley commented 2 months ago

I think another form of wrapping that will be very common is using a default backend when all arguments of array_namespace are Python scalars.

asmeurer commented 2 months ago

That would be easy to add. I would caution against it though unless you are doing it for backwards compatibility. If you default to NumPy, for instance, you could end up in a situation where a user gets a NumPy array from one function because they passed in scalars, and another array from another function because they passed in arrays.

lucascolley commented 2 months ago

Yeah, I'm thinking that the reason would be to maintain BC