You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
9.0 KiB

  1. /*===========================================================================
  2. Copyright (C) 2007-2015 Yves Renard, Julien Pommier.
  3. This file is a part of GETFEM++
  4. Getfem++ is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version along with the GCC Runtime Library
  8. Exception either version 3.1 or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  12. License and GCC Runtime Library Exception for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  16. ===========================================================================*/
  17. // SQUARED_MATRIX_PARAM
  18. // VECTOR_PARAM;
  19. // VECTOR_PARAM;
  20. // RECTANGULAR_MATRIX_PARAM;
  21. // VECTOR_PARAM;
  22. // VECTOR_PARAM;
  23. // ENDPARAM;
  24. #include "gmm/gmm_kernel.h"
  25. #include "gmm/gmm_dense_lu.h"
  26. #include "gmm/gmm_dense_qr.h"
  27. #include "gmm/gmm_condition_number.h"
  28. using std::endl; using std::cout; using std::cerr;
  29. using std::ends; using std::cin;
  30. using gmm::size_type;
  31. template <typename MAT1 , typename MAT2, typename VECT1, typename VECT2,
  32. typename VECT3, typename VECT4>
  33. void test_procedure2(const MAT1 &m1_, const VECT1 &v1_, const VECT2 &v2_,
  34. const MAT2 &m2_, const VECT3 &v3_, const VECT4 &v4_) {
  35. VECT1 &v1 = const_cast<VECT1 &>(v1_);
  36. VECT2 &v2 = const_cast<VECT2 &>(v2_);
  37. VECT3 &v3 = const_cast<VECT3 &>(v3_);
  38. VECT4 &v4 = const_cast<VECT4 &>(v4_);
  39. MAT1 &m1 = const_cast<MAT1 &>(m1_);
  40. MAT2 &m2 = const_cast<MAT2 &>(m2_);
  41. typedef typename gmm::linalg_traits<MAT1>::value_type T;
  42. typedef typename gmm::number_traits<T>::magnitude_type R;
  43. R prec = gmm::default_tol(R());
  44. size_type m = gmm::vect_size(v1), n = gmm::vect_size(v3);
  45. size_type nn = std::min(m,n), mm = std::max(m, n);
  46. std::vector<T> v6(m);
  47. R det = gmm::abs(gmm::lu_det(m1)), error;
  48. R cond = gmm::condition_number(m1);
  49. if (prec * cond < R(1)/R(10000) && det != R(0)) {
  50. gmm::lu_solve(m1, v6, v2);
  51. gmm::mult(m1, v6, v1);
  52. gmm::add(gmm::scaled(v1, T(-1)), v2, v6);
  53. if (!((error = gmm::vect_norm2(v6)) <= prec * cond * R(10000)))
  54. GMM_ASSERT1(false, "Error too large: "<< error);
  55. gmm::lu_solve(gmm::transposed(m1), v6, v2);
  56. gmm::mult(gmm::transposed(m1), v6, v1);
  57. gmm::add(gmm::scaled(v1, T(-1)), v2, v6);
  58. if (!((error = gmm::vect_norm2(v6)) <= prec * cond * R(10000)))
  59. GMM_ASSERT1(false, "Error too large: "<< error);
  60. gmm::lu_solve(gmm::conjugated(m1), v6, v2);
  61. gmm::mult(gmm::conjugated(m1), v6, v1);
  62. gmm::add(gmm::scaled(v1, T(-1)), v2, v6);
  63. if (!((error = gmm::vect_norm2(v6)) <= prec * cond * R(10000)))
  64. GMM_ASSERT1(false, "Error too large: "<< error);
  65. gmm::lu_solve(gmm::transposed(gmm::conjugated(m1)), v6, v2);
  66. gmm::mult(gmm::transposed(gmm::conjugated(m1)), v6, v1);
  67. gmm::add(gmm::scaled(v1, T(-1)), v2, v6);
  68. if (!((error = gmm::vect_norm2(v6)) <= prec * cond * R(10000)))
  69. GMM_ASSERT1(false, "Error too large: "<< error);
  70. gmm::lu_solve(gmm::transposed(gmm::scaled(m1, T(-6))), v6, v2);
  71. gmm::mult(gmm::transposed(gmm::scaled(m1, T(-6))), v6, v1);
  72. gmm::add(gmm::scaled(v1, T(-1)), v2, v6);
  73. if (!((error = gmm::vect_norm2(v6)) <= prec * cond * R(10000)))
  74. GMM_ASSERT1(false, "Error too large: "<< error);
  75. }
  76. gmm::dense_matrix<T> q(mm, nn), r(nn, nn);
  77. if (m >= n) {
  78. std::vector<T> v5(m);
  79. gmm::mult(m2, v3, v2);
  80. gmm::qr_factor(m2, q, r);
  81. gmm::mult(r, v3, v4);
  82. gmm::mult(q, v4, gmm::scaled(v2, T(-1)), v5);
  83. if (!((error = gmm::vect_norm2(v5)) <= prec * R(10000)))
  84. GMM_ASSERT1(false, "Error too large: "<< error);
  85. }
  86. else {
  87. std::vector<T> v5(n);
  88. gmm::mult(gmm::conjugated(m2), v2, v3);
  89. gmm::qr_factor(gmm::conjugated(m2), q, r);
  90. gmm::mult(r, v2, v1);
  91. gmm::mult(q, v1, gmm::scaled(v3, T(-1)), v5);
  92. if (!((error = gmm::vect_norm2(v5)) <= prec * R(10000)))
  93. GMM_ASSERT1(false, "Error too large: "<< error);
  94. }
  95. }
  96. template<typename MAT> void test_mat_swap(MAT &, gmm::linalg_modifiable) {}
  97. template<typename MAT> void test_mat_swap(MAT &, gmm::linalg_const) {}
  98. template<typename MAT> void test_mat_swap(MAT &M, gmm::linalg_false) {
  99. typedef typename gmm::linalg_traits<MAT>::value_type T;
  100. typedef typename gmm::number_traits<T>::magnitude_type R;
  101. size_type m = gmm::mat_nrows(M), n = gmm::mat_ncols(M);
  102. MAT M2(m, n);
  103. gmm::dense_matrix<T> M3(m, n);
  104. gmm::copy(M, M3);
  105. std::swap(M, M2);
  106. gmm::add(gmm::scaled(M2, T(-1)), M3);
  107. if (gmm::mat_euclidean_norm(M3) > R(0) || gmm::mat_euclidean_norm(M) > R(0))
  108. GMM_ASSERT1(false, "Error in swap");
  109. }
  110. template<typename VECT> void test_vect_swap(VECT &, gmm::linalg_modifiable) {}
  111. template<typename VECT> void test_vect_swap(VECT &, gmm::linalg_const) {}
  112. template<typename VECT> void test_vect_swap(VECT &V, gmm::linalg_false) {
  113. typedef typename gmm::linalg_traits<VECT>::value_type T;
  114. typedef typename gmm::number_traits<T>::magnitude_type R;
  115. size_type n = gmm::vect_size(V);
  116. VECT V2(n);
  117. std::vector<T> V3(n);
  118. gmm::copy(V, V3);
  119. std::swap(V, V2);
  120. gmm::add(gmm::scaled(V2, T(-1)), V3);
  121. if (gmm::vect_norm2(V3) > R(0) || gmm::vect_norm2(V) > R(0))
  122. GMM_ASSERT1(false, "Error in swap");
  123. }
  124. template <typename MAT1 , typename MAT2, typename VECT1, typename VECT2,
  125. typename VECT3, typename VECT4>
  126. bool test_procedure(const MAT1 &m1_, const VECT1 &v1_, const VECT2 &v2_,
  127. const MAT2 &m2_, const VECT3 &v3_, const VECT4 &v4_) {
  128. VECT1 &v1 = const_cast<VECT1 &>(v1_);
  129. VECT2 &v2 = const_cast<VECT2 &>(v2_);
  130. VECT3 &v3 = const_cast<VECT3 &>(v3_);
  131. VECT4 &v4 = const_cast<VECT4 &>(v4_);
  132. MAT1 &m1 = const_cast<MAT1 &>(m1_);
  133. MAT2 &m2 = const_cast<MAT2 &>(m2_);
  134. typedef typename gmm::linalg_traits<MAT1>::value_type T;
  135. typedef typename gmm::number_traits<T>::magnitude_type R;
  136. R prec = gmm::default_tol(R());
  137. static size_type nb_iter(0);
  138. ++nb_iter;
  139. test_procedure2(m1, v1, v2, m2, v3, v4);
  140. size_type m = gmm::vect_size(v1), n = gmm::vect_size(v3);
  141. gmm::csr_matrix<T> mm1(m, m);
  142. gmm::copy(m1, mm1);
  143. gmm::csc_matrix<T> mm2(m, n);
  144. gmm::copy(m2, mm2);
  145. test_procedure2(mm1, v1, v2, mm2, v3, v4);
  146. size_type mm = m / 2, nn = n / 2;
  147. gmm::sub_interval SUBI(0, mm), SUBJ(0, nn);
  148. test_procedure2(gmm::sub_matrix(mm1, SUBI),
  149. gmm::sub_vector(v1, SUBI),
  150. gmm::sub_vector(v2, SUBI),
  151. gmm::sub_matrix(mm2, SUBI, SUBJ),
  152. gmm::sub_vector(v3, SUBJ),
  153. gmm::sub_vector(v4, SUBJ));
  154. gmm::add(gmm::scaled(mm1, T(-1)), m1);
  155. gmm::add(gmm::scaled(mm2, T(-1)), m2);
  156. R error = gmm::mat_euclidean_norm(m1) + gmm::mat_euclidean_norm(m2);
  157. if (!(error <= prec * R(10000)))
  158. GMM_ASSERT1(false, "Error too large: "<< error);
  159. // test for row_vector and col_vector
  160. std::vector<T> v5(gmm::vect_size(v2));
  161. gmm::mult(m2, v3, v2);
  162. gmm::copy(v2, v5);
  163. gmm::mult(m2, gmm::col_vector(v3), gmm::col_vector(v2));
  164. gmm::add(gmm::scaled(v5, T(-1)), v2);
  165. error = gmm::vect_norm2(v2);
  166. if (!(error <= prec))
  167. GMM_ASSERT1(false, "Error too large: " << error);
  168. gmm::mult(gmm::row_vector(gmm::conjugated(v3)), gmm::conjugated(m2),
  169. gmm::row_vector(v2));
  170. gmm::add(gmm::conjugated(gmm::scaled(v5, T(-1))), v2);
  171. error = gmm::vect_norm2(v2);
  172. if (!(error <= prec))
  173. GMM_ASSERT1(false, "Error too large: " << error);
  174. if (gmm::is_original_linalg(m1)) {
  175. size_type a = gmm::mat_nrows(m1), b = gmm::mat_ncols(m1);
  176. size_type a2 = gmm::irandom(size_type(a));
  177. size_type b2 = gmm::irandom(size_type(b));
  178. gmm::dense_matrix<T> m3(a, b);
  179. gmm::copy(m1, m3);
  180. gmm::resize(m1, a+a2, b+b2);
  181. for (size_type i = 0; i < a+a2; ++i)
  182. for (size_type j = 0; j < b+b2; ++j) {
  183. if (i < a && j < b) {
  184. if (m3(i, j) != m1(i, j))
  185. GMM_ASSERT1(false, "Error in resize");
  186. }
  187. else
  188. if (m1(i, j) != T(0))
  189. GMM_ASSERT1(false, "Error in resize");
  190. }
  191. gmm::resize(m1, a2, b2);
  192. for (size_type i = 0; i < a2; ++i)
  193. for (size_type j = 0; j < b2; ++j)
  194. if (m3(i, j) != m1(i, j))
  195. GMM_ASSERT1(false, "Error in resize");
  196. }
  197. if (gmm::is_original_linalg(v1)) {
  198. size_type a = gmm::vect_size(v1);
  199. size_type a2 = gmm::irandom(size_type(a));
  200. std::vector<T> v6(a);
  201. gmm::copy(v1, v6);
  202. gmm::resize(v1, a+a2);
  203. for (size_type i = 0; i < a+a2; ++i) {
  204. if (i < a) {
  205. if (v1[i] != v6[i])
  206. GMM_ASSERT1(false, "Error in resize");
  207. }
  208. else
  209. if (v1[i] != T(0))
  210. GMM_ASSERT1(false, "Error in resize");
  211. }
  212. gmm::resize(v1, a2);
  213. for (size_type i = 0; i < a2; ++i)
  214. if (v1[i] != v6[i])
  215. GMM_ASSERT1(false, "Error in resize");
  216. }
  217. test_mat_swap(m1, typename gmm::linalg_traits<MAT1>::is_reference());
  218. test_vect_swap(v1, typename gmm::linalg_traits<VECT1>::is_reference());
  219. if (nb_iter == 100) return true;
  220. return false;
  221. }