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)
| 1224 | |
| 1225 | @array_function_dispatch(_roll_dispatcher) |
| 1226 | def roll(a, shift, axis=None): |
| 1227 | """ |
| 1228 | Roll array elements along a given axis. |
| 1229 | |
| 1230 | Elements that roll beyond the last position are re-introduced at |
| 1231 | the first. |
| 1232 | |
| 1233 | Parameters |
| 1234 | ---------- |
| 1235 | a : array_like |
| 1236 | Input array. |
| 1237 | shift : int or tuple of ints |
| 1238 | The number of places by which elements are shifted. If a tuple, |
| 1239 | then `axis` must be a tuple of the same size, and each of the |
| 1240 | given axes is shifted by the corresponding number. If an int |
| 1241 | while `axis` is a tuple of ints, then the same value is used for |
| 1242 | all given axes. |
| 1243 | axis : int or tuple of ints, optional |
| 1244 | Axis or axes along which elements are shifted. By default, the |
| 1245 | array is flattened before shifting, after which the original |
| 1246 | shape is restored. |
| 1247 | |
| 1248 | Returns |
| 1249 | ------- |
| 1250 | res : ndarray |
| 1251 | Output array, with the same shape as `a`. |
| 1252 | |
| 1253 | See Also |
| 1254 | -------- |
| 1255 | rollaxis : Roll the specified axis backwards, until it lies in a |
| 1256 | given position. |
| 1257 | |
| 1258 | Notes |
| 1259 | ----- |
| 1260 | Supports rolling over multiple dimensions simultaneously. |
| 1261 | |
| 1262 | Examples |
| 1263 | -------- |
| 1264 | >>> import numpy as np |
| 1265 | >>> x = np.arange(10) |
| 1266 | >>> np.roll(x, 2) |
| 1267 | array([8, 9, 0, 1, 2, 3, 4, 5, 6, 7]) |
| 1268 | >>> np.roll(x, -2) |
| 1269 | array([2, 3, 4, 5, 6, 7, 8, 9, 0, 1]) |
| 1270 | |
| 1271 | >>> x2 = np.reshape(x, (2, 5)) |
| 1272 | >>> x2 |
| 1273 | array([[0, 1, 2, 3, 4], |
| 1274 | [5, 6, 7, 8, 9]]) |
| 1275 | >>> np.roll(x2, 1) |
| 1276 | array([[9, 0, 1, 2, 3], |
| 1277 | [4, 5, 6, 7, 8]]) |
| 1278 | >>> np.roll(x2, -1) |
| 1279 | array([[1, 2, 3, 4, 5], |
| 1280 | [6, 7, 8, 9, 0]]) |
| 1281 | >>> np.roll(x2, 1, axis=0) |
| 1282 | array([[5, 6, 7, 8, 9], |
| 1283 | [0, 1, 2, 3, 4]]) |
no test coverage detected
searching dependent graphs…