1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
use crate*;
/// Reverses the order of elements in an array along the given axis.
///
/// See also [`flip`].
/// Reverses the order of elements in an array along the given axis.
///
/// The shape of the array will be preserved after flipping.
///
/// # Parameters
///
/// - `tensor`: [`&TensorAny<R, T, B, D>`](TensorAny)
///
/// - The input tensor.
/// - Note on variant [`into_flip`]: This takes ownership [`Tensor<R, T, B, D>`] of input tensor,
/// and will not perform change to underlying data, only layout changes.
///
/// - `axes`: TryInto [`AxesIndex<isize>`]
///
/// - Axis or axes along which to flip over.
/// - If `axes` is a single integer, flipping is performed along that axis.
/// - If `axes` is a tuple/list of integers, flipping is performed on all specified axes.
/// - If `axes` is `None`, the function will flip over all axes.
/// - If `axes` is an empty tuple `()`, no axes are flipped (returns a view of the original
/// tensor).
/// - Negative values are supported and indicate counting dimensions from the back.
///
/// # Returns
///
/// - [`TensorView<'_, T, B, D>`](TensorView)
///
/// - A view of the input tensor with the entries along the specified axes reversed.
/// - The shape of the array is preserved, but the elements are reordered.
/// - The underlying data is not copied; only the layout of the view is modified.
/// - If you want to convert the tensor itself (taking the ownership instead of returning view),
/// use [`into_flip`] instead.
///
/// # Examples
///
/// ## Flipping along a single axis
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// let a = rt::arange((8, &device)).into_shape([2, 2, 2]);
/// println!("{a}");
/// // [[[ 0 1]
/// // [ 2 3]]
/// //
/// // [[ 4 5]
/// // [ 6 7]]]
/// ```
///
/// Flipping the first (0) axis (which is equivalent to slicing with step -1 along that axis):
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// let a = rt::arange((8, &device)).into_shape([2, 2, 2]);
/// let b = a.flip(0);
/// println!("{b}");
/// // [[[ 4 5]
/// // [ 6 7]]
/// //
/// // [[ 0 1]
/// // [ 2 3]]]
/// assert!(rt::allclose(a.i(slice!(None, None, -1)), &b, None));
/// ```
///
/// Flipping the second (1) axis:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// let a = rt::arange((8, &device)).into_shape([2, 2, 2]);
/// let b = a.flip(1);
/// println!("{b}");
/// // [[[ 2 3]
/// // [ 0 1]]
/// //
/// // [[ 6 7]
/// // [ 4 5]]]
/// assert!(rt::allclose(a.i((.., slice!(None, None, -1))), &b, None));
/// ```
///
/// ## Flipping along multiple axes
///
/// Flipping the first (0) and last (-1 or in this specific case, 2) axes:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// let a = rt::arange((8, &device)).into_shape([2, 2, 2]);
/// let b = a.flip([0, -1]);
/// println!("{b}");
/// // [[[ 5 4]
/// // [ 7 6]]
/// //
/// // [[ 1 0]
/// // [ 3 2]]]
/// ```
///
/// ## Flipping all axes
///
/// You can specify `None` to flip all axes:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// let a = rt::arange((8, &device)).into_shape([2, 2, 2]);
/// let b = a.flip(None);
/// println!("{b}");
/// // [[[ 7 6]
/// // [ 5 4]]
/// //
/// // [[ 3 2]
/// // [ 1 0]]]
/// ```
///
/// ## No flipping (empty axes)
///
/// You can specify an empty tuple `()` to flip no axes (returns a view of the original tensor):
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// let a = rt::arange((8, &device)).into_shape([2, 2, 2]);
/// let b = a.flip(());
/// println!("{b}");
/// // [[[ 0 1]
/// // [ 2 3]]
/// //
/// // [[ 4 5]
/// // [ 6 7]]]
/// ```
///
/// # Panics
///
/// - If some index in `axes` is greater than the number of axes in the original tensor.
/// - If `axes` has duplicated values.
///
/// For a fallible version, use [`flip_f`].
///
/// # Notes of API accordance
///
/// - Array-API: `flip(x, /, *, axis=None)` ([`flip`](https://data-apis.org/array-api/latest/API_specification/generated/array_api.flip.html))
/// - NumPy: `flip(m, axis=None)` ([`numpy.flip`](https://numpy.org/doc/stable/reference/generated/numpy.flip.html))
/// - RSTSR: `rt::flip(tensor, axes)`
///
/// RSTSR's behavior matches NumPy and Array-API:
/// - `a.flip(None)` flips all axes
/// - `a.flip(())` flips no axes (returns a view of the original tensor)
///
/// # See also
///
/// ## Related functions in RSTSR
///
/// - [`i`](TensorAny::i) or [`slice`](slice()): Basic indexing and slicing of tensors, without
/// modification of the underlying data.
///
/// ## Variants of this function
///
/// - [`flip`]: Borrowing version.
/// - [`flip_f`]: Fallible version.
/// - [`into_flip`]: Consuming version.
/// - [`into_flip_f`]: Consuming and fallible version, actual implementation.
/// - Associated methods on [`TensorAny`]:
///
/// - [`TensorAny::flip`]
/// - [`TensorAny::flip_f`]
/// - [`TensorAny::into_flip`]
/// - [`TensorAny::into_flip_f`]
/// Reverses the order of elements in an array along the given axis.
///
/// See also [`flip`].
/// Reverses the order of elements in an array along the given axis.
///
/// See also [`flip`].