Skip to content

RFC: allow scalars and 0D arrays in concat #946

Open
@ev-br

Description

@ev-br

The following patterns are quite common [1]: x = np.r_(x[0], x, x[-1]) and x = np.r_[0, x]. Neither of these can be directly replaced by xp.concat because the latter requires that The arrays must have the same shape, except in the dimension specified by axis.
The most common case IME is that x is a 1D array, which gets appended or prepended by a scalar.

An Array API replacement is something along the lines of

def npr(xp, *arys):
    arys = [xp.asarray(a) for a in arys]
    arys = [xpx.atleast_nd(a, ndim=1, xp=xp) for a in arys]
    return xp.concat(arys)

which requires array_api_extra and is generally a bit clunky. There was at least one case where a scipy change which was missing atleast_1d broke jax.scipy.

Allowing 0D arrays and python scalars in concat would obviate the need for these sorts of helpers.

[1] At least in scipy,

$ git grep "np.r_"  |wc -l
169

Metadata

Metadata

Assignees

No one assigned

    Labels

    API changeChanges to existing functions or objects in the API.RFCRequest for comments. Feature requests and proposed changes.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions