diff options
Diffstat (limited to 'libs/numeric/ublas/test/test_assignment.cpp')
-rw-r--r-- | libs/numeric/ublas/test/test_assignment.cpp | 149 |
1 files changed, 58 insertions, 91 deletions
diff --git a/libs/numeric/ublas/test/test_assignment.cpp b/libs/numeric/ublas/test/test_assignment.cpp index 894f4512d..c402ac2f8 100644 --- a/libs/numeric/ublas/test/test_assignment.cpp +++ b/libs/numeric/ublas/test/test_assignment.cpp @@ -13,77 +13,49 @@ #include <boost/numeric/ublas/matrix_sparse.hpp> #include <boost/numeric/ublas/io.hpp> #include <boost/numeric/ublas/matrix.hpp> -#include "libs/numeric/ublas/test/utils.hpp" #include <boost/timer.hpp> #include <ctime> +#include "common/testhelper.hpp" +#include "utils.hpp" using namespace boost::numeric::ublas; -namespace tans { -template <class AE> -typename AE::value_type mean_square(const matrix_expression<AE> &me) { - typename AE::value_type s(0); - typename AE::size_type i, j; - for (i=0; i!= me().size1(); i++) { - for (j=0; j!= me().size2(); j++) { - s+= scalar_traits<typename AE::value_type>::type_abs(me()(i,j)); - } - } - return s/me().size1()*me().size2(); -} - - -template <class AE> -typename AE::value_type mean_square(const vector_expression<AE> &ve) { - // We could have use norm2 here, but ublas' ABS does not support unsigned types. - typename AE::value_type s(0); - typename AE::size_type i; - for (i=0; i!= ve().size(); i++) { - s+=scalar_traits<typename AE::value_type>::type_abs(ve()(i)); - } - return s/ve().size(); -} -const double TOL=0.0; - -} - template <class V> bool test_vector() { bool pass = true; - using namespace tans; V a(3), ra(3); a <<= 1, 2, 3; ra(0) = 1; ra(1) = 2; ra(2) = 3; - pass &= (mean_square(a-ra)<=TOL); + pass &= compare_to(a, ra); V b(7), rb(7); b<<= a, 10, a; rb(0) = 1; rb(1) = 2; rb(2) = 3; rb(3)=10, rb(4)= 1; rb(5)=2; rb(6)=3; - pass &= (mean_square(b-rb)<=TOL); + pass &= compare_to(b, rb); { V c(6), rc(6); c <<= 1, move(2), 3 ,4, 5, move(-5), 10, 10; rc(0) = 1; rc(1) = 10; rc(2) = 10; rc(3) = 3; rc(4) = 4; rc(5) = 5; - pass &= (mean_square(c-rc)<=TOL); + pass &= compare_to(c, rc); V d(6), rd(6); d <<= 1, move_to(3), 3 ,4, 5, move_to(1), 10, 10; rd(0) = 1; rd(1) = 10; rd(2) = 10; rd(3) = 3; rd(4) = 4; rd(5) = 5; - pass &= (mean_square(d-rd)<=TOL); + pass &= compare_to(d, rd); } { V c(6), rc(6); c <<= 1, move<2>(), 3 ,4, 5, move<-5>(), 10, 10; rc(0) = 1; rc(1) = 10; rc(2) = 10; rc(3) = 3; rc(4) = 4; rc(5) = 5; - pass &= (mean_square(c-rc)<=TOL); + pass &= compare_to(c, rc); V d(6), rd(6); d <<= 1, move_to<3>(), 3 ,4, 5, move_to<1>(), 10, 10; rd(0) = 1; rd(1) = 10; rd(2) = 10; rd(3) = 3; rd(4) = 4; rd(5) = 5; - pass &= (mean_square(d-rd)<=TOL); + pass &= compare_to(d, rd); } @@ -93,7 +65,7 @@ bool test_vector() { V fa(3); fa<<= 1, 2, 3; f <<= fill_policy::index_plus_assign(), fa; rf <<= 6,7,8, 5, 5, 5; - pass &= (mean_square(f-rf)<=TOL); + pass &= compare_to(f, rf); } { @@ -102,7 +74,7 @@ bool test_vector() { V fa(3); fa<<= 1, 2, 3; f <<= fill_policy::index_minus_assign(), fa; rf <<= 4,3,2, 5, 5, 5; - pass &= (mean_square(f-rf)<=TOL); + pass &= compare_to(f, rf); } return pass; @@ -111,32 +83,31 @@ bool test_vector() { template <class V> bool test_vector_sparse_push_back() { bool pass = true; - using namespace tans; V a(3), ra(3); a <<= fill_policy::sparse_push_back(), 1, 2, 3; ra(0) = 1; ra(1) = 2; ra(2) = 3; - pass &= (mean_square(a-ra)<=TOL); + pass &= compare_to(a, ra); V b(7), rb(7); b<<= fill_policy::sparse_push_back(), a, 10, a; rb(0) = 1; rb(1) = 2; rb(2) = 3; rb(3)=10, rb(4)= 1; rb(5)=2; rb(6)=3; - pass &= (mean_square(b-rb)<=TOL); + pass &= compare_to(b, rb); V c(6), rc(6); c <<= fill_policy::sparse_push_back(), 1, move(2), 3 ,4, 5; // Move back (i.e. negative is dangerous for push_back) rc(0) = 1; rc(1) = 0; rc(2) = 0; rc(3) = 3; rc(4) = 4; rc(5) = 5; - pass &= (mean_square(c-rc)<=TOL); + pass &= compare_to(c, rc); V d(6), rd(6); d <<= fill_policy::sparse_push_back(), 1, move_to(3), 3 ,4, 5; // Move back (i.e. before current index is dangerous for push_back) rd(0) = 1; rd(1) = 0; rd(2) = 0; rd(3) = 3; rd(4) = 4; rd(5) = 5; - pass &= (mean_square(d-rd)<=TOL); + pass &= compare_to(d, rd); V e(6), re(6); e <<= fill_policy::sparse_push_back(), 1, move_to(3), 3 ,4, 5, fill_policy::sparse_insert(), move_to(1), 10, 10; // If you want to move back, use this re(0) = 1; re(1) = 10; re(2) = 10; re(3) = 3; re(4) = 4; re(5) = 5; - pass &= (mean_square(e-re)<=TOL); + pass &= compare_to(e, re); return pass; } @@ -145,28 +116,27 @@ bool test_vector_sparse_push_back() { template <class V> bool test_vector_sparse_insert() { bool pass = true; - using namespace tans; V a(3), ra(3); a <<= fill_policy::sparse_insert(), 1, 2, 3; ra(0) = 1; ra(1) = 2; ra(2) = 3; - pass &= (mean_square(a-ra)<=TOL); + pass &= compare_to(a, ra); V b(7), rb(7); b<<= fill_policy::sparse_insert(), a, 10, a; rb(0) = 1; rb(1) = 2; rb(2) = 3; rb(3)=10, rb(4)= 1; rb(5)=2; rb(6)=3; - pass &= (mean_square(b-rb)<=TOL); + pass &= compare_to(b, rb); V c(6), rc(6); c <<= fill_policy::sparse_insert(), 1, move(2), 3 ,4, 5, move(-5), 10, 10; // Move back (i.e. negative is dangerous for sparse) rc(0) = 1; rc(1) = 10; rc(2) = 10; rc(3) = 3; rc(4) = 4; rc(5) = 5; - pass &= (mean_square(c-rc)<=TOL); + pass &= compare_to(c, rc); V d(6), rd(6); d <<= fill_policy::sparse_insert(), 1, move_to(3), 3 ,4, 5, move_to(1), 10, 10; // Move back (i.e.before is dangerous for sparse) rd(0) = 1; rd(1) = 10; rd(2) = 10; rd(3) = 3; rd(4) = 4; rd(5) = 5; - pass &= (mean_square(d-rd)<=TOL); + pass &= compare_to(d, rd); return pass; @@ -176,14 +146,13 @@ bool test_vector_sparse_insert() { template <class V> bool test_matrix() { bool pass = true; - using namespace tans; V A(3,3), RA(3,3); A <<= 1, 2, 3, 4, 5, 6, 7, 8, 9; RA(0,0)= 1; RA(0,1)=2; RA(0,2)=3; RA(1,0)= 4; RA(1,1)=5; RA(1,2)=6; RA(2,0)= 7; RA(2,1)=8; RA(2,2)=9; - pass &= (mean_square(A-RA)<=TOL); + pass &= compare_to(A, RA); { V B(3,3), RB(3,3); @@ -191,7 +160,7 @@ bool test_matrix() { b<<= 4,5,6; B<<= 1, 2, 3, b, 7, project(b, range(1,3)); RB<<=1, 2, 3, 4, 5, 6, 7, 5, 6; // If the first worked we can now probably use it. - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -200,7 +169,7 @@ bool test_matrix() { b<<= 4,5,6; B<<= move(1,0), b, move_to(0,0), 1, 2, 3, move(1,0), 7, project(b, range(1,3)); RB<<=1, 2, 3, 4, 5, 6, 7, 5, 6; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -209,7 +178,7 @@ bool test_matrix() { b<<= 1, 2, 3, 4, 5, 6, 7, 8, 9; B<<=b; RB<<=1, 2, 3, 4, 5, 6, 7, 8, 9; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -223,7 +192,7 @@ bool test_matrix() { 4,5,4,5, 2,3,2,3, 4,5,4,5; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -236,7 +205,7 @@ bool test_matrix() { 4,5,0,0, 0,0,2,3, 0,0,4,5; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -249,7 +218,7 @@ bool test_matrix() { 4,5,0,0, 0,0,2,3, 0,0,4,5; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -262,7 +231,7 @@ bool test_matrix() { 0,2,3,0, 0,4,5,0, 0,0,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -273,7 +242,7 @@ bool test_matrix() { 1,2,0,0, 4,5,0,0, 0,0,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -284,7 +253,7 @@ bool test_matrix() { 0,3,5,0, 0,6,0,0, 0,0,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -295,7 +264,7 @@ bool test_matrix() { 0,3,0,0, 0,0,0,0, 4,5,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -306,7 +275,7 @@ bool test_matrix() { 0,3,0,0, 4,5,6,7, 8,0,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -317,7 +286,7 @@ bool test_matrix() { 0,3,0,0, 4,5,6,7, 8,9,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -330,7 +299,7 @@ bool test_matrix() { 1,2,3,1, 1,4,5,1, 1,1,1,1; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -343,7 +312,7 @@ bool test_matrix() { 5,4,3,5, 5,2,1,5, 5,5,5,5; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } @@ -353,14 +322,13 @@ bool test_matrix() { template <class V> bool test_matrix_sparse_push_back() { bool pass = true; - using namespace tans; V A(3,3), RA(3,3); A <<= fill_policy::sparse_push_back(), 1, 2, 3, 4, 5, 6, 7, 8, 9; RA(0,0)= 1; RA(0,1)=2; RA(0,2)=3; RA(1,0)= 4; RA(1,1)=5; RA(1,2)=6; RA(2,0)= 7; RA(2,1)=8; RA(2,2)=9; - pass &= (mean_square(A-RA)<=TOL); + pass &= compare_to(A, RA); { V B(3,3), RB(3,3); @@ -368,7 +336,7 @@ bool test_matrix_sparse_push_back() { b<<= 4,5,6; B<<=fill_policy::sparse_push_back(), 1, 2, 3, b, 7, project(b, range(1,3)); RB<<= 1, 2, 3, 4, 5, 6, 7, 5, 6; // If the first worked we can now probably use it. - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -377,7 +345,7 @@ bool test_matrix_sparse_push_back() { b<<= 4,5,6; B<<=fill_policy::sparse_push_back(), move(1,0), b, fill_policy::sparse_insert(), move_to(0,0), 1, 2, 3, move(1,0), 7, project(b, range(1,3)); RB<<=1, 2, 3, 4, 5, 6, 7, 5, 6; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -386,7 +354,7 @@ bool test_matrix_sparse_push_back() { b<<= 1, 2, 3, 4, 5, 6, 7, 8, 9; B<<=b; RB<<=1, 2, 3, 4, 5, 6, 7, 8, 9; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } @@ -401,7 +369,7 @@ bool test_matrix_sparse_push_back() { 4,5,4,5, 2,3,2,3, 4,5,4,5; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } @@ -414,7 +382,7 @@ bool test_matrix_sparse_push_back() { 4,5,0,0, 0,0,2,3, 0,0,4,5; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -426,7 +394,7 @@ bool test_matrix_sparse_push_back() { 0,2,3,0, 0,4,5,0, 0,0,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -437,7 +405,7 @@ bool test_matrix_sparse_push_back() { 1,2,0,0, 4,5,0,0, 0,0,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } // The next will not work with sparse push_back because elements that are prior to the ones already in are attempted to be added /* @@ -449,7 +417,7 @@ bool test_matrix_sparse_push_back() { 0,3,5,0, 0,6,0,0, 0,0,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } */ { @@ -460,7 +428,7 @@ bool test_matrix_sparse_push_back() { 0,3,0,0, 0,0,0,0, 4,5,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -471,7 +439,7 @@ bool test_matrix_sparse_push_back() { 0,3,0,0, 4,5,6,7, 8,0,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } // The next will not work with sparse push_back because elements that are prior to the ones already in are attempted to be added @@ -484,7 +452,7 @@ bool test_matrix_sparse_push_back() { 0,3,0,0, 4,5,6,7, 8,9,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } */ return pass; @@ -493,14 +461,13 @@ bool test_matrix_sparse_push_back() { template <class V> bool test_matrix_sparse_insert() { bool pass = true; - using namespace tans; V A(3,3), RA(3,3); A <<= fill_policy::sparse_insert(), 1, 2, 3, 4, 5, 6, 7, 8, 9; RA(0,0)= 1; RA(0,1)=2; RA(0,2)=3; RA(1,0)= 4; RA(1,1)=5; RA(1,2)=6; RA(2,0)= 7; RA(2,1)=8; RA(2,2)=9; - pass &= (mean_square(A-RA)<=TOL); + pass &= compare_to(A, RA); { V B(3,3), RB(3,3); @@ -508,7 +475,7 @@ bool test_matrix_sparse_insert() { b<<= 4,5,6; B<<=fill_policy::sparse_insert(), 1, 2, 3, b, 7, project(b, range(1,3)); RB<<=1, 2, 3, 4, 5, 6, 7, 5, 6; // If the first worked we can now probably use it. - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -517,7 +484,7 @@ bool test_matrix_sparse_insert() { b<<= 4,5,6; B<<=fill_policy::sparse_insert(), move(1,0), b, fill_policy::sparse_insert(), move_to(0,0), 1, 2, 3, move(1,0), 7, project(b, range(1,3)); RB<<=1, 2, 3, 4, 5, 6, 7, 5, 6; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -526,7 +493,7 @@ bool test_matrix_sparse_insert() { b<<= 1, 2, 3, 4, 5, 6, 7, 8, 9; B<<=b; RB<<=1, 2, 3, 4, 5, 6, 7, 8, 9; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } @@ -540,7 +507,7 @@ bool test_matrix_sparse_insert() { 4,5,4,5, 2,3,2,3, 4,5,4,5; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } @@ -553,7 +520,7 @@ bool test_matrix_sparse_insert() { 4,5,0,0, 0,0,2,3, 0,0,4,5; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -565,7 +532,7 @@ bool test_matrix_sparse_insert() { 0,2,3,0, 0,4,5,0, 0,0,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -576,7 +543,7 @@ bool test_matrix_sparse_insert() { 1,2,0,0, 4,5,0,0, 0,0,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -587,7 +554,7 @@ bool test_matrix_sparse_insert() { 0,3,5,0, 0,6,0,0, 0,0,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -598,7 +565,7 @@ bool test_matrix_sparse_insert() { 0,3,0,0, 0,0,0,0, 4,5,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -609,7 +576,7 @@ bool test_matrix_sparse_insert() { 0,3,0,0, 4,5,6,7, 8,0,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } { @@ -620,7 +587,7 @@ bool test_matrix_sparse_insert() { 0,3,0,0, 4,5,6,7, 8,9,0,0; - pass &= (mean_square(B-RB)<=TOL); + pass &= compare_to(B, RB); } return pass; |