Skip to content

Commit 7ef6931

Browse files
committed
Add Cholesky trait
1 parent e5e710b commit 7ef6931

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

src/cholesky.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
use ndarray::*;
3+
4+
use super::error::*;
5+
use super::layout::*;
6+
7+
use impl2::{LapackScalar, UPLO};
8+
9+
pub trait Cholesky<K> {
10+
fn cholesky(self, UPLO) -> Result<K>;
11+
}
12+
13+
impl<A, S> Cholesky<ArrayBase<S, Ix2>> for ArrayBase<S, Ix2>
14+
where A: LapackScalar,
15+
S: DataMut<Elem = A>
16+
{
17+
fn cholesky(mut self, uplo: UPLO) -> Result<ArrayBase<S, Ix2>> {
18+
A::cholesky(self.square_layout()?, uplo, self.as_allocated_mut()?)?;
19+
Ok(self)
20+
}
21+
}
22+
23+
impl<'a, A, S> Cholesky<&'a mut ArrayBase<S, Ix2>> for &'a mut ArrayBase<S, Ix2>
24+
where A: LapackScalar,
25+
S: DataMut<Elem = A>
26+
{
27+
fn cholesky(mut self, uplo: UPLO) -> Result<&'a mut ArrayBase<S, Ix2>> {
28+
A::cholesky(self.square_layout()?, uplo, self.as_allocated_mut()?)?;
29+
Ok(self)
30+
}
31+
}
32+
33+
impl<'a, A, Si, So> Cholesky<ArrayBase<So, Ix2>> for &'a ArrayBase<Si, Ix2>
34+
where A: LapackScalar + Copy,
35+
Si: Data<Elem = A>,
36+
So: DataMut<Elem = A> + DataOwned
37+
{
38+
fn cholesky(self, uplo: UPLO) -> Result<ArrayBase<So, Ix2>> {
39+
let mut a = replicate(self);
40+
A::cholesky(a.square_layout()?, uplo, a.as_allocated_mut()?)?;
41+
Ok(a)
42+
}
43+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ pub mod qr;
5252
pub mod svd;
5353
pub mod opnorm;
5454
pub mod solve;
55+
pub mod cholesky;
5556
pub mod eigh;
5657

5758
pub mod vector;

0 commit comments

Comments
 (0)