tensors-rs 0.1.2

Compact NumPy-like dense tensor primitives for safe numerical Rust.
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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
//! Owned three-dimensional row-major arrays.

use core::ops::{Index, IndexMut};

use crate::array2::Array2;
use crate::error::{Error, Result};
use crate::numeric::Float;
use crate::rand::SmallRng;
use crate::view2::{ArrayView2, ArrayViewMut2};
use crate::view3::{ArrayView3, ArrayViewMut3};

/// Axis selector for 3D arrays.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Axis3 {
    /// First axis.
    Axis0,
    /// Second axis.
    Axis1,
    /// Third axis.
    Axis2,
}

impl Axis3 {
    pub(crate) fn index(self) -> usize {
        match self {
            Self::Axis0 => 0,
            Self::Axis1 => 1,
            Self::Axis2 => 2,
        }
    }
}

/// Owned 3D row-major array.
#[derive(Clone, Debug, PartialEq)]
pub struct Array3<T> {
    data: Vec<T>,
    shape: [usize; 3],
}

impl<T> Array3<T> {
    /// Build an array from row-major data.
    pub fn from_vec(shape: [usize; 3], data: Vec<T>) -> Result<Self> {
        let expected = shape
            .iter()
            .try_fold(1usize, |acc, &dim| acc.checked_mul(dim))
            .ok_or(Error::DimensionTooLarge)?;
        if data.len() != expected {
            return Err(Error::shape(vec![expected], vec![data.len()]));
        }
        Ok(Self { data, shape })
    }

    /// Build an array from a function over `(i, j, k)`.
    pub fn from_fn(shape: [usize; 3], mut f: impl FnMut(usize, usize, usize) -> T) -> Self {
        let mut data = Vec::with_capacity(shape.iter().product());
        for i in 0..shape[0] {
            for j in 0..shape[1] {
                for k in 0..shape[2] {
                    data.push(f(i, j, k));
                }
            }
        }
        Self { data, shape }
    }

    /// Fallibly build an array from a function over `(i, j, k)`.
    pub fn try_from_fn(
        shape: [usize; 3],
        mut f: impl FnMut(usize, usize, usize) -> T,
    ) -> Result<Self> {
        let len = checked_len(shape)?;
        let mut data = Vec::new();
        data.try_reserve_exact(len)
            .map_err(|_| Error::AllocationFailed)?;
        for i in 0..shape[0] {
            for j in 0..shape[1] {
                for k in 0..shape[2] {
                    data.push(f(i, j, k));
                }
            }
        }
        Ok(Self { data, shape })
    }

    /// Shape as `[dim0, dim1, dim2]`.
    pub fn shape(&self) -> [usize; 3] {
        self.shape
    }

    /// Row-major strides in elements.
    pub fn strides(&self) -> [isize; 3] {
        [
            (self.shape[1] * self.shape[2]) as isize,
            self.shape[2] as isize,
            1,
        ]
    }

    /// Number of elements.
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Whether the array has zero elements.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Borrow the backing slice.
    pub fn as_slice(&self) -> &[T] {
        &self.data
    }

    /// Borrow the backing slice mutably.
    pub fn as_mut_slice(&mut self) -> &mut [T] {
        &mut self.data
    }

