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
//! Utility operations trait.
use crate::dtype::DType;
use crate::error::{Error, Result};
use crate::runtime::Runtime;
use crate::tensor::Tensor;
/// Indexing mode for meshgrid
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MeshgridIndexing {
/// Matrix indexing (default): first dimension corresponds to first input
Ij,
/// Cartesian indexing: first two inputs are swapped (x=columns, y=rows)
Xy,
}
/// Utility operations
pub trait UtilityOps<R: Runtime> {
/// Clamp tensor values to a range: clamp(x, min, max) = min(max(x, min), max)
///
/// Element-wise clamps each value to be within [min_val, max_val].
///
/// # Arguments
///
/// * `a` - Input tensor
/// * `min_val` - Minimum value (inclusive)
/// * `max_val` - Maximum value (inclusive)
///
/// # Returns
///
/// Tensor with same shape and dtype as input, with values clamped to range
fn clamp(&self, a: &Tensor<R>, min_val: f64, max_val: f64) -> Result<Tensor<R>> {
let _ = (a, min_val, max_val);
Err(Error::NotImplemented {
feature: "UtilityOps::clamp",
})
}
/// Fill tensor with a constant value
///
/// Creates a new tensor with the specified shape and dtype, filled with the given value.
///
/// # Arguments
///
/// * `shape` - Shape of the output tensor
/// * `value` - Value to fill the tensor with
/// * `dtype` - Data type of the output tensor
///
/// # Returns
///
/// New tensor filled with the constant value
fn fill(&self, shape: &[usize], value: f64, dtype: DType) -> Result<Tensor<R>> {
let _ = (shape, value, dtype);
Err(Error::NotImplemented {
feature: "UtilityOps::fill",
})
}
/// Create a 1D tensor with evenly spaced values within a half-open interval [start, stop)
///
/// Values are generated using the formula: start + step * i for i in 0..n
/// where n = ceil((stop - start) / step)
///
/// # Arguments
///
/// * `start` - Start of the interval (inclusive)
/// * `stop` - End of the interval (exclusive)
/// * `step` - Spacing between values (must be positive if start < stop, negative if start > stop)
/// * `dtype` - Data type of the output tensor
///
/// # Returns
///
/// 1D tensor with evenly spaced values
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::UtilityOps;
///
/// let t = client.arange(0.0, 5.0, 1.0, DType::F32)?; // [0, 1, 2, 3, 4]
/// let t = client.arange(0.0, 5.0, 2.0, DType::F32)?; // [0, 2, 4]
/// let t = client.arange(5.0, 0.0, -1.0, DType::F32)?; // [5, 4, 3, 2, 1]
/// # Ok::<(), numr::error::Error>(())
/// ```
fn arange(&self, start: f64, stop: f64, step: f64, dtype: DType) -> Result<Tensor<R>> {
let _ = (start, stop, step, dtype);
Err(Error::NotImplemented {
feature: "UtilityOps::arange",
})
}
/// Create a 1D tensor with evenly spaced values over a specified interval
///
/// Returns `steps` evenly spaced values from `start` to `stop` (inclusive).
/// Values are: start + (stop - start) * i / (steps - 1) for i in 0..steps
///
/// # Arguments
///
/// * `start` - Start of the interval
/// * `stop` - End of the interval (inclusive)
/// * `steps` - Number of values to generate (must be >= 2)
/// * `dtype` - Data type of the output tensor (must be floating point)
///
/// # Returns
///
/// 1D tensor with evenly spaced values
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::UtilityOps;
///
/// let t = client.linspace(0.0, 10.0, 5, DType::F32)?; // [0, 2.5, 5, 7.5, 10]
/// let t = client.linspace(0.0, 1.0, 3, DType::F64)?; // [0, 0.5, 1]
/// # Ok::<(), numr::error::Error>(())
/// ```
fn linspace(&self, start: f64, stop: f64, steps: usize, dtype: DType) -> Result<Tensor<R>> {
let _ = (start, stop, steps, dtype);
Err(Error::NotImplemented {
feature: "UtilityOps::linspace",
})
}
/// Create a 2D identity matrix (or batch of identity matrices)
///
/// Creates a tensor where the diagonal elements are 1 and all others are 0.
/// For rectangular matrices, the diagonal is the main diagonal.
///
/// # Arguments
///
/// * `n` - Number of rows
/// * `m` - Number of columns (if None, defaults to n for square matrix)
/// * `dtype` - Data type of the output tensor
///
/// # Returns
///
/// 2D tensor of shape [n, m] with ones on the diagonal
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::UtilityOps;
///
/// let eye = client.eye(3, None, DType::F32)?; // 3x3 identity matrix
/// let rect = client.eye(2, Some(4), DType::F32)?; // 2x4 matrix with diagonal ones
/// # Ok::<(), numr::error::Error>(())
/// ```
fn eye(&self, n: usize, m: Option<usize>, dtype: DType) -> Result<Tensor<R>> {
let _ = (n, m, dtype);
Err(Error::NotImplemented {
feature: "UtilityOps::eye",
})
}
/// One-hot encode integer indices
///
/// Creates a tensor where each index value is expanded into a one-hot vector.
/// The output has one additional dimension of size `num_classes` appended.
///
/// # Arguments
///
/// * `indices` - Integer tensor of any shape [...]. Values must be in [0, num_classes).
/// * `num_classes` - Number of classes (size of the one-hot dimension)
///
/// # Returns
///
/// F32 tensor of shape [..., num_classes] where output[..., k] = 1.0
/// if indices[...] == k, else 0.0.
///
/// # Errors
///
/// - `UnsupportedDType` if indices is not an integer type
/// - `InvalidArgument` if num_classes == 0
///
/// # Example
///
/// ```
/// # use numr::prelude::*;
/// # let device = CpuDevice::new();
/// # let client = CpuRuntime::default_client(&device);
/// use numr::ops::UtilityOps;
///
/// let indices = Tensor::<CpuRuntime>::from_slice(&[0i64, 2, 1], &[3], &device);
/// let oh = client.one_hot(&indices, 4)?;
/// // oh = [[1, 0, 0, 0],
/// // [0, 0, 1, 0],
/// // [0, 1, 0, 0]]
/// # Ok::<(), numr::error::Error>(())
/// ```
fn one_hot(&self, indices: &Tensor<R>, num_classes: usize) -> Result<Tensor<R>> {
let _ = (indices, num_classes);
Err(Error::NotImplemented {
feature: "UtilityOps::one_hot",
})
}
/// Create coordinate grids from 1-D coordinate vectors
///
/// Given N 1-D tensors, returns N N-D tensors where each output tensor
/// represents one coordinate along one axis of the N-D grid.
///
/// # Arguments
///
/// * `tensors` - Slice of 1-D input tensors (the coordinate vectors)
/// * `indexing` - Grid indexing convention (Ij for matrix, Xy for Cartesian)
///
/// # Returns
///
/// Vec of N tensors, each with shape [len(t0), len(t1), ..., len(tN-1)]
/// (or with first two dims swapped for Xy indexing)
fn meshgrid(
&self,
tensors: &[&Tensor<R>],
indexing: MeshgridIndexing,
) -> Result<Vec<Tensor<R>>> {
let _ = (tensors, indexing);
Err(Error::NotImplemented {
feature: "UtilityOps::meshgrid",
})
}
}