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
275
276
277
278
279
280
281
282
283
use crate*;
/// Expands the shape of an array by inserting a new axis (dimension) of size one at the position
/// specified by `axis`.
///
/// See also [`expand_dims`].
/// Expands the shape of an array by inserting a new axis (dimension) of size one at the position
/// specified by `axis`.
///
/// # Parameters
///
/// - `tensor`: [`&TensorAny<R, T, B, D>`](TensorAny)
///
/// - The input tensor.
/// - Note on variant [`into_expand_dims`]: 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>`]
///
/// - Position in the expanded axes where the new axis (or axes) is placed.
/// - Can be a single integer, or a list/tuple of integers.
/// - Negative values are supported and indicate counting dimensions from the back.
///
/// # Returns
///
/// - [`TensorView<'_, T, B, IxD>`](TensorView)
///
/// - A view of the input tensor with the new axis (or axes) inserted.
/// - If you want to convert the tensor itself (taking the ownership instead of returning view),
/// use [`into_expand_dims`] instead.
///
/// # Panics
///
/// - If `axis` is greater than the number of axes in the original tensor.
/// - If expanded axis has duplicated values.
///
/// For a fallible version, use [`expand_dims_f`].
///
/// # Examples
///
/// Expand dims at axis 0, which is equivalent to `x.i(None)`:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// let x = rt::arange((2, &device));
/// let y = x.expand_dims(0);
/// println!("{y}");
/// // [[ 0 1]]
/// println!("y shape: {:?}", y.shape());
/// // y shape: [1, 2]
/// assert_eq!(y.shape(), &[1, 2]);
/// assert_eq!(x.i(None).shape(), y.shape());
/// ```
///
/// Expand dims at axis -1 (last axis), which is equivalent to `x.i((Ellipsis, None))`:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// let x = rt::arange((2, &device));
/// let y = x.expand_dims(-1);
/// println!("{y}");
/// // [[ 0]
/// // [ 1]]
/// println!("y shape: {:?}", y.shape());
/// // y shape: [2, 1]
/// assert_eq!(y.shape(), &[2, 1]);
/// assert_eq!(x.i((Ellipsis, None)).shape(), &[2, 1]);
/// ```
///
/// Expand dims at axes 0 and 1, which is equivalent to `x.i((None, None))`:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// let x = rt::arange((2, &device));
/// let y = x.expand_dims([0, 1]);
/// println!("{y}");
/// // [[[ 0 1]]]
/// println!("y shape: {:?}", y.shape());
/// // y shape: [1, 1, 2]
/// assert_eq!(y.shape(), &[1, 1, 2]);
/// assert_eq!(x.i((None, None)).shape(), &[1, 1, 2]);
/// ```
///
/// Expand dims at axes 0 and 2, which is equivalent to `x.i((None, Ellipsis, None))`:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let mut device = DeviceCpu::default();
/// # device.set_default_order(RowMajor);
/// let x = rt::arange((2, &device));
/// let y = x.expand_dims([0, 2]);
/// println!("{y}");
/// // [[[ 0]]
/// // [[ 1]]]
/// println!("y shape: {:?}", y.shape());
/// // y shape: [1, 2, 1]
/// assert_eq!(y.shape(), &[1, 2, 1]);
/// assert_eq!(x.i((None, Ellipsis, None)).shape(), &[1, 2, 1]);
/// ```
///
/// # Notes of API accordance
///
/// - Array-API: `expand_dims(x, /, axis)` ([`expand_dims` in Array-API](https://data-apis.org/array-api/latest/API_specification/generated/array_api.expand_dims.html))
/// - NumPy: `expand_dims(a, axis)` ([`numpy.expand_dims`](https://numpy.org/doc/stable/reference/generated/numpy.expand_dims.html))
/// - RSTSR: `rt::expand_dims(tensor, axes)`
///
/// # See also
///
/// ## Related functions in RSTSR
///
/// - [`i`](TensorAny::i) or [`slice`](slice()): Basic indexing and slicing of tensors, without
/// modification of the underlying data.
/// - [`squeeze`]: Removes singleton dimensions (axes) from `x`.
///
/// ## Variants of this function
///
/// - [expand_dims] / [`expand_dims_f`]: Returning a view.
/// - [`into_expand_dims`] / [`into_expand_dims_f`]: Consuming version.
/// - [`unsqueeze`] / [`unsqueeze_f`]: Alias of [`expand_dims`] / [`expand_dims_f`].
/// - [`into_unsqueeze`] / [`into_unsqueeze_f`]: Alias of [`into_expand_dims`] /
/// [`into_expand_dims_f`].
///
/// - Associated methods on [`TensorAny`]:
///
/// - [`TensorAny::expand_dims`] / [`TensorAny::expand_dims_f`]
/// - [`TensorAny::into_expand_dims`] / [`TensorAny::into_expand_dims_f`]
/// - [`TensorAny::unsqueeze`] / [`TensorAny::unsqueeze_f`]
/// - [`TensorAny::into_unsqueeze`] / [`TensorAny::into_unsqueeze_f`]
/// Expands the shape of an array by inserting a new axis (dimension) of size one at the position
/// specified by `axis`.
///
/// See also [`expand_dims`].
/// Expands the shape of an array by inserting a new axis (dimension) of size one at the position
/// specified by `axis`.
///
/// See also [`expand_dims`].
pub use expand_dims as unsqueeze;
pub use expand_dims_f as unsqueeze_f;
pub use into_expand_dims as into_unsqueeze;
pub use into_expand_dims_f as into_unsqueeze_f;