ENH: Introduce multiple pair parameters in the 'repeat' function#23937
ENH: Introduce multiple pair parameters in the 'repeat' function#23937pedro-sl wants to merge 5 commits intonumpy:mainfrom
Conversation
Co-authored-by: Paulo Almeida <[email protected]>
|
This seems to me to be better as a separate function that will use Either way, this is the kind of expansion of the API that needs to be discussed on the mailing list first. |
|
Expanding a bit on @rkern's comment: There is some discussion of adding |
|
Thank you for your input! There is no problem in creating a new separate function - 'repeats' - that uses 'repeat' and preserves all the new functionalities that were implemented. |

This feature enables pairs of parameters to be passed to the NumPy's repeat function: tuple arguments can now be passed to the 'repeats' parameter and the ‘axis’ parameter can now also receive a sequence of integers. Data types are properly checked. Flattened output arrays (non-specified axes) and repeats broadcasted to the size of the paired axis are also taken into account. For instance, if two pairs of arguments are used and the second one doesn't have an axis specified, a flat output array is returned. This feature was first suggested in #21435.
Moreover, the multiple repeats are processed in ascending order, meaning the repeats that result in a smaller size of its axis in the intermediate output array are processed first. This adjustment renders a processing time reduction of approximately 50% in significantly large repeats (i.e. over 100 repeats per axis).
This enhancement makes the repeat function more versatile and elegant. The greater the number of dimensions of the input array to be repeated over a axis, the more useful this feature is.
Usage example:
>>> x = np.array([[1,2],[3,4]])>>> x = np.repeat(x, ([3, 3], [1, 2]), (1, 0))array([[1, 1, 1, 2, 2, 2],
[3, 3, 3, 4, 4, 4],
[3, 3, 3, 4, 4, 4]])
>>> x = np.repeat(x, (3, [1, 2], 1), (1, 0))
array([1, 1, 1, 2, 2, 2,
3, 3, 3, 4, 4, 4,
3, 3, 3, 4, 4, 4])