test42.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. #include "utils.hpp"
  14. // Test matrix & vector expression templates
  15. template<class V, class M, int N>
  16. struct test_my_matrix_vector {
  17. typedef typename V::value_type value_type;
  18. template<class VP, class MP>
  19. void test_with (VP &v1, VP &v2, MP &m1) const {
  20. {
  21. #ifndef USE_DIAGONAL
  22. // Rows and columns
  23. initialize_matrix (m1);
  24. for (int i = 0; i < N; ++ i) {
  25. v2 = ublas::row (m1, i);
  26. std::cout << "row (m, " << i << ") = " << v2 << std::endl;
  27. v2 = ublas::column (m1, i);
  28. std::cout << "column (m, " << i << ") = " << v2 << std::endl;
  29. }
  30. // Outer product
  31. initialize_vector (v1);
  32. initialize_vector (v2);
  33. v1 (0) = 0;
  34. v1 (N - 1) = 0;
  35. m1 = ublas::outer_prod (v1, v2);
  36. std::cout << "outer_prod (v1, v2) = " << m1 << std::endl;
  37. // Matrix vector product
  38. initialize_matrix (m1);
  39. initialize_vector (v1);
  40. v2 = ublas::prod (m1, v1);
  41. std::cout << "prod (m1, v1) = " << v2 << std::endl;
  42. v2 = ublas::prod (v1, m1);
  43. std::cout << "prod (v1, m1) = " << v2 << std::endl;
  44. #else
  45. BOOST_UBLAS_NOT_USED(v1);
  46. BOOST_UBLAS_NOT_USED(v2);
  47. BOOST_UBLAS_NOT_USED(m1);
  48. #endif
  49. }
  50. }
  51. void operator () () const {
  52. {
  53. V v1 (N), v2 (N);
  54. #ifdef USE_BANDED
  55. M m1 (N, N, 1, 1);
  56. #endif
  57. #ifdef USE_DIAGONAL
  58. M m1 (N, N);
  59. #endif
  60. test_with (v1, v2, m1);
  61. ublas::matrix_row<M> mr1 (m1, 1), mr2 (m1, 1);
  62. test_with (mr1, mr2, m1);
  63. ublas::matrix_column<M> mc1 (m1, 1), mc2 (m1, 1);
  64. test_with (mc1, mc2, m1);
  65. #ifdef USE_RANGE
  66. ublas::matrix_vector_range<M> mvr1 (m1, ublas::range (0, N), ublas::range (0, N)),
  67. mvr2 (m1, ublas::range (0, N), ublas::range (0, N));
  68. test_with (mvr1, mvr2, m1);
  69. #endif
  70. #ifdef USE_SLICE
  71. ublas::matrix_vector_slice<M> mvs1 (m1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)),
  72. mvs2 (m1, ublas::slice (0, 1, N), ublas::slice (0, 1, N));
  73. test_with (mvs1, mvs2, m1);
  74. #endif
  75. }
  76. }
  77. void operator () (int) const {
  78. #ifdef USE_ADAPTOR
  79. {
  80. #ifdef USE_BANDED
  81. V v1 (N), v2 (N);
  82. M m1 (N, N, 1, 1);
  83. ublas::banded_adaptor<M> bam1 (m1, 1, 1);
  84. test_with (v1, v2, bam1);
  85. ublas::matrix_row<ublas::banded_adaptor<M> > mr1 (bam1, 1), mr2 (bam1, 1);
  86. test_with (mr1, mr2, bam1);
  87. ublas::matrix_column<ublas::banded_adaptor<M> > mc1 (bam1, 1), mc2 (bam1, 1);
  88. test_with (mc1, mc2, bam1);
  89. #ifdef USE_RANGE
  90. ublas::matrix_vector_range<ublas::banded_adaptor<M> > mvr1 (bam1, ublas::range (0, N), ublas::range (0, N)),
  91. mvr2 (bam1, ublas::range (0, N), ublas::range (0, N));
  92. test_with (mvr1, mvr2, bam1);
  93. #endif
  94. #ifdef USE_SLICE
  95. ublas::matrix_vector_slice<ublas::banded_adaptor<M> > mvs1 (bam1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)),
  96. mvs2 (bam1, ublas::slice (0, 1, N), ublas::slice (0, 1, N));
  97. test_with (mvs1, mvs2, bam1);
  98. #endif
  99. #endif
  100. #ifdef USE_DIAGONAL
  101. V v1 (N), v2 (N);
  102. M m1 (N, N);
  103. ublas::diagonal_adaptor<M> dam1 (m1);
  104. test_with (v1, v2, dam1);
  105. ublas::matrix_row<ublas::diagonal_adaptor<M> > mr1 (dam1, 1), mr2 (dam1, 1);
  106. test_with (mr1, mr2, dam1);
  107. ublas::matrix_column<ublas::diagonal_adaptor<M> > mc1 (dam1, 1), mc2 (dam1, 1);
  108. test_with (mc1, mc2, dam1);
  109. #ifdef USE_RANGE
  110. ublas::matrix_vector_range<ublas::diagonal_adaptor<M> > mvr1 (dam1, ublas::range (0, N), ublas::range (0, N)),
  111. mvr2 (dam1, ublas::range (0, N), ublas::range (0, N));
  112. test_with (mvr1, mvr2, dam1);
  113. #endif
  114. #ifdef USE_SLICE
  115. ublas::matrix_vector_slice<ublas::diagonal_adaptor<M> > mvs1 (dam1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)),
  116. mvs2 (dam1, ublas::slice (0, 1, N), ublas::slice (0, 1, N));
  117. test_with (mvs1, mvs2, dam1);
  118. #endif
  119. #endif
  120. }
  121. #endif
  122. }
  123. };
  124. // Test matrix & vector
  125. void test_matrix_vector () {
  126. std::cout << "test_matrix_vector" << std::endl;
  127. #ifdef USE_BANDED
  128. #ifdef USE_BOUNDED_ARRAY
  129. #ifdef USE_FLOAT
  130. std::cout << "float, bounded_array" << std::endl;
  131. test_my_matrix_vector<ublas::vector<float, ublas::bounded_array<float, 3> >,
  132. ublas::banded_matrix<float, ublas::row_major, ublas::bounded_array<float, 3 * 3> >, 3> () ();
  133. test_my_matrix_vector<ublas::vector<float, ublas::bounded_array<float, 3> >,
  134. ublas::banded_matrix<float, ublas::row_major, ublas::bounded_array<float, 3 * 3> >, 3> () (0);
  135. #endif
  136. #ifdef USE_DOUBLE
  137. std::cout << "double, bounded_array" << std::endl;
  138. test_my_matrix_vector<ublas::vector<double, ublas::bounded_array<double, 3> >,
  139. ublas::banded_matrix<double, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3> () ();
  140. test_my_matrix_vector<ublas::vector<double, ublas::bounded_array<double, 3> >,
  141. ublas::banded_matrix<double, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3> () (0);
  142. #endif
  143. #ifdef USE_STD_COMPLEX
  144. #ifdef USE_FLOAT
  145. std::cout << "std::complex<float>, bounded_array" << std::endl;
  146. test_my_matrix_vector<ublas::vector<std::complex<float>, ublas::bounded_array<std::complex<float>, 3> >,
  147. ublas::banded_matrix<std::complex<float>, ublas::row_major, ublas::bounded_array<std::complex<float>, 3 * 3> >, 3> () ();
  148. test_my_matrix_vector<ublas::vector<std::complex<float>, ublas::bounded_array<std::complex<float>, 3> >,
  149. ublas::banded_matrix<std::complex<float>, ublas::row_major, ublas::bounded_array<std::complex<float>, 3 * 3> >, 3> () (0);
  150. #endif
  151. #ifdef USE_DOUBLE
  152. std::cout << "std::complex<double>, bounded_array" << std::endl;
  153. test_my_matrix_vector<ublas::vector<std::complex<double>, ublas::bounded_array<std::complex<double>, 3> >,
  154. ublas::banded_matrix<std::complex<double>, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3> () ();
  155. test_my_matrix_vector<ublas::vector<std::complex<double>, ublas::bounded_array<std::complex<double>, 3> >,
  156. ublas::banded_matrix<std::complex<double>, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3> () (0);
  157. #endif
  158. #endif
  159. #endif
  160. #ifdef USE_UNBOUNDED_ARRAY
  161. #ifdef USE_FLOAT
  162. std::cout << "float, unbounded_array" << std::endl;
  163. test_my_matrix_vector<ublas::vector<float, ublas::unbounded_array<float> >,
  164. ublas::banded_matrix<float, ublas::row_major, ublas::unbounded_array<float> >, 3> () ();
  165. test_my_matrix_vector<ublas::vector<float, ublas::unbounded_array<float> >,
  166. ublas::banded_matrix<float, ublas::row_major, ublas::unbounded_array<float> >, 3> () (0);
  167. #endif
  168. #ifdef USE_DOUBLE
  169. std::cout << "double, unbounded_array" << std::endl;
  170. test_my_matrix_vector<ublas::vector<double, ublas::unbounded_array<double> >,
  171. ublas::banded_matrix<double, ublas::row_major, ublas::unbounded_array<double> >, 3> () ();
  172. test_my_matrix_vector<ublas::vector<double, ublas::unbounded_array<double> >,
  173. ublas::banded_matrix<double, ublas::row_major, ublas::unbounded_array<double> >, 3> () (0);
  174. #endif
  175. #ifdef USE_STD_COMPLEX
  176. #ifdef USE_FLOAT
  177. std::cout << "std::complex<float>, unbounded_array" << std::endl;
  178. test_my_matrix_vector<ublas::vector<std::complex<float>, ublas::unbounded_array<std::complex<float> > >,
  179. ublas::banded_matrix<std::complex<float>, ublas::row_major, ublas::unbounded_array<std::complex<float> > >, 3> () ();
  180. test_my_matrix_vector<ublas::vector<std::complex<float>, ublas::unbounded_array<std::complex<float> > >,
  181. ublas::banded_matrix<std::complex<float>, ublas::row_major, ublas::unbounded_array<std::complex<float> > >, 3> () (0);
  182. #endif
  183. #ifdef USE_DOUBLE
  184. std::cout << "std::complex<double>, unbounded_array" << std::endl;
  185. test_my_matrix_vector<ublas::vector<std::complex<double>, ublas::unbounded_array<std::complex<double> > >,
  186. ublas::banded_matrix<std::complex<double>, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3> () ();
  187. test_my_matrix_vector<ublas::vector<std::complex<double>, ublas::unbounded_array<std::complex<double> > >,
  188. ublas::banded_matrix<std::complex<double>, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3> () (0);
  189. #endif
  190. #endif
  191. #endif
  192. #ifdef USE_STD_VECTOR
  193. #ifdef USE_FLOAT
  194. std::cout << "float, std::vector" << std::endl;
  195. test_my_matrix_vector<ublas::vector<float, std::vector<float> >,
  196. ublas::banded_matrix<float, ublas::row_major, std::vector<float> >, 3> () ();
  197. test_my_matrix_vector<ublas::vector<float, std::vector<float> >,
  198. ublas::banded_matrix<float, ublas::row_major, std::vector<float> >, 3> () (0);
  199. #endif
  200. #ifdef USE_DOUBLE
  201. std::cout << "double, std::vector" << std::endl;
  202. test_my_matrix_vector<ublas::vector<double, std::vector<double> >,
  203. ublas::banded_matrix<double, ublas::row_major, std::vector<double> >, 3> () ();
  204. test_my_matrix_vector<ublas::vector<double, std::vector<double> >,
  205. ublas::banded_matrix<double, ublas::row_major, std::vector<double> >, 3> () (0);
  206. #endif
  207. #ifdef USE_STD_COMPLEX
  208. #ifdef USE_FLOAT
  209. std::cout << "std::complex<float>, std::vector" << std::endl;
  210. test_my_matrix_vector<ublas::vector<std::complex<float>, std::vector<std::complex<float> > >,
  211. ublas::banded_matrix<std::complex<float>, ublas::row_major, std::vector<std::complex<float> > >, 3> () ();
  212. test_my_matrix_vector<ublas::vector<std::complex<float>, std::vector<std::complex<float> > >,
  213. ublas::banded_matrix<std::complex<float>, ublas::row_major, std::vector<std::complex<float> > >, 3> () (0);
  214. #endif
  215. #ifdef USE_DOUBLE
  216. std::cout << "std::complex<double>, std::vector" << std::endl;
  217. test_my_matrix_vector<ublas::vector<std::complex<double>, std::vector<std::complex<double> > >,
  218. ublas::banded_matrix<std::complex<double>, ublas::row_major, std::vector<std::complex<double> > >, 3> () ();
  219. test_my_matrix_vector<ublas::vector<std::complex<double>, std::vector<std::complex<double> > >,
  220. ublas::banded_matrix<std::complex<double>, ublas::row_major, std::vector<std::complex<double> > >, 3> () (0);
  221. #endif
  222. #endif
  223. #endif
  224. #endif
  225. #ifdef USE_DIAGONAL
  226. #ifdef USE_BOUNDED_ARRAY
  227. #ifdef USE_FLOAT
  228. std::cout << "float, bounded_array" << std::endl;
  229. test_my_matrix_vector<ublas::vector<float, ublas::bounded_array<float, 3> >,
  230. ublas::diagonal_matrix<float, ublas::row_major, ublas::bounded_array<float, 3 * 3> >, 3> () ();
  231. test_my_matrix_vector<ublas::vector<float, ublas::bounded_array<float, 3> >,
  232. ublas::diagonal_matrix<float, ublas::row_major, ublas::bounded_array<float, 3 * 3> >, 3> () (0);
  233. #endif
  234. #ifdef USE_DOUBLE
  235. std::cout << "double, bounded_array" << std::endl;
  236. test_my_matrix_vector<ublas::vector<double, ublas::bounded_array<double, 3> >,
  237. ublas::diagonal_matrix<double, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3> () ();
  238. test_my_matrix_vector<ublas::vector<double, ublas::bounded_array<double, 3> >,
  239. ublas::diagonal_matrix<double, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3> () (0);
  240. #endif
  241. #ifdef USE_STD_COMPLEX
  242. #ifdef USE_FLOAT
  243. std::cout << "std::complex<float>, bounded_array" << std::endl;
  244. test_my_matrix_vector<ublas::vector<std::complex<float>, ublas::bounded_array<std::complex<float>, 3> >,
  245. ublas::diagonal_matrix<std::complex<float>, ublas::row_major, ublas::bounded_array<std::complex<float>, 3 * 3> >, 3> () ();
  246. test_my_matrix_vector<ublas::vector<std::complex<float>, ublas::bounded_array<std::complex<float>, 3> >,
  247. ublas::diagonal_matrix<std::complex<float>, ublas::row_major, ublas::bounded_array<std::complex<float>, 3 * 3> >, 3> () (0);
  248. #endif
  249. #ifdef USE_DOUBLE
  250. std::cout << "std::complex<double>, bounded_array" << std::endl;
  251. test_my_matrix_vector<ublas::vector<std::complex<double>, ublas::bounded_array<std::complex<double>, 3> >,
  252. ublas::diagonal_matrix<std::complex<double>, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3> () ();
  253. test_my_matrix_vector<ublas::vector<std::complex<double>, ublas::bounded_array<std::complex<double>, 3> >,
  254. ublas::diagonal_matrix<std::complex<double>, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3> () (0);
  255. #endif
  256. #endif
  257. #endif
  258. #ifdef USE_UNBOUNDED_ARRAY
  259. #ifdef USE_FLOAT
  260. std::cout << "float, unbounded_array" << std::endl;
  261. test_my_matrix_vector<ublas::vector<float, ublas::unbounded_array<float> >,
  262. ublas::diagonal_matrix<float, ublas::row_major, ublas::unbounded_array<float> >, 3> () ();
  263. test_my_matrix_vector<ublas::vector<float, ublas::unbounded_array<float> >,
  264. ublas::diagonal_matrix<float, ublas::row_major, ublas::unbounded_array<float> >, 3> () (0);
  265. #endif
  266. #ifdef USE_DOUBLE
  267. std::cout << "double, unbounded_array" << std::endl;
  268. test_my_matrix_vector<ublas::vector<double, ublas::unbounded_array<double> >,
  269. ublas::diagonal_matrix<double, ublas::row_major, ublas::unbounded_array<double> >, 3> () ();
  270. test_my_matrix_vector<ublas::vector<double, ublas::unbounded_array<double> >,
  271. ublas::diagonal_matrix<double, ublas::row_major, ublas::unbounded_array<double> >, 3> () (0);
  272. #endif
  273. #ifdef USE_STD_COMPLEX
  274. #ifdef USE_FLOAT
  275. std::cout << "std::complex<float>, unbounded_array" << std::endl;
  276. test_my_matrix_vector<ublas::vector<std::complex<float>, ublas::unbounded_array<std::complex<float> > >,
  277. ublas::diagonal_matrix<std::complex<float>, ublas::row_major, ublas::unbounded_array<std::complex<float> > >, 3> () ();
  278. test_my_matrix_vector<ublas::vector<std::complex<float>, ublas::unbounded_array<std::complex<float> > >,
  279. ublas::diagonal_matrix<std::complex<float>, ublas::row_major, ublas::unbounded_array<std::complex<float> > >, 3> () (0);
  280. #endif
  281. #ifdef USE_DOUBLE
  282. std::cout << "std::complex<double>, unbounded_array" << std::endl;
  283. test_my_matrix_vector<ublas::vector<std::complex<double>, ublas::unbounded_array<std::complex<double> > >,
  284. ublas::diagonal_matrix<std::complex<double>, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3> () ();
  285. test_my_matrix_vector<ublas::vector<std::complex<double>, ublas::unbounded_array<std::complex<double> > >,
  286. ublas::diagonal_matrix<std::complex<double>, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3> () (0);
  287. #endif
  288. #endif
  289. #endif
  290. #ifdef USE_STD_VECTOR
  291. #ifdef USE_FLOAT
  292. std::cout << "float, std::vector" << std::endl;
  293. test_my_matrix_vector<ublas::vector<float, std::vector<float> >,
  294. ublas::diagonal_matrix<float, ublas::row_major, std::vector<float> >, 3> () ();
  295. test_my_matrix_vector<ublas::vector<float, std::vector<float> >,
  296. ublas::diagonal_matrix<float, ublas::row_major, std::vector<float> >, 3> () (0);
  297. #endif
  298. #ifdef USE_DOUBLE
  299. std::cout << "double, std::vector" << std::endl;
  300. test_my_matrix_vector<ublas::vector<double, std::vector<double> >,
  301. ublas::diagonal_matrix<double, ublas::row_major, std::vector<double> >, 3> () ();
  302. test_my_matrix_vector<ublas::vector<double, std::vector<double> >,
  303. ublas::diagonal_matrix<double, ublas::row_major, std::vector<double> >, 3> () (0);
  304. #endif
  305. #ifdef USE_STD_COMPLEX
  306. #ifdef USE_FLOAT
  307. std::cout << "std::complex<float>, std::vector" << std::endl;
  308. test_my_matrix_vector<ublas::vector<std::complex<float>, std::vector<std::complex<float> > >,
  309. ublas::diagonal_matrix<std::complex<float>, ublas::row_major, std::vector<std::complex<float> > >, 3> () ();
  310. test_my_matrix_vector<ublas::vector<std::complex<float>, std::vector<std::complex<float> > >,
  311. ublas::diagonal_matrix<std::complex<float>, ublas::row_major, std::vector<std::complex<float> > >, 3> () (0);
  312. #endif
  313. #ifdef USE_DOUBLE
  314. std::cout << "std::complex<double>, std::vector" << std::endl;
  315. test_my_matrix_vector<ublas::vector<std::complex<double>, std::vector<std::complex<double> > >,
  316. ublas::diagonal_matrix<std::complex<double>, ublas::row_major, std::vector<std::complex<double> > >, 3> () ();
  317. test_my_matrix_vector<ublas::vector<std::complex<double>, std::vector<std::complex<double> > >,
  318. ublas::diagonal_matrix<std::complex<double>, ublas::row_major, std::vector<std::complex<double> > >, 3> () (0);
  319. #endif
  320. #endif
  321. #endif
  322. #endif
  323. }