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
284
285
286
//! Shape manipulation operations trait.
use crate::error::Result;
use crate::runtime::Runtime;
use crate::tensor::Tensor;
/// Shape manipulation operations
pub trait ShapeOps<R: Runtime> {
/// Concatenate tensors along a dimension
///
/// Joins a sequence of tensors along an existing dimension. All tensors must
/// have the same shape except in the concatenation dimension.
///
/// # Arguments
///
/// * `tensors` - Slice of tensor references to concatenate
/// * `dim` - Dimension along which to concatenate (supports negative indexing)
///
/// # Returns
///
/// New tensor containing the concatenated data
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::ShapeOps;
///
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0], &[2], &device);
/// let b = Tensor::<CpuRuntime>::from_slice(&[3.0f32, 4.0, 5.0], &[3], &device);
/// let c = client.cat(&[&a, &b], 0)?; // Shape: [5]
/// # Ok::<(), numr::error::Error>(())
/// ```
fn cat(&self, tensors: &[&Tensor<R>], dim: isize) -> Result<Tensor<R>>;
/// Stack tensors along a new dimension
///
/// Joins a sequence of tensors along a new dimension. All tensors must have
/// exactly the same shape.
///
/// # Arguments
///
/// * `tensors` - Slice of tensor references to stack
/// * `dim` - Dimension at which to insert the new stacking dimension
///
/// # Returns
///
/// New tensor with an additional dimension
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::ShapeOps;
///
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0], &[2], &device);
/// let b = Tensor::<CpuRuntime>::from_slice(&[3.0f32, 4.0], &[2], &device);
/// let c = client.stack(&[&a, &b], 0)?; // Shape: [2, 2]
/// # Ok::<(), numr::error::Error>(())
/// ```
fn stack(&self, tensors: &[&Tensor<R>], dim: isize) -> Result<Tensor<R>>;
/// Split a tensor into chunks of a given size along a dimension
///
/// Splits the tensor into chunks. The last chunk will be smaller if the
/// dimension size is not evenly divisible by split_size.
///
/// # Arguments
///
/// * `tensor` - Tensor to split
/// * `split_size` - Size of each chunk (except possibly the last)
/// * `dim` - Dimension along which to split (supports negative indexing)
///
/// # Returns
///
/// Vector of tensor views (zero-copy) into the original tensor
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::ShapeOps;
///
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0], &[5], &device);
/// let chunks = client.split(&a, 2, 0)?; // [2], [2], [1]
/// # Ok::<(), numr::error::Error>(())
/// ```
fn split(&self, tensor: &Tensor<R>, split_size: usize, dim: isize) -> Result<Vec<Tensor<R>>>;
/// Split a tensor into a specific number of chunks along a dimension
///
/// Splits the tensor into approximately equal chunks. If the dimension
/// is not evenly divisible, earlier chunks will be one element larger.
///
/// # Arguments
///
/// * `tensor` - Tensor to chunk
/// * `chunks` - Number of chunks to create
/// * `dim` - Dimension along which to chunk (supports negative indexing)
///
/// # Returns
///
/// Vector of tensor views (zero-copy) into the original tensor
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::ShapeOps;
///
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0], &[5], &device);
/// let chunks = client.chunk(&a, 2, 0)?; // [3], [2]
/// # Ok::<(), numr::error::Error>(())
/// ```
fn chunk(&self, tensor: &Tensor<R>, chunks: usize, dim: isize) -> Result<Vec<Tensor<R>>>;
/// Repeat tensor along each dimension
///
/// Creates a new tensor by repeating the input tensor along each dimension.
/// The `repeats` slice specifies how many times to repeat along each dimension.
///
/// # Arguments
///
/// * `tensor` - Input tensor
/// * `repeats` - Number of repetitions for each dimension. Length must match tensor ndim.
///
/// # Returns
///
/// New tensor with shape `` `[dim_0 * repeats[0], dim_1 * repeats[1], ...]` ``
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::ShapeOps;
///
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[2, 2], &device);
/// let repeated = client.repeat(&a, &[2, 3])?; // Shape: [4, 6]
/// // Result: [[1,2,1,2,1,2], [3,4,3,4,3,4], [1,2,1,2,1,2], [3,4,3,4,3,4]]
/// # Ok::<(), numr::error::Error>(())
/// ```
fn repeat(&self, tensor: &Tensor<R>, repeats: &[usize]) -> Result<Tensor<R>>;
/// Pad tensor with a constant value
///
/// Adds padding to the tensor along specified dimensions. The `padding` slice
/// contains pairs of (before, after) padding sizes, starting from the last dimension.
///
/// # Arguments
///
/// * `tensor` - Input tensor
/// * `padding` - Padding sizes as pairs: `` `[last_before, last_after, second_last_before, ...]` ``
/// * `value` - Value to use for padding
///
/// # Returns
///
/// New tensor with padded dimensions
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::ShapeOps;
///
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[2, 2], &device);
/// // Pad last dim by 1 on each side
/// let padded = client.pad(&a, &[1, 1], 0.0)?; // Shape: [2, 4]
/// // Result: [[0,1,2,0], [0,3,4,0]]
/// # Ok::<(), numr::error::Error>(())
/// ```
fn pad(&self, tensor: &Tensor<R>, padding: &[usize], value: f64) -> Result<Tensor<R>>;
/// Roll tensor elements along a dimension
///
/// Shifts elements circularly along a dimension. Elements that roll beyond
/// the last position wrap around to the first position.
///
/// # Arguments
///
/// * `tensor` - Input tensor
/// * `shift` - Number of positions to shift (negative = shift left, positive = shift right)
/// * `dim` - Dimension along which to roll (supports negative indexing)
///
/// # Returns
///
/// New tensor with rolled elements
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::ShapeOps;
///
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0], &[4], &device);
/// let rolled = client.roll(&a, 1, 0)?; // [4, 1, 2, 3]
/// let rolled = client.roll(&a, -1, 0)?; // [2, 3, 4, 1]
/// # Ok::<(), numr::error::Error>(())
/// ```
fn roll(&self, tensor: &Tensor<R>, shift: isize, dim: isize) -> Result<Tensor<R>>;
/// Extract sliding local windows along a dimension.
///
/// Returns a tensor containing all windows of length `size` sampled every `step`
/// elements along `dim`. The output has one extra dimension, with the window-size
/// dimension appended at the end.
///
/// If input shape is `` `[d0, ..., d_dim, ..., dn]` ``, output shape is
/// `` `[d0, ..., num_windows, ..., dn, size]` `` where:
/// `` `num_windows = (d_dim - size) / step + 1` ``.
///
/// # Arguments
///
/// * `tensor` - Input tensor
/// * `dim` - Dimension along which to extract windows (supports negative indexing)
/// * `size` - Window size (must be > 0 and <= dimension size)
/// * `step` - Stride between window starts (must be > 0)
///
/// # Returns
///
/// New tensor containing extracted windows
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::ShapeOps;
///
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0, 4.0, 5.0], &[5], &device);
/// let windows = client.unfold(&a, 0, 3, 1)?; // Shape: [3, 3]
/// // Result: [[1,2,3], [2,3,4], [3,4,5]]
/// # Ok::<(), numr::error::Error>(())
/// ```
fn unfold(&self, tensor: &Tensor<R>, dim: isize, size: usize, step: usize)
-> Result<Tensor<R>>;
/// Repeat each element along a dimension.
///
/// Unlike `repeat`, which tiles whole tensor blocks along each dimension,
/// `repeat_interleave` repeats individual elements in-place along one dimension.
///
/// # Arguments
///
/// * `tensor` - Input tensor
/// * `repeats` - Number of times to repeat each element (must be > 0)
/// * `dim` - Dimension to repeat along (supports negative indexing). If `None`, input is flattened first.
///
/// # Returns
///
/// New tensor with repeated elements
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::ShapeOps;
///
/// let a = Tensor::<CpuRuntime>::from_slice(&[1.0f32, 2.0, 3.0], &[3], &device);
/// let out = client.repeat_interleave(&a, 2, Some(0))?;
/// // Result: [1, 1, 2, 2, 3, 3]
/// # Ok::<(), numr::error::Error>(())
/// ```
fn repeat_interleave(
&self,
tensor: &Tensor<R>,
repeats: usize,
dim: Option<isize>,
) -> Result<Tensor<R>>;
}