vectra 0.2.4

A multi-dimensional array library for Rust, similar to NumPy
Documentation
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
use itertools::Itertools;
use num_traits::cast;

use crate::{
    NumExt,
    core::Array,
    utils::{broadcast_shapes, compute_strides, dyn_dim_to_static, negative_idx_to_positive},
};
use std::ops::{Add, AddAssign, Div, Index, IndexMut, Mul, MulAssign, Neg, Not, Sub, SubAssign};

// impl<T> Index<isize> for Array<1, T> {
//     type Output = T;

//     fn index(&self, index: isize) -> &Self::Output {
//         let flat_index = self.index_to_flat([index]);
//         &self.data[flat_index]
//     }
// }

// impl<T> IndexMut<isize> for Array<1, T> {
//     fn index_mut(&mut self, index: isize) -> &mut Self::Output {
//         let flat_index = self.index_to_flat([index]);
//         &mut self.data[flat_index]
//     }
// }

// macro_rules! impl_index {
//     ($($dim:expr),*) => {
//         $(
//             impl<T> Index<isize> for Array<$dim, T> {
//                 type Output = Array<{$dim - 1}, T>;

//                 fn index(&self, index: isize) -> &Self::Output {
//                     let flat_index = self.index_to_flat([index]);
//                     &self.data[flat_index]
//                 }
//             }

//             // impl<T> IndexMut<isize> for Array<$dim, T> {
//             //     fn index_mut(&mut self, index: isize) -> &mut Self::Output {
//             //         let flat_index = self.index_to_flat([index]);
//             //         &mut self.data[flat_index]
//             //     }
//             // }
//         )*
//     };
// }

// impl_index!(2, 3, 4, 5, 6, 7, 8, 9, 10);

// impl<const D: usize, T> Index<isize> for Array<D, T>
// where
//     [(); D - 2]:,
// {
//     type Output = T;

//     fn index(&self, index: isize) -> &Self::Output {
//         let flat_index = self.index_to_flat([index]);
//         &self.data[flat_index]
//     }
// }

impl<const D: usize, T> Index<[isize; D]> for Array<D, T> {
    type Output = T;

    fn index(&self, indices: [isize; D]) -> &Self::Output {
        let flat_index = self.index_to_flat(indices);
        &self.data[flat_index]
    }
}

impl<const D: usize, T> IndexMut<[isize; D]> for Array<D, T> {
    fn index_mut(&mut self, indices: [isize; D]) -> &mut Self::Output {
        let flat_index = self.index_to_flat(indices);
        &mut self.data[flat_index]
    }
}

impl<const D: usize, T: Neg<Output = T> + Clone> Neg for Array<D, T> {
    type Output = Array<D, T>;

    fn neg(self) -> Self::Output {
        self.map_into(|x| -x)
    }
}

impl<const D: usize, T: Neg<Output = T> + Clone> Neg for &Array<D, T> {
    type Output = Array<D, T>;

    fn neg(self) -> Self::Output {
        self.map(|x| -x.clone())
    }
}

impl<const D: usize, T: Not<Output = T> + Clone> Not for Array<D, T> {
    type Output = Array<D, T>;

    fn not(self) -> Self::Output {
        self.map_into(|x| !x)
    }
}

// Arithmetic operations between arrays
impl<const D: usize, T: NumExt> AddAssign for Array<D, T> {
    fn add_assign(&mut self, rhs: Self) {
        let result = add_impl(self, &rhs);
        *self = result;
    }
}

impl<const D: usize, T: NumExt> AddAssign<&Array<D, T>> for Array<D, T> {
    fn add_assign(&mut self, rhs: &Array<D, T>) {
        let result = add_impl(self, rhs);
        *self = result;
    }
}

impl<const D: usize, T: NumExt> Add for &Array<D, T> {
    type Output = Array<D, T>;

    fn add(self, rhs: Self) -> Self::Output {
        add_impl(self, rhs)
    }
}

fn add_impl<const D: usize, T: NumExt>(left: &Array<D, T>, right: &Array<D, T>) -> Array<D, T> {
    let target_shape =
        broadcast_shapes(left.shape, right.shape).expect("Cannot broadcast arrays for add");

    let left = left.broadcast_to(target_shape);
    let right = right.broadcast_to(target_shape);

    let result_data = left
        .multi_iter()
        .zip(right.multi_iter())
        .map(|((_, a), (_, b))| a.clone() + b.clone())
        .collect();

    let major_order = crate::core::MajorOrder::RowMajor;
    let strides = compute_strides(left.shape, major_order);

    Array {
        data: result_data,
        shape: left.shape.clone(),
        strides,
        major_order,
    }
}

impl<const D: usize, T: NumExt> SubAssign for Array<D, T> {
    fn sub_assign(&mut self, rhs: Self) {
        let result = sub_impl(self, &rhs);
        *self = result;
    }
}

impl<const D: usize, T: NumExt> Sub for &Array<D, T> {
    type Output = Array<D, T>;