    /// Immutable strided view.
    pub fn view(&self) -> ArrayView3<'_, T> {
        ArrayView3::from_raw_parts(&self.data, self.shape, self.strides(), 0)
    }

    /// Mutable strided view.
    pub fn view_mut(&mut self) -> ArrayViewMut3<'_, T> {
        ArrayViewMut3::from_raw_parts(
            &mut self.data,
            self.shape,
            [
                (self.shape[1] * self.shape[2]) as isize,
                self.shape[2] as isize,
                1,
            ],
            0,
        )
    }

    /// Get an element reference.
    pub fn get(&self, i: usize, j: usize, k: usize) -> Option<&T> {
        (i < self.shape[0] && j < self.shape[1] && k < self.shape[2])
            .then(|| &self.data[self.linear_index(i, j, k)])
    }

    /// Get a mutable element reference.
    pub fn get_mut(&mut self, i: usize, j: usize, k: usize) -> Option<&mut T> {
        if i >= self.shape[0] || j >= self.shape[1] || k >= self.shape[2] {
            return None;
        }
        let idx = self.linear_index(i, j, k);
        Some(&mut self.data[idx])
    }

    /// Extract a 2D matrix view by fixing one axis.
    pub fn matrix_at(&self, axis: Axis3, index: usize) -> Result<ArrayView2<'_, T>> {
        self.view().matrix_at(axis.index(), index)
    }

    /// Visit each 2D matrix slice along `axis` in order.
    pub fn for_each_matrix(
        &self,
        axis: Axis3,
        f: impl FnMut(usize, ArrayView2<'_, T>) -> Result<()>,
    ) -> Result<()> {
        self.view().for_each_matrix(axis.index(), f)
    }

    /// Extract a mutable 2D matrix view by fixing one axis.
    pub fn matrix_at_mut(&mut self, axis: Axis3, index: usize) -> Result<ArrayViewMut2<'_, T>> {
        let axis = axis.index();
        if index >= self.shape[axis] {
            return Err(Error::IndexOutOfBounds);
        }
        let strides = self.strides();
        let axes: Vec<usize> = (0..3).filter(|&candidate| candidate != axis).collect();
        ArrayViewMut2::new(
            &mut self.data,
            [self.shape[axes[0]], self.shape[axes[1]]],
            [strides[axes[0]], strides[axes[1]]],
            index as isize * strides[axis],
        )
    }

    /// Visit each mutable 2D matrix slice along `axis` in order.
    pub fn for_each_matrix_mut(
        &mut self,
        axis: Axis3,
        mut f: impl FnMut(usize, ArrayViewMut2<'_, T>) -> Result<()>,
    ) -> Result<()> {
        let axis = axis.index();
        if axis >= 3 {
            return Err(Error::AxisOutOfBounds { axis, ndim: 3 });
        }
        for index in 0..self.shape[axis] {
            f(
                index,
                self.matrix_at_mut(
                    match axis {
                        0 => Axis3::Axis0,
                        1 => Axis3::Axis1,
                        _ => Axis3::Axis2,
                    },
                    index,
                )?,
            )?;
        }
        Ok(())
    }

    fn linear_index(&self, i: usize, j: usize, k: usize) -> usize {
        (i * self.shape[1] + j) * self.shape[2] + k
    }
}

impl<T: Clone> Array3<T> {
    /// Fill a new array with `value`.
    pub fn filled(shape: [usize; 3], value: T) -> Self {
        Self {
            data: vec![value; shape.iter().product()],
            shape,
        }
    }

    /// Fallibly fill a new array with `value`.
    pub fn try_filled(shape: [usize; 3], value: T) -> Result<Self> {
        let len = checked_len(shape)?;
        let mut data = Vec::new();
        data.try_reserve_exact(len)
            .map_err(|_| Error::AllocationFailed)?;
        data.resize(len, value);
        Ok(Self { data, shape })
    }

    /// Clone into compact row-major storage.
    pub fn clone_contiguous(view: ArrayView3<'_, T>) -> Self {
        Self::from_fn(view.shape(), |i, j, k| view[(i, j, k)].clone())
    }

    /// Unfold the tensor along `axis` into a row-major matrix.
    ///
    /// For shape `[i, j, k]`, mode-0 unfolding has shape `[i, j * k]`,
    /// mode-1 has shape `[j, i * k]`, and mode-2 has shape `[k, i * j]`.
    pub fn unfold(&self, axis: Axis3) -> Array2<T> {
        unfold_view(self.view(), axis)
    }

    /// Fold a mode-unfolded matrix back into a tensor with `shape`.
    pub fn fold(axis: Axis3, shape: [usize; 3], matrix: ArrayView2<'_, T>) -> Result<Self> {
        fold_view(axis, shape, matrix)
    }
}

