test71.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. //
  11. #include "test7.hpp"
  12. // Test vector expression templates
  13. template<class V, int N>
  14. struct test_my_vector {
  15. typedef typename V::value_type value_type;
  16. typedef typename V::size_type size_type;
  17. typedef typename ublas::type_traits<value_type>::real_type real_type;
  18. template<class VP>
  19. void test_with (VP &v1, VP &v2, VP &v3) const {
  20. {
  21. value_type t;
  22. size_type i;
  23. real_type n;
  24. // Copy and swap
  25. initialize_vector (v1);
  26. initialize_vector (v2);
  27. v1 = v2;
  28. std::cout << "v1 = v2 = " << v1 << std::endl;
  29. v1.assign_temporary (v2);
  30. std::cout << "v1.assign_temporary (v2) = " << v1 << std::endl;
  31. v1.swap (v2);
  32. std::cout << "v1.swap (v2) = " << v1 << " " << v2 << std::endl;
  33. // Zero assignment
  34. v1 = ublas::zero_vector<value_type> (v1.size ());
  35. std::cout << "v1.zero_vector = " << v1 << std::endl;
  36. v1 = v2;
  37. // Unary vector operations resulting in a vector
  38. initialize_vector (v1);
  39. v2 = - v1;
  40. std::cout << "- v1 = " << v2 << std::endl;
  41. v2 = ublas::conj (v1);
  42. std::cout << "conj (v1) = " << v2 << std::endl;
  43. // Binary vector operations resulting in a vector
  44. initialize_vector (v1);
  45. initialize_vector (v2);
  46. v3 = v1 + v2;
  47. std::cout << "v1 + v2 = " << v3 << std::endl;
  48. v3 = v1 - v2;
  49. std::cout << "v1 - v2 = " << v3 << std::endl;
  50. // Scaling a vector
  51. t = value_type (N);
  52. initialize_vector (v1);
  53. v2 = value_type (1.) * v1;
  54. std::cout << "1. * v1 = " << v2 << std::endl;
  55. // v2 = t * v1;
  56. std::cout << "N * v1 = " << v2 << std::endl;
  57. initialize_vector (v1);
  58. // v2 = v1 * value_type (1.);
  59. std::cout << "v1 * 1. = " << v2 << std::endl;
  60. // v2 = v1 * t;
  61. std::cout << "v1 * N = " << v2 << std::endl;
  62. // Some assignments
  63. initialize_vector (v1);
  64. initialize_vector (v2);
  65. v2 += v1;
  66. std::cout << "v2 += v1 = " << v2 << std::endl;
  67. v2 -= v1;
  68. std::cout << "v2 -= v1 = " << v2 << std::endl;
  69. v2 = v2 + v1;
  70. std::cout << "v2 = v2 + v1 = " << v2 << std::endl;
  71. v2 = v2 - v1;
  72. std::cout << "v2 = v2 - v1 = " << v2 << std::endl;
  73. v1 *= value_type (1.);
  74. std::cout << "v1 *= 1. = " << v1 << std::endl;
  75. v1 *= t;
  76. std::cout << "v1 *= N = " << v1 << std::endl;
  77. // Unary vector operations resulting in a scalar
  78. initialize_vector (v1);
  79. t = ublas::sum (v1);
  80. std::cout << "sum (v1) = " << t << std::endl;
  81. n = ublas::norm_1 (v1);
  82. std::cout << "norm_1 (v1) = " << n << std::endl;
  83. n = ublas::norm_2 (v1);
  84. std::cout << "norm_2 (v1) = " << n << std::endl;
  85. n = ublas::norm_inf (v1);
  86. std::cout << "norm_inf (v1) = " << n << std::endl;
  87. i = ublas::index_norm_inf (v1);
  88. std::cout << "index_norm_inf (v1) = " << i << std::endl;
  89. // Binary vector operations resulting in a scalar
  90. initialize_vector (v1);
  91. initialize_vector (v2);
  92. t = ublas::inner_prod (v1, v2);
  93. std::cout << "inner_prod (v1, v2) = " << t << std::endl;
  94. }
  95. }
  96. void operator () () const {
  97. {
  98. V v1 (N), v2 (N), v3 (N);
  99. test_with (v1, v2, v3);
  100. #ifdef USE_RANGE
  101. ublas::vector_range<V> vr1 (v1, ublas::range (0, N)),
  102. vr2 (v2, ublas::range (0, N)),
  103. vr3 (v3, ublas::range (0, N));
  104. test_with (vr1, vr2, vr3);
  105. #endif
  106. #ifdef USE_SLICE
  107. ublas::vector_slice<V> vs1 (v1, ublas::slice (0, 1, N)),
  108. vs2 (v2, ublas::slice (0, 1, N)),
  109. vs3 (v3, ublas::slice (0, 1, N));
  110. test_with (vs1, vs2, vs3);
  111. #endif
  112. }
  113. }
  114. };
  115. // Test vector
  116. void test_vector () {
  117. std::cout << "test_vector" << std::endl;
  118. #ifdef USE_BOUNDED_ARRAY
  119. #ifdef USE_FLOAT
  120. std::cout << "boost::numeric::interval<float>, bounded_array" << std::endl;
  121. test_my_vector<ublas::vector<boost::numeric::interval<float>, ublas::bounded_array<boost::numeric::interval<float>, 3> >, 3 > () ();
  122. #endif
  123. #ifdef USE_DOUBLE
  124. std::cout << "boost::numeric::interval<double>, bounded_array" << std::endl;
  125. test_my_vector<ublas::vector<boost::numeric::interval<double>, ublas::bounded_array<boost::numeric::interval<double>, 3> >, 3 > () ();
  126. #endif
  127. #endif
  128. #ifdef USE_UNBOUNDED_ARRAY
  129. #ifdef USE_FLOAT
  130. std::cout << "boost::numeric::interval<float>, unbounded_array" << std::endl;
  131. test_my_vector<ublas::vector<boost::numeric::interval<float>, ublas::unbounded_array<boost::numeric::interval<float> > >, 3 > () ();
  132. #endif
  133. #ifdef USE_DOUBLE
  134. std::cout << "boost::numeric::interval<double>, unbounded_array" << std::endl;
  135. test_my_vector<ublas::vector<boost::numeric::interval<double>, ublas::unbounded_array<boost::numeric::interval<double> > >, 3 > () ();
  136. #endif
  137. #endif
  138. #ifdef USE_STD_VECTOR
  139. #ifdef USE_FLOAT
  140. std::cout << "boost::numeric::interval<float>, std::vector" << std::endl;
  141. test_my_vector<ublas::vector<boost::numeric::interval<float>, std::vector<boost::numeric::interval<float> > >, 3 > () ();
  142. #endif
  143. #ifdef USE_DOUBLE
  144. std::cout << "boost::numeric::interval<double>, std::vector" << std::endl;
  145. test_my_vector<ublas::vector<boost::numeric::interval<double>, std::vector<boost::numeric::interval<double> > >, 3 > () ();
  146. #endif
  147. #endif
  148. }