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::Matrix;
use lapack::dposv;

impl Matrix {
    /// # Inverse
    /// for positive definite matrix
    pub fn poinv(self) -> Result<Matrix, String> {
        if self.rows != self.columns {
            return Err("dimension mismatch".to_owned());
        }

        let mut slf = self;
        let mut solution_matrix = Matrix::identity(slf.rows);
        let mut info = 0;

        unsafe {
            dposv(
                'U' as u8,
                slf.rows as i32,
                slf.rows as i32,
                &mut slf.elements,
                slf.rows as i32,
                &mut solution_matrix.elements,
                slf.rows as i32,
                &mut info,
            );
        }

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