test43.cpp 11 KB

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