From f3b154dfe669067f7cd0205acdab5c4398f1a13b Mon Sep 17 00:00:00 2001 From: Davis King Date: Sat, 24 Nov 2012 16:20:10 -0500 Subject: [PATCH] Added some sparse_matrix_vector_multiply() unit tests. --- dlib/test/sparse_vector.cpp | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/dlib/test/sparse_vector.cpp b/dlib/test/sparse_vector.cpp index 037cb1d8d..0f124a56e 100644 --- a/dlib/test/sparse_vector.cpp +++ b/dlib/test/sparse_vector.cpp @@ -67,6 +67,50 @@ namespace // ---------------------------------------------------------------------------------------- + void test_sparse_matrix_vector_multiply1() + { + print_spinner(); + std::map sv; + sv[2] = 8; + sv[6] = 2.3; + + matrix v; + v = 0; + v(2) = 8; + v(6) = 2.3; + + + matrix r1, r2; + + r1 = gaussian_randm(4,10)*v; + r2 = sparse_matrix_vector_multiply(gaussian_randm(4,std::numeric_limits::max()),sv); + + DLIB_TEST(max(abs(r1-r2)) < 1e-15); + } + +// ---------------------------------------------------------------------------------------- + + void test_sparse_matrix_vector_multiply2() + { + std::vector > sv; + sv.push_back(make_pair(6, 1.42)); + sv.push_back(make_pair(3, 5)); + + matrix v; + v = 0; + v(3) = 5; + v(6) = 1.42; + + + matrix r1, r2; + + r1 = gaussian_randm(3,9)*v; + r2 = sparse_matrix_vector_multiply(gaussian_randm(3,std::numeric_limits::max()),sv); + + DLIB_TEST(max(abs(r1-r2)) < 1e-15); + } + +// ---------------------------------------------------------------------------------------- class sparse_vector_tester : public tester { @@ -194,6 +238,8 @@ namespace } test_sparse_matrix_vector_multiplies(); + test_sparse_matrix_vector_multiply1(); + test_sparse_matrix_vector_multiply2(); } };