test33.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 "test3.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. #ifndef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
  42. // Project range and slice
  43. initialize_matrix(m1);
  44. initialize_matrix(m2);
  45. project(m1, ublas::range(0, 1), ublas::range(0, 1)) = project(m2, ublas::range(0, 1), ublas::range(0, 1));
  46. project(m1, ublas::range(0, 1), ublas::range(0, 1)) = project(m2, ublas::slice(0, 1, 1), ublas::slice(0, 1, 1));
  47. project(m1, ublas::slice(2, -1, 2), ublas::slice(2, -1, 2)) = project(m2, ublas::slice(0, 1, 2), ublas::slice(0, 1, 2));
  48. project(m1, ublas::slice(2, -1, 2), ublas::slice(2, -1, 2)) = project(m2, ublas::range(0, 2), ublas::range(0, 2));
  49. std::cout << "m1 = range/slice " << m1 << std::endl;
  50. #endif
  51. // Unary matrix operations resulting in a matrix
  52. initialize_matrix(m1);
  53. m2 = -m1;
  54. std::cout << "- m1 = " << m2 << std::endl;
  55. m2 = ublas::conj(m1);
  56. std::cout << "conj (m1) = " << m2 << std::endl;
  57. // Binary matrix operations resulting in a matrix
  58. initialize_matrix(m1);
  59. initialize_matrix(m2);
  60. initialize_matrix(m3);
  61. m3 = m1 + m2;
  62. std::cout << "m1 + m2 = " << m3 << std::endl;
  63. m3 = m1 - m2;
  64. std::cout << "m1 - m2 = " << m3 << std::endl;
  65. // Scaling a matrix
  66. t = N;
  67. initialize_matrix(m1);
  68. m2 = value_type(1.) * m1;
  69. std::cout << "1. * m1 = " << m2 << std::endl;
  70. m2 = t * m1;
  71. std::cout << "N * m1 = " << m2 << std::endl;
  72. initialize_matrix(m1);
  73. m2 = m1 * value_type(1.);
  74. std::cout << "m1 * 1. = " << m2 << std::endl;
  75. m2 = m1 * t;
  76. std::cout << "m1 * N = " << m2 << std::endl;
  77. // Some assignments
  78. initialize_matrix(m1);
  79. initialize_matrix(m2);
  80. m2 += m1;
  81. std::cout << "m2 += m1 = " << m2 << std::endl;
  82. m2 -= m1;
  83. std::cout << "m2 -= m1 = " << m2 << std::endl;
  84. m2 = m2 + m1;
  85. std::cout << "m2 = m2 + m1 = " << m2 << std::endl;
  86. m2 = m2 - m1;
  87. std::cout << "m2 = m2 - m1 = " << m2 << std::endl;
  88. m1 *= value_type(1.);
  89. std::cout << "m1 *= 1. = " << m1 << std::endl;
  90. m1 *= t;
  91. std::cout << "m1 *= N = " << m1 << std::endl;
  92. // Transpose
  93. initialize_matrix(m1);
  94. m2 = ublas::trans(m1);
  95. std::cout << "trans (m1) = " << m2 << std::endl;
  96. // Hermitean
  97. initialize_matrix(m1);
  98. m2 = ublas::herm(m1);
  99. std::cout << "herm (m1) = " << m2 << std::endl;
  100. // Matrix multiplication
  101. initialize_matrix(m1);
  102. initialize_matrix(m2);
  103. m3 = ublas::prod(m1, m2);
  104. std::cout << "prod (m1, m2) = " << m3 << std::endl;
  105. }
  106. }
  107. void operator()() const
  108. {
  109. {
  110. M m1(N, N, N * N), m2(N, N, N * N), m3(N, N, N * N);
  111. test_with(m1, m2, m3);
  112. #ifdef USE_RANGE
  113. ublas::matrix_range<M> mr1(m1, ublas::range(0, N), ublas::range(0, N)),
  114. mr2(m2, ublas::range(0, N), ublas::range(0, N)),
  115. mr3(m3, ublas::range(0, N), ublas::range(0, N));
  116. test_with(mr1, mr2, mr3);
  117. #endif
  118. #ifdef USE_SLICE
  119. ublas::matrix_slice<M> ms1(m1, ublas::slice(0, 1, N), ublas::slice(0, 1, N)),
  120. ms2(m2, ublas::slice(0, 1, N), ublas::slice(0, 1, N)),
  121. ms3(m3, ublas::slice(0, 1, N), ublas::slice(0, 1, N));
  122. test_with(ms1, ms2, ms3);
  123. #endif
  124. }
  125. }
  126. };
  127. // Test matrix
  128. void test_matrix()
  129. {
  130. std::cout << "test_matrix" << std::endl;
  131. #ifdef USE_SPARSE_MATRIX
  132. #ifdef USE_MAP_ARRAY
  133. #ifdef USE_FLOAT
  134. std::cout << "mp_test_type, mapped_matrix map_array" << std::endl;
  135. test_my_matrix<ublas::mapped_matrix<mp_test_type, ublas::row_major, ublas::map_array<std::size_t, mp_test_type> >, 3>()();
  136. #endif
  137. #ifdef USE_DOUBLE
  138. std::cout << "double, mapped_matrix map_array" << std::endl;
  139. test_my_matrix<ublas::mapped_matrix<double, ublas::row_major, ublas::map_array<std::size_t, double> >, 3>()();
  140. #endif
  141. #ifdef USE_STD_COMPLEX
  142. #ifdef USE_FLOAT
  143. std::cout << "std::complex<mp_test_type>, mapped_matrix map_array" << std::endl;
  144. test_my_matrix<ublas::mapped_matrix<std::complex<mp_test_type>, ublas::row_major, ublas::map_array<std::size_t, std::complex<mp_test_type> > >, 3>()();
  145. #endif
  146. #ifdef USE_DOUBLE
  147. std::cout << "std::complex<double>, mapped_matrix map_array" << std::endl;
  148. test_my_matrix<ublas::mapped_matrix<std::complex<double>, ublas::row_major, ublas::map_array<std::size_t, std::complex<double> > >, 3>()();
  149. #endif
  150. #endif
  151. #endif
  152. #ifdef USE_STD_MAP
  153. #ifdef USE_FLOAT
  154. std::cout << "mp_test_type, mapped_matrix std::map" << std::endl;
  155. test_my_matrix<ublas::mapped_matrix<mp_test_type, ublas::row_major, std::map<std::size_t, mp_test_type> >, 3>()();
  156. #endif
  157. #ifdef USE_DOUBLE
  158. std::cout << "double, mapped_matrix std::map" << std::endl;
  159. test_my_matrix<ublas::mapped_matrix<double, ublas::row_major, std::map<std::size_t, double> >, 3>()();
  160. #endif
  161. #ifdef USE_STD_COMPLEX
  162. #ifdef USE_FLOAT
  163. std::cout << "std::complex<mp_test_type>, mapped_matrix std::map" << std::endl;
  164. test_my_matrix<ublas::mapped_matrix<std::complex<mp_test_type>, ublas::row_major, std::map<std::size_t, std::complex<mp_test_type> > >, 3>()();
  165. #endif
  166. #ifdef USE_DOUBLE
  167. std::cout << "std::complex<double>, mapped_matrix std::map" << std::endl;
  168. test_my_matrix<ublas::mapped_matrix<std::complex<double>, ublas::row_major, std::map<std::size_t, std::complex<double> > >, 3>()();
  169. #endif
  170. #endif
  171. #endif
  172. #endif
  173. #ifdef USE_SPARSE_VECTOR_OF_SPARSE_VECTOR
  174. #ifdef USE_MAP_ARRAY
  175. #ifdef USE_FLOAT
  176. std::cout << "mp_test_type, mapped_vector_of_mapped_vector map_array" << std::endl;
  177. test_my_matrix<ublas::mapped_vector_of_mapped_vector<mp_test_type, ublas::row_major, ublas::map_array<std::size_t, ublas::map_array<std::size_t, mp_test_type> > >, 3>()();
  178. #endif
  179. #ifdef USE_DOUBLE
  180. std::cout << "double, mapped_vector_of_mapped_vector map_array" << std::endl;
  181. test_my_matrix<ublas::mapped_vector_of_mapped_vector<double, ublas::row_major, ublas::map_array<std::size_t, ublas::map_array<std::size_t, double> > >, 3>()();
  182. #endif
  183. #ifdef USE_STD_COMPLEX
  184. #ifdef USE_FLOAT
  185. std::cout << "std::complex<mp_test_type>, mapped_vector_of_mapped_vector map_array" << std::endl;
  186. test_my_matrix<ublas::mapped_vector_of_mapped_vector<std::complex<mp_test_type>, ublas::row_major, ublas::map_array<std::size_t, ublas::map_array<std::size_t, std::complex<mp_test_type> > > >, 3>()();
  187. #endif
  188. #ifdef USE_DOUBLE
  189. std::cout << "std::complex<double>, mapped_vector_of_mapped_vectormap_array" << std::endl;
  190. test_my_matrix<ublas::mapped_vector_of_mapped_vector<std::complex<double>, ublas::row_major, ublas::map_array<std::size_t, ublas::map_array<std::size_t, std::complex<double> > > >, 3>()();
  191. #endif
  192. #endif
  193. #endif
  194. #ifdef USE_STD_MAP
  195. #ifdef USE_FLOAT
  196. std::cout << "mp_test_type, mapped_vector_of_mapped_vector std::map" << std::endl;
  197. test_my_matrix<ublas::mapped_vector_of_mapped_vector<mp_test_type, ublas::row_major, std::map<std::size_t, std::map<std::size_t, mp_test_type> > >, 3>()();
  198. #endif
  199. #ifdef USE_DOUBLE
  200. std::cout << "double, mapped_vector_of_mapped_vector std::map" << std::endl;
  201. test_my_matrix<ublas::mapped_vector_of_mapped_vector<double, ublas::row_major, std::map<std::size_t, std::map<std::size_t, double> > >, 3>()();
  202. #endif
  203. #ifdef USE_STD_COMPLEX
  204. #ifdef USE_FLOAT
  205. std::cout << "std::complex<mp_test_type>, mapped_vector_of_mapped_vector std::map" << std::endl;
  206. test_my_matrix<ublas::mapped_vector_of_mapped_vector<std::complex<mp_test_type>, ublas::row_major, std::map<std::size_t, std::map<std::size_t, std::complex<mp_test_type> > > >, 3>()();
  207. #endif
  208. #ifdef USE_DOUBLE
  209. std::cout << "std::complex<double>, mapped_vector_of_mapped_vector std::map" << std::endl;
  210. test_my_matrix<ublas::mapped_vector_of_mapped_vector<std::complex<double>, ublas::row_major, std::map<std::size_t, std::map<std::size_t, std::complex<double> > > >, 3>()();
  211. #endif
  212. #endif
  213. #endif
  214. #endif
  215. #ifdef USE_GENERALIZED_VECTOR_OF_VECTOR
  216. #ifdef USE_MAP_ARRAY
  217. #ifdef USE_FLOAT
  218. std::cout << "mp_test_type,generalized_vector_of_vector map_array" << std::endl;
  219. test_my_matrix<ublas::generalized_vector_of_vector<mp_test_type, ublas::row_major, ublas::vector<ublas::mapped_vector<mp_test_type, ublas::map_array<std::size_t, mp_test_type> > > >, 3>()();
  220. test_my_matrix<ublas::generalized_vector_of_vector<mp_test_type, ublas::row_major, ublas::mapped_vector<ublas::mapped_vector<mp_test_type, ublas::map_array<std::size_t, mp_test_type> >, ublas::map_array<std::size_t, ublas::mapped_vector<mp_test_type, ublas::map_array<std::size_t, mp_test_type> > > > >, 3>()();
  221. #endif
  222. #ifdef USE_DOUBLE
  223. std::cout << "double, generalized_vector_of_vector map_array" << std::endl;
  224. test_my_matrix<ublas::generalized_vector_of_vector<double, ublas::row_major, ublas::vector<ublas::mapped_vector<double, ublas::map_array<std::size_t, double> > > >, 3>()();
  225. test_my_matrix<ublas::generalized_vector_of_vector<double, ublas::row_major, ublas::mapped_vector<ublas::mapped_vector<double, ublas::map_array<std::size_t, double> >, ublas::map_array<std::size_t, ublas::mapped_vector<double, ublas::map_array<std::size_t, double> > > > >, 3>()();
  226. #endif
  227. #ifdef USE_STD_COMPLEX
  228. #ifdef USE_FLOAT
  229. std::cout << "std::complex<mp_test_type>, generalized_vector_of_vector map_array" << std::endl;
  230. test_my_matrix<ublas::generalized_vector_of_vector<std::complex<mp_test_type>, ublas::row_major, ublas::vector<ublas::mapped_vector<std::complex<mp_test_type>, ublas::map_array<std::size_t, std::complex<mp_test_type> > > > >, 3>()();
  231. test_my_matrix<ublas::generalized_vector_of_vector<std::complex<mp_test_type>, ublas::row_major, ublas::mapped_vector<ublas::mapped_vector<std::complex<mp_test_type>, ublas::map_array<std::size_t, std::complex<mp_test_type> > >, ublas::map_array<std::size_t, ublas::mapped_vector<std::complex<mp_test_type>, ublas::map_array<std::size_t, std::complex<mp_test_type> > > > > >, 3>()();
  232. #endif
  233. #ifdef USE_DOUBLE
  234. std::cout << "std::complex<double>, generalized_vector_of_vector map_array" << std::endl;
  235. test_my_matrix<ublas::generalized_vector_of_vector<std::complex<double>, ublas::row_major, ublas::vector<ublas::mapped_vector<std::complex<double>, ublas::map_array<std::size_t, std::complex<double> > > > >, 3>()();
  236. test_my_matrix<ublas::generalized_vector_of_vector<std::complex<double>, ublas::row_major, ublas::mapped_vector<ublas::mapped_vector<std::complex<double>, ublas::map_array<std::size_t, std::complex<double> > >, ublas::map_array<std::size_t, ublas::mapped_vector<std::complex<double>, ublas::map_array<std::size_t, std::complex<double> > > > > >, 3>()();
  237. #endif
  238. #endif
  239. #endif
  240. #ifdef USE_STD_MAP
  241. #ifdef USE_FLOAT
  242. std::cout << "mp_test_type, generalized_vector_of_vector std::map" << std::endl;
  243. test_my_matrix<ublas::generalized_vector_of_vector<mp_test_type, ublas::row_major, ublas::vector<ublas::mapped_vector<mp_test_type, std::map<std::size_t, mp_test_type> > > >, 3>()();
  244. test_my_matrix<ublas::generalized_vector_of_vector<mp_test_type, ublas::row_major, ublas::mapped_vector<ublas::mapped_vector<mp_test_type, std::map<std::size_t, mp_test_type> >, std::map<std::size_t, ublas::mapped_vector<mp_test_type, std::map<std::size_t, mp_test_type> > > > >, 3>()();
  245. #endif
  246. #ifdef USE_DOUBLE
  247. std::cout << "double, generalized_vector_of_vector std::map" << std::endl;
  248. test_my_matrix<ublas::generalized_vector_of_vector<double, ublas::row_major, ublas::vector<ublas::mapped_vector<double, std::map<std::size_t, double> > > >, 3>()();
  249. test_my_matrix<ublas::generalized_vector_of_vector<double, ublas::row_major, ublas::mapped_vector<ublas::mapped_vector<double, std::map<std::size_t, double> >, std::map<std::size_t, ublas::mapped_vector<double, std::map<std::size_t, double> > > > >, 3>()();
  250. #endif
  251. #ifdef USE_STD_COMPLEX
  252. #ifdef USE_FLOAT
  253. std::cout << "std::complex<mp_test_type>, generalized_vector_of_vector std::map" << std::endl;
  254. test_my_matrix<ublas::generalized_vector_of_vector<std::complex<mp_test_type>, ublas::row_major, ublas::vector<ublas::mapped_vector<std::complex<mp_test_type>, std::map<std::size_t, std::complex<mp_test_type> > > > >, 3>()();
  255. test_my_matrix<ublas::generalized_vector_of_vector<std::complex<mp_test_type>, ublas::row_major, ublas::mapped_vector<ublas::mapped_vector<std::complex<mp_test_type>, std::map<std::size_t, std::complex<mp_test_type> > >, std::map<std::size_t, ublas::mapped_vector<std::complex<mp_test_type>, std::map<std::size_t, std::complex<mp_test_type> > > > > >, 3>()();
  256. #endif
  257. #ifdef USE_DOUBLE
  258. std::cout << "std::complex<double>, generalized_vector_of_vector std::map" << std::endl;
  259. test_my_matrix<ublas::generalized_vector_of_vector<std::complex<double>, ublas::row_major, ublas::vector<ublas::mapped_vector<std::complex<double>, std::map<std::size_t, std::complex<double> > > > >, 3>()();
  260. test_my_matrix<ublas::generalized_vector_of_vector<std::complex<double>, ublas::row_major, ublas::mapped_vector<ublas::mapped_vector<std::complex<double>, std::map<std::size_t, std::complex<double> > >, std::map<std::size_t, ublas::mapped_vector<std::complex<double>, std::map<std::size_t, std::complex<double> > > > > >, 3>()();
  261. #endif
  262. #endif
  263. #endif
  264. #endif
  265. #ifdef USE_COMPRESSED_MATRIX
  266. #ifdef USE_FLOAT
  267. std::cout << "mp_test_type compressed_matrix" << std::endl;
  268. test_my_matrix<ublas::compressed_matrix<mp_test_type>, 3>()();
  269. #endif
  270. #ifdef USE_DOUBLE
  271. std::cout << "double compressed_matrix" << std::endl;
  272. test_my_matrix<ublas::compressed_matrix<double>, 3>()();
  273. #endif
  274. #ifdef USE_STD_COMPLEX
  275. #ifdef USE_FLOAT
  276. std::cout << "std::complex<mp_test_type> compressed_matrix" << std::endl;
  277. test_my_matrix<ublas::compressed_matrix<std::complex<mp_test_type> >, 3>()();
  278. #endif
  279. #ifdef USE_DOUBLE
  280. std::cout << "std::complex<double> compressed_matrix" << std::endl;
  281. test_my_matrix<ublas::compressed_matrix<std::complex<double> >, 3>()();
  282. #endif
  283. #endif
  284. #endif
  285. #ifdef USE_COORDINATE_MATRIX
  286. #ifdef USE_FLOAT
  287. std::cout << "mp_test_type coordinate_matrix" << std::endl;
  288. test_my_matrix<ublas::coordinate_matrix<mp_test_type>, 3>()();
  289. #endif
  290. #ifdef USE_DOUBLE
  291. std::cout << "double coordinate_matrix" << std::endl;
  292. test_my_matrix<ublas::coordinate_matrix<double>, 3>()();
  293. #endif
  294. #ifdef USE_STD_COMPLEX
  295. #ifdef USE_FLOAT
  296. std::cout << "std::complex<mp_test_type> coordinate_matrix" << std::endl;
  297. test_my_matrix<ublas::coordinate_matrix<std::complex<mp_test_type> >, 3>()();
  298. #endif
  299. #ifdef USE_DOUBLE
  300. std::cout << "std::complex<double> coordinate_matrix" << std::endl;
  301. test_my_matrix<ublas::coordinate_matrix<std::complex<double> >, 3>()();
  302. #endif
  303. #endif
  304. #endif
  305. }