Skip to content

Commit b74552e

Browse files
authored
Merge pull request #343 from rust-ndarray/lax-qr-strict
Merge `QR_` to `Lapack` trait, add strict memory management API
2 parents 0b133cf + f0b2c86 commit b74552e

File tree

2 files changed

+191
-152
lines changed

2 files changed

+191
-152
lines changed

lax/src/lib.rs

+33-3
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ pub mod layout;
9090
pub mod eig;
9191
pub mod eigh;
9292
pub mod eigh_generalized;
93+
pub mod qr;
9394

9495
mod alloc;
9596
mod cholesky;
9697
mod least_squares;
9798
mod opnorm;
98-
mod qr;
9999
mod rcond;
100100
mod solve;
101101
mod solveh;
@@ -108,7 +108,6 @@ pub use self::cholesky::*;
108108
pub use self::flags::*;
109109
pub use self::least_squares::*;
110110
pub use self::opnorm::*;
111-
pub use self::qr::*;
112111
pub use self::rcond::*;
113112
pub use self::solve::*;
114113
pub use self::solveh::*;
@@ -126,7 +125,6 @@ pub type Pivot = Vec<i32>;
126125
/// Trait for primitive types which implements LAPACK subroutines
127126
pub trait Lapack:
128127
OperatorNorm_
129-
+ QR_
130128
+ SVD_
131129
+ SVDDC_
132130
+ Solve_
@@ -160,6 +158,18 @@ pub trait Lapack:
160158
a: &mut [Self],
161159
b: &mut [Self],
162160
) -> Result<Vec<Self::Real>>;
161+
162+
/// Execute Householder reflection as the first step of QR-decomposition
163+
///
164+
/// For C-continuous array,
165+
/// this will call LQ-decomposition of the transposed matrix $ A^T = LQ^T $
166+
fn householder(l: MatrixLayout, a: &mut [Self]) -> Result<Vec<Self>>;
167+
168+
/// Reconstruct Q-matrix from Householder-reflectors
169+
fn q(l: MatrixLayout, a: &mut [Self], tau: &[Self]) -> Result<()>;
170+
171+
/// Execute QR-decomposition at once
172+
fn qr(l: MatrixLayout, a: &mut [Self]) -> Result<Vec<Self>>;
163173
}
164174

165175
macro_rules! impl_lapack {
@@ -198,6 +208,26 @@ macro_rules! impl_lapack {
198208
let work = EighGeneralizedWork::<$s>::new(calc_eigenvec, layout)?;
199209
work.eval(uplo, a, b)
200210
}
211+
212+
fn householder(l: MatrixLayout, a: &mut [Self]) -> Result<Vec<Self>> {
213+
use qr::*;
214+
let work = HouseholderWork::<$s>::new(l)?;
215+
work.eval(a)
216+
}
217+
218+
fn q(l: MatrixLayout, a: &mut [Self], tau: &[Self]) -> Result<()> {
219+
use qr::*;
220+
let mut work = QWork::<$s>::new(l)?;
221+
work.calc(a, tau)?;
222+
Ok(())
223+
}
224+
225+
fn qr(l: MatrixLayout, a: &mut [Self]) -> Result<Vec<Self>> {
226+
let tau = Self::householder(l, a)?;
227+
let r = Vec::from(&*a);
228+
Self::q(l, a, &tau)?;
229+
Ok(r)
230+
}
201231
}
202232
};
203233
}

0 commit comments

Comments
 (0)