multi_array.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. [auto_generated]
  3. libs/numeric/odeint/test/multi_array.cpp
  4. [begin_description]
  5. tba.
  6. [end_description]
  7. Copyright 2009-2012 Karsten Ahnert
  8. Copyright 2009-2012 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. #include <boost/config.hpp>
  14. #ifdef BOOST_MSVC
  15. #pragma warning(disable:4996)
  16. #endif
  17. #define BOOST_TEST_MODULE odeint_multi_array
  18. #include <boost/test/unit_test.hpp>
  19. #include <boost/numeric/odeint/algebra/multi_array_algebra.hpp>
  20. #include <boost/numeric/odeint/util/multi_array_adaption.hpp>
  21. #include <boost/numeric/odeint/stepper/runge_kutta4.hpp>
  22. #include <boost/numeric/odeint/integrate/integrate_const.hpp>
  23. #include <boost/numeric/odeint/stepper/runge_kutta_dopri5.hpp>
  24. #include <boost/numeric/odeint/stepper/generation.hpp>
  25. using namespace boost::unit_test;
  26. using namespace boost::numeric::odeint;
  27. typedef boost::multi_array< double , 1 > vector_type;
  28. typedef boost::multi_array< double , 2 > matrix_type;
  29. typedef boost::multi_array< double , 3 > tensor_type;
  30. BOOST_AUTO_TEST_SUITE( multi_array_test )
  31. BOOST_AUTO_TEST_CASE( test_resizeable )
  32. {
  33. BOOST_CHECK( is_resizeable< vector_type >::value );
  34. BOOST_CHECK( is_resizeable< matrix_type >::value );
  35. BOOST_CHECK( is_resizeable< tensor_type >::value );
  36. }
  37. BOOST_AUTO_TEST_CASE( test_same_size_vector1 )
  38. {
  39. vector_type v1( boost::extents[12] );
  40. vector_type v2( boost::extents[12] );
  41. BOOST_CHECK( same_size( v1 , v2 ) );
  42. }
  43. BOOST_AUTO_TEST_CASE( test_same_size_vector2 )
  44. {
  45. vector_type v1( boost::extents[12] );
  46. vector_type v2( boost::extents[13] );
  47. BOOST_CHECK( !same_size( v1 , v2 ) );
  48. }
  49. BOOST_AUTO_TEST_CASE( test_same_size_vector3 )
  50. {
  51. vector_type v1( boost::extents[12] );
  52. vector_type v2( boost::extents[vector_type::extent_range(-1,11)] );
  53. BOOST_CHECK( !same_size( v1 , v2 ) );
  54. }
  55. BOOST_AUTO_TEST_CASE( test_same_size_vector4 )
  56. {
  57. vector_type v1( boost::extents[12] );
  58. vector_type v2( boost::extents[vector_type::extent_range(-1,10)] );
  59. BOOST_CHECK( !same_size( v1 , v2 ) );
  60. }
  61. BOOST_AUTO_TEST_CASE( test_same_size_vector5 )
  62. {
  63. vector_type v1( boost::extents[vector_type::extent_range(-1,10)] );
  64. vector_type v2( boost::extents[vector_type::extent_range(-1,10)] );
  65. BOOST_CHECK( same_size( v1 , v2 ) );
  66. }
  67. BOOST_AUTO_TEST_CASE( test_same_size_matrix1 )
  68. {
  69. matrix_type m1( boost::extents[12][4] );
  70. matrix_type m2( boost::extents[12][4] );
  71. BOOST_CHECK( same_size( m1 , m2 ) );
  72. }
  73. BOOST_AUTO_TEST_CASE( test_same_size_matrix2 )
  74. {
  75. matrix_type m1( boost::extents[12][4] );
  76. matrix_type m2( boost::extents[12][3] );
  77. BOOST_CHECK( !same_size( m1 , m2 ) );
  78. }
  79. BOOST_AUTO_TEST_CASE( test_same_size_matrix3 )
  80. {
  81. matrix_type m1( boost::extents[12][matrix_type::extent_range(-1,2)] );
  82. matrix_type m2( boost::extents[12][3] );
  83. BOOST_CHECK( !same_size( m1 , m2 ) );
  84. }
  85. BOOST_AUTO_TEST_CASE( test_same_size_matrix4 )
  86. {
  87. matrix_type m1( boost::extents[12][matrix_type::extent_range(-1,1)] );
  88. matrix_type m2( boost::extents[12][3] );
  89. BOOST_CHECK( !same_size( m1 , m2 ) );
  90. }
  91. BOOST_AUTO_TEST_CASE( test_same_size_tensor1 )
  92. {
  93. tensor_type t1( boost::extents[ tensor_type::extent_range( -2 , 9 ) ]
  94. [ tensor_type::extent_range( 5 , 11 ) ]
  95. [ tensor_type::extent_range( -1 , 2 ) ] );
  96. tensor_type t2( boost::extents[ tensor_type::extent_range( 2 , 13 ) ]
  97. [ tensor_type::extent_range( -1 , 5 ) ]
  98. [ tensor_type::extent_range( 1 , 4 ) ] );
  99. BOOST_CHECK( !same_size( t1 , t2 ) );
  100. }
  101. BOOST_AUTO_TEST_CASE( test_same_size_tensor2 )
  102. {
  103. tensor_type t1( boost::extents[ tensor_type::extent_range( -2 , 9 ) ]
  104. [ tensor_type::extent_range( 5 , 11 ) ]
  105. [ tensor_type::extent_range( -1 , 2 ) ] );
  106. tensor_type t2( boost::extents[ tensor_type::extent_range( -2 , 9 ) ]
  107. [ tensor_type::extent_range( 5 , 11 ) ]
  108. [ tensor_type::extent_range( -1 , 2 ) ] );
  109. BOOST_CHECK( same_size( t1 , t2 ) );
  110. }
  111. // Tests for tensor_type
  112. BOOST_AUTO_TEST_CASE( test_resize_vector1 )
  113. {
  114. vector_type v1( boost::extents[4] );
  115. vector_type v2;
  116. resize( v2 , v1 );
  117. BOOST_CHECK( same_size( v1 , v2 ) );
  118. }
  119. BOOST_AUTO_TEST_CASE( test_resize_vector2 )
  120. {
  121. vector_type v1( boost::extents[ vector_type::extent_range( -2 , 9 ) ] );
  122. vector_type v2;
  123. resize( v2 , v1 );
  124. BOOST_CHECK( same_size( v1 , v2 ) );
  125. }
  126. BOOST_AUTO_TEST_CASE( test_resize_vector3 )
  127. {
  128. vector_type v1( boost::extents[ vector_type::extent_range( -2 , 9 ) ] );
  129. vector_type v2( boost::extents[ vector_type::extent_range( 2 , 9 ) ] );
  130. BOOST_CHECK( !same_size( v1 , v2 ) );
  131. resize( v2 , v1 );
  132. BOOST_CHECK( same_size( v1 , v2 ) );
  133. }
  134. struct mult4
  135. {
  136. void operator()( double &a , double const &b ) const
  137. {
  138. a = b * 4.0;
  139. }
  140. };
  141. BOOST_AUTO_TEST_CASE( test_for_each2_vector )
  142. {
  143. vector_type v1( boost::extents[ vector_type::extent_range( -2 , 9 ) ] );
  144. vector_type v2( boost::extents[ vector_type::extent_range( 2 , 13 ) ] );
  145. for( int i=-2 ; i<9 ; ++i )
  146. v1[i] = i * 2;
  147. multi_array_algebra::for_each2( v2 , v1 , mult4() );
  148. for( int i=2 ; i<13 ; ++i )
  149. BOOST_CHECK_EQUAL( v2[i] , v1[i-4]*4.0 );
  150. }
  151. struct vector_ode
  152. {
  153. const static size_t n = 128;
  154. void operator()( const vector_type &x , vector_type &dxdt , double t ) const
  155. {
  156. dxdt[-1] = x[n] - 2.0 * x[-1] + x[0];
  157. for( size_t i=0 ; i<n ; ++i )
  158. dxdt[i] = x[i-1] - 2.0 * x[i] + x[i+1];
  159. dxdt[n] = x[-1] - 2.0 * x[n] + x[n-1];
  160. }
  161. };
  162. BOOST_AUTO_TEST_CASE( test_rk4_vector )
  163. {
  164. vector_type v1( boost::extents[ vector_type::extent_range( -1 , vector_ode::n+1 ) ] );
  165. integrate_const( runge_kutta4< vector_type >() , vector_ode() , v1 , 0.0 , 1.0 , 0.01 );
  166. }
  167. BOOST_AUTO_TEST_CASE( test_dopri5_vector )
  168. {
  169. vector_type v1( boost::extents[ vector_type::extent_range( -1 , vector_ode::n+1 ) ] );
  170. integrate_const( make_dense_output( 1.0e-6 , 1.0e-6 , runge_kutta_dopri5< vector_type >() ) , vector_ode() , v1 , 0.0 , 1.0 , 0.01 );
  171. }
  172. BOOST_AUTO_TEST_SUITE_END()