From dcf7b8ad018c074db9586bf139fba600e76a6016 Mon Sep 17 00:00:00 2001 From: Vincent Barrielle Date: Sun, 11 May 2014 21:20:41 +0200 Subject: [PATCH] generic implementation of QR decomposition But static matrices can't use it yet, they need to implement the Row/Col slicing traits. --- src/linalg/decompositions.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/linalg/decompositions.rs b/src/linalg/decompositions.rs index 3cc1a0f5..6c28b30f 100644 --- a/src/linalg/decompositions.rs +++ b/src/linalg/decompositions.rs @@ -1,5 +1,4 @@ use std::num::{Zero, Float}; -use na::DMat; use traits::operations::Transpose; use traits::structure::{ColSlice, Eye, Indexable}; use traits::geometry::Norm; @@ -34,11 +33,14 @@ fn householder_matrix(m: &DMat) -> (DMat, DMat) { - let rows = m.nrows(); - let cols = m.ncols(); +pub fn decomp_qr + Norm, + Mat: Clone + Eye + ColSlice + Transpose + + Indexable<(uint, uint), N> + Mul> + (m: &Mat) -> (Mat, Mat) { + let (rows, cols) = m.shape(); assert!(rows >= cols); - let mut q : DMat = Eye::new_identity(rows); + let mut q : Mat = Eye::new_identity(rows); let mut r = m.clone(); let iterations = min(rows - 1, cols); @@ -57,7 +59,7 @@ pub fn decomp_qr(m: &DMat) -> (DMat, DMat) { v.unsafe_set(0, x - alpha); } let _ = v.normalize(); - let qk: DMat = householder_matrix(rows, 0, v); + let qk: Mat = householder_matrix(rows, 0, v); r = qk * r; q = q * Transpose::transpose_cpy(&qk); }