vector-victor 0.1.3

Yet another linear algebra crate
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

extern crate core;

use index::MatrixIndex;
use itertools::Itertools;
use num_traits::{Bounded, One, Zero};
use std::cmp::min;
use std::fmt::Debug;
use std::iter::{zip, Flatten};
use std::ops::{Index, IndexMut};

pub mod decompose;
pub mod index;
mod math;
mod ops;

mod swizzle;
mod util;

/** A 2D array of values which can be operated upon.

Matrices have a fixed size known at compile time */
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct Matrix<T, const M: usize, const N: usize>
where
    T: Copy,
{
    data: [[T; N]; M], // Row-Major order
}

/// An alias for a [Matrix] with a single column
pub type Vector<T, const N: usize> = Matrix<T, N, 1>;

// CONSTRUCTORS

// Default
impl<T: Copy + Default, const M: usize, const N: usize> Default for Matrix<T, M, N> {
    fn default() -> Self {
        Matrix::fill(T::default())
    }
}

// Zero
impl<T: Copy + Zero, const M: usize, const N: usize> Zero for Matrix<T, M, N> {
    fn zero() -> Self {
        Matrix::fill(T::zero())
    }

    fn is_zero(&self) -> bool {
        self.elements().all(|e| e.is_zero())
    }
}

// One
impl<T: Copy + One, const M: usize, const N: usize> One for Matrix<T, M, N> {
    fn one() -> Self {
        Matrix::fill(T::one())
    }
}

// min_value and max_value
// LowerBounded and UpperBounded are automatically implemented from this
impl<T: Copy + Bounded, const N: usize, const M: usize> Bounded for Matrix<T, N, M> {
    fn min_value() -> Self {
        Self::fill(T::min_value())
    }

    fn max_value() -> Self {
        Self::fill(T::max_value())
    }
}

// Identity
impl<T: Copy + Zero + One, const N: usize> Matrix<T, N, N> {
    /** Create an identity matrix, a square matrix where the diagonals are 1 and
    all other elements are 0.

    for example,

    $bbI = \[\[1,0,0],\[0,1,0],\[0,0,1]]$

    Matrix multiplication between a matrix and the identity matrix always results in itself

    $bbA xx bbI = bbA$

    # Examples
    ```
    # use vector_victor::Matrix;
    let i = Matrix::<i32,3,3>::identity();
    assert_eq!(i, Matrix::mat([[1, 0, 0],
                               [0, 1, 0],
                               [0, 0, 1]]))
    ```

    Note that the identity only exists for matrices that are square, so this doesnt work:
    ```compile_fail
    # use vector_victor::Matrix;
    let i = Matrix::<i32,4,2>::identity();
    ``` */
    #[must_use]
    pub fn identity() -> Self {
        let mut result = Self::zero();
        for i in 0..N {
            result[(i, i)] = T::one();
        }
        return result;
    }
}

// Matrix constructors
impl<T: Copy, const M: usize, const N: usize> Matrix<T, M, N> {
    /** Generate a new matrix from a 2D Array

    # Arguments

    * `data`: A 2D array of elements to copy into the new matrix

    # Examples

    ```
    # use vector_victor::Matrix;
    let a = Matrix::mat([[1,2,3,4];4]);
    ``` */
    #[must_use]
    pub fn mat(data: [[T; N]; M]) -> Self {
        assert!(M > 0, "Matrix must have at least 1 row");
        assert!(N > 0, "Matrix must have at least 1 column");
        Matrix::<T, M, N> { data }
    }

    /** Generate a new matrix from a single scalar

    # Arguments

    * `scalar`: Scalar value to copy into the new matrix.

    # Examples

    ```
    # use vector_victor::Matrix;
    // these are equivalent
    assert_eq!(Matrix::<i32,4,4>::fill(5), Matrix::mat([[5;4];4]))
    ``` */
    #[must_use]
    pub fn fill(scalar: T) -> Matrix<T, M, N> {
        assert!(M > 0, "Matrix must have at least 1 row");
        assert!(N > 0, "Matrix must have at least 1 column");
        Matrix::<T, M, N> {
            data: [[scalar; N]; M],
        }
    }

