{ } Raw JSON

bundles / scipy 1.18.0 / scipy / _external / array_api_extra / _delegation / expand_dims

function

scipy._external.array_api_extra._delegation:expand_dims

source: /scipy/_external/array_api_extra/_delegation.py:241

Signature

def expand_dims(a: Array, /, axis: int | tuple[int, ...] = (0,), xp: ModuleType | None = None) → Array

Summary

Expand the shape of an array.

Extended Summary

Insert (a) new axis/axes that will appear at the position(s) specified by axis in the expanded array shape.

This is xp.expand_dims for axis an int or a tuple of ints. Roughly equivalent to numpy.expand_dims for NumPy arrays.

Parameters

a : array

Array to have its shape expanded.

axis : int or tuple of ints, optional

Position(s) in the expanded axes where the new axis (or axes) is/are placed. If multiple positions are provided, they should be unique (note that a position given by a positive index could also be referred to by a negative index - that will also result in an error). Default: (0,).

xp : array_namespace, optional

The standard-compatible namespace for a. Default: infer.

Returns

: array

a with an expanded shape.

Examples

import array_api_strict as xp
import array_api_extra as xpx
x = xp.asarray([1, 2])
x.shape
⚠
Traceback (most recent call last):
  File "<doctest scipy._external.array_api_extra._delegation:expand_dims[0]>", line 1, in <module>
    x.shape
    ^
NameError: name 'x' is not defined
The following is equivalent to ``x[xp.newaxis, :]`` or ``x[xp.newaxis]``:
y = xpx.expand_dims(x, axis=0, xp=xp)
y
y.shape
⚠
Traceback (most recent call last):
  File "<doctest scipy._external.array_api_extra._delegation:expand_dims[0]>", line 1, in <module>
    y.shape
    ^
NameError: name 'y' is not defined
The following is equivalent to ``x[:, xp.newaxis]``:
y = xpx.expand_dims(x, axis=1, xp=xp)
y
y.shape
⚠
Traceback (most recent call last):
  File "<doctest scipy._external.array_api_extra._delegation:expand_dims[0]>", line 1, in <module>
    y.shape
    ^
NameError: name 'y' is not defined
``axis`` may also be a tuple:
y = xpx.expand_dims(x, axis=(0, 1), xp=xp)
y
⚠
Traceback (most recent call last):
  File "<doctest scipy._external.array_api_extra._delegation:expand_dims[0]>", line 1, in <module>
    y
NameError: name 'y' is not defined
y = xpx.expand_dims(x, axis=(2, 0), xp=xp)
y
⚠
Traceback (most recent call last):
  File "<doctest scipy._external.array_api_extra._delegation:expand_dims[0]>", line 1, in <module>
    y
NameError: name 'y' is not defined

Aliases

  • scipy.differentiate.xpx.expand_dims