test63.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 "test6.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 = m1 - 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. m3 = ublas::prod (m1, m2);
  88. std::cout << "prod (m1, m2) = " << m3 << std::endl;
  89. }
  90. }
  91. void operator () () const {
  92. {
  93. M m1 (N, N), m2 (N, N), m3 (N, N);
  94. test_with (m1, m2, m3);
  95. #ifdef USE_RANGE
  96. ublas::matrix_range<M> mr1 (m1, ublas::range (0, N), ublas::range (0, N)),
  97. mr2 (m2, ublas::range (0, N), ublas::range (0, N)),
  98. mr3 (m3, ublas::range (0, N), ublas::range (0, N));
  99. test_with (mr1, mr2, mr3);
  100. #endif
  101. #ifdef USE_SLICE
  102. ublas::matrix_slice<M> ms1 (m1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)),
  103. ms2 (m2, ublas::slice (0, 1, N), ublas::slice (0, 1, N)),
  104. ms3 (m3, ublas::slice (0, 1, N), ublas::slice (0, 1, N));
  105. test_with (ms1, ms2, ms3);
  106. #endif
  107. }
  108. #ifdef USE_ADAPTOR
  109. {
  110. M m1 (N, N), m2 (N, N), m3 (N, N);
  111. ublas::symmetric_adaptor<M> sam1 (m1), sam2 (m2), sam3 (m3);
  112. test_with (sam1, sam2, sam3);
  113. #ifdef USE_RANGE
  114. ublas::matrix_range<ublas::symmetric_adaptor<M> > mr1 (sam1, ublas::range (0, N), ublas::range (0, N)),
  115. mr2 (sam2, ublas::range (0, N), ublas::range (0, N)),
  116. mr3 (sam3, ublas::range (0, N), ublas::range (0, N));
  117. test_with (mr1, mr2, mr3);
  118. #endif
  119. #ifdef USE_SLICE
  120. ublas::matrix_slice<ublas::symmetric_adaptor<M> > ms1 (sam1, ublas::slice (0, 1, N), ublas::slice (0, 1, N)),
  121. ms2 (sam2, ublas::slice (0, 1, N), ublas::slice (0, 1, N)),
  122. ms3 (sam3, ublas::slice (0, 1, N), ublas::slice (0, 1, N));
  123. test_with (ms1, ms2, ms3);
  124. #endif
  125. }
  126. #endif
  127. }
  128. };
  129. // Test matrix
  130. void test_matrix () {
  131. std::cout << "test_matrix" << std::endl;
  132. #ifdef USE_BOUNDED_ARRAY
  133. #ifdef USE_FLOAT
  134. std::cout << "float, bounded_array" << std::endl;
  135. test_my_matrix<ublas::symmetric_matrix<float, ublas::lower, ublas::row_major, ublas::bounded_array<float, 3 * 3> >, 3 > () ();
  136. #endif
  137. #ifdef USE_DOUBLE
  138. std::cout << "double, bounded_array" << std::endl;
  139. test_my_matrix<ublas::symmetric_matrix<double, ublas::lower, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3 > () ();
  140. #endif
  141. #ifdef USE_STD_COMPLEX
  142. #ifdef USE_FLOAT
  143. std::cout << "std::complex<float>, bounded_array" << std::endl;
  144. test_my_matrix<ublas::symmetric_matrix<std::complex<float>, ublas::lower, ublas::row_major, ublas::bounded_array<std::complex<float>, 3 * 3> >, 3 > () ();
  145. #endif
  146. #ifdef USE_DOUBLE
  147. std::cout << "std::complex<double>, bounded_array" << std::endl;
  148. test_my_matrix<ublas::symmetric_matrix<std::complex<double>, ublas::lower, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3 > () ();
  149. #endif
  150. #endif
  151. #endif
  152. #ifdef USE_UNBOUNDED_ARRAY
  153. #ifdef USE_FLOAT
  154. std::cout << "float, unbounded_array" << std::endl;
  155. test_my_matrix<ublas::symmetric_matrix<float, ublas::lower, ublas::row_major, ublas::unbounded_array<float> >, 3 > () ();
  156. #endif
  157. #ifdef USE_DOUBLE
  158. std::cout << "double, unbounded_array" << std::endl;
  159. test_my_matrix<ublas::symmetric_matrix<double, ublas::lower, ublas::row_major, ublas::unbounded_array<double> >, 3 > () ();
  160. #endif
  161. #ifdef USE_STD_COMPLEX
  162. #ifdef USE_FLOAT
  163. std::cout << "std::complex<float>, unbounded_array" << std::endl;
  164. test_my_matrix<ublas::symmetric_matrix<std::complex<float>, ublas::lower, ublas::row_major, ublas::unbounded_array<std::complex<float> > >, 3 > () ();
  165. #endif
  166. #ifdef USE_DOUBLE
  167. std::cout << "std::complex<double>, unbounded_array" << std::endl;
  168. test_my_matrix<ublas::symmetric_matrix<std::complex<double>, ublas::lower, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3 > () ();
  169. #endif
  170. #endif
  171. #endif
  172. #ifdef USE_STD_VECTOR
  173. #ifdef USE_FLOAT
  174. std::cout << "float, std::vector" << std::endl;
  175. test_my_matrix<ublas::symmetric_matrix<float, ublas::lower, ublas::row_major, std::vector<float> >, 3 > () ();
  176. #endif
  177. #ifdef USE_DOUBLE
  178. std::cout << "double, std::vector" << std::endl;
  179. test_my_matrix<ublas::symmetric_matrix<double, ublas::lower, ublas::row_major, std::vector<double> >, 3 > () ();
  180. #endif
  181. #ifdef USE_STD_COMPLEX
  182. #ifdef USE_FLOAT
  183. std::cout << "std::complex<float>, std::vector" << std::endl;
  184. test_my_matrix<ublas::symmetric_matrix<std::complex<float>, ublas::lower, ublas::row_major, std::vector<std::complex<float> > >, 3 > () ();
  185. #endif
  186. #ifdef USE_DOUBLE
  187. std::cout << "std::complex<double>, std::vector" << std::endl;
  188. test_my_matrix<ublas::symmetric_matrix<std::complex<double>, ublas::lower, ublas::row_major, std::vector<std::complex<double> > >, 3 > () ();
  189. #endif
  190. #endif
  191. #endif
  192. }