test_fixed_containers.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #undef BOOST_UBLAS_NO_EXCEPTIONS
  2. #include "common/testhelper.hpp"
  3. #include <boost/numeric/ublas/vector.hpp>
  4. #include <boost/numeric/ublas/matrix.hpp>
  5. #include <boost/numeric/ublas/assignment.hpp>
  6. #include <boost/numeric/ublas/io.hpp>
  7. #include <string>
  8. #include <complex>
  9. #include <iomanip>
  10. #include "utils.hpp"
  11. #ifdef BOOST_UBLAS_CPP_GE_2011
  12. using namespace boost::numeric::ublas;
  13. using std::cout;
  14. using std::endl;
  15. template <typename T>
  16. struct data_type {
  17. typedef T value_type;
  18. };
  19. template <typename T>
  20. struct data_type< std::complex<T> > {
  21. typedef typename std::complex<T>::value_type value_type;
  22. };
  23. template < class T >
  24. bool test_vector( std::string type_name)
  25. {
  26. typedef typename data_type<T>::value_type component_type;
  27. BOOST_UBLAS_DEBUG_TRACE( std::string("Testing for: ") + type_name );
  28. bool pass = true;
  29. {
  30. typedef fixed_vector<T, 1> vec1;
  31. vec1 v1( static_cast<component_type>(122.0) );
  32. pass &= ( v1(0) == static_cast<component_type>(122.0) );
  33. }
  34. {
  35. typedef fixed_vector<T, 3> vec3;
  36. vec3 v1(static_cast<component_type>(0.0),
  37. static_cast<component_type>(0.0),
  38. static_cast<component_type>(0.0));
  39. pass &=(sizeof( vec3 ) == v1.size() * sizeof( T ) ) ;
  40. vector<T> v( 3, 0 ) ;
  41. pass &= compare( v1, v );
  42. v1 <<= static_cast<component_type>(10.0), 10, 33;
  43. v <<= static_cast<component_type>(10.0), 10, 33;
  44. pass &= compare( v1, v );
  45. vec3 v2;
  46. v2( 0 ) = static_cast<component_type>(10.0);
  47. v2( 1 ) = 10;
  48. v2( 2 ) = 33;
  49. pass &= compare( v, v2 );
  50. v2 += v;
  51. pass &= compare( v2, 2*v );
  52. v1 = 2*v1 + v - 6*v2;
  53. pass &= compare( v1, (3-2*6)*v );
  54. vec3 v3{ static_cast<component_type>(-90.0),
  55. static_cast<component_type>(-90.0),
  56. static_cast<component_type>(-297.0) };
  57. pass &= compare( v3, v1 );
  58. vec3 v4 = { static_cast<component_type>(-90.0),
  59. static_cast<component_type>(-90.0),
  60. static_cast<component_type>(-297.0) };
  61. pass &= compare( v4, v1 );
  62. vec3 v5( static_cast<component_type>(-90.0),
  63. static_cast<component_type>(-90.0),
  64. static_cast<component_type>(-297.0) );
  65. pass &= compare( v5, v1 );
  66. vec3 v6( static_cast<component_type>(5.0),
  67. static_cast<component_type>(8.0),
  68. static_cast<component_type>(9.0) );
  69. matrix<T> M = outer_prod( v6, v6), L( 3, 3);
  70. L <<= 25, 40, 45, 40, 64, 72, 45, 72, 81;
  71. pass &= compare( M, L );
  72. L <<= 1, 2, 3, 4, 5, 6, 7, 8, 9;
  73. v6 <<= 4, 5, 6;
  74. vec3 v7 ( static_cast<component_type>(32.0),
  75. static_cast<component_type>(77.0),
  76. static_cast<component_type>(122.0) );
  77. pass &= compare( v7, prod(L, v6) );
  78. vec3 v8(prod(L, v6));
  79. pass &= compare( v7, v8 );
  80. }
  81. {
  82. const std::size_t N = 33;
  83. typedef fixed_vector<T, N> vec33;
  84. vec33 v1;
  85. vector<T> v( N );
  86. for ( std::size_t i = 0; i != v1.size(); i++)
  87. {
  88. v1( i ) = static_cast<component_type>(3.14159*i);
  89. v ( i ) = static_cast<component_type>(3.14159*i);
  90. }
  91. pass &= compare( v1, v );
  92. auto ip = inner_prod( v, v);
  93. auto ip1 = inner_prod( v1, v1);
  94. pass &= ( ip == ip1 ) ;
  95. T c = 0;
  96. for (auto i = v1.begin(); i != v1.end(); i++)
  97. {
  98. *i = c;
  99. c = c + 1;
  100. }
  101. c = 0;
  102. for (auto i = v.begin(); i != v.end(); i++)
  103. {
  104. *i = c;
  105. c = c + 1;
  106. }
  107. pass &= compare( v1, v );
  108. // Check if bad index indeed works
  109. try {
  110. T a;
  111. a=v1( 100 );
  112. BOOST_UBLAS_NOT_USED( a );
  113. } catch ( bad_index &e) {
  114. std::cout << " Caught (GOOD): " << e.what() << endl;
  115. pass &= true;
  116. }
  117. }
  118. return pass;
  119. }
  120. template < class T >
  121. bool test_matrix( std::string type_name)
  122. {
  123. typedef typename data_type<T>::value_type component_type;
  124. BOOST_UBLAS_DEBUG_TRACE( std::string("Testing for: ") + type_name );
  125. bool pass = true;
  126. typedef fixed_matrix<T, 3, 4> mat34;
  127. typedef fixed_matrix<T, 4, 3> mat43;
  128. typedef fixed_matrix<T, 3, 3> mat33;
  129. {
  130. typedef fixed_matrix<T, 1, 1> mat1;
  131. mat1 m1( static_cast<component_type>(122.0) );
  132. pass &= ( m1(0, 0) == static_cast<component_type>(122.0) );
  133. }
  134. {
  135. mat34 m1( T(static_cast<component_type>(3.0)) );
  136. pass &=(sizeof( mat34 ) == m1.size1()*m1.size2()*sizeof( T ) ) ;
  137. matrix<T> m( 3, 4, static_cast<component_type>(3.0) ) ;
  138. pass &= compare( m1, m );
  139. cout << m1 << endl;
  140. cout << m << endl;
  141. m1 <<= 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12;
  142. m <<= 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12;
  143. pass &= compare( m1, m );
  144. cout << m1 << endl;
  145. cout << m << endl;
  146. mat34 m2( static_cast<component_type>(0.0) );
  147. T count = 1 ;
  148. for ( std::size_t i = 0; i != m2.size1(); i++)
  149. {
  150. for (std::size_t j = 0; j!= m2.size2(); j++)
  151. {
  152. m2( i, j ) = count;
  153. count = count + 1;
  154. }
  155. }
  156. pass &= compare( m2, m );
  157. cout << m2 << endl;
  158. }
  159. {
  160. mat34 m1((T)1, (T)2, (T)3, (T)3, (T)3, (T)2, (T)5, (T)4, (T)2, (T)6, (T)5, (T)2);
  161. mat43 m2((T)4, (T)5, (T)6, (T)3, (T)2, (T)2, (T)1, (T)4, (T)2, (T)6, (T)5, (T)2);
  162. mat33 m3(prod(m1, m2));
  163. matrix<T> m(3, 3);
  164. m <<= 31,36,22,47,59,40,43,52,38;
  165. pass &= compare(m ,m3);
  166. mat33 m4;
  167. m4 <<= (T)1, (T)2, (T)1, (T)2, (T)1, (T)3, (T)1, (T)2, (T) 5;
  168. m3 = prod(m4, trans(m4));
  169. m<<=6,7,10,7,14,19,10,19,30;
  170. cout << m3 << endl;
  171. pass &= compare(m ,m3);
  172. m3 = 2 * m4 - 1 * m3;
  173. cout << m3 << endl;
  174. m <<= -4,-3,-8,-3,-12,-13,-8,-15,-20;
  175. pass &= compare(m, m3);
  176. m = m3;
  177. m3 = trans(m);
  178. pass &= compare(m3, trans(m));
  179. // Check if bad index indeed works
  180. try {
  181. T a;
  182. a=m1( 100, 100 );
  183. BOOST_UBLAS_NOT_USED( a );
  184. } catch ( bad_index &e) {
  185. std::cout << " Caught (GOOD): " << e.what() << endl;
  186. pass &= true;
  187. }
  188. }
  189. return pass;
  190. }
  191. BOOST_UBLAS_TEST_DEF (test_fixed) {
  192. BOOST_UBLAS_DEBUG_TRACE( "Starting fixed container tests" );
  193. BOOST_UBLAS_TEST_CHECK( test_vector< double >( "double") );
  194. BOOST_UBLAS_TEST_CHECK( test_vector< float >( "float") );
  195. BOOST_UBLAS_TEST_CHECK( test_vector< int >( "int") );
  196. BOOST_UBLAS_TEST_CHECK( test_vector< std::complex<double> >( "std::complex<double>") );
  197. BOOST_UBLAS_TEST_CHECK( test_vector< std::complex<float> >( "std::complex<float>") );
  198. BOOST_UBLAS_TEST_CHECK( test_vector< std::complex<int> >( "std::complex<int>") );
  199. BOOST_UBLAS_TEST_CHECK( test_matrix< double >( "double") );
  200. BOOST_UBLAS_TEST_CHECK( test_matrix< float >( "float") );
  201. BOOST_UBLAS_TEST_CHECK( test_matrix< int >( "int") );
  202. BOOST_UBLAS_TEST_CHECK( test_matrix< std::complex<double> >( "std::complex<double>") );
  203. BOOST_UBLAS_TEST_CHECK( test_matrix< std::complex<float> >( "std::complex<float>") );
  204. BOOST_UBLAS_TEST_CHECK( test_matrix< std::complex<int> >( "std::complex<int>") );
  205. }
  206. int main () {
  207. BOOST_UBLAS_TEST_BEGIN();
  208. BOOST_UBLAS_TEST_DO( test_fixed );
  209. BOOST_UBLAS_TEST_END();
  210. }
  211. #else
  212. int main () {
  213. BOOST_UBLAS_TEST_BEGIN();
  214. BOOST_UBLAS_TEST_END();
  215. }
  216. #endif // BOOST_UBLAS_CPP_GE_2011