You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ENH: Add a "broadcast" option to numpy.concatenate #28549
Often, I've wanted to concatenate arrays with different ndims along a particular axis, broadcasting the other axes as needed. Others have sought this functionality as well:
ValueError('all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)')
It would be convenient to add to numpy.concatenate an optional boolean argument called broadcast that broadcasts the input arrays along the axes that are not the concatenation axis, before concatenating them. Its default value can be False, which is the current behavior.
I like this idea. Note that this would have to be done in C, and a bit of thought is needed on how/whether the argument should also be available for functions that use concatenate, like *stack, block, etc. (and possibly unstack should have an "unbroadcast" argument).
I don't mind it, although I don't love that the concate signature would get very long, which does make a broadcast_arrays approach also appealing (but less obvious, but I am not sure that this type of concatenation is common?).
The fact that concatenate is in C doesn't change: the concatenate code should already support it, you just have to skip raising that specific error.
Also note that axis should always be negative so it counts from the back.
Yeah good point, you could allow positive, but only if all dimensions match (not sure that is useful).
Came here from a recent discussion in the Array API community meeting, data-apis/array-api#946
The original motivation of the array API discussion was a desire to replace uses of np._r: np.r_[0, x] and np.r_[x[0], x, x[-1]].
These constructs appear some ~150 times in SciPy, and I believe the vast majority of cases have x a 1D array, which gets extended by a scalar or two.
Currently, replacing these kinds of things requires an explicit atleast_1d, which is both ugly and easy to miss (we broke the Jax CI at least once).
From this POV, having concat handle scalars and 0D arrays would be a very nice change.
Signature-wise, doesconcat really need an extra keyword? It already has all the information to convert scalars into arrays of the proper shape--- this would of course complicate the implementation, as it'll need to make a separate pass across its arguments to figure out the shape of the result. And it'll make it slower but concat is unlikely to be a performance bottleneck anywhere, I'd think.
API-wise, a minimal great change would be if concat required its arguments to be either arrays of the same shape or scalars or 0D arrays (which are internally broadcast to the array shape).
As a more expansive change, it could follow the np.diff-like API, where append and prepend need to be the same (or broadcastable?) in all dimensions apart from the concatenation axis.
That would make the result shape data-dependent though.
And FWIW, it would be perfectly fine if this is confined to np.concat / np.concatenate only, while stack functions continue to require exactly matching shapes.
Activity
mhvk commentedon Mar 18, 2025
I like this idea. Note that this would have to be done in C, and a bit of thought is needed on how/whether the argument should also be available for functions that use
concatenate, like*stack,block, etc. (and possiblyunstackshould have an "unbroadcast" argument).mhvk commentedon Mar 18, 2025
Note: an alternative would be to add an
exclude_axisargument tobroadcast_arrays.Also note that
axisshould always be negative so it counts from the back.carlosgmartin commentedon Mar 18, 2025
@mhvk Good point. That would be more general.
We could also allow multiple axes to be excluded.
seberg commentedon Mar 18, 2025
I don't mind it, although I don't love that the
concatesignature would get very long, which does make abroadcast_arraysapproach also appealing (but less obvious, but I am not sure that this type of concatenation is common?).The fact that concatenate is in C doesn't change: the concatenate code should already support it, you just have to skip raising that specific error.
Yeah good point, you could allow positive, but only if all dimensions match (not sure that is useful).
ev-br commentedon May 31, 2025
Came here from a recent discussion in the Array API community meeting, data-apis/array-api#946
The original motivation of the array API discussion was a desire to replace uses of
np._r:np.r_[0, x]andnp.r_[x[0], x, x[-1]].These constructs appear some ~150 times in SciPy, and I believe the vast majority of cases have
xa 1D array, which gets extended by a scalar or two.Currently, replacing these kinds of things requires an explicit
atleast_1d, which is both ugly and easy to miss (we broke the Jax CI at least once).From this POV, having
concathandle scalars and 0D arrays would be a very nice change.Signature-wise, does
concatreally need an extra keyword? It already has all the information to convert scalars into arrays of the proper shape--- this would of course complicate the implementation, as it'll need to make a separate pass across its arguments to figure out the shape of the result. And it'll make it slower butconcatis unlikely to be a performance bottleneck anywhere, I'd think.API-wise, a minimal great change would be if
concatrequired its arguments to be either arrays of the same shape or scalars or 0D arrays (which are internally broadcast to the array shape).As a more expansive change, it could follow the
np.diff-like API, whereappendandprependneed to be the same (or broadcastable?) in all dimensions apart from the concatenation axis.That would make the result shape data-dependent though.
And FWIW, it would be perfectly fine if this is confined to
np.concat / np.concatenateonly, whilestackfunctions continue to require exactly matching shapes.