/// Unfold a 3D tensor view along `axis` into a row-major matrix.
pub fn unfold_view<T: Clone>(a: ArrayView3<'_, T>, axis: Axis3) -> Array2<T> {
    let shape = a.shape();
    match axis {
        Axis3::Axis0 => Array2::from_fn([shape[0], shape[1] * shape[2]], |row, col| {
            let j = col / shape[2];
            let k = col % shape[2];
            a[(row, j, k)].clone()
        }),
        Axis3::Axis1 => Array2::from_fn([shape[1], shape[0] * shape[2]], |row, col| {
            let i = col / shape[2];
            let k = col % shape[2];
            a[(i, row, k)].clone()
        }),
        Axis3::Axis2 => Array2::from_fn([shape[2], shape[0] * shape[1]], |row, col| {
            let i = col / shape[1];
            let j = col % shape[1];
            a[(i, j, row)].clone()
        }),
    }
}

/// Fold a mode-unfolded matrix back into a 3D tensor with `shape`.
pub fn fold_view<T: Clone>(
    axis: Axis3,
    shape: [usize; 3],
    matrix: ArrayView2<'_, T>,
) -> Result<Array3<T>> {
    let expected = match axis {
        Axis3::Axis0 => [shape[0], shape[1] * shape[2]],
        Axis3::Axis1 => [shape[1], shape[0] * shape[2]],
        Axis3::Axis2 => [shape[2], shape[0] * shape[1]],
    };
    if matrix.shape() != expected {
        return Err(Error::shape(expected, matrix.shape()));
    }
    Ok(Array3::from_fn(shape, |i, j, k| match axis {
        Axis3::Axis0 => matrix[(i, j * shape[2] + k)].clone(),
        Axis3::Axis1 => matrix[(j, i * shape[2] + k)].clone(),
        Axis3::Axis2 => matrix[(k, i * shape[1] + j)].clone(),
    }))
}

impl<T: Float> Array3<T> {
    /// Array filled with zeros.
    pub fn zeros(shape: [usize; 3]) -> Self {
        Self::filled(shape, T::zero())
    }

    /// Fallibly allocate an array filled with zeros.
    pub fn try_zeros(shape: [usize; 3]) -> Result<Self> {
        Self::try_filled(shape, T::zero())
    }

    /// Array filled with ones.
    pub fn ones(shape: [usize; 3]) -> Self {
        Self::filled(shape, T::one())
    }

    /// Fallibly allocate an array filled with ones.
    pub fn try_ones(shape: [usize; 3]) -> Result<Self> {
        Self::try_filled(shape, T::one())
    }

    /// Allocate another array with the same shape, filled with zeros.
    pub fn zeros_like(&self) -> Self {
        Self::zeros(self.shape)
    }

    /// Scale in place.
    pub fn scale(&mut self, alpha: T) {
        for value in &mut self.data {
            *value *= alpha;
        }
    }

    /// Return `self * alpha` without modifying `self`.
    pub fn scaled(&self, alpha: T) -> Self {
        Self::from_fn(self.shape, |i, j, k| self[(i, j, k)] * alpha)
    }

    /// Write `self * alpha` into `out` without modifying `self`.
    pub fn scaled_into(&self, alpha: T, mut out: ArrayViewMut3<'_, T>) -> Result<()> {
        if self.shape != out.shape() {
            return Err(Error::shape(self.shape, out.shape()));
        }
        for i in 0..self.shape[0] {
            for j in 0..self.shape[1] {
                for k in 0..self.shape[2] {
                    out[(i, j, k)] = self[(i, j, k)] * alpha;
                }
            }
        }
        Ok(())
    }

    /// Return `self + other` without modifying either input.
    pub fn add(&self, other: ArrayView3<'_, T>) -> Result<Self> {
        self.zip_map(other, |left, right| left + right)
    }

    /// Write `self + other` into `out` without modifying either input.
    pub fn add_into(&self, other: ArrayView3<'_, T>, out: ArrayViewMut3<'_, T>) -> Result<()> {
        self.zip_map_into(other, out, |left, right| left + right)
    }

    /// Return `self - other` without modifying either input.
    pub fn sub(&self, other: ArrayView3<'_, T>) -> Result<Self> {
        self.zip_map(other, |left, right| left - right)
    }

