test62.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 "test6.hpp"
  13. // Test matrix & vector expression templates
  14. template<class V, class M, int N>
  15. struct test_my_matrix_vector {
  16. typedef typename V::value_type value_type;
  17. template<class VP, class MP>
  18. void test_with (VP &v1, VP &v2, MP &m1) const {
  19. {
  20. // Rows and columns
  21. initialize_matrix (m1);
  22. for (int i = 0; i < N; ++ i) {
  23. v2 = ublas::row (m1, i);
  24. std::cout << "row (m, " << i << ") = " << v2 << std::endl;
  25. v2 = ublas::column (m1, i);
  26. std::cout << "column (m, " << i << ") = " << v2 << std::endl;
  27. }
  28. // Outer product
  29. initialize_vector (v1);
  30. initialize_vector (v2);
  31. v1 (0) = 0;
  32. v1 (N - 1) = 0;
  33. v2 (0) = 0;
  34. v2 (N - 1) = 0;
  35. m1 = ublas::outer_prod (v1, v2);
  36. std::cout << "outer_prod (v1, v2) = " << m1 << std::endl;
  37. // Matrix vector product
  38. initialize_matrix (m1);
  39. initialize_vector (v1);
  40. v2 = ublas::prod (m1, v1);
  41. std::cout << "prod (m1, v1) = " << v2 << std::endl;
  42. v2 = ublas::prod (v1, m1);
  43. std::cout << "prod (v1, m1) = " << v2 << std::endl;
  44. }
  45. }
  46. void operator () () const {
  47. {
  48. V v1 (N), v2 (N);
  49. M m1 (N, N);
  50. test_with (v1, v2, m1);
  51. ublas::matrix_row<M> mr1 (m1, N - 1), mr2 (m1, N - 1);
  52. test_with (mr1, mr2, m1);
  53. ublas::matrix_column<M> mc1 (m1, 0), mc2 (m1, 0);
  54. test_with (mc1, mc2, m1);
  55. #ifdef USE_RANGE
  56. ublas::matrix_vector_range<M> mvr1 (m1, ublas::range (0, N), ublas::range (0, N)),
  57. mvr2 (m1, ublas::range (0, N), ublas::range (0, N));
  58. test_with (mvr1, mvr2, m1);
  59. #endif
  60. #ifdef USE_SLICE
  61. ublas::matrix_vector_slice<M> mvs1 (m1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)),
  62. mvs2 (m1, ublas::slice (0, 1, N), ublas::slice (0, 1, N));
  63. test_with (mvs1, mvs2, m1);
  64. #endif
  65. }
  66. }
  67. void operator () (int) const {
  68. #ifdef USE_ADAPTOR
  69. {
  70. V v1 (N), v2 (N);
  71. M m1 (N, N);
  72. ublas::symmetric_adaptor<M> tam1 (m1);
  73. test_with (v1, v2, tam1);
  74. ublas::matrix_row<ublas::symmetric_adaptor<M> > mr1 (tam1, N - 1), mr2 (tam1, N - 1);
  75. test_with (mr1, mr2, tam1);
  76. ublas::matrix_column<ublas::symmetric_adaptor<M> > mc1 (tam1, 0), mc2 (tam1, 0);
  77. test_with (mc1, mc2, tam1);
  78. #ifdef USE_RANGE
  79. ublas::matrix_vector_range<ublas::symmetric_adaptor<M> > mvr1 (tam1, ublas::range (0, N), ublas::range (0, N)),
  80. mvr2 (tam1, ublas::range (0, N), ublas::range (0, N));
  81. test_with (mvr1, mvr2, tam1);
  82. #endif
  83. #ifdef USE_SLICE
  84. ublas::matrix_vector_slice<ublas::symmetric_adaptor<M> > mvs1 (tam1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)),
  85. mvs2 (tam1, ublas::slice (0, 1, N), ublas::slice (0, 1, N));
  86. test_with (mvs1, mvs2, tam1);
  87. #endif
  88. }
  89. #endif
  90. }
  91. };
  92. // Test matrix & vector
  93. void test_matrix_vector () {
  94. std::cout << "test_matrix_vector" << std::endl;
  95. #ifdef USE_BOUNDED_ARRAY
  96. #ifdef USE_FLOAT
  97. std::cout << "float, bounded_array" << std::endl;
  98. test_my_matrix_vector<ublas::vector<float, ublas::bounded_array<float, 3> >,
  99. ublas::symmetric_matrix<float, ublas::lower, ublas::row_major, ublas::bounded_array<float, 3 * 3> >, 3> () ();
  100. test_my_matrix_vector<ublas::vector<float, ublas::bounded_array<float, 3> >,
  101. ublas::symmetric_matrix<float, ublas::lower, ublas::row_major, ublas::bounded_array<float, 3 * 3> >, 3> () (0);
  102. #endif
  103. #ifdef USE_DOUBLE
  104. std::cout << "double, bounded_array" << std::endl;
  105. test_my_matrix_vector<ublas::vector<double, ublas::bounded_array<double, 3> >,
  106. ublas::symmetric_matrix<double, ublas::lower, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3> () ();
  107. test_my_matrix_vector<ublas::vector<double, ublas::bounded_array<double, 3> >,
  108. ublas::symmetric_matrix<double, ublas::lower, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3> () (0);
  109. #endif
  110. #ifdef USE_STD_COMPLEX
  111. #ifdef USE_FLOAT
  112. std::cout << "std::complex<float>, bounded_array" << std::endl;
  113. test_my_matrix_vector<ublas::vector<std::complex<float>, ublas::bounded_array<std::complex<float>, 3> >,
  114. ublas::symmetric_matrix<std::complex<float>, ublas::lower, ublas::row_major, ublas::bounded_array<std::complex<float>, 3 * 3> >, 3> () ();
  115. test_my_matrix_vector<ublas::vector<std::complex<float>, ublas::bounded_array<std::complex<float>, 3> >,
  116. ublas::symmetric_matrix<std::complex<float>, ublas::lower, ublas::row_major, ublas::bounded_array<std::complex<float>, 3 * 3> >, 3> () (0);
  117. #endif
  118. #ifdef USE_DOUBLE
  119. std::cout << "std::complex<double>, bounded_array" << std::endl;
  120. test_my_matrix_vector<ublas::vector<std::complex<double>, ublas::bounded_array<std::complex<double>, 3> >,
  121. ublas::symmetric_matrix<std::complex<double>, ublas::lower, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3> () ();
  122. test_my_matrix_vector<ublas::vector<std::complex<double>, ublas::bounded_array<std::complex<double>, 3> >,
  123. ublas::symmetric_matrix<std::complex<double>, ublas::lower, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3> () (0);
  124. #endif
  125. #endif
  126. #endif
  127. #ifdef USE_UNBOUNDED_ARRAY
  128. #ifdef USE_FLOAT
  129. std::cout << "float, unbounded_array" << std::endl;
  130. test_my_matrix_vector<ublas::vector<float, ublas::unbounded_array<float> >,
  131. ublas::symmetric_matrix<float, ublas::lower, ublas::row_major, ublas::unbounded_array<float> >, 3> () ();
  132. test_my_matrix_vector<ublas::vector<float, ublas::unbounded_array<float> >,
  133. ublas::symmetric_matrix<float, ublas::lower, ublas::row_major, ublas::unbounded_array<float> >, 3> () (0);
  134. #endif
  135. #ifdef USE_DOUBLE
  136. std::cout << "double, unbounded_array" << std::endl;
  137. test_my_matrix_vector<ublas::vector<double, ublas::unbounded_array<double> >,
  138. ublas::symmetric_matrix<double, ublas::lower, ublas::row_major, ublas::unbounded_array<double> >, 3> () ();
  139. test_my_matrix_vector<ublas::vector<double, ublas::unbounded_array<double> >,
  140. ublas::symmetric_matrix<double, ublas::lower, ublas::row_major, ublas::unbounded_array<double> >, 3> () (0);
  141. #endif
  142. #ifdef USE_STD_COMPLEX
  143. #ifdef USE_FLOAT
  144. std::cout << "std::complex<float>, unbounded_array" << std::endl;
  145. test_my_matrix_vector<ublas::vector<std::complex<float>, ublas::unbounded_array<std::complex<float> > >,
  146. ublas::symmetric_matrix<std::complex<float>, ublas::lower, ublas::row_major, ublas::unbounded_array<std::complex<float> > >, 3> () ();
  147. test_my_matrix_vector<ublas::vector<std::complex<float>, ublas::unbounded_array<std::complex<float> > >,
  148. ublas::symmetric_matrix<std::complex<float>, ublas::lower, ublas::row_major, ublas::unbounded_array<std::complex<float> > >, 3> () (0);
  149. #endif
  150. #ifdef USE_DOUBLE
  151. std::cout << "std::complex<double>, unbounded_array" << std::endl;
  152. test_my_matrix_vector<ublas::vector<std::complex<double>, ublas::unbounded_array<std::complex<double> > >,
  153. ublas::symmetric_matrix<std::complex<double>, ublas::lower, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3> () ();
  154. test_my_matrix_vector<ublas::vector<std::complex<double>, ublas::unbounded_array<std::complex<double> > >,
  155. ublas::symmetric_matrix<std::complex<double>, ublas::lower, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3> () (0);
  156. #endif
  157. #endif
  158. #endif
  159. #ifdef USE_STD_VECTOR
  160. #ifdef USE_FLOAT
  161. std::cout << "float, std::vector" << std::endl;
  162. test_my_matrix_vector<ublas::vector<float, std::vector<float> >,
  163. ublas::symmetric_matrix<float, ublas::lower, ublas::row_major, std::vector<float> >, 3> () ();
  164. test_my_matrix_vector<ublas::vector<float, std::vector<float> >,
  165. ublas::symmetric_matrix<float, ublas::lower, ublas::row_major, std::vector<float> >, 3> () (0);
  166. #endif
  167. #ifdef USE_DOUBLE
  168. std::cout << "double, std::vector" << std::endl;
  169. test_my_matrix_vector<ublas::vector<double, std::vector<double> >,
  170. ublas::symmetric_matrix<double, ublas::lower, ublas::row_major, std::vector<double> >, 3> () ();
  171. test_my_matrix_vector<ublas::vector<double, std::vector<double> >,
  172. ublas::symmetric_matrix<double, ublas::lower, ublas::row_major, std::vector<double> >, 3> () (0);
  173. #endif
  174. #ifdef USE_STD_COMPLEX
  175. #ifdef USE_FLOAT
  176. std::cout << "std::complex<float>, std::vector" << std::endl;
  177. test_my_matrix_vector<ublas::vector<std::complex<float>, std::vector<std::complex<float> > >,
  178. ublas::symmetric_matrix<std::complex<float>, ublas::lower, ublas::row_major, std::vector<std::complex<float> > >, 3> () ();
  179. test_my_matrix_vector<ublas::vector<std::complex<float>, std::vector<std::complex<float> > >,
  180. ublas::symmetric_matrix<std::complex<float>, ublas::lower, ublas::row_major, std::vector<std::complex<float> > >, 3> () (0);
  181. #endif
  182. #ifdef USE_DOUBLE
  183. std::cout << "std::complex<double>, std::vector" << std::endl;
  184. test_my_matrix_vector<ublas::vector<std::complex<double>, std::vector<std::complex<double> > >,
  185. ublas::symmetric_matrix<std::complex<double>, ublas::lower, ublas::row_major, std::vector<std::complex<double> > >, 3> () ();
  186. test_my_matrix_vector<ublas::vector<std::complex<double>, std::vector<std::complex<double> > >,
  187. ublas::symmetric_matrix<std::complex<double>, ublas::lower, ublas::row_major, std::vector<std::complex<double> > >, 3> () (0);
  188. #endif
  189. #endif
  190. #endif
  191. }