test13.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 "test1.hpp"
  16. // Test matrix expression templates
  17. template <class M, int N>
  18. struct test_my_matrix
  19. {
  20. typedef typename M::value_type value_type;
  21. template <class VP>
  22. void test_container_with(VP& v1) const
  23. {
  24. // Container type tests in addition to expression types
  25. // Insert and erase
  26. v1.insert_element(0, 0, 55);
  27. v1.erase_element(1, 1);
  28. v1.clear();
  29. }
  30. template <class MP>
  31. void test_expression_with(MP& m1, MP& m2, MP& m3) const
  32. {
  33. value_type t;
  34. // Default Construct
  35. default_construct<MP>::test();
  36. // Copy and swap
  37. initialize_matrix(m1);
  38. initialize_matrix(m2);
  39. m1 = m2;
  40. std::cout << "m1 = m2 = " << m1 << std::endl;
  41. m1.assign_temporary(m2);
  42. std::cout << "m1.assign_temporary (m2) = " << m1 << std::endl;
  43. m1.swap(m2);
  44. std::cout << "m1.swap (m2) = " << m1 << " " << m2 << std::endl;
  45. // Zero assignment
  46. m1 = ublas::zero_matrix<>(m1.size1(), m1.size2());
  47. std::cout << "m1.zero_matrix = " << m1 << std::endl;
  48. m1 = m2;
  49. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  50. // Project range and slice
  51. initialize_matrix(m1);
  52. initialize_matrix(m2);
  53. project(m1, ublas::range(0, 1), ublas::range(0, 1)) = project(m2, ublas::range(0, 1), ublas::range(0, 1));
  54. project(m1, ublas::range(0, 1), ublas::range(0, 1)) = project(m2, ublas::slice(0, 1, 1), ublas::slice(0, 1, 1));
  55. project(m1, ublas::slice(2, -1, 2), ublas::slice(2, -1, 2)) = project(m2, ublas::slice(0, 1, 2), ublas::slice(0, 1, 2));
  56. project(m1, ublas::slice(2, -1, 2), ublas::slice(2, -1, 2)) = project(m2, ublas::range(0, 2), ublas::range(0, 2));
  57. std::cout << "m1 = range/slice " << m1 << std::endl;
  58. #endif
  59. // Unary matrix operations resulting in a matrix
  60. initialize_matrix(m1);
  61. m2 = -m1;
  62. std::cout << "- m1 = " << m2 << std::endl;
  63. m2 = ublas::conj(m1);
  64. std::cout << "conj (m1) = " << m2 << std::endl;
  65. // Binary matrix operations resulting in a matrix
  66. initialize_matrix(m1);
  67. initialize_matrix(m2);
  68. m3 = m1 + m2;
  69. std::cout << "m1 + m2 = " << m3 << std::endl;
  70. m3 = m1 - m2;
  71. std::cout << "m1 - m2 = " << m3 << std::endl;
  72. m3 = ublas::element_prod(m1, m2);
  73. std::cout << "element_prod (m1, m2) = " << m3 << std::endl;
  74. // Scaling a matrix
  75. t = N;
  76. initialize_matrix(m1);
  77. m2 = value_type(1.) * m1;
  78. std::cout << "1. * m1 = " << m2 << std::endl;
  79. m2 = t * m1;
  80. std::cout << "N * m1 = " << m2 << std::endl;
  81. initialize_matrix(m1);
  82. m2 = m1 * value_type(1.);
  83. std::cout << "m1 * 1. = " << m2 << std::endl;
  84. m2 = m1 * t;
  85. std::cout << "m1 * N = " << m2 << std::endl;
  86. // Some assignments
  87. initialize_matrix(m1);
  88. initialize_matrix(m2);
  89. m2 += m1;
  90. std::cout << "m2 += m1 = " << m2 << std::endl;
  91. m2 -= m1;
  92. std::cout << "m2 -= m1 = " << m2 << std::endl;
  93. m2 = m2 + m1;
  94. std::cout << "m2 = m2 + m1 = " << m2 << std::endl;
  95. m2 = m2 - m1;
  96. std::cout << "m2 = m2 - m1 = " << m2 << std::endl;
  97. m1 *= value_type(1.);
  98. std::cout << "m1 *= 1. = " << m1 << std::endl;
  99. m1 *= t;
  100. std::cout << "m1 *= N = " << m1 << std::endl;
  101. // Transpose
  102. initialize_matrix(m1);
  103. m2 = ublas::trans(m1);
  104. std::cout << "trans (m1) = " << m2 << std::endl;
  105. // Hermitean
  106. initialize_matrix(m1);
  107. m2 = ublas::herm(m1);
  108. std::cout << "herm (m1) = " << m2 << std::endl;
  109. // Matrix multiplication
  110. initialize_matrix(m1);
  111. initialize_matrix(m2);
  112. m3 = ublas::prod(m1, m2);
  113. std::cout << "prod (m1, m2) = " << m3 << std::endl;
  114. }
  115. void operator()() const
  116. {
  117. M m1(N, N), m2(N, N), m3(N, N);
  118. test_expression_with(m1, m2, m3);
  119. test_container_with(m1);
  120. #ifdef USE_RANGE
  121. ublas::matrix_range<M> mr1(m1, ublas::range(0, N), ublas::range(0, N)),
  122. mr2(m2, ublas::range(0, N), ublas::range(0, N)),
  123. mr3(m3, ublas::range(0, N), ublas::range(0, N));
  124. test_expression_with(mr1, mr2, mr3);
  125. #endif
  126. #ifdef USE_SLICE
  127. ublas::matrix_slice<M> ms1(m1, ublas::slice(0, 1, N), ublas::slice(0, 1, N)),
  128. ms2(m2, ublas::slice(0, 1, N), ublas::slice(0, 1, N)),
  129. ms3(m3, ublas::slice(0, 1, N), ublas::slice(0, 1, N));
  130. test_expression_with(ms1, ms2, ms3);
  131. #endif
  132. }
  133. };
  134. // Test matrix
  135. void test_matrix()
  136. {
  137. std::cout << "test_matrix" << std::endl;
  138. #ifdef USE_MATRIX
  139. #ifdef USE_BOUNDED_ARRAY
  140. #ifdef USE_FLOAT
  141. std::cout << "mp_test_type, bounded_array" << std::endl;
  142. test_my_matrix<ublas::matrix<mp_test_type, ublas::row_major, ublas::bounded_array<mp_test_type, 3 * 3> >, 3>()();
  143. #endif
  144. #ifdef USE_DOUBLE
  145. std::cout << "double, bounded_array" << std::endl;
  146. test_my_matrix<ublas::matrix<double, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3>()();
  147. #endif
  148. #ifdef USE_STD_COMPLEX
  149. #ifdef USE_FLOAT
  150. std::cout << "std::complex<mp_test_type>, bounded_array" << std::endl;
  151. test_my_matrix<ublas::matrix<std::complex<mp_test_type>, ublas::row_major, ublas::bounded_array<std::complex<mp_test_type>, 3 * 3> >, 3>()();
  152. #endif
  153. #ifdef USE_DOUBLE
  154. std::cout << "std::complex<double>, bounded_array" << std::endl;
  155. test_my_matrix<ublas::matrix<std::complex<double>, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3>()();
  156. #endif
  157. #endif
  158. #endif
  159. #ifdef USE_UNBOUNDED_ARRAY
  160. #ifdef USE_FLOAT
  161. std::cout << "mp_test_type, unbounded_array" << std::endl;
  162. test_my_matrix<ublas::matrix<mp_test_type, ublas::row_major, ublas::unbounded_array<mp_test_type> >, 3>()();
  163. #endif
  164. #ifdef USE_DOUBLE
  165. std::cout << "double, unbounded_array" << std::endl;
  166. test_my_matrix<ublas::matrix<double, ublas::row_major, ublas::unbounded_array<double> >, 3>()();
  167. #endif
  168. #ifdef USE_STD_COMPLEX
  169. #ifdef USE_FLOAT
  170. std::cout << "std::complex<mp_test_type>, unbounded_array" << std::endl;
  171. test_my_matrix<ublas::matrix<std::complex<mp_test_type>, ublas::row_major, ublas::unbounded_array<std::complex<mp_test_type> > >, 3>()();
  172. #endif
  173. #ifdef USE_DOUBLE
  174. std::cout << "std::complex<double>, unbounded_array" << std::endl;
  175. test_my_matrix<ublas::matrix<std::complex<double>, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3>()();
  176. #endif
  177. #endif
  178. #endif
  179. #ifdef USE_STD_VECTOR
  180. #ifdef USE_FLOAT
  181. std::cout << "mp_test_type, std::vector" << std::endl;
  182. test_my_matrix<ublas::matrix<mp_test_type, ublas::row_major, std::vector<mp_test_type> >, 3>()();
  183. #endif
  184. #ifdef USE_DOUBLE
  185. std::cout << "double, std::vector" << std::endl;
  186. test_my_matrix<ublas::matrix<double, ublas::row_major, std::vector<double> >, 3>()();
  187. #endif
  188. #ifdef USE_STD_COMPLEX
  189. #ifdef USE_FLOAT
  190. std::cout << "std::complex<mp_test_type>, std::vector" << std::endl;
  191. test_my_matrix<ublas::matrix<std::complex<mp_test_type>, ublas::row_major, std::vector<std::complex<mp_test_type> > >, 3>()();
  192. #endif
  193. #ifdef USE_DOUBLE
  194. std::cout << "std::complex<double>, std::vector" << std::endl;
  195. test_my_matrix<ublas::matrix<std::complex<double>, ublas::row_major, std::vector<std::complex<double> > >, 3>()();
  196. #endif
  197. #endif
  198. #endif
  199. #endif
  200. #ifdef USE_BOUNDED_MATRIX
  201. #ifdef USE_FLOAT
  202. std::cout << "mp_test_type, bounded" << std::endl;
  203. test_my_matrix<ublas::bounded_matrix<mp_test_type, 3, 3>, 3>()();
  204. #endif
  205. #ifdef USE_DOUBLE
  206. std::cout << "double, bounded" << std::endl;
  207. test_my_matrix<ublas::bounded_matrix<double, 3, 3>, 3>()();
  208. #endif
  209. #ifdef USE_STD_COMPLEX
  210. #ifdef USE_FLOAT
  211. std::cout << "std::complex<mp_test_type>, bounded" << std::endl;
  212. test_my_matrix<ublas::bounded_matrix<std::complex<mp_test_type>, 3, 3>, 3>()();
  213. #endif
  214. #ifdef USE_DOUBLE
  215. std::cout << "std::complex<double>, bounded" << std::endl;
  216. test_my_matrix<ublas::bounded_matrix<std::complex<double>, 3, 3>, 3>()();
  217. #endif
  218. #endif
  219. #endif
  220. #ifdef USE_VECTOR_OF_VECTOR
  221. #ifdef USE_BOUNDED_ARRAY
  222. #ifdef USE_FLOAT
  223. std::cout << "mp_test_type, bounded_array" << std::endl;
  224. test_my_matrix<ublas::vector_of_vector<mp_test_type, ublas::row_major, ublas::bounded_array<ublas::bounded_array<mp_test_type, 3>, 3 + 1> >, 3>()();
  225. #endif
  226. #ifdef USE_DOUBLE
  227. std::cout << "double, bounded_array" << std::endl;
  228. test_my_matrix<ublas::vector_of_vector<double, ublas::row_major, ublas::bounded_array<ublas::bounded_array<double, 3>, 3 + 1> >, 3>()();
  229. #endif
  230. #ifdef USE_STD_COMPLEX
  231. #ifdef USE_FLOAT
  232. std::cout << "std::complex<mp_test_type>, bounded_array" << std::endl;
  233. test_my_matrix<ublas::vector_of_vector<std::complex<mp_test_type>, ublas::row_major, ublas::bounded_array<ublas::bounded_array<std::complex<mp_test_type>, 3>, 3 + 1> >, 3>()();
  234. #endif
  235. #ifdef USE_DOUBLE
  236. std::cout << "std::complex<double>, bounded_array" << std::endl;
  237. test_my_matrix<ublas::vector_of_vector<std::complex<double>, ublas::row_major, ublas::bounded_array<ublas::bounded_array<std::complex<double>, 3>, 3 + 1> >, 3>()();
  238. #endif
  239. #endif
  240. #endif
  241. #ifdef USE_UNBOUNDED_ARRAY
  242. #ifdef USE_FLOAT
  243. std::cout << "mp_test_type, unbounded_array" << std::endl;
  244. test_my_matrix<ublas::vector_of_vector<mp_test_type, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<mp_test_type> > >, 3>()();
  245. #endif
  246. #ifdef USE_DOUBLE
  247. std::cout << "double, unbounded_array" << std::endl;
  248. test_my_matrix<ublas::vector_of_vector<double, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<double> > >, 3>()();
  249. #endif
  250. #ifdef USE_STD_COMPLEX
  251. #ifdef USE_FLOAT
  252. std::cout << "std::complex<mp_test_type>, unbounded_array" << std::endl;
  253. test_my_matrix<ublas::vector_of_vector<std::complex<mp_test_type>, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<std::complex<mp_test_type> > > >, 3>()();
  254. #endif
  255. #ifdef USE_DOUBLE
  256. std::cout << "std::complex<double>, unbounded_array" << std::endl;
  257. test_my_matrix<ublas::vector_of_vector<std::complex<double>, ublas::row_major, ublas::unbounded_array<ublas::unbounded_array<std::complex<double> > > >, 3>()();
  258. #endif
  259. #endif
  260. #endif
  261. #ifdef USE_STD_VECTOR
  262. #ifdef USE_FLOAT
  263. std::cout << "mp_test_type, std::vector" << std::endl;
  264. test_my_matrix<ublas::vector_of_vector<mp_test_type, ublas::row_major, std::vector<std::vector<mp_test_type> > >, 3>()();
  265. #endif
  266. #ifdef USE_DOUBLE
  267. std::cout << "double, std::vector" << std::endl;
  268. test_my_matrix<ublas::vector_of_vector<double, ublas::row_major, std::vector<std::vector<double> > >, 3>()();
  269. #endif
  270. #ifdef USE_STD_COMPLEX
  271. #ifdef USE_FLOAT
  272. std::cout << "std::complex<mp_test_type>, std::vector" << std::endl;
  273. test_my_matrix<ublas::vector_of_vector<std::complex<mp_test_type>, ublas::row_major, std::vector<std::vector<std::complex<mp_test_type> > > >, 3>()();
  274. #endif
  275. #ifdef USE_DOUBLE
  276. std::cout << "std::complex<double>, std::vector" << std::endl;
  277. test_my_matrix<ublas::vector_of_vector<std::complex<double>, ublas::row_major, std::vector<std::vector<std::complex<double> > > >, 3>()();
  278. #endif
  279. #endif
  280. #endif
  281. #endif
  282. }