Skip to content

broadcast

broadcast(src: array, other: array, dim: int) -> array

Make the shape broadcastable between arrays src and other. May be required in some situations like index broadcasting.

Parameters:

Name Type Description Default
src array

source array to broadcast.

required
other array

other array to match the shape.

required

Returns:

Type Description
array

Array with new broadcastable shape.

Source code in mlx_graphs/utils/array_ops.py
def broadcast(src: mx.array, other: mx.array, dim: int) -> mx.array:
    """
    Make the shape broadcastable between arrays src and other.
    May be required in some situations like index broadcasting.

    Args:
        src: source array to broadcast.
        other: other array to match the shape.

    Returns:
        Array with new broadcastable shape.
    """
    if dim < 0:
        dim = other.ndim + dim
    if src.ndim == 1:
        for _ in range(0, dim):
            src = mx.expand_dims(src, 0)
    for _ in range(src.ndim, other.ndim):
        src = mx.expand_dims(src, -1)
    src = expand(src, other.shape)
    return src

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