2014-05-10 04:14:37 +08:00
|
|
|
|
use std::num::{Zero, Float};
|
2014-05-10 05:05:23 +08:00
|
|
|
|
use traits::operations::Transpose;
|
2014-05-12 01:46:04 +08:00
|
|
|
|
use traits::structure::{ColSlice, Eye, Indexable};
|
2014-05-10 04:14:37 +08:00
|
|
|
|
use traits::geometry::Norm;
|
|
|
|
|
use std::cmp::min;
|
|
|
|
|
|
2014-05-12 01:46:04 +08:00
|
|
|
|
/// Get the householder matrix corresponding to a reflexion to the hyperplane
|
|
|
|
|
/// defined by `vec̀ . It can be a reflexion contained in a subspace.
|
|
|
|
|
///
|
|
|
|
|
/// # Arguments
|
|
|
|
|
/// * `dim` - the dimension of the space the resulting matrix operates in
|
|
|
|
|
/// * `start` - the starting dimension of the subspace of the reflexion
|
|
|
|
|
/// * `vec` - the vector defining the reflection.
|
2014-05-13 03:54:59 +08:00
|
|
|
|
pub fn householder_matrix<N: Float,
|
|
|
|
|
M: Eye + Indexable<(uint, uint), N>,
|
2014-05-12 01:46:04 +08:00
|
|
|
|
V: Indexable<uint, N>>
|
2014-05-13 03:54:59 +08:00
|
|
|
|
(dim: uint, start: uint, vec: V) -> M {
|
|
|
|
|
let mut qk : M = Eye::new_identity(dim);
|
2014-05-12 01:46:04 +08:00
|
|
|
|
let stop = start + vec.shape();
|
|
|
|
|
assert!(stop <= dim);
|
|
|
|
|
for j in range(start, stop) {
|
|
|
|
|
for i in range(start, stop) {
|
|
|
|
|
unsafe {
|
|
|
|
|
let vv = vec.unsafe_at(i) * vec.unsafe_at(j);
|
|
|
|
|
let qkij = qk.unsafe_at((i, j));
|
|
|
|
|
qk.unsafe_set((i, j), qkij - vv - vv);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
qk
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-10 04:14:37 +08:00
|
|
|
|
/// QR decomposition using Householder reflections
|
|
|
|
|
/// # Arguments
|
2014-05-10 18:48:25 +08:00
|
|
|
|
/// * `m` - matrix to decompose
|
2014-05-12 03:20:41 +08:00
|
|
|
|
pub fn decomp_qr<N: Float,
|
|
|
|
|
V: Indexable<uint, N> + Norm<N>,
|
2014-05-13 03:54:59 +08:00
|
|
|
|
M: Clone + Eye + ColSlice<V> + Transpose
|
|
|
|
|
+ Indexable<(uint, uint), N> + Mul<M, M>>
|
|
|
|
|
(m: &M) -> (M, M) {
|
2014-05-12 03:20:41 +08:00
|
|
|
|
let (rows, cols) = m.shape();
|
2014-05-10 04:14:37 +08:00
|
|
|
|
assert!(rows >= cols);
|
2014-05-13 03:54:59 +08:00
|
|
|
|
let mut q : M = Eye::new_identity(rows);
|
2014-05-10 04:14:37 +08:00
|
|
|
|
let mut r = m.clone();
|
|
|
|
|
|
2014-05-10 18:48:25 +08:00
|
|
|
|
let iterations = min(rows - 1, cols);
|
2014-05-10 04:14:37 +08:00
|
|
|
|
|
|
|
|
|
for ite in range(0u, iterations) {
|
|
|
|
|
let mut v = r.col_slice(ite, ite, rows);
|
|
|
|
|
let alpha =
|
2014-05-12 01:46:04 +08:00
|
|
|
|
if unsafe { v.unsafe_at(ite) } >= Zero::zero() {
|
2014-05-10 04:14:37 +08:00
|
|
|
|
-Norm::norm(&v)
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Norm::norm(&v)
|
|
|
|
|
};
|
|
|
|
|
unsafe {
|
2014-05-12 01:46:04 +08:00
|
|
|
|
let x = v.unsafe_at(0);
|
|
|
|
|
v.unsafe_set(0, x - alpha);
|
2014-05-10 04:14:37 +08:00
|
|
|
|
}
|
|
|
|
|
let _ = v.normalize();
|
2014-05-13 03:54:59 +08:00
|
|
|
|
let qk: M = householder_matrix(rows, 0, v);
|
2014-05-10 04:14:37 +08:00
|
|
|
|
r = qk * r;
|
|
|
|
|
q = q * Transpose::transpose_cpy(&qk);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(q, r)
|
|
|
|
|
}
|
|
|
|
|
|