complete_dims

Contents

complete_dims#

complete_dims(arr, xdims, squeeze=True)[source]#

Complete the dimensions of an array with trailing length-1 axes.

  • If xdims >= 0, add xdims dimensions, the final shape is arr.shape + (1,) * xdims;

  • If xdims < 0, add as many dimensions as necessary to reach |xdims| dimensions at least, the final shape is arr.shape + (1,) * (|xdims| - arr.ndim).

Parameters:
  • arr (array_like) – Input array.

  • xdims (int) – Number of extra dimensions to append (if positive), or minimal number of dimensions of the output (if negative).

  • squeeze (bool, optional) – Remove the leading length-1 dimensions (up to the first non-trivial one). Default is True.

Returns:

numpy.ndarray – Expanded array.

Examples

>>> complete_dims(1, 2, False).shape  # () + (1,)*2
(1, 1)
>>> complete_dims(1, 2).shape
()
>>> complete_dims([[0, 1, 2]], 4, False).shape  # (1, 3) + (1,)*4
(1, 3, 1, 1, 1, 1)
>>> complete_dims([[0, 1, 2]], 4).shape
(3, 1, 1, 1, 1)
>>> complete_dims([[0, 1, 2]], -4, False).shape  # (1, 3) + (1,)*(4 - 2)
(1, 3, 1, 1)
>>> complete_dims([[0, 1, 2]], -4).shape
(3, 1, 1)