test33.cpp 17 KB

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