matrix-42 0.1.0

A linear algebra library written in Rust with basic matrix and vector operations
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
use std::{
    f32::consts::PI,
    fmt::{Debug, Display},
    ops::{Add, AddAssign, Index, IndexMut, Mul, MulAssign, Sub, SubAssign},
};

use crate::{
    scalar::{MulAdd, Scalar},
    vector::{Dot, Vector},
    V,
};

#[derive(Clone)]
pub struct Matrix<K> {
    pub _d: Vec<K>,
    pub rows: usize,
    pub cols: usize,
}

pub trait Transpose<K> {
    fn transpose(&self) -> Matrix<K>;
}

#[macro_export]
macro_rules! M {
    ($values:expr) => {
        Matrix::from($values)
    };
}

impl<K: Scalar> Debug for Matrix<K> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str("[")?;
        for i in 0..self.rows {
            write!(f, "{:?}", &self[i])?;
            if i != self.rows - 1 {
                writeln!(f, ",")?;
            }
        }
        f.write_str("]")?;
        Ok(())
    }
}

impl<K: Scalar> Display for Matrix<K> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        f.write_str("[")?;
        for i in 0..self.rows {
            write!(f, "{:?}", &self[i])?;
            if i != self.rows - 1 {
                writeln!(f, ",")?;
            }
        }
        f.write_str("]")?;
        Ok(())
    }
}

impl<K: Clone, const N: usize, const D: usize> From<[[K; D]; N]> for Matrix<K> {
    fn from(value: [[K; D]; N]) -> Self {
        let mut vec = Vec::with_capacity(N * D);
        (0..N).for_each(|i| {
            for j in 0..D {
                vec.push(value[i][j].clone());
            }
        });

        Matrix {
            rows: N,
            cols: D,
            _d: vec,
        }
    }
}

impl<K> Index<usize> for Matrix<K> {
    type Output = [K];

    fn index(&self, index: usize) -> &[K] {
        let start = index * self.cols;
        &self._d[start..start + self.cols]
    }
}

impl<K> IndexMut<usize> for Matrix<K> {
    fn index_mut(&mut self, index: usize) -> &mut [K] {
        let start = index * self.cols;
        &mut self._d[start..start + self.cols]
    }
}

impl<K: Scalar> Add for Matrix<K> {
    type Output = Self;

    fn add(self, other: Self) -> Self::Output {
        assert_eq!(
            self.shape(),
            other.shape(),
            "matrices must be the same size"
        );

        let mut vec = Vec::with_capacity(self.rows * self.cols);
        for i in 0..self._d.len() {
            vec.push(self._d[i] + other._d[i]);
        }

        Matrix {
            _d: vec,
            cols: self.cols,
            rows: self.rows,
        }
    }
}

impl<K: Scalar> AddAssign<&Matrix<K>> for Matrix<K> {
    fn add_assign(&mut self, rhs: &Matrix<K>) {
        assert_eq!(self.shape(), rhs.shape(), "matrices must be the same size");

        for i in 0..self._d.len() {
            self._d[i] += rhs._d[i]
        }
    }
}

impl<K: Scalar> Sub for Matrix<K> {
    type Output = Self;

    fn sub(self, other: Self) -> Self::Output {
        assert_eq!(
            self.shape(),
            other.shape(),
            "matrices must be the same size"
        );

        let mut vec = Vec::with_capacity(self.rows * self.cols);
        for i in 0..self._d.len() {
            vec.push(self._d[i] - other._d[i]);
        }

        Matrix {
            _d: vec,
            cols: self.cols,
            rows: self.rows,
        }
    }
}

impl<K: Scalar> SubAssign<&Matrix<K>> for Matrix<K> {
    fn sub_assign(&mut self, rhs: &Matrix<K>) {
        assert_eq!(self.shape(), rhs.shape(), "matrices must be the same size");

        for i in 0..self._d.len() {
            self._d[i] -= rhs._d[i];
        }
    }
}

impl<K: Scalar + Mul<U, Output = K>, U: Scalar> Mul<U> for Matrix<K> {
    type Output = Matrix<K>;

    fn mul(self, a: U) -> Self::Output {
        let mut vec = Vec::with_capacity(self._d.len());
        for i in 0..self._d.len() {
            vec.push(self._d[i] * a);
        }

        Matrix {
            _d: vec,
            cols: self.cols,
            rows: self.rows,
        }
    }
}

impl<K: Scalar + MulAssign<U>, U: Scalar> MulAssign<&U> for Matrix<K> {
    fn mul_assign(&mut self, rhs: &U) {
        for i in 0..self._d.len() {
            self._d[i] *= *rhs;
        }
    }
}

impl<K: Scalar + Mul<U, Output = K> + MulAdd<U, K>, U: Scalar>
    MulAdd<U, Matrix<K>> for Matrix<K>
{
    fn mul_add(self, a: &U, b: &Matrix<K>) -> Self {
        assert!(self.shape() == b.shape(), "matrices must be the same size");

        let mut vec = Vec::with_capacity(self.rows * self.cols);

        for i in 0..self._d.len() {
            vec.push(self._d[i].mul_add(a, &b._d[i]));
        }

        Matrix {
            _d: vec,
            rows: self.rows,
            cols: self.cols,
        }
    }
}

