test31.cpp 8.0 KB

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