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*;
/// Removes singleton dimensions (axes) from a tensor.
///
/// See also [`squeeze`].
/// Removes singleton dimensions (axes) from `x`.
///
/// See also [`squeeze`].
/// Removes singleton dimensions (axes) from a tensor.
///
/// # Parameters
///
/// - `tensor`: [`&TensorAny<R, T, B, D>`](TensorAny)
///
/// - The input tensor.
/// - Note on variant [`into_squeeze`]: 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>`]
///
/// - The axis (or axes) to squeeze.
/// - If `axes` is a single integer, squeezing is performed along that axis.
/// - If `axes` is a tuple/list of integers, squeezing is performed on all specified axes.
/// - If `axes` is `None`, the function will squeeze all axes with size 1.
/// - If `axes` is an empty tuple `()`, no axes are squeezed.
/// - Negative values are supported and indicate counting dimensions from the back.
/// - Each axis in `axes` must have size 1; otherwise an error is raised.
///
/// # Returns
///
/// - [`TensorView<'_, T, B, IxD>`](TensorView)
///
/// - A view of the input tensor with the specified singleton dimensions removed.
/// - 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_squeeze`] instead.
///
/// # Panics
///
/// - If an axis specified does not have size 1.
/// - If an axis is out of bounds.
/// - If `axes` has duplicated values.
///
/// # Examples
///
/// ## Squeezing a single axis
///
/// Squeeze a tensor along axis 0:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// let a: Tensor<f64, _> = rt::zeros(([1, 3, 1, 4], &device));
/// let b = a.squeeze(0);
/// assert_eq!(b.shape(), &[3, 1, 4]);
/// ```
///
/// Squeeze a tensor along the axis 2 (third axis with size 1):
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// # let a: Tensor<f64, _> = rt::zeros(([1, 3, 1, 4], &device));
/// let b = a.squeeze(2);
/// assert_eq!(b.shape(), &[1, 3, 4]);
/// ```
///
/// Squeeze using negative index (-2 refers to the axis with size 1):
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// # let a: Tensor<f64, _> = rt::zeros(([1, 3, 1, 4], &device));
/// let b = a.squeeze(-2);
/// assert_eq!(b.shape(), &[1, 3, 4]);
/// ```
///
/// ## Squeezing multiple axes
///
/// Squeeze multiple axes at once:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// # let a: Tensor<f64, _> = rt::zeros(([1, 3, 1, 4, 1], &device));
/// let b = a.squeeze([0, 2]);
/// assert_eq!(b.shape(), &[3, 4, 1]);
/// ```
///
/// Use negative indices to squeeze from the back:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// # let a: Tensor<f64, _> = rt::zeros(([1, 3, 1, 4, 1], &device));
/// let b = a.squeeze([0, -1]);
/// assert_eq!(b.shape(), &[3, 1, 4]);
/// ```
///
/// ## Squeezing all singleton axes
///
/// Use `None` to squeeze all axes with size 1:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// let a: Tensor<f64, _> = rt::zeros(([1, 3, 1, 4, 1], &device));
/// let b = a.squeeze(None);
/// assert_eq!(b.shape(), &[3, 4]);
/// ```
///
/// ## No squeezing (empty axes)
///
/// Use an empty tuple `()` to squeeze 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: Tensor<f64, _> = rt::zeros(([1, 3, 1, 4, 1], &device));
/// let b = a.squeeze(());
/// assert_eq!(b.shape(), &[1, 3, 1, 4, 1]);
/// ```
///
/// # Notes of API accordance
///
/// - Array-API: `squeeze(x, /, axis)` ([`squeeze`](https://data-apis.org/array-api/latest/API_specification/generated/array_api.squeeze.html))
/// - NumPy: `squeeze(a, axis=None)` ([`numpy.squeeze`](https://numpy.org/doc/stable/reference/generated/numpy.squeeze.html))
/// - RSTSR: `rt::squeeze(tensor, axes)`
///
/// RSTSR's behavior matches NumPy and Array-API:
/// - `a.squeeze(None)` squeezes all axes with size 1
/// - `a.squeeze(())` squeezes no axes (returns a view of the original tensor)
///
/// # See also
///
/// ## Related functions in RSTSR
///
/// - [`expand_dims`]: Adds singleton dimensions (axes) to a tensor.
///
/// ## Variants of this function
///
/// - [`squeeze`] / [`squeeze_f`]: Returning a view.
/// - [`into_squeeze`] / [`into_squeeze_f`]: Consuming version.
/// - Associated methods on [`TensorAny`]:
///
/// - [`TensorAny::squeeze`] / [`TensorAny::squeeze_f`]
/// - [`TensorAny::into_squeeze`] / [`TensorAny::into_squeeze_f`]
/// Removes singleton dimensions (axes) from a tensor.
///
/// See also [`squeeze`].