    /** Create a matrix from an iterator of vectors

    # Arguments

    * `iter`: iterator of vectors to copy into rows

    # Examples

    The following is another way of performing [`Matrix::transpose()`]
    ```
    # use vector_victor::Matrix;
    let my_matrix = Matrix::mat([[1, 2, 3],
                                 [4, 5, 6]]);

    let transpose : Matrix<_,3,2>= Matrix::from_rows(my_matrix.cols());

    assert_eq!(transpose, Matrix::mat([[1, 4],
                                       [2, 5],
                                       [3, 6]]))
    ``` */
    #[must_use]
    pub fn from_rows<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = Vector<T, N>>,
        Self: Default,
    {
        let mut result = Self::default();
        for (m, row) in iter.into_iter().enumerate().take(M) {
            result.set_row(m, &row)
        }
        result
    }

    /** Create a matrix from an iterator of vectors

    # Arguments

    * `iter`: iterator of vectors to copy into columns

    # Examples

    The following is another way of performing [`Matrix::transpose()`]
    ```
    # use vector_victor::Matrix;
    let my_matrix = Matrix::mat([[1, 2, 3],
                                 [4, 5, 6]]);

    let transpose : Matrix<_,3,2>= Matrix::from_cols(my_matrix.rows());

    assert_eq!(transpose, Matrix::mat([[1, 4],
                                       [2, 5],
                                       [3, 6]]))
    ``` */
    #[must_use]
    pub fn from_cols<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = Vector<T, M>>,
        Self: Default,
    {
        let mut result = Self::default();
        for (n, col) in iter.into_iter().enumerate().take(N) {
            result.set_col(n, &col)
        }
        result
    }
}

// Vector constructor
impl<T: Copy, const N: usize> Vector<T, N> {
    /** Create a vector from a 1D array.
    Note that vectors are always column vectors unless explicitly instantiated as row vectors

    # Examples
    ```
    # use vector_victor::{Matrix, Vector};
    // these are equivalent
    assert_eq!(Vector::vec([1,2,3,4]), Matrix::mat([[1],[2],[3],[4]]));
    ``` */
    pub fn vec(data: [T; N]) -> Self {
        assert!(N > 0, "Vector must have at least 1 element");
        return Vector::<T, N> {
            data: data.map(|e| [e]),
        };
    }
}

