From ebe1ed1f16033949d1a7c3c0d933ef50f63e18a9 Mon Sep 17 00:00:00 2001 From: Nathan Stien Date: Sat, 26 Jul 2014 19:03:37 -0500 Subject: [PATCH] Fix coordinate calculation in DMat::from_fn(). The builder function `f` was receiving incorrect coordinates, often involving a uint underflow. Added a test case to verify the new behavior. --- .gitignore | 1 + src/structs/dmat.rs | 2 +- src/tests/mat.rs | 13 +++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 2da46d5c..a3b337ba 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ doc lib TODO +target/ diff --git a/src/structs/dmat.rs b/src/structs/dmat.rs index 91d4df74..2982c184 100644 --- a/src/structs/dmat.rs +++ b/src/structs/dmat.rs @@ -139,7 +139,7 @@ impl DMat { 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)) } } diff --git a/src/tests/mat.rs b/src/tests/mat.rs index 699305ed..5a9da8e2 100644 --- a/src/tests/mat.rs +++ b/src/tests/mat.rs @@ -266,3 +266,16 @@ fn test_decomp_qr_mat5() { fn test_decomp_qr_mat6() { test_decomp_qr_impl!(Mat6); } + + +#[test] +fn test_from_fn() { + let actual: DMat = DMat::from_fn( 3, 4, + |i,j| 10*i + j); + let expected: DMat = 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); +}