Skip to content

Commit

Permalink
Rewrite Cholesky in terms of dot()
Browse files Browse the repository at this point in the history
Benchmarks show significant performance gains.
  • Loading branch information
Andlon committed Feb 11, 2017
1 parent 56f961a commit 0e34586
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/matrix/decomposition/cholesky.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use matrix::{Matrix, BaseMatrix};
use error::{Error, ErrorKind};
use matrix::decomposition::Decomposition;
use utils::dot;

use std::any::Any;

Expand All @@ -14,7 +15,7 @@ pub struct Cholesky<T> {

impl<T> Cholesky<T> where T: Float {
/// TODO
fn decompose(matrix: Matrix<T>) -> Result<Self, Error> {
pub fn decompose(matrix: Matrix<T>) -> Result<Self, Error> {
assert!(matrix.rows() == matrix.cols(),
"Matrix must be square for Cholesky decomposition.");
let n = matrix.rows();
Expand All @@ -36,10 +37,17 @@ impl<T> Cholesky<T> where T: Float {
// Resolve each submatrix (j .. n, j .. n)
for j in 0 .. n {
if j > 0 {
// This is essentially a GAXPY operation y = y - Bx
// where B is the [j .. n, 0 .. j] submatrix of A,
// x is the [ j, 0 .. j ] submatrix of A,
// and y is the [ j .. n, j ] submatrix of A
for k in j .. n {
for l in 0 .. j {
a[[k, j]] = a[[k, j]] - a[[k, l]] * a[[j, l]];
}
let kj_dot = {
let j_row = a.row(j).raw_slice();
let k_row = a.row(k).raw_slice();
dot(&k_row[0 .. j], &j_row[0 .. j])
};
a[[k, j]] = a[[k, j]] - kj_dot;
}
}

Expand Down

0 comments on commit 0e34586

Please sign in to comment.