test11.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright (c) 2000-2002
  2. // Joerg Walter, Mathias Koch
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // The authors gratefully acknowledge the support of
  9. // GeNeSys mbH & Co. KG in producing this work.
  10. #if defined(__GNUC__) && (__GNUC__ >= 9)
  11. #pragma GCC diagnostic ignored "-Wdeprecated-copy"
  12. #endif
  13. #include "test1.hpp"
  14. // Test vector expression templates
  15. template <class V, int N>
  16. struct test_my_vector
  17. {
  18. typedef typename V::value_type value_type;
  19. typedef typename V::size_type size_type;
  20. typedef typename ublas::type_traits<value_type>::real_type real_type;
  21. template <class VP>
  22. void test_container_with(VP& v1) const
  23. {
  24. // Container type tests in addition to expression types
  25. // Insert and erase
  26. v1.insert_element(0, 55);
  27. v1.erase_element(1);
  28. v1.clear();
  29. }
  30. template <class VP>
  31. void test_expression_with(VP& v1, VP& v2, VP& v3) const
  32. {
  33. // Expression type tests
  34. value_type t;
  35. size_type i;
  36. real_type n;
  37. // Default Construct
  38. default_construct<VP>::test();
  39. // Copy and swap
  40. initialize_vector(v1);
  41. initialize_vector(v2);
  42. v1 = v2;
  43. std::cout << "v1 = v2 = " << v1 << std::endl;
  44. v1.assign_temporary(v2);
  45. std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
  46. v1.swap(v2);
  47. std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;
  48. // Zero assignment
  49. v1 = ublas::zero_vector<>(v1.size());
  50. std::cout << "v1.zero_vector = " << v1 << std::endl;
  51. v1 = v2;
  52. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  53. // Project range and slice
  54. initialize_vector(v1);
  55. initialize_vector(v2);
  56. project(v1, ublas::range(0, 1)) = project(v2, ublas::range(0, 1));
  57. project(v1, ublas::range(0, 1)) = project(v2, ublas::slice(0, 1, 1));
  58. project(v1, ublas::slice(2, -1, 2)) = project(v2, ublas::slice(0, 1, 2));
  59. project(v1, ublas::slice(2, -1, 2)) = project(v2, ublas::range(0, 2));
  60. std::cout << "v1 = range/slice " << v1 << std::endl;
  61. #endif
  62. // Unary vector operations resulting in a vector
  63. initialize_vector(v1);
  64. v2 = -v1;
  65. std::cout << "- v1 = " << v2 << std::endl;
  66. v2 = ublas::conj(v1);
  67. std::cout << "conj (v1) = " << v2 << std::endl;
  68. // Binary vector operations resulting in a vector
  69. initialize_vector(v1);
  70. initialize_vector(v2);
  71. v3 = v1 + v2;
  72. std::cout << "v1 + v2 = " << v3 << std::endl;
  73. v3 = v1 - v2;
  74. std::cout << "v1 - v2 = " << v3 << std::endl;
  75. v3 = ublas::element_prod(v1, v2);
  76. std::cout << "element_prod (v1, v2) = " << v3 << std::endl;
  77. // Scaling a vector
  78. t = N;
  79. initialize_vector(v1);
  80. v2 = value_type(1.) * v1;
  81. std::cout << "1. * v1 = " << v2 << std::endl;
  82. v2 = t * v1;
  83. std::cout << "N * v1 = " << v2 << std::endl;
  84. initialize_vector(v1);
  85. v2 = v1 * value_type(1.);
  86. std::cout << "v1 * 1. = " << v2 << std::endl;
  87. v2 = v1 * t;
  88. std::cout << "v1 * value_type(N) = " << v2 << std::endl;
  89. // test interop with integer
  90. v2 = v1 * N;
  91. std::cout << "v1 * N = " << v2 << std::endl;
  92. // Some assignments
  93. initialize_vector(v1);
  94. initialize_vector(v2);
  95. v2 += v1;
  96. std::cout << "v2 += v1 = " << v2 << std::endl;
  97. v2 -= v1;
  98. std::cout << "v2 -= v1 = " << v2 << std::endl;
  99. v2 = v2 + v1;
  100. std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
  101. v2 = v2 - v1;
  102. std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
  103. v1 *= value_type(1.);
  104. std::cout << "v1 *= 1. = " << v1 << std::endl;
  105. v1 *= t;
  106. std::cout << "v1 *= value_type(N) = " << v1 << std::endl;
  107. // test interop with integer
  108. v1 *= N;
  109. std::cout << "v1 *= N = " << v1 << std::endl;
  110. // Unary vector operations resulting in a scalar
  111. initialize_vector(v1);
  112. t = ublas::sum(v1);
  113. std::cout << "sum (v1) = " << t << std::endl;
  114. n = ublas::norm_1(v1);
  115. std::cout << "norm_1 (v1) = " << n << std::endl;
  116. n = ublas::norm_2(v1);
  117. std::cout << "norm_2 (v1) = " << n << std::endl;
  118. n = ublas::norm_inf(v1);
  119. std::cout << "norm_inf (v1) = " << n << std::endl;
  120. i = ublas::index_norm_inf(v1);
  121. std::cout << "index_norm_inf (v1) = " << i << std::endl;
  122. // Binary vector operations resulting in a scalar
  123. initialize_vector(v1);
  124. initialize_vector(v2);
  125. t = ublas::inner_prod(v1, v2);
  126. std::cout << "inner_prod (v1, v2) = " << t << std::endl;
  127. // Scalar and Binary vector expression resulting in a vector
  128. initialize_vector(v1);
  129. initialize_vector(v2);
  130. v1 = v1 * ublas::inner_prod(v1, v2);
  131. std::cout << "v1 * inner_prod (v1, v2) = " << v1 << std::endl;
  132. }
  133. void operator()() const
  134. {
  135. V v1(N), v2(N), v3(N);
  136. test_expression_with(v1, v2, v3);
  137. test_container_with(v1);
  138. #ifdef USE_RANGE
  139. ublas::vector_range<V> vr1(v1, ublas::range(0, N)),
  140. vr2(v2, ublas::range(0, N)),
  141. vr3(v3, ublas::range(0, N));
  142. test_expression_with(vr1, vr2, vr3);
  143. #endif
  144. #ifdef USE_SLICE
  145. ublas::vector_slice<V> vs1(v1, ublas::slice(0, 1, N)),
  146. vs2(v2, ublas::slice(0, 1, N)),
  147. vs3(v3, ublas::slice(0, 1, N));
  148. test_expression_with(vs1, vs2, vs3);
  149. #endif
  150. }
  151. };
  152. // Test vector
  153. void test_vector()
  154. {
  155. std::cout << "test_vector" << std::endl;
  156. #ifdef USE_BOUNDED_ARRAY
  157. #ifdef USE_FLOAT
  158. std::cout << "mp_test_type, bounded_array" << std::endl;
  159. test_my_vector<ublas::vector<mp_test_type, ublas::bounded_array<mp_test_type, 3> >, 3>()();
  160. #endif
  161. #ifdef USE_DOUBLE
  162. std::cout << "double, bounded_array" << std::endl;
  163. test_my_vector<ublas::vector<double, ublas::bounded_array<double, 3> >, 3>()();
  164. #endif
  165. #ifdef USE_STD_COMPLEX
  166. #ifdef USE_FLOAT
  167. std::cout << "std::complex<mp_test_type>, bounded_array" << std::endl;
  168. test_my_vector<ublas::vector<std::complex<mp_test_type>, ublas::bounded_array<std::complex<mp_test_type>, 3> >, 3>()();
  169. #endif
  170. #ifdef USE_DOUBLE
  171. std::cout << "std::complex<double>, bounded_array" << std::endl;
  172. test_my_vector<ublas::vector<std::complex<double>, ublas::bounded_array<std::complex<double>, 3> >, 3>()();
  173. #endif
  174. #endif
  175. #endif
  176. #ifdef USE_UNBOUNDED_ARRAY
  177. #ifdef USE_FLOAT
  178. std::cout << "mp_test_type, unbounded_array" << std::endl;
  179. test_my_vector<ublas::vector<mp_test_type, ublas::unbounded_array<mp_test_type> >, 3>()();
  180. #endif
  181. #ifdef USE_DOUBLE
  182. std::cout << "double, unbounded_array" << std::endl;
  183. test_my_vector<ublas::vector<double, ublas::unbounded_array<double> >, 3>()();
  184. #endif
  185. #ifdef USE_STD_COMPLEX
  186. #ifdef USE_FLOAT
  187. std::cout << "std::complex<mp_test_type>, unbounded_array" << std::endl;
  188. test_my_vector<ublas::vector<std::complex<mp_test_type>, ublas::unbounded_array<std::complex<mp_test_type> > >, 3>()();
  189. #endif
  190. #ifdef USE_DOUBLE
  191. std::cout << "std::complex<double>, unbounded_array" << std::endl;
  192. test_my_vector<ublas::vector<std::complex<double>, ublas::unbounded_array<std::complex<double> > >, 3>()();
  193. #endif
  194. #endif
  195. #endif
  196. #ifdef USE_STD_VECTOR
  197. #ifdef USE_FLOAT
  198. std::cout << "mp_test_type, std::vector" << std::endl;
  199. test_my_vector<ublas::vector<mp_test_type, std::vector<mp_test_type> >, 3>()();
  200. #endif
  201. #ifdef USE_DOUBLE
  202. std::cout << "double, std::vector" << std::endl;
  203. test_my_vector<ublas::vector<double, std::vector<double> >, 3>()();
  204. #endif
  205. #ifdef USE_STD_COMPLEX
  206. #ifdef USE_FLOAT
  207. std::cout << "std::complex<mp_test_type>, std::vector" << std::endl;
  208. test_my_vector<ublas::vector<std::complex<mp_test_type>, std::vector<std::complex<mp_test_type> > >, 3>()();
  209. #endif
  210. #ifdef USE_DOUBLE
  211. std::cout << "std::complex<double>, std::vector" << std::endl;
  212. test_my_vector<ublas::vector<std::complex<double>, std::vector<std::complex<double> > >, 3>()();
  213. #endif
  214. #endif
  215. #endif
  216. #ifdef USE_BOUNDED_VECTOR
  217. #ifdef USE_FLOAT
  218. std::cout << "mp_test_type, bounded" << std::endl;
  219. test_my_vector<ublas::bounded_vector<mp_test_type, 3>, 3>()();
  220. #endif
  221. #ifdef USE_DOUBLE
  222. std::cout << "double, bounded" << std::endl;
  223. test_my_vector<ublas::bounded_vector<double, 3>, 3>()();
  224. #endif
  225. #ifdef USE_STD_COMPLEX
  226. #ifdef USE_FLOAT
  227. std::cout << "std::complex<mp_test_type>, bounded" << std::endl;
  228. test_my_vector<ublas::bounded_vector<std::complex<mp_test_type>, 3>, 3>()();
  229. #endif
  230. #ifdef USE_DOUBLE
  231. std::cout << "std::complex<double>, bounded" << std::endl;
  232. test_my_vector<ublas::bounded_vector<std::complex<double>, 3>, 3>()();
  233. #endif
  234. #endif
  235. #endif
  236. }