test31.cpp 8.2 KB

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