MCPcopy Create free account
hub / github.com/numpy/numpy / roll

Function roll

numpy/core/numeric.py:1130–1233  ·  view source on GitHub ↗

Roll array elements along a given axis. Elements that roll beyond the last position are re-introduced at the first. Parameters ---------- a : array_like Input array. shift : int or tuple of ints The number of places by which elements are shifted. If a

(a, shift, axis=None)

Source from the content-addressed store, hash-verified

1128
1129@array_function_dispatch(_roll_dispatcher)
1130def roll(a, shift, axis=None):
1131 """
1132 Roll array elements along a given axis.
1133
1134 Elements that roll beyond the last position are re-introduced at
1135 the first.
1136
1137 Parameters
1138 ----------
1139 a : array_like
1140 Input array.
1141 shift : int or tuple of ints
1142 The number of places by which elements are shifted. If a tuple,
1143 then `axis` must be a tuple of the same size, and each of the
1144 given axes is shifted by the corresponding number. If an int
1145 while `axis` is a tuple of ints, then the same value is used for
1146 all given axes.
1147 axis : int or tuple of ints, optional
1148 Axis or axes along which elements are shifted. By default, the
1149 array is flattened before shifting, after which the original
1150 shape is restored.
1151
1152 Returns
1153 -------
1154 res : ndarray
1155 Output array, with the same shape as `a`.
1156
1157 See Also
1158 --------
1159 rollaxis : Roll the specified axis backwards, until it lies in a
1160 given position.
1161
1162 Notes
1163 -----
1164 .. versionadded:: 1.12.0
1165
1166 Supports rolling over multiple dimensions simultaneously.
1167
1168 Examples
1169 --------
1170 >>> x = np.arange(10)
1171 >>> np.roll(x, 2)
1172 array([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])
1173 >>> np.roll(x, -2)
1174 array([2, 3, 4, 5, 6, 7, 8, 9, 0, 1])
1175
1176 >>> x2 = np.reshape(x, (2, 5))
1177 >>> x2
1178 array([[0, 1, 2, 3, 4],
1179 [5, 6, 7, 8, 9]])
1180 >>> np.roll(x2, 1)
1181 array([[9, 0, 1, 2, 3],
1182 [4, 5, 6, 7, 8]])
1183 >>> np.roll(x2, -1)
1184 array([[1, 2, 3, 4, 5],
1185 [6, 7, 8, 9, 0]])
1186 >>> np.roll(x2, 1, axis=0)
1187 array([[5, 6, 7, 8, 9],

Callers 2

fftshiftFunction · 0.90
ifftshiftFunction · 0.90

Calls 5

asanyarrayFunction · 0.85
normalize_axis_tupleFunction · 0.85
reshapeMethod · 0.80
empty_likeFunction · 0.70
ravelMethod · 0.45

Tested by

no test coverage detected