array_api_extra.atleast_nd

array_api_extra.atleast_nd(x, /, *, ndim, xp)

Recursively expand the dimension of an array to at least ndim.

Parameters:
  • x (array)

  • ndim (int) – The minimum number of dimensions for the result.

  • xp (array_namespace) – The standard-compatible namespace for x.

Returns:

res – An array with res.ndim >= ndim. If x.ndim >= ndim, x is returned. If x.ndim < ndim, x is expanded by prepending new axes until res.ndim equals ndim.

Return type:

array

Examples

>>> import array_api_strict as xp
>>> import array_api_extra as xpx
>>> x = xp.asarray([1])
>>> xpx.atleast_nd(x, ndim=3, xp=xp)
Array([[[1]]], dtype=array_api_strict.int64)
>>> x = xp.asarray([[[1, 2],
...                  [3, 4]]])
>>> xpx.atleast_nd(x, ndim=1, xp=xp) is x
True