test52.cpp 10 KB

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