    /// Write `self - other` into `out` without modifying either input.
    pub fn sub_into(&self, other: ArrayView3<'_, T>, out: ArrayViewMut3<'_, T>) -> Result<()> {
        self.zip_map_into(other, out, |left, right| left - right)
    }

    /// Return the elementwise product `self * other` without modifying either input.
    pub fn mul(&self, other: ArrayView3<'_, T>) -> Result<Self> {
        self.zip_map(other, |left, right| left * right)
    }

    /// Write the elementwise product into `out` without modifying either input.
    pub fn mul_into(&self, other: ArrayView3<'_, T>, out: ArrayViewMut3<'_, T>) -> Result<()> {
        self.zip_map_into(other, out, |left, right| left * right)
    }

    /// Return the Hadamard product without modifying either input.
    pub fn hadamard(&self, other: ArrayView3<'_, T>) -> Result<Self> {
        self.mul(other)
    }

    /// Write the Hadamard product into `out` without modifying either input.
    pub fn hadamard_into(&self, other: ArrayView3<'_, T>, out: ArrayViewMut3<'_, T>) -> Result<()> {
        self.mul_into(other, out)
    }

    /// Return the elementwise quotient `self / other` without modifying either input.
    pub fn div(&self, other: ArrayView3<'_, T>) -> Result<Self> {
        self.zip_map(other, |left, right| left / right)
    }

    /// Write the elementwise quotient into `out` without modifying either input.
    pub fn div_into(&self, other: ArrayView3<'_, T>, out: ArrayViewMut3<'_, T>) -> Result<()> {
        self.zip_map_into(other, out, |left, right| left / right)
    }

    /// Return `self + alpha * x` without modifying either input.
    pub fn axpy_result(&self, alpha: T, x: ArrayView3<'_, T>) -> Result<Self> {
        self.zip_map(x, |left, right| left + alpha * right)
    }

    /// Write `self + alpha * x` into `out` without modifying either input.
    pub fn axpy_into(
        &self,
        alpha: T,
        x: ArrayView3<'_, T>,
        out: ArrayViewMut3<'_, T>,
    ) -> Result<()> {
        self.zip_map_into(x, out, |left, right| left + alpha * right)
    }

    /// In-place addition.
    pub fn add_assign_view(&mut self, other: ArrayView3<'_, T>) -> Result<()> {
        self.zip_map_inplace(other, |left, right| left + right)
    }

    /// In-place subtraction.
    pub fn sub_assign_view(&mut self, other: ArrayView3<'_, T>) -> Result<()> {
        self.zip_map_inplace(other, |left, right| left - right)
    }

    /// Hadamard product in place.
    pub fn mul_assign_view(&mut self, other: ArrayView3<'_, T>) -> Result<()> {
        self.zip_map_inplace(other, |left, right| left * right)
    }

    /// In-place elementwise division.
    pub fn div_assign_view(&mut self, other: ArrayView3<'_, T>) -> Result<()> {
        self.zip_map_inplace(other, |left, right| left / right)
    }

    /// Compute `self += alpha * x`.
    pub fn axpy(&mut self, alpha: T, x: ArrayView3<'_, T>) -> Result<()> {
        if self.shape != x.shape() {
            return Err(Error::shape(self.shape, x.shape()));
        }
        for i in 0..self.shape[0] {
            for j in 0..self.shape[1] {
                for k in 0..self.shape[2] {
                    self[(i, j, k)] += alpha * x[(i, j, k)];
                }
            }
        }
        Ok(())
    }

    /// Return an elementwise zip-map without modifying either input.
    pub fn zip_map(&self, other: ArrayView3<'_, T>, mut f: impl FnMut(T, T) -> T) -> Result<Self> {
        if self.shape != other.shape() {
            return Err(Error::shape(self.shape, other.shape()));
        }
        Ok(Self::from_fn(self.shape, |i, j, k| {
            f(self[(i, j, k)], other[(i, j, k)])
        }))
    }

