test43.cpp 12 KB

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