impl<'a, K: Scalar> Mul<&Vector<K>> for &Matrix<K>
where
    [K]: Dot<K>,
{
    type Output = Vector<K>;

    fn mul(self, rhs: &Vector<K>) -> Self::Output {
        assert_eq!(
            self.shape().0,
            rhs.size(),
            "bad input for matrix and vector column multiplication"
        );

        let mut vec = Vec::with_capacity(rhs.size());
        for i in 0..rhs.size() {
            vec.push(self[i].dot(rhs));
        }
        V!(vec)
    }
}

impl<K: Scalar> Mul<&Matrix<K>> for &Matrix<K> {
    type Output = Matrix<K>;

    fn mul(self, rhs: &Matrix<K>) -> Self::Output {
        assert_eq!(
            self.cols, rhs.rows,
            "bad input for matrix and matrix multiplication"
        );

        let cols = rhs.cols;
        let rows = self.rows;
        let mut vec = Vec::with_capacity(rows * cols);

        for i in 0..rows {
            for j in 0..cols {
                let mut val = self[i][0] * rhs[0][j];
                for r in 1..self.cols {
                    val += self[i][r] * rhs[r][j];
                }
                vec.push(val);
            }
        }

        Matrix {
            _d: vec,
            rows,
            cols,
        }
    }
}

impl<K: Scalar> Matrix<K> {
    pub fn shape(&self) -> (usize, usize) {
        (self.rows, self.cols)
    }

    pub fn is_square(&self) -> bool {
        self.rows == self.cols
    }

    pub fn add(&mut self, v: &Matrix<K>) {
        *self += v;
    }

    pub fn sub(&mut self, v: &Matrix<K>) {
        *self -= v;
    }

    pub fn scl(&mut self, a: K) {
        *self *= &a;
    }

    pub fn mul_vec(&self, vec: &Vector<K>) -> Vector<K>
    where
        [K]: Dot<K>,
    {
        self * vec
    }

    pub fn mul_mat(&self, mat: &Matrix<K>) -> Matrix<K> {
        self * mat
    }

    pub fn trace(&self) -> Option<K> {
        assert!(self.is_square(), "matrix must be squared");

        match self.rows {
            0 => None,
            _ => Some((0..self.rows).map(|i| self[i][i]).sum()),
        }
    }

    pub fn row_echelon(&self) -> Matrix<K> {
        let mut ret = self.clone();
        ret.row_echelon_mut();
        ret
    }

    pub fn row_echelon_mut(&mut self) -> &mut Self {
        let mut col: usize = 0;

        for r in 0..self.rows {
            let mut p = None;

            for j in col..self.cols {
                for i in r..self.rows {
                    if self[i][j].is_non_zero() {
                        p = Some((i, j));
                        break;
                    }
                }

                if p.is_some() {
                    break;
                }
            }

            if p.is_none() {
                break;
            }
            let cur = p.unwrap();
            col = cur.1;
            if r != cur.0 {
                for c in col..self.cols {
                    let tmp = self[r][c];
                    self[r][c] = self[cur.0][c];
                    self[cur.0][c] = tmp;
                }
            }

            let v = self[r][col];

            self[r][col] = K::one();
            for i in col + 1..self.cols {
                self[r][i] /= v;
            }

            for i in 0..self.rows {
                if i == r {
                    continue;
                }
                let cur = self[i][col];
                self[i][col] = K::default();
                for j in col + 1..self.cols {
                    let x = self[r][j];
                    self[i][j] -= x * cur;
                }
            }

            col += 1;
        }

        self
    }

    pub fn determinant(&self) -> K {
        assert!(self.is_square(), "matrix must be squared");

        fn det<K: Scalar>(
            mat: &[K],
            row_size: usize,
            cur: usize,
            skip_cols: &[bool],
        ) -> K {
            let mut cols = Vec::with_capacity(skip_cols.len());

            for (col, skip) in skip_cols.iter().enumerate() {
                if !*skip {
                    cols.push(col);
                }
            }

            match cur {
                0 => K::default(),
                1 => mat[0],
                2 => {
                    mat[cols[0]] * mat[row_size + cols[1]]
                        - mat[cols[1]] * mat[row_size + cols[0]]
                }
                _ => {
                    let mut result = K::default();
                    for (i, col) in cols.iter().enumerate() {
                        let mut m_col = skip_cols.to_vec();
                        m_col[*col] = true;
                        let ret = mat[*col]
                            * det(&mat[row_size..], row_size, cur - 1, &m_col);
                        result += if i % 2 == 1 { -ret } else { ret };
                        m_col[*col] = false;
                    }
                    result
                }
            }
        }

        det(
            self._d.as_slice(),
            self.rows,
            self.rows,
            &vec![false; self.cols],
        )
    }

    pub fn inverse(&self) -> Result<Matrix<K>, &'static str> {
        assert!(self.is_square(), "matrix must be squared");

