Skip to content

repeat

Repeat each element of an array after themselves

Parameters:

Name Type Description Default
a SparseArray

Input sparse arrays

required
repeats int

The number of repetitions for each element. (Uneven repeats are not yet Implemented.)

required
axis int

The axis along which to repeat values. Returns a flattened sparse array if not specified.

None

Returns:

Name Type Description
out SparseArray

A sparse array which has the same shape as a, except along the given axis.

Source code in sparse/numba_backend/_common.py
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
def repeat(a, repeats, axis=None):
    """
    Repeat each element of an array after themselves

    Parameters
    ----------
    a : SparseArray
        Input sparse arrays
    repeats : int
        The number of repetitions for each element.
        (Uneven repeats are not yet Implemented.)
    axis : int, optional
        The axis along which to repeat values. Returns a flattened sparse array if not specified.

    Returns
    -------
    out : SparseArray
        A sparse array which has the same shape as a, except along the given axis.
    """
    if not isinstance(a, SparseArray):
        raise TypeError("`a` must be a SparseArray.")

    if not isinstance(repeats, int):
        raise ValueError("`repeats` must be an integer, uneven repeats are not yet Implemented.")
    axes = list(range(a.ndim))
    new_shape = list(a.shape)
    axis_is_none = False
    if axis is None:
        a = a.reshape(-1)
        axis = 0
        axis_is_none = True
    if axis < 0:
        axis = a.ndim + axis
    axes[a.ndim - 1], axes[axis] = axes[axis], axes[a.ndim - 1]
    new_shape[axis] *= repeats
    a = expand_dims(a, axis=axis + 1)
    shape_to_broadcast = a.shape[: axis + 1] + (a.shape[axis + 1] * repeats,) + a.shape[axis + 2 :]
    a = broadcast_to(a, shape_to_broadcast)
    if not axis_is_none:
        return a.reshape(new_shape)
    return a.reshape(new_shape).flatten()