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
//! Cholesky decomposition

use ndarray::*;
use num_traits::Zero;

use super::convert::*;
use super::error::*;
use super::layout::*;
use super::triangular::IntoTriangular;

use lapack_traits::LapackScalar;
pub use lapack_traits::UPLO;

pub trait Cholesky<K> {
    fn cholesky(self, UPLO) -> Result<K>;
}

impl<A, S> Cholesky<ArrayBase<S, Ix2>> for ArrayBase<S, Ix2>
where
    A: LapackScalar + Zero,
    S: DataMut<Elem = A>,
{
    fn cholesky(mut self, uplo: UPLO) -> Result<ArrayBase<S, Ix2>> {
        A::cholesky(self.square_layout()?, uplo, self.as_allocated_mut()?)?;
        Ok(self.into_triangular(uplo))
    }
}

impl<'a, A, S> Cholesky<&'a mut ArrayBase<S, Ix2>> for &'a mut ArrayBase<S, Ix2>
where
    A: LapackScalar + Zero,
    S: DataMut<Elem = A>,
{
    fn cholesky(mut self, uplo: UPLO) -> Result<&'a mut ArrayBase<S, Ix2>> {
        A::cholesky(self.square_layout()?, uplo, self.as_allocated_mut()?)?;
        Ok(self.into_triangular(uplo))
    }
}

impl<'a, A, Si, So> Cholesky<ArrayBase<So, Ix2>> for &'a ArrayBase<Si, Ix2>
where
    A: LapackScalar + Copy + Zero,
    Si: Data<Elem = A>,
    So: DataMut<Elem = A> + DataOwned,
{
    fn cholesky(self, uplo: UPLO) -> Result<ArrayBase<So, Ix2>> {
        let mut a = replicate(self);
        A::cholesky(a.square_layout()?, uplo, a.as_allocated_mut()?)?;
        Ok(a.into_triangular(uplo))
    }
}