Skip to content

Commit

Permalink
Fix coordinate calculation in DMat::from_fn().
Browse files Browse the repository at this point in the history
The builder function `f` was receiving incorrect coordinates, often
involving a uint underflow.

Added a test case to verify the new behavior.
  • Loading branch information
Nathan Stien committed Jul 27, 2014
1 parent 97c2e71 commit ebe1ed1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
doc
lib
TODO
target/
2 changes: 1 addition & 1 deletion src/structs/dmat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl<N> DMat<N> {
DMat {
nrows: nrows,
ncols: ncols,
mij: Vec::from_fn(nrows * ncols, |i| { let m = i % ncols; f(m, m - i * ncols) })
mij: Vec::from_fn(nrows * ncols, |i| f(i % nrows, i / nrows))
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/tests/mat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,16 @@ fn test_decomp_qr_mat5() {
fn test_decomp_qr_mat6() {
test_decomp_qr_impl!(Mat6<f64>);
}


#[test]
fn test_from_fn() {
let actual: DMat<uint> = DMat::from_fn( 3, 4,
|i,j| 10*i + j);
let expected: DMat<uint> = DMat::from_row_vec(3, 4,
[0_0, 0_1, 0_2, 0_3,
1_0, 1_1, 1_2, 1_3,
2_0, 2_1, 2_2, 2_3 ]);

assert_eq!(actual, expected);
}

0 comments on commit ebe1ed1

Please sign in to comment.