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
use crate*;
/// Convert layout to the other dimension.
///
/// See also [`to_dim`].
/// Convert layout to the other dimension.
///
/// This function converts the dimensionality of a tensor's layout to a target dimension type.
/// No data is copied; only the layout representation is modified.
///
/// # Parameters
///
/// - `tensor`: [`&TensorAny<R, T, B, D>`](TensorAny)
///
/// - The input tensor.
///
/// - generic parameter `D2`: [`DimAPI`]
///
/// - Target dimension type.
/// - [`IxD`] or [`IxDyn`] (equilvalent to [`Vec<usize>`]) can be used for dynamic dimension.
/// - [`Ix<N>`] (given `const N: usize`, equilvalent to [`[usize; N]`](https://doc.rust-lang.org/core/primitive.array.html))
/// can be used for static dimension.
/// - Dynamic dimensionality is usually more preferred for general use cases, while static
/// dimensionality is more suitable for performance-critical code, especially frequent indexing
/// and non-contiguous memory access.
///
/// # Returns
///
/// - [`TensorView<'_, T, B, D2>`](TensorView)
///
/// - A view of the input tensor with the layout converted to the target dimension type.
/// - 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_dim`](TensorAny::into_dim) instead.
///
/// # Examples
///
/// In most cases, RSTSR will generate dynamic dimension ([`IxD`]) in most cases, including indexing
/// [`slice`](slice()), reshaping [`reshape`], creation [`asarray`].
///
/// You can debug print tensor or it's layout to verify the dimensionality, or call `const_ndim` to
/// check whether the dimension is static or dynamic.
///
/// ```rust
/// use rstsr::prelude::*;
/// let a = rt::arange(6).into_shape([2, 3]); // shape: (2, 3), IxD
/// println!("a: {:?}", a);
/// // `None` here indicates dynamic dimension
/// assert_eq!(a.shape().const_ndim(), None);
/// ```
///
/// You can convert the dimension to static dimension by associate method:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let a = rt::arange(6).into_shape([2, 3]); // shape: (2, 3), IxD
/// let b = a.to_dim::<Ix2>(); // shape: (2, 3), Ix2
/// assert_eq!(b.shape().const_ndim(), Some(2));
/// ```
///
/// You can use `.to_dim::<IxD>()` or `.to_dyn()` to convert back to dynamic dimension:
///
/// ```rust
/// # use rstsr::prelude::*;
/// # let a = rt::arange(6).into_shape([2, 3]); // shape: (2, 3), IxD
/// # let b = a.to_dim::<Ix2>(); // shape: (2, 3), Ix2
/// let c = b.to_dyn(); // shape: (2, 3), IxD
/// assert_eq!(c.shape().const_ndim(), None);
/// ```
///
/// # Panics
///
/// - The shape of the tensor is not compatible with the target dimension type.
///
/// ```rust,should_panic
/// use rstsr::prelude::*;
/// let a = rt::arange(6).into_shape([2, 3]); // shape: (2, 3), IxD
/// let b = a.to_dim::<Ix3>(); // shape: (2, 3), Ix3, panics
/// ```
///
/// # See also
///
/// ## Similar functions from other crates/libraries
///
/// - ndarray: [`into_dimensionality`](https://docs.rs/ndarray/latest/ndarray/struct.ArrayBase.html#method.into_dimensionality)
/// - ndarray: [`into_dyn`](https://docs.rs/ndarray/latest/ndarray/struct.ArrayBase.html#method.into_dyn)
///
/// ## Variants of this function
///
/// - [`to_dim`] / [`to_dim_f`]: Returning a view.
/// - [`into_dim`] / [`into_dim_f`]: Consuming version.
/// - [`to_dyn`]: Convert to dynamic dimension, returning a view (infallable by definition).
/// - [`into_dyn`]: Convert to dynamic dimension, consuming version (infallible by definition).
/// - Associated methods on [`TensorAny`]:
///
/// - [`TensorAny::to_dim`] / [`TensorAny::to_dim_f`]
/// - [`TensorAny::into_dim`] / [`TensorAny::into_dim_f`]
/// - [`TensorAny::to_dyn`]
/// - [`TensorAny::into_dyn`]
/// Convert layout to the other dimension.
///
/// See also [`to_dim`].
/// Convert layout to the other dimension.
///
/// See also [`to_dim`].
/// Convert layout to the dynamic dimension [`IxD`].
///
/// See also [`to_dim`].
/// Convert layout to the dynamic dimension [`IxD`].
///
/// See also [`to_dim`].