Skip to content

Commit 4674bce

Browse files
committed
Simplify Cholesky
1 parent 8197d0a commit 4674bce

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

src/cholesky.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,58 @@
11
//! Cholesky decomposition
22
33
use ndarray::*;
4-
use num_traits::Zero;
54

65
use super::convert::*;
76
use super::error::*;
87
use super::layout::*;
98
use super::triangular::IntoTriangular;
9+
use super::types::*;
1010

11-
use lapack_traits::LapackScalar;
1211
pub use lapack_traits::UPLO;
1312

14-
pub trait Cholesky<K> {
15-
fn cholesky(self, UPLO) -> Result<K>;
13+
pub trait Cholesky {
14+
type Output;
15+
fn cholesky(&self, UPLO) -> Result<Self::Output>;
1616
}
1717

18-
impl<A, S> Cholesky<ArrayBase<S, Ix2>> for ArrayBase<S, Ix2>
18+
pub trait CholeskyInto: Sized {
19+
fn cholesky_into(self, UPLO) -> Result<Self>;
20+
}
21+
22+
pub trait CholeskyMut {
23+
fn cholesky_mut(&mut self, UPLO) -> Result<&mut Self>;
24+
}
25+
26+
impl<A, S> CholeskyInto for ArrayBase<S, Ix2>
1927
where
20-
A: LapackScalar + Zero,
28+
A: Scalar,
2129
S: DataMut<Elem = A>,
2230
{
23-
fn cholesky(mut self, uplo: UPLO) -> Result<ArrayBase<S, Ix2>> {
31+
fn cholesky_into(mut self, uplo: UPLO) -> Result<Self> {
2432
A::cholesky(self.square_layout()?, uplo, self.as_allocated_mut()?)?;
2533
Ok(self.into_triangular(uplo))
2634
}
2735
}
2836

29-
impl<'a, A, S> Cholesky<&'a mut ArrayBase<S, Ix2>> for &'a mut ArrayBase<S, Ix2>
37+
impl<A, S> CholeskyMut for ArrayBase<S, Ix2>
3038
where
31-
A: LapackScalar + Zero,
39+
A: Scalar,
3240
S: DataMut<Elem = A>,
3341
{
34-
fn cholesky(mut self, uplo: UPLO) -> Result<&'a mut ArrayBase<S, Ix2>> {
42+
fn cholesky_mut(&mut self, uplo: UPLO) -> Result<&mut Self> {
3543
A::cholesky(self.square_layout()?, uplo, self.as_allocated_mut()?)?;
3644
Ok(self.into_triangular(uplo))
3745
}
3846
}
3947

40-
impl<'a, A, Si, So> Cholesky<ArrayBase<So, Ix2>> for &'a ArrayBase<Si, Ix2>
48+
impl<A, S> Cholesky for ArrayBase<S, Ix2>
4149
where
42-
A: LapackScalar + Copy + Zero,
43-
Si: Data<Elem = A>,
44-
So: DataMut<Elem = A> + DataOwned,
50+
A: Scalar,
51+
S: Data<Elem = A>,
4552
{
46-
fn cholesky(self, uplo: UPLO) -> Result<ArrayBase<So, Ix2>> {
53+
type Output = Array2<A>;
54+
55+
fn cholesky(&self, uplo: UPLO) -> Result<Self::Output> {
4756
let mut a = replicate(self);
4857
A::cholesky(a.square_layout()?, uplo, a.as_allocated_mut()?)?;
4958
Ok(a.into_triangular(uplo))

0 commit comments

Comments
 (0)