    fn sub(self, rhs: Self) -> Self::Output {
        sub_impl(self, rhs)
    }
}

fn sub_impl<const D: usize, T: NumExt>(left: &Array<D, T>, right: &Array<D, T>) -> Array<D, T> {
    let target_shape =
        broadcast_shapes(left.shape, right.shape).expect("Cannot broadcast arrays for sub");

    let left = left.broadcast_to(target_shape);
    let right = right.broadcast_to(target_shape);

    let result_data = left
        .multi_iter()
        .zip(right.multi_iter())
        .map(|((_, a), (_, b))| a.clone() - b.clone())
        .collect();

    let major_order = crate::core::MajorOrder::RowMajor;
    let strides = compute_strides(left.shape, major_order);

    Array {
        data: result_data,
        shape: left.shape.clone(),
        strides,
        major_order,
    }
}

impl<const D: usize, T: NumExt> MulAssign<&Array<D, T>> for Array<D, T> {
    fn mul_assign(&mut self, rhs: &Array<D, T>) {
        *self = mul_impl(&*self, rhs);
    }
}

impl<const D: usize, T: NumExt> Mul for &Array<D, T> {
    type Output = Array<D, T>;

    fn mul(self, rhs: Self) -> Self::Output {
        mul_impl(self, &rhs)
    }
}

fn mul_impl<const D: usize, T: NumExt>(left: &Array<D, T>, right: &Array<D, T>) -> Array<D, T> {
    let target_shape =
        broadcast_shapes(left.shape, right.shape).expect("Cannot broadcast arrays for mul");

    let left = left.broadcast_to(target_shape);
    let right = right.broadcast_to(target_shape);

    let result_data = left
        .multi_iter()
        .zip(right.multi_iter())
        .map(|((_, a), (_, b))| a.clone() * b.clone())
        .collect();

    let major_order = crate::core::MajorOrder::RowMajor;
    let strides = compute_strides(left.shape, major_order);

    Array {
        data: result_data,
        shape: left.shape.clone(),
        strides,
        major_order,
    }
}

impl<const D: usize, T: NumExt> Div for &Array<D, T> {
    type Output = Array<D, T>;

    fn div(self, rhs: Self) -> Self::Output {
        div_impl(self, rhs)
    }
}

fn div_impl<const D: usize, T: NumExt>(left: &Array<D, T>, right: &Array<D, T>) -> Array<D, T>
where
    T: Div<Output = T> + Clone,
{
    let target_shape =
        broadcast_shapes(left.shape, right.shape).expect("Cannot broadcast arrays for div");

    let left = left.broadcast_to(target_shape);
    let right = right.broadcast_to(target_shape);

    let result_data = left
        .multi_iter()
        .zip(right.multi_iter())
        .map(|((_, a), (_, b))| a.clone() / b.clone())
        .collect();

    let major_order = crate::core::MajorOrder::RowMajor;
    let strides = compute_strides(left.shape, major_order);

    Array {
        data: result_data,
        shape: left.shape.clone(),
        strides,
        major_order,
    }
}

// Scalar operations
impl<const D: usize, T: NumExt> Array<D, T> {
    /// Add scalar to all elements
    pub fn add_scalar(mut self, scalar: T) -> Self {
        self.data.iter_mut().for_each(|x| *x += scalar.clone());
        self
    }

    /// Subtract scalar from all elements
    pub fn sub_scalar(mut self, scalar: T) -> Self {
        self.data.iter_mut().for_each(|x| *x -= scalar.clone());
        self
    }

    /// Multiply all elements by scalar
    pub fn mul_scalar(mut self, scalar: T) -> Self {
        self.data.iter_mut().for_each(|x| *x *= scalar.clone());
        self
    }

    /// Divide all elements by scalar
    pub fn div_scalar(mut self, scalar: T) -> Self {
        self.data.iter_mut().for_each(|x| *x /= scalar.clone());
        self
    }
}

impl<const D: usize, T: NumExt> Array<D, T> {
    pub fn one_hot<F: NumExt>(&self, num_classes: usize) -> Array<{ D + 1 }, F> {
        self.one_hot_fill(num_classes, F::one(), F::zero(), -1)
    }

    pub fn one_hot_fill<F>(
        &self,
        num_classes: usize,
        on_value: F,
        off_value: F,
        axis: isize,
    ) -> Array<{ D + 1 }, F>
    where
        F: NumExt,
    {
        let indices = self
            .map(|v| {
                let value = cast::<_, isize>(v.clone()).expect("Failed to cast T to usize");

                if value < -(num_classes as isize) || value >= num_classes as isize {
                    panic!("self value as index meet failure, contains values greater than or equal to num_classes");
                }

                negative_idx_to_positive(value, num_classes)
            })
            .unsqueeze(axis);

        let axis = negative_idx_to_positive(axis, D + 1);

        let mut shape = self.shape().to_vec();
        shape.insert(axis, num_classes);
        let mut target_shape = [0; D + 1];
        target_shape.copy_from_slice(&shape);

        let mut result = Array::full(target_shape, off_value);

        for (mut idx, value) in indices.multi_iter() {
            idx[axis] = *value;

            let raw_idx = result.positive_index_to_flat(idx);
            result.data[raw_idx] = on_value.clone();
        }

        result
    }
}

