test53.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "test5.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. // Transpose of a triangular isn't triangular of the same kind
  84. std::cout << "trans (m1) = " << ublas::trans(m1) << std::endl;
  85. // Hermitian
  86. initialize_matrix(m1);
  87. // Hermitian of a triangular isn't hermitian of the same kind
  88. std::cout << "herm (m1) = " << ublas::herm(m1) << std::endl;
  89. // Matrix multiplication
  90. initialize_matrix(m1);
  91. initialize_matrix(m2);
  92. m3 = ublas::prod(m1, m2);
  93. std::cout << "prod (m1, m2) = " << m3 << std::endl;
  94. }
  95. }
  96. void operator()() const
  97. {
  98. {
  99. M m1(N, N), m2(N, N), m3(N, N);
  100. test_with(m1, m2, m3);
  101. #ifdef USE_RANGE
  102. ublas::matrix_range<M> mr1(m1, ublas::range(0, N), ublas::range(0, N)),
  103. mr2(m2, ublas::range(0, N), ublas::range(0, N)),
  104. mr3(m3, ublas::range(0, N), ublas::range(0, N));
  105. test_with(mr1, mr2, mr3);
  106. #endif
  107. #ifdef USE_SLICE
  108. ublas::matrix_slice<M> ms1(m1, ublas::slice(0, 1, N), ublas::slice(0, 1, N)),
  109. ms2(m2, ublas::slice(0, 1, N), ublas::slice(0, 1, N)),
  110. ms3(m3, ublas::slice(0, 1, N), ublas::slice(0, 1, N));
  111. test_with(ms1, ms2, ms3);
  112. #endif
  113. }
  114. #ifdef USE_ADAPTOR
  115. {
  116. M m1(N, N), m2(N, N), m3(N, N);
  117. ublas::triangular_adaptor<M> tam1(m1), tam2(m2), tam3(m3);
  118. test_with(tam1, tam2, tam3);
  119. #ifdef USE_RANGE
  120. ublas::matrix_range<ublas::triangular_adaptor<M> > mr1(tam1, ublas::range(0, N), ublas::range(0, N)),
  121. mr2(tam2, ublas::range(0, N), ublas::range(0, N)),
  122. mr3(tam3, ublas::range(0, N), ublas::range(0, N));
  123. test_with(mr1, mr2, mr3);
  124. #endif
  125. #ifdef USE_SLICE
  126. ublas::matrix_slice<ublas::triangular_adaptor<M> > ms1(tam1, ublas::slice(0, 1, N), ublas::slice(0, 1, N)),
  127. ms2(tam2, ublas::slice(0, 1, N), ublas::slice(0, 1, N)),
  128. ms3(tam3, ublas::slice(0, 1, N), ublas::slice(0, 1, N));
  129. test_with(ms1, ms2, ms3);
  130. #endif
  131. }
  132. #endif
  133. }
  134. };
  135. // Test matrix
  136. void test_matrix()
  137. {
  138. std::cout << "test_matrix" << std::endl;
  139. #ifdef USE_BOUNDED_ARRAY
  140. #ifdef USE_FLOAT
  141. std::cout << "mp_test_type, bounded_array" << std::endl;
  142. test_my_matrix<ublas::triangular_matrix<mp_test_type, ublas::lower, ublas::row_major, ublas::bounded_array<mp_test_type, 3 * 3> >, 3>()();
  143. #endif
  144. #ifdef USE_DOUBLE
  145. std::cout << "double, bounded_array" << std::endl;
  146. test_my_matrix<ublas::triangular_matrix<double, ublas::lower, ublas::row_major, ublas::bounded_array<double, 3 * 3> >, 3>()();
  147. #endif
  148. #ifdef USE_STD_COMPLEX
  149. #ifdef USE_FLOAT
  150. std::cout << "std::complex<mp_test_type>, bounded_array" << std::endl;
  151. test_my_matrix<ublas::triangular_matrix<std::complex<mp_test_type>, ublas::lower, ublas::row_major, ublas::bounded_array<std::complex<mp_test_type>, 3 * 3> >, 3>()();
  152. #endif
  153. #ifdef USE_DOUBLE
  154. std::cout << "std::complex<double>, bounded_array" << std::endl;
  155. test_my_matrix<ublas::triangular_matrix<std::complex<double>, ublas::lower, ublas::row_major, ublas::bounded_array<std::complex<double>, 3 * 3> >, 3>()();
  156. #endif
  157. #endif
  158. #endif
  159. #ifdef USE_UNBOUNDED_ARRAY
  160. #ifdef USE_FLOAT
  161. std::cout << "mp_test_type, unbounded_array" << std::endl;
  162. test_my_matrix<ublas::triangular_matrix<mp_test_type, ublas::lower, ublas::row_major, ublas::unbounded_array<mp_test_type> >, 3>()();
  163. #endif
  164. #ifdef USE_DOUBLE
  165. std::cout << "double, unbounded_array" << std::endl;
  166. test_my_matrix<ublas::triangular_matrix<double, ublas::lower, ublas::row_major, ublas::unbounded_array<double> >, 3>()();
  167. #endif
  168. #ifdef USE_STD_COMPLEX
  169. #ifdef USE_FLOAT
  170. std::cout << "std::complex<mp_test_type>, unbounded_array" << std::endl;
  171. test_my_matrix<ublas::triangular_matrix<std::complex<mp_test_type>, ublas::lower, ublas::row_major, ublas::unbounded_array<std::complex<mp_test_type> > >, 3>()();
  172. #endif
  173. #ifdef USE_DOUBLE
  174. std::cout << "std::complex<double>, unbounded_array" << std::endl;
  175. test_my_matrix<ublas::triangular_matrix<std::complex<double>, ublas::lower, ublas::row_major, ublas::unbounded_array<std::complex<double> > >, 3>()();
  176. #endif
  177. #endif
  178. #endif
  179. #ifdef USE_STD_VECTOR
  180. #ifdef USE_FLOAT
  181. std::cout << "mp_test_type, std::vector" << std::endl;
  182. test_my_matrix<ublas::triangular_matrix<mp_test_type, ublas::lower, ublas::row_major, std::vector<mp_test_type> >, 3>()();
  183. #endif
  184. #ifdef USE_DOUBLE
  185. std::cout << "double, std::vector" << std::endl;
  186. test_my_matrix<ublas::triangular_matrix<double, ublas::lower, ublas::row_major, std::vector<double> >, 3>()();
  187. #endif
  188. #ifdef USE_STD_COMPLEX
  189. #ifdef USE_FLOAT
  190. std::cout << "std::complex<mp_test_type>, std::vector" << std::endl;
  191. test_my_matrix<ublas::triangular_matrix<std::complex<mp_test_type>, ublas::lower, ublas::row_major, std::vector<std::complex<mp_test_type> > >, 3>()();
  192. #endif
  193. #ifdef USE_DOUBLE
  194. std::cout << "std::complex<double>, std::vector" << std::endl;
  195. test_my_matrix<ublas::triangular_matrix<std::complex<double>, ublas::lower, ublas::row_major, std::vector<std::complex<double> > >, 3>()();
  196. #endif
  197. #endif
  198. #endif
  199. }