3 libs/numeric/odeint/test/check_operations.cpp
6 This file tests interaction between the algebras and the operations.
9 Copyright 2011-2012 Karsten Ahnert
10 Copyright 2011-2013 Mario Mulansky
12 Distributed under the Boost Software License, Version 1.0.
13 (See accompanying file LICENSE_1_0.txt or
14 copy at http://www.boost.org/LICENSE_1_0.txt)
17 #define BOOST_TEST_MODULE odeint_standard_algebra
23 #include <boost/array.hpp>
25 #include <boost/test/unit_test.hpp>
26 #include <boost/test/floating_point_comparison.hpp>
28 #include <boost/units/systems/si/length.hpp>
29 #include <boost/units/systems/si/time.hpp>
30 #include <boost/units/systems/si/velocity.hpp>
31 #include <boost/units/systems/si/io.hpp>
33 #include <boost/numeric/odeint/algebra/default_operations.hpp>
34 #include <boost/numeric/odeint/algebra/range_algebra.hpp>
35 #include <boost/numeric/odeint/algebra/array_algebra.hpp>
37 #include <boost/mpl/list.hpp>
39 namespace units = boost::units;
40 namespace si = boost::units::si;
42 using boost::numeric::odeint::default_operations;
43 using boost::numeric::odeint::range_algebra;
44 using boost::numeric::odeint::array_algebra;
47 BOOST_AUTO_TEST_SUITE( standard_algebra_test )
49 typedef boost::mpl::list< range_algebra , array_algebra > algebra_types;
50 range_algebra algebra;
52 BOOST_AUTO_TEST_CASE_TEMPLATE( for_each2 , algebra_type , algebra_types )
55 boost::array< double , 2 > x1 = {{ 1.0 , 1.0 }} , x2 = {{ 2.0 , 2.0 }};
56 algebra.for_each2( x1 , x2 , default_operations::scale_sum1<>( 1.0 ) );
57 BOOST_CHECK_CLOSE( x1[0] , 2.0 , 1.0e-10 );
58 BOOST_CHECK_CLOSE( x1[1] , 2.0 , 1.0e-10 );
63 BOOST_AUTO_TEST_CASE_TEMPLATE( for_each3 , algebra_type , algebra_types )
66 boost::array< double , 2 > x1 = {{ 1.0 , 1.0 }} , x2 = {{ 2.0 , 2.0 }} , x3 = {{ 3.0 , 3.0 }};
67 algebra.for_each3( x1 , x2 , x3 , default_operations::scale_sum2<>( 1.0 , 2.0 ) );
68 BOOST_CHECK_CLOSE( x1[0] , 8.0 , 1.0e-10 );
69 BOOST_CHECK_CLOSE( x1[1] , 8.0 , 1.0e-10 );
73 BOOST_AUTO_TEST_CASE_TEMPLATE( for_each4 , algebra_type , algebra_types )
76 boost::array< double , 2 > x1 = {{ 1.0 , 1.0 }} , x2 = {{ 2.0 , 2.0 }} , x3 = {{ 3.0 , 3.0 }} , x4 = {{ 4.0 , 4.0 }};
77 algebra.for_each4( x1 , x2 , x3 , x4 , default_operations::scale_sum3<>( 1.0 , 2.0 , 3.0 ) );
78 BOOST_CHECK_CLOSE( x1[0] , 20.0 , 1.0e-10 );
79 BOOST_CHECK_CLOSE( x1[1] , 20.0 , 1.0e-10 );
82 BOOST_AUTO_TEST_CASE_TEMPLATE( for_each5 , algebra_type , algebra_types )
85 boost::array< double , 2 > x1 = {{ 1.0 , 1.0 }} , x2 = {{ 2.0 , 2.0 }} , x3 = {{ 3.0 , 3.0 }} , x4 = {{ 4.0 , 4.0 }} , x5 = {{ 5.0 , 5.0 }};
86 algebra.for_each5( x1 , x2 , x3 , x4 , x5 , default_operations::scale_sum4<>( 1.0 , 2.0 , 3.0 , 4.0 ) );
87 BOOST_CHECK_CLOSE( x1[0] , 40.0 , 1.0e-10 );
88 BOOST_CHECK_CLOSE( x1[1] , 40.0 , 1.0e-10 );
91 BOOST_AUTO_TEST_CASE_TEMPLATE( for_each6 , algebra_type , algebra_types )
94 boost::array< double , 2 > x1 = {{ 1.0 , 1.0 }} , x2 = {{ 2.0 , 2.0 }} , x3 = {{ 3.0 , 3.0 }} , x4 = {{ 4.0 , 4.0 }} , x5 = {{ 5.0 , 5.0 }} , x6 = {{ 6.0 , 6.0 }};
95 algebra.for_each6( x1 , x2 , x3 , x4 , x5 , x6 ,default_operations::scale_sum5<>( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 ) );
96 BOOST_CHECK_CLOSE( x1[0] , 70.0 , 1.0e-10 );
97 BOOST_CHECK_CLOSE( x1[1] , 70.0 , 1.0e-10 );
100 BOOST_AUTO_TEST_CASE_TEMPLATE( for_each7 , algebra_type , algebra_types )
102 algebra_type algebra;
103 boost::array< double , 2 > x1 = {{ 1.0 , 1.0 }} , x2 = {{ 2.0 , 2.0 }} , x3 = {{ 3.0 , 3.0 }} , x4 = {{ 4.0 , 4.0 }} , x5 = {{ 5.0 , 5.0 }} , x6 = {{ 6.0 , 6.0 }} , x7 = {{ 7.0 , 7.0 }};
104 algebra.for_each7( x1 , x2 , x3 , x4 , x5 , x6 , x7 , default_operations::scale_sum6<>( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 ) );
105 BOOST_CHECK_CLOSE( x1[0] , 112.0 , 1.0e-10 );
106 BOOST_CHECK_CLOSE( x1[1] , 112.0 , 1.0e-10 );
109 BOOST_AUTO_TEST_CASE_TEMPLATE( for_each8 , algebra_type , algebra_types )
111 algebra_type algebra;
112 boost::array< double , 2 > x1 = {{ 1.0 , 1.0 }} , x2 = {{ 2.0 , 2.0 }} , x3 = {{ 3.0 , 3.0 }} , x4 = {{ 4.0 , 4.0 }} , x5 = {{ 5.0 , 5.0 }} , x6 = {{ 6.0 , 6.0 }} , x7 = {{ 7.0 , 7.0 }} , x8 = {{ 8.0 , 8.0 }};
113 algebra.for_each8( x1 , x2 , x3 , x4 , x5 , x6 , x7 , x8 , default_operations::scale_sum7<>( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 ) );
114 BOOST_CHECK_CLOSE( x1[0] , 168.0 , 1.0e-10 );
115 BOOST_CHECK_CLOSE( x1[1] , 168.0 , 1.0e-10 );
118 BOOST_AUTO_TEST_CASE_TEMPLATE( norm_inf , algebra_type , algebra_types )
120 algebra_type algebra;
121 boost::array< double , 2 > x = {{ 1.25 , 2.25 }};
122 double nrm = algebra.norm_inf( x );
123 BOOST_CHECK_CLOSE( nrm , 2.25 , 1.0e-10 );
124 BOOST_CHECK_CLOSE( x[0] , 1.25 , 1.0e-10 );
125 BOOST_CHECK_CLOSE( x[1] , 2.25 , 1.0e-10 );
129 BOOST_AUTO_TEST_CASE( for_each2_with_units )
131 range_algebra algebra;
132 typedef units::quantity< si::time , double > time_type;
133 typedef units::quantity< si::length , double > length_type;
134 typedef units::quantity< si::velocity , double > velocity_type;
135 boost::array< length_type , 2 > x1 = {{ 1.0 * si::meter , 1.0 * si::meter }};
136 boost::array< velocity_type , 2 > x2 = {{ 2.0 * si::meter / si::seconds , 2.0 * si::meter / si::seconds }};
137 algebra.for_each2( x1 , x2 , default_operations::scale_sum1< time_type >( 0.1 * si::second ) );
138 BOOST_CHECK_CLOSE( x1[0].value() , 0.2 , 1.0e-10 );
139 BOOST_CHECK_CLOSE( x1[1].value() , 0.2 , 1.0e-10 );
143 BOOST_AUTO_TEST_SUITE_END()