Skip to content

Commit 98a5cb4

Browse files
committed
Add high-level API
1 parent 395e314 commit 98a5cb4

File tree

4 files changed

+70
-4
lines changed

4 files changed

+70
-4
lines changed

src/impl2/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ pub use self::solve::*;
1111

1212
use super::error::*;
1313

14-
use super::error::*;
15-
16-
pub trait LapackScalar: OperatorNorm_ + QR_ + SVD_ {}
17-
impl<A> LapackScalar for A where A: OperatorNorm_ + QR_ + SVD_ {}
14+
pub trait LapackScalar: OperatorNorm_ + QR_ + SVD_ + Solve_ {}
15+
impl<A> LapackScalar for A where A: OperatorNorm_ + QR_ + SVD_ + Solve_ {}
1816

1917
pub fn into_result<T>(info: i32, val: T) -> Result<T> {
2018
if info == 0 {

src/impl2/solve.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use super::into_result;
99

1010
pub type Pivot = Vec<i32>;
1111

12+
#[derive(Debug, Clone, Copy)]
1213
#[repr(u8)]
1314
pub enum Transpose {
1415
No = b'N',

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ pub mod impl2;
5151
pub mod qr;
5252
pub mod svd;
5353
pub mod opnorm;
54+
pub mod solve;
5455

5556
pub mod vector;
5657
pub mod matrix;

src/solve.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
2+
use ndarray::*;
3+
use super::layout::*;
4+
use super::error::*;
5+
use super::impl2::*;
6+
7+
pub use impl2::{Pivot, Transpose};
8+
9+
pub struct Factorized<S: Data> {
10+
pub a: ArrayBase<S, Ix2>,
11+
pub ipiv: Pivot,
12+
}
13+
14+
impl<A, S> Factorized<S>
15+
where A: LapackScalar,
16+
S: Data<Elem = A>
17+
{
18+
pub fn solve<Sb>(&self, t: Transpose, mut rhs: ArrayBase<Sb, Ix1>) -> Result<ArrayBase<Sb, Ix1>>
19+
where Sb: DataMut<Elem = A>
20+
{
21+
A::solve(self.a.layout()?,
22+
t,
23+
self.a.as_allocated()?,
24+
&self.ipiv,
25+
rhs.as_slice_mut().unwrap())?;
26+
Ok(rhs)
27+
}
28+
}
29+
30+
impl<A, S> Factorized<S>
31+
where A: LapackScalar,
32+
S: DataMut<Elem = A>
33+
{
34+
pub fn into_inverse(mut self) -> Result<ArrayBase<S, Ix2>> {
35+
A::inv(self.a.layout()?, self.a.as_allocated_mut()?, &self.ipiv)?;
36+
Ok(self.a)
37+
}
38+
}
39+
40+
pub trait Factorize<S: Data> {
41+
fn factorize(self) -> Result<Factorized<S>>;
42+
}
43+
44+
impl<A, S> Factorize<S> for ArrayBase<S, Ix2>
45+
where A: LapackScalar,
46+
S: DataMut<Elem = A>
47+
{
48+
fn factorize(mut self) -> Result<Factorized<S>> {
49+
let ipiv = A::lu(self.layout()?, self.as_allocated_mut()?)?;
50+
Ok(Factorized {
51+
a: self,
52+
ipiv: ipiv,
53+
})
54+
}
55+
}
56+
57+
impl<'a, A, S> Factorize<OwnedRepr<A>> for &'a ArrayBase<S, Ix2>
58+
where A: LapackScalar + Clone,
59+
S: Data<Elem = A>
60+
{
61+
fn factorize(self) -> Result<Factorized<OwnedRepr<A>>> {
62+
let mut a = self.to_owned();
63+
let ipiv = A::lu(a.layout()?, a.as_allocated_mut()?)?;
64+
Ok(Factorized { a: a, ipiv: ipiv })
65+
}
66+
}

0 commit comments

Comments
 (0)