    /// Write an elementwise zip-map into `out` without modifying either input.
    pub fn zip_map_into(
        &self,
        other: ArrayView3<'_, T>,
        mut out: ArrayViewMut3<'_, T>,
        mut f: impl FnMut(T, T) -> T,
    ) -> Result<()> {
        if self.shape != other.shape() {
            return Err(Error::shape(self.shape, other.shape()));
        }
        if self.shape != out.shape() {
            return Err(Error::shape(self.shape, out.shape()));
        }
        for i in 0..self.shape[0] {
            for j in 0..self.shape[1] {
                for k in 0..self.shape[2] {
                    out[(i, j, k)] = f(self[(i, j, k)], other[(i, j, k)]);
                }
            }
        }
        Ok(())
    }

    /// Map values in place.
    pub fn map_inplace(&mut self, mut f: impl FnMut(T) -> T) {
        for value in &mut self.data {
            *value = f(*value);
        }
    }

    /// Zip-map another view into this array in place.
    pub fn zip_map_inplace(
        &mut self,
        other: ArrayView3<'_, T>,
        mut f: impl FnMut(T, T) -> T,
    ) -> Result<()> {
        if self.shape != other.shape() {
            return Err(Error::shape(self.shape, other.shape()));
        }
        for i in 0..self.shape[0] {
            for j in 0..self.shape[1] {
                for k in 0..self.shape[2] {
                    self[(i, j, k)] = f(self[(i, j, k)], other[(i, j, k)]);
                }
            }
        }
        Ok(())
    }

    /// Fill with deterministic uniform random values in `[0, 1)`.
    pub fn fill_uniform(&mut self, seed: u64) {
        let mut rng = SmallRng::new(seed);
        for value in &mut self.data {
            *value = rng.uniform();
        }
    }

    /// Fill with deterministic standard-normal random values.
    pub fn fill_randn(&mut self, seed: u64) {
        let mut rng = SmallRng::new(seed);
        for value in &mut self.data {
            *value = rng.normal();
        }
    }

    /// Sum all elements.
    pub fn sum(&self) -> T {
        self.data.iter().copied().sum()
    }

    /// Mean of all elements, or zero for an empty array.
    pub fn mean(&self) -> T {
        if self.is_empty() {
            T::zero()
        } else {
            self.sum() / T::from_f64(self.len() as f64)
        }
    }

    /// Frobenius norm.
    pub fn norm_frobenius(&self) -> T {
        self.data
            .iter()
            .copied()
            .map(|value| value * value)
            .sum::<T>()
            .sqrt()
    }

    /// Maximum absolute element, or zero for an empty array.
    pub fn max_abs(&self) -> T {
        self.data
            .iter()
            .copied()
            .map(T::abs)
            .fold(
                T::zero(),
                |best, value| if value > best { value } else { best },
            )
    }

    /// Dot product of two tensors treated as flattened row-major buffers.
    pub fn dot(&self, other: ArrayView3<'_, T>) -> Result<T> {
        if self.shape != other.shape() {
            return Err(Error::shape(self.shape, other.shape()));
        }
        let mut sum = T::zero();
        for i in 0..self.shape[0] {
            for j in 0..self.shape[1] {
                for k in 0..self.shape[2] {
                    sum += self[(i, j, k)] * other[(i, j, k)];
                }
            }
        }
        Ok(sum)
    }
}

impl<T> Index<(usize, usize, usize)> for Array3<T> {
    type Output = T;

    fn index(&self, index: (usize, usize, usize)) -> &Self::Output {
        self.get(index.0, index.1, index.2)
            .expect("array index out of bounds")
    }
}

fn checked_len(shape: [usize; 3]) -> Result<usize> {
    shape
        .iter()
        .try_fold(1usize, |acc, &dim| acc.checked_mul(dim))
        .ok_or(Error::DimensionTooLarge)
}

impl<T> IndexMut<(usize, usize, usize)> for Array3<T> {
    fn index_mut(&mut self, index: (usize, usize, usize)) -> &mut Self::Output {
        self.get_mut(index.0, index.1, index.2)
            .expect("array index out of bounds")
    }
}