impl<const D: usize, T> Array<D, T> {
    pub fn select(&self, axis: isize, indices: &[usize]) -> Array<D, T>
    where
        T: Clone,
    {
        let axis = negative_idx_to_positive(axis, D);
        let axis_size = self.shape()[axis];

        let mut non_axis_shape = self.shape().to_vec();
        non_axis_shape.remove(axis);

        let indices: Vec<_> = indices
            .into_iter()
            .filter(|&idx| idx < &axis_size)
            .cloned()
            .collect();
        let mut products: Vec<_> = non_axis_shape
            .iter()
            .map(|&n| 0..n)
            .map(|v| v.into_iter().collect::<Vec<_>>())
            .collect();
        // let indices: Vec<_> = indices.into_iter().sorted().cloned().collect();
        non_axis_shape.insert(axis, indices.len());

        products.insert(axis, indices);

        let data: Vec<_> = products
            .into_iter()
            .multi_cartesian_product()
            .map(|idx| {
                let idx = dyn_dim_to_static::<D, _>(&idx).map(|a| a as isize);
                self[idx].clone()
            })
            .collect();

        let mut shape = [0; D];
        shape.copy_from_slice(&non_axis_shape);
        Array::from_vec(data, shape)
    }
    pub fn multi_iter(&self) -> impl Iterator<Item = ([usize; D], &T)> {
        self.shape
            .into_iter()
            .map(|n| 0..n)
            .multi_cartesian_product()
            .map(|idx| {
                let idx = dyn_dim_to_static::<D, _>(&idx);

                let data = self.index(idx.map(|i| i as isize));
                (idx, data)
            })
    }

    pub fn multi_iter_mut<F>(&mut self, mut f: F)
    where
        F: FnMut([usize; D], &mut T),
    {
        for idx in self
            .shape()
            .into_iter()
            .map(|n| 0..n)
            .multi_cartesian_product()
        {
            let idx = dyn_dim_to_static(&idx);
            let data = self.index_mut(idx.map(|i| i as isize));

            f(idx, data);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_multi_iter_mut() {
        let arr = Array::from_vec(vec![1, 2, 3, 4], [2, 2]);
        let mut iter = arr.multi_iter();
        assert_eq!(iter.next(), Some(([0, 0], &1)));
        assert_eq!(iter.next(), Some(([0, 1], &2)));
        assert_eq!(iter.next(), Some(([1, 0], &3)));
        assert_eq!(iter.next(), Some(([1, 1], &4)));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_arithmetic() {
        let a = Array::from_vec(vec![1.0, 2.0, 3.0, 4.0], [2, 2]);
        let b = Array::from_vec(vec![2.0, 2.0, 2.0, 2.0], [2, 2]);

        let sum = &a + &b;
        assert_eq!(sum[[0, 0]], 3.0);
        assert_eq!(sum[[1, 1]], 6.0);
        assert_eq!(sum[[-1, -1]], 6.0);

        let product = &a * &b;
        assert_eq!(product[[0, 0]], 2.0);
        assert_eq!(product[[1, 1]], 8.0);

        let c = Array::from_vec(vec![1.0, 2.0], [1, 2]);
        let d = &a + &c;
        println!("d= {d}");
    }

    #[test]
    fn test_one_hot() {
        // let a = cast::<_, usize>(2.2);
        // println!("a: {a:?}");
        let arr = Array::from_vec(vec![1, 2, 2, 4], [4]);
        let one_hot = arr.one_hot::<f32>(5);

        println!("arr= {arr:?} one_hot= {one_hot:?}");

        assert_eq!(
            one_hot,
            Array::from_vec(
                vec![
                    0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0,
                    0.0, 0.0, 0.0, 1.0
                ],
                [4, 5]
            )
        );

        let indices = Array::from_vec(vec![0, 2, 1, -1], [2, 2]);
        let res = indices.one_hot_fill(3, 5.0, 0.0, -1);
        println!("res: {res:?}");
        assert_eq!(
            res,
            Array::from_vec(
                vec![5.0, 0.0, 0.0, 0.0, 0.0, 5.0, 0.0, 5.0, 0.0, 0.0, 0.0, 5.0],
                [2, 2, 3]
            )
        );
    }

    #[test]
    fn test_select() {
        let a = Array::from_vec(vec![1, 2, 3, 4], [2, 2]);
        assert_eq!(a.select(0, &[0]), Array::from_vec(vec![1, 2], [1, 2]));
        assert_eq!(a.select(-1, &[0]), Array::from_vec(vec![1, 3], [2, 1]));
        assert_eq!(a.select(-1, &[0, 10]), Array::from_vec(vec![1, 3], [2, 1]));
    }
}