opensrdk_linear_algebra/matrix/sp_hp/pp/
trf.rs

1use crate::matrix::MatrixError;
2use crate::number::c64;
3use crate::Number;
4use crate::SymmetricPackedMatrix;
5use lapack::{dpptrf, zpptrf};
6use serde::Deserialize;
7use serde::Serialize;
8
9#[derive(Clone, Debug, Default, PartialEq, Hash, Serialize, Deserialize)]
10pub struct PPTRF<T = f64>(pub SymmetricPackedMatrix<T>)
11where
12    T: Number;
13
14impl SymmetricPackedMatrix {
15    /// # Cholesky decomposition
16    /// for positive definite f64 matrix
17    ///
18    /// https://en.wikipedia.org/wiki/Cholesky_decomposition
19    ///
20    /// `A = L * L^T`
21    pub fn pptrf(self) -> Result<PPTRF, MatrixError> {
22        let n = self.dim;
23
24        let mut info = 0;
25        let mut slf = self;
26        let n = n as i32;
27
28        unsafe {
29            dpptrf('L' as u8, n, &mut slf.elems, &mut info);
30        }
31
32        match info {
33            0 => Ok(PPTRF(slf)),
34            _ => Err(MatrixError::LapackRoutineError {
35                routine: "dpptrf".to_owned(),
36                info,
37            }),
38        }
39    }
40}
41
42impl SymmetricPackedMatrix<c64> {
43    /// # Cholesky decomposition
44    /// for positive definite c64 matrix
45    ///
46    /// https://en.wikipedia.org/wiki/Cholesky_decomposition
47    ///
48    /// `A = L * L^*`
49    pub fn pptrf(self) -> Result<PPTRF<c64>, MatrixError> {
50        let n = self.dim;
51
52        let mut info = 0;
53        let mut slf = self;
54        let n = n as i32;
55
56        unsafe {
57            zpptrf('L' as u8, n, &mut slf.elems, &mut info);
58        }
59
60        match info {
61            0 => Ok(PPTRF::<c64>(slf)),
62            _ => Err(MatrixError::LapackRoutineError {
63                routine: "zpptrf".to_owned(),
64                info,
65            }),
66        }
67    }
68}