Skip to content

expand

expand(array: array, new_shape: tuple) -> array

Expand the dimensions of an array to a new shape.

Parameters:

Name Type Description Default
array array

The array to expand.

required
new_shape tuple

The new shape desired. The new dimensions must be compatible with the original shape of the array.

required

Returns:

Type Description
array

A view of the array with the new shape.

Source code in mlx_graphs/utils/array_ops.py
def expand(array: mx.array, new_shape: tuple) -> mx.array:
    """
    Expand the dimensions of an array to a new shape.

    Args:
        array: The array to expand.
        new_shape: The new shape desired. The new dimensions must be compatible
            with the original shape of the array.

    Returns:
        A view of the array with the new shape.
    """
    orig_shape = array.shape

    if not all(new_dim >= orig_dim for new_dim, orig_dim in zip(new_shape, orig_shape)):
        raise ValueError("New shape must be greater than or equal to original shape")

    broadcast_shape = tuple(
        max(orig_dim, new_dim) for orig_dim, new_dim in zip(orig_shape, new_shape)
    )
    return mx.broadcast_to(array, broadcast_shape)

from mlx_graphs.utils.array_ops import expand · mlx_graphs/utils/array_ops.py