rust_linear_algebra/matrix/
inverse.rs

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
use super::identity::identity_mat;
use super::Matrix;
use crate::num::MinusOne;
use crate::num::One;
use std::fmt::Debug;
use std::ops::{Add, Div, Mul, Sub, SubAssign};

impl<K> Matrix<K>
where
    K: Debug
        + Copy
        + One
        + MinusOne
        + Default
        + PartialEq
        + Add<Output = K>
        + Sub<Output = K>
        + SubAssign<K>
        + Mul<Output = K>
        + Div<Output = K>,
{
    pub fn inverse(&self) -> Result<Matrix<K>, &str> {
        if self.cols() != self.rows() {
            return Err("Matrix must be a square");
        }
        let det = self.determinant();
        if det == K::default() {
            return Err("Matrix is singular and cannot be inverted");
        }
        let mut identity = identity_mat::<K>(self.cols(), self.rows());
        let _ = self.gaussian_elimination(None, Some(&mut identity));
        Ok(identity)
    }
}