// ACCESSORS AND MUTATORS
impl<T: Copy, const M: usize, const N: usize> Matrix<T, M, N> {
    /** Returns an iterator over the elements of the matrix in row-major order.

    This is identical to the behavior of [`IntoIterator`](#associatedtype.IntoIter)

    # Examples
    ```
    # use vector_victor::Matrix;
    let my_matrix = Matrix::mat([[1, 2],
                                 [3, 4]]);

    itertools::assert_equal(my_matrix.elements(), [1,2,3,4].iter())
    ``` */
    #[must_use]
    pub fn elements<'s>(&'s self) -> impl Iterator<Item = &'s T> + 's {
        self.data.iter().flatten()
    }

    /** Returns a mutable iterator over the elements of the matrix in row-major order.

    # Examples
    ```
    # use vector_victor::Matrix;
    let mut my_matrix = Matrix::mat([[1, 2],
                                     [3, 4]]);

    for elem in my_matrix.elements_mut() {*elem += 2;}
    itertools::assert_equal(my_matrix.elements(), [3,4,5,6].iter())
    ``` */
    #[must_use]
    pub fn elements_mut<'s>(&'s mut self) -> impl Iterator<Item = &'s mut T> + 's {
        self.data.iter_mut().flatten()
    }

    /** returns an iterator over the elements along the diagonal of a matrix

    # Examples
    ```
    # use vector_victor::Matrix;
    let my_matrix = Matrix::mat([[1, 2, 3],
                                 [4, 5, 6],
                                 [7, 8, 9],
                                 [10,11,12]]);

    itertools::assert_equal(my_matrix.diagonals(), [1,5,9].iter())
    ``` */
    #[must_use]
    pub fn diagonals<'s>(&'s self) -> impl Iterator<Item = &'s T> + 's {
        (0..min(N, M)).map(|n| &self[(n, n)])
    }

    /** Returns an iterator over the elements directly below the diagonal of a matrix

    # Examples
    ```
    # use vector_victor::Matrix;
    let my_matrix = Matrix::mat([[1, 2, 3],
                                 [4, 5, 6],
                                 [7, 8, 9],
                                 [10,11,12]]);

    itertools::assert_equal(my_matrix.subdiagonals(), [4,8,12].iter());
    ``` */
    #[must_use]
    pub fn subdiagonals<'s>(&'s self) -> impl Iterator<Item = &'s T> + 's {
        (0..min(N, M - 1)).map(|n| &self[(n + 1, n)])
    }

    /** Returns a reference to the element at that position in the matrix, or `None` if out of bounds.

    [`Index`](#impl-Index%3CI%3E-for-Matrix%3CT,+M,+N%3E) behaves similarly,
    but will panic if the index is out of bounds instead of returning an option

    # Arguments

    * `index`: a 1D or 2D index into the matrix. See [MatrixIndex] for more information on matrix indexing.

    # Examples

    ```
    # use vector_victor::Matrix;
    let my_matrix = Matrix::mat([[1, 2],
                                 [3, 4]]);

    // element at index 2 is the same as the element at row 1, column 0.
    assert_eq!(my_matrix.get(2), my_matrix.get((1,0)));

    // my_matrix.get() is equivalent to my_matrix[],
    // but returns an Option instead of panicking
    assert_eq!(my_matrix.get(2), Some(&my_matrix[2]));

    // index 4 is out of range, so get(4) returns None.
    assert_eq!(my_matrix.get(4), None);
    ``` */
    #[inline]
    #[must_use]
    pub fn get(&self, index: impl MatrixIndex) -> Option<&T> {
        let (m, n) = index.to_2d(M, N)?;
        Some(&self.data[m][n])
    }

    /** Returns a mutable reference to the element at that position in the matrix,
    or `None` if out of bounds.

    [`IndexMut`](#impl-IndexMut%3CI%3E-for-Matrix%3CT,+M,+N%3E) behaves similarly,
    but will panic if the index is out of bounds instead of returning an option

    # Arguments

    * `index`: a 1D or 2D index into the matrix. See [MatrixIndex] for more information
    on matrix indexing.

    # Examples

    ```
    # use vector_victor::Matrix;
    let mut my_matrix = Matrix::mat([[1, 2],
                                     [3, 4]]);

    match my_matrix.get_mut(2) {
        Some(t) => *t = 5,
        None => panic!()};
    assert_eq!(my_matrix, Matrix::mat([[1,2],[5,4]]))
    ``` */
    #[inline]
    #[must_use]
    pub fn get_mut(&mut self, index: impl MatrixIndex) -> Option<&mut T> {
        let (m, n) = index.to_2d(M, N)?;
        Some(&mut self.data[m][n])
    }

    /** Returns a row of the matrix.

    # Panics

    Panics if row index `m` is out of bounds.

    # Examples

    ```
    # use vector_victor::{Matrix, Vector};
    let my_matrix = Matrix::mat([[1, 2],
                                 [3, 4]]);

    // row at index 1
    assert_eq!(my_matrix.row(1), Vector::vec([3,4]));
    ``` */
    #[inline]
    #[must_use]
    pub fn row(&self, m: usize) -> Vector<T, N> {
        assert!(
            m < M,
            "Row index {} out of bounds for {}×{} matrix",
            m,
            M,
            N
        );
        Vector::<T, N>::vec(self.data[m])
    }

    /** Sets a row of the matrix.

    # Panics

    Panics if row index `m` is out of bounds.

    # Examples

    ```
    # use vector_victor::{Matrix, Vector};
    let mut my_matrix = Matrix::mat([[1, 2],
                                     [3, 4]]);
    // row at index 1
    my_matrix.set_row(1, &Vector::vec([5,6]));
    assert_eq!(my_matrix, Matrix::mat([[1,2],[5,6]]));
    ``` */
    #[inline]
    pub fn set_row(&mut self, m: usize, val: &Vector<T, N>) {
        assert!(
            m < M,
            "Row index {} out of bounds for {}×{} matrix",
            m,
            M,
            N
        );
        for n in 0..N {
            self.data[m][n] = val.data[n][0];
        }
    }

    /** Returns a column of the matrix.

    # Panics

    Panics if column index `n` is out of bounds.

    # Examples

    ```
    # use vector_victor::{Matrix, Vector};
    let my_matrix = Matrix::mat([[1, 2],
                                 [3, 4]]);

    // column at index 1
    assert_eq!(my_matrix.col(1), Vector::vec([2,4]));
    ``` */
    #[inline]
    #[must_use]
    pub fn col(&self, n: usize) -> Vector<T, M> {
        assert!(
            n < N,
            "Column index {} out of bounds for {}×{} matrix",
            n,
            M,
            N
        );
        Vector::<T, M>::vec(self.data.map(|r| r[n]))
    }

    /** Sets a column of the matrix.

    # Panics

    Panics if column index `n` is out of bounds.

    # Examples

    ```
    # use vector_victor::{Matrix, Vector};
    let mut my_matrix = Matrix::mat([[1, 2],
                                     [3, 4]]);
    // column at index 1
    my_matrix.set_col(1, &Vector::vec([5,6]));
    assert_eq!(my_matrix, Matrix::mat([[1,5],[3,6]]));
    ``` */
    #[inline]
    pub fn set_col(&mut self, n: usize, val: &Vector<T, M>) {
        assert!(
            n < N,
            "Column index {} out of bounds for {}×{} matrix",
            n,
            M,
            N
        );

        for m in 0..M {
            self.data[m][n] = val.data[m][0];
        }
    }

    /// Returns an iterator over the rows of the matrix, returning them as column vectors.
    #[must_use]
    pub fn rows<'a>(&'a self) -> impl Iterator<Item = Vector<T, N>> + 'a {
        (0..M).map(|m| self.row(m))
    }

    /// Returns an iterator over the columns of the matrix, returning them as column vectors.
    #[must_use]
    pub fn cols<'a>(&'a self) -> impl Iterator<Item = Vector<T, M>> + 'a {
        (0..N).map(|n| self.col(n))
    }

    /** Interchange two rows

    # Panics

    Panics if row index `m1` or `m2` are out of bounds */
    pub fn pivot_row(&mut self, m1: usize, m2: usize) {
        let tmp = self.row(m2);
        self.set_row(m2, &self.row(m1));
        self.set_row(m1, &tmp);
    }

    /** Interchange two columns

    # Panics

    Panics if column index `n1` or `n2` are out of bounds */
    pub fn pivot_col(&mut self, n1: usize, n2: usize) {
        let tmp = self.col(n2);
        self.set_col(n2, &self.col(n1));
        self.set_col(n1, &tmp);
    }

    /** Apply a permutation matrix to the rows of a matrix

    # Arguments

    * `ms`: a [`Vector`] of [`usize`] of length P. Each entry is the index of the row that will
    appear in the result

    Returns: a P×N matrix

    # Panics

    Panics if any of the row indices in `ms` is out of bounds

    # Examples

    ```
    # use vector_victor::{Matrix, Vector};
    let my_matrix = Matrix::mat([[1, 2, 3],
                                 [4, 5, 6],
                                 [7, 8, 9]]);

    let permuted = my_matrix.permute_rows(&Vector::vec([1, 0, 2]));
    assert_eq!(permuted, Matrix::mat([[4, 5, 6],
                                      [1, 2, 3],
                                      [7, 8, 9]]))
    ``` */
    #[must_use]
    pub fn permute_rows<const P: usize>(&self, ms: &Vector<usize, P>) -> Matrix<T, P, N>
    where
        T: Default,
    {
        Matrix::<T, P, N>::from_rows(ms.elements().map(|&m| self.row(m)))
    }

    /** Apply a permutation matrix to the columns of a matrix

    # Arguments

    * `ns`: a [`Vector`] of [`usize`] of length P. Each entry is the index of the column that will
    appear in the result

    Returns: a P×N matrix

    # Panics

    Panics if any of the column indices in `ns` is out of bounds */
    #[must_use]
    pub fn permute_cols<const P: usize>(&self, ns: &Vector<usize, P>) -> Matrix<T, M, P>
    where
        T: Default,
    {
        Matrix::<T, M, P>::from_cols(ns.elements().map(|&n| self.col(n)))
    }

    /** Returns the transpose $M^T$ of the matrix, or the matrix flipped across its diagonal.

    # Examples
    ```
    # use vector_victor::Matrix;
    let my_matrix = Matrix::mat([[1, 2, 3],
                                 [4, 5, 6]]);

    assert_eq!(
        my_matrix.transpose(),
        Matrix::mat([[1, 4],
                     [2, 5],
                     [3, 6]]))
    ``` */
    pub fn transpose(&self) -> Matrix<T, N, M>
    where
        Matrix<T, N, M>: Default,
    {
        Matrix::<T, N, M>::from_rows(self.cols())
    }
}

