range_algebra.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/check_operations.cpp
  4. [begin_description]
  5. This file tests interaction between the algebras and the operations.
  6. [end_description]
  7. Copyright 2011-2012 Karsten Ahnert
  8. Copyright 2011-2013 Mario Mulansky
  9. Distributed under the Boost Software License, Version 1.0.
  10. (See accompanying file LICENSE_1_0.txt or
  11. copy at http://www.boost.org/LICENSE_1_0.txt)
  12. */
  13. #define BOOST_TEST_MODULE odeint_standard_algebra
  14. #include <cmath>
  15. #include <complex>
  16. #include <utility>
  17. #include <functional>
  18. #include <boost/array.hpp>
  19. #include <boost/test/unit_test.hpp>
  20. #include <boost/test/floating_point_comparison.hpp>
  21. #include <boost/units/systems/si/length.hpp>
  22. #include <boost/units/systems/si/time.hpp>
  23. #include <boost/units/systems/si/velocity.hpp>
  24. #include <boost/units/systems/si/io.hpp>
  25. #include <boost/numeric/odeint/algebra/default_operations.hpp>
  26. #include <boost/numeric/odeint/algebra/range_algebra.hpp>
  27. #include <boost/numeric/odeint/algebra/array_algebra.hpp>
  28. #include <boost/mpl/list.hpp>
  29. namespace units = boost::units;
  30. namespace si = boost::units::si;
  31. using boost::numeric::odeint::default_operations;
  32. using boost::numeric::odeint::range_algebra;
  33. using boost::numeric::odeint::array_algebra;
  34. BOOST_AUTO_TEST_SUITE( standard_algebra_test )
  35. typedef boost::mpl::list< range_algebra , array_algebra > algebra_types;
  36. range_algebra algebra;
  37. BOOST_AUTO_TEST_CASE_TEMPLATE( for_each2 , algebra_type , algebra_types )
  38. {
  39. algebra_type algebra;
  40. boost::array< double , 2 > x1 = {{ 1.0 , 1.0 }} , x2 = {{ 2.0 , 2.0 }};
  41. algebra.for_each2( x1 , x2 , default_operations::scale_sum1<>( 1.0 ) );
  42. BOOST_CHECK_CLOSE( x1[0] , 2.0 , 1.0e-10 );
  43. BOOST_CHECK_CLOSE( x1[1] , 2.0 , 1.0e-10 );
  44. }
  45. BOOST_AUTO_TEST_CASE_TEMPLATE( for_each3 , algebra_type , algebra_types )
  46. {
  47. algebra_type algebra;
  48. boost::array< double , 2 > x1 = {{ 1.0 , 1.0 }} , x2 = {{ 2.0 , 2.0 }} , x3 = {{ 3.0 , 3.0 }};
  49. algebra.for_each3( x1 , x2 , x3 , default_operations::scale_sum2<>( 1.0 , 2.0 ) );
  50. BOOST_CHECK_CLOSE( x1[0] , 8.0 , 1.0e-10 );
  51. BOOST_CHECK_CLOSE( x1[1] , 8.0 , 1.0e-10 );
  52. }
  53. BOOST_AUTO_TEST_CASE_TEMPLATE( for_each4 , algebra_type , algebra_types )
  54. {
  55. algebra_type algebra;
  56. boost::array< double , 2 > x1 = {{ 1.0 , 1.0 }} , x2 = {{ 2.0 , 2.0 }} , x3 = {{ 3.0 , 3.0 }} , x4 = {{ 4.0 , 4.0 }};
  57. algebra.for_each4( x1 , x2 , x3 , x4 , default_operations::scale_sum3<>( 1.0 , 2.0 , 3.0 ) );
  58. BOOST_CHECK_CLOSE( x1[0] , 20.0 , 1.0e-10 );
  59. BOOST_CHECK_CLOSE( x1[1] , 20.0 , 1.0e-10 );
  60. }
  61. BOOST_AUTO_TEST_CASE_TEMPLATE( for_each5 , algebra_type , algebra_types )
  62. {
  63. algebra_type algebra;
  64. 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 }};
  65. algebra.for_each5( x1 , x2 , x3 , x4 , x5 , default_operations::scale_sum4<>( 1.0 , 2.0 , 3.0 , 4.0 ) );
  66. BOOST_CHECK_CLOSE( x1[0] , 40.0 , 1.0e-10 );
  67. BOOST_CHECK_CLOSE( x1[1] , 40.0 , 1.0e-10 );
  68. }
  69. BOOST_AUTO_TEST_CASE_TEMPLATE( for_each6 , algebra_type , algebra_types )
  70. {
  71. algebra_type algebra;
  72. 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 }};
  73. algebra.for_each6( x1 , x2 , x3 , x4 , x5 , x6 ,default_operations::scale_sum5<>( 1.0 , 2.0 , 3.0 , 4.0 , 5.0 ) );
  74. BOOST_CHECK_CLOSE( x1[0] , 70.0 , 1.0e-10 );
  75. BOOST_CHECK_CLOSE( x1[1] , 70.0 , 1.0e-10 );
  76. }
  77. BOOST_AUTO_TEST_CASE_TEMPLATE( for_each7 , algebra_type , algebra_types )
  78. {
  79. algebra_type algebra;
  80. 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 }};
  81. 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 ) );
  82. BOOST_CHECK_CLOSE( x1[0] , 112.0 , 1.0e-10 );
  83. BOOST_CHECK_CLOSE( x1[1] , 112.0 , 1.0e-10 );
  84. }
  85. BOOST_AUTO_TEST_CASE_TEMPLATE( for_each8 , algebra_type , algebra_types )
  86. {
  87. algebra_type algebra;
  88. 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 }};
  89. 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 ) );
  90. BOOST_CHECK_CLOSE( x1[0] , 168.0 , 1.0e-10 );
  91. BOOST_CHECK_CLOSE( x1[1] , 168.0 , 1.0e-10 );
  92. }
  93. BOOST_AUTO_TEST_CASE_TEMPLATE( norm_inf , algebra_type , algebra_types )
  94. {
  95. algebra_type algebra;
  96. boost::array< double , 2 > x = {{ 1.25 , 2.25 }};
  97. double nrm = algebra.norm_inf( x );
  98. BOOST_CHECK_CLOSE( nrm , 2.25 , 1.0e-10 );
  99. BOOST_CHECK_CLOSE( x[0] , 1.25 , 1.0e-10 );
  100. BOOST_CHECK_CLOSE( x[1] , 2.25 , 1.0e-10 );
  101. }
  102. BOOST_AUTO_TEST_CASE( for_each2_with_units )
  103. {
  104. range_algebra algebra;
  105. typedef units::quantity< si::time , double > time_type;
  106. typedef units::quantity< si::length , double > length_type;
  107. typedef units::quantity< si::velocity , double > velocity_type;
  108. boost::array< length_type , 2 > x1 = {{ 1.0 * si::meter , 1.0 * si::meter }};
  109. boost::array< velocity_type , 2 > x2 = {{ 2.0 * si::meter / si::seconds , 2.0 * si::meter / si::seconds }};
  110. algebra.for_each2( x1 , x2 , default_operations::scale_sum1< time_type >( 0.1 * si::second ) );
  111. BOOST_CHECK_CLOSE( x1[0].value() , 0.2 , 1.0e-10 );
  112. BOOST_CHECK_CLOSE( x1[1].value() , 0.2 , 1.0e-10 );
  113. }
  114. BOOST_AUTO_TEST_SUITE_END()