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 crate::{
    matrix::{operations::identity::identity, Matrix},
    types::{Square, Standard},
};
use lapack::dgesv;

impl Matrix<Square> {
    /// # Inverse
    /// for Square Matrix
    pub fn inv(mut self) -> Result<Matrix, String> {
        let mut solution_matrix = identity(self.rows).transmute::<Standard>();

        let mut ipiv = vec![0; self.rows];
        let mut info = 0;

        unsafe {
            dgesv(
                self.rows as i32,
                self.rows as i32,
                &mut self.elements,
                self.rows as i32,
                &mut ipiv,
                &mut solution_matrix.elements,
                self.rows as i32,
                &mut info,
            );
        }

        match info {
            0 => Ok(solution_matrix),
            i => Err(i.to_string()),
        }
    }
}