// Index
impl<I, T, const M: usize, const N: usize> Index<I> for Matrix<T, M, N>
where
    I: MatrixIndex,
    T: Copy,
{
    type Output = T;

    #[inline(always)]
    fn index(&self, index: I) -> &Self::Output {
        self.get(index).expect(&*format!(
            "index {:?} out of range for {}×{} Matrix",
            index, M, N
        ))
    }
}

// IndexMut
impl<I, T, const M: usize, const N: usize> IndexMut<I> for Matrix<T, M, N>
where
    I: MatrixIndex,
    T: Copy,
{
    #[inline(always)]
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        self.get_mut(index).expect(&*format!(
            "index {:?} out of range for {}×{} Matrix",
            index, M, N
        ))
    }
}

// CONVERSIONS

// Convert from 2D Array (equivalent to new)
impl<T: Copy, const M: usize, const N: usize> From<[[T; N]; M]> for Matrix<T, M, N> {
    fn from(data: [[T; N]; M]) -> Self {
        Self::mat(data)
    }
}

// Convert from 1D Array (equivalent to vec)
impl<T: Copy, const M: usize> From<[T; M]> for Vector<T, M> {
    fn from(data: [T; M]) -> Self {
        Self::vec(data)
    }
}

// Convert from scalar (equivalent to fill)
impl<T: Copy, const M: usize, const N: usize> From<T> for Matrix<T, M, N> {
    fn from(scalar: T) -> Self {
        Self::fill(scalar)
    }
}

// Convert to 2D Array
impl<T: Copy + Debug, const M: usize, const N: usize> Into<[[T; N]; M]> for Matrix<T, M, N> {
    fn into(self) -> [[T; N]; M] {
        self.rows()
            .map(|row| row.into())
            .collect_vec()
            .try_into()
            .unwrap()
    }
}

// convert to 1D Array
impl<T: Copy + Debug, const M: usize> Into<[T; M]> for Vector<T, M> {
    fn into(self) -> [T; M] {
        self.elements().cloned().collect_vec().try_into().unwrap()
    }
}

// IntoIter
impl<T: Copy, const M: usize, const N: usize> IntoIterator for Matrix<T, M, N> {
    type Item = T;
    type IntoIter = Flatten<std::array::IntoIter<[T; N], M>>;

    fn into_iter(self) -> Self::IntoIter {
        self.data.into_iter().flatten()
    }
}

// FromIterator
impl<T: Copy, const M: usize, const N: usize> FromIterator<T> for Matrix<T, M, N>
where
    Self: Default,
{
    fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
        let mut result: Self = Default::default();
        for (l, r) in zip(result.elements_mut(), iter) {
            *l = r;
        }
        result
    }
}