test13.cpp 11 KB

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