        assert_ne!(self.determinant(), K::default(), "matrix must be singular");

        let mut aug_m = Matrix {
            _d: vec![K::default(); self.rows * self.cols * 2],
            cols: self.cols * 2,
            rows: self.rows,
        };

        for row in 0..self.rows {
            for col in 0..self.cols {
                aug_m._d[row * (self.cols * 2) + col] = self[row][col];
            }
            aug_m._d[row * (self.cols * 2) + self.cols + row] = K::one();
        }

        aug_m.row_echelon_mut();

        let mut vec = Vec::with_capacity(self.rows * self.cols);

        for row in aug_m._d.chunks(self.cols * 2) {
            for &v in row.iter().skip(self.cols) {
                vec.push(v);
            }
        }

        Ok(Matrix {
            _d: vec,
            cols: self.cols,
            rows: self.rows,
        })
    }

    pub fn rank(&self) -> usize {
        let mat = self.row_echelon();
        let mut rank = 0;

        let mut cur = 0;
        for row in mat._d.chunks(self.cols) {
            for (j, v) in row.iter().enumerate().skip(cur) {
                if v.is_non_zero() {
                    cur = j + 1;
                    rank += 1;
                    break;
                }
            }
        }

        rank
    }

    pub fn is_independent(&self) -> bool {
        self.rank() == self.rows
    }

    pub fn row_space(&self) -> Matrix<K> {
        let mat = self.row_echelon();
        let mut vec = vec![];

        let mut cur = 0;
        let mut rank = 0;
        for row in mat._d.chunks(self.cols) {
            for (j, v) in row.iter().enumerate().skip(cur) {
                if *v != K::default() {
                    vec.copy_from_slice(row);
                    cur = j + 1;
                    rank += 1;
                    break;
                }
            }
        }

        Matrix {
            cols: self.cols,
            rows: rank,
            _d: vec,
        }
    }

    pub fn col_space(&self) -> Matrix<K> {
        let mat = self.row_echelon();
        let mut vec = vec![];

        let mut cur = 0;
        let mut rank = 0;
        for row in mat._d.chunks(self.cols) {
            for (j, v) in row.iter().enumerate().skip(cur) {
                if *v != K::default() {
                    for r in self._d.chunks(self.cols) {
                        vec.push(r[cur])
                    }
                    cur = j + 1;
                    rank += 1;
                    break;
                }
            }
        }

        Matrix {
            cols: self.rows,
            rows: rank,
            _d: vec,
        }
    }
}

pub fn projection(fov: f32, ratio: f32, n: f32, f: f32) -> Matrix<f32> {
    // let Ps the projection of P on the image plane in 3D space.
    // using the similar triangle rule:
    //  (Ps)x = near * Px / -Pz
    //  (Ps)y = near * Py / -Pz
    // (-Pz is negative because camera is in opposite side)

    // (Ps)w
    // we start by deriving the last row of the matrix,
    // to help us convert later the homogeneous point [x, y, z, w]
    // to cartesian point [x/w, y/w, z/w]
    // since camera is in the opposite side, (Ps)w = -Pz
    // therefore, m32 -1

    // (Ps)x
    // left <= (Ps)x <= right
    // we derive the value where the range is [-1, 1] in NDC space
    // -1 <= 2 * n * Px / (-Pz * (r - l)) - (r + l) / (r - l) <= 1
    // since (Ps)x will be divided at the end of the process by -Pz
    // when we convert Ps from homogeneous to cartesian coordinates
    // to have an identical formula we must assign these factors
    // m00 (2*n/(r-l) and m02 ((r+l)/(r-l)) to the 1st and 3rd of (Ps)x

    // (Ps)y
    // similarly, we drive the value (Ps)y to be in [-1, 1] in NDC space
    // we subtitute m11 (2 * n / (top - bottom)) and m12 ((top + right) / (top - bottom))

    // (Ps)z
    // since x and y of P don't depend on getting the z coordinate of Ps
    // we are left with (Ps)z = (A * Pz + B * Pw) / -Pz (Pw is always 1)
    // we derive A and B when (Ps)z is in range [0, 1]
    // and subtitute a system of two eq -n*A+B=0 and -f*A+B=f
    // we have A = m22 = -f/(f-n) and B = m23 = -(f*n)/(f-n)

    // using trigonometry, we can express:
    // tan(fov / 2) = opposite / adjacent = BC / AB = top / near
    // top = tan(fov / 2) * near
    // right = top * aspect_ratio
    let tan = ((fov / 2.) * (PI / 180.)).tan();
    let t = n * tan;
    let r = t * ratio;
    let (l, b) = (-r, -t);

    M!([
        [2. * n / (r - l), 0., (r + l) / (r - l), 0.], // (Ps)x
        [0., 2. * n / (t - b), (t + b) / (t - b), 0.], // (Ps)y
        [0., 0., f / (n - f), (n * f) / (n - f)],      // (Ps)z
        [0., 0., -1., 0.],                             // (Ps)w
    ])
}