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.

317 lines
14 KiB

  1. /* -*- c++ -*- (enables emacs c++ mode) */
  2. /*===========================================================================
  3. Copyright (C) 2003-2012 Yves Renard, Caroline Lecalvez
  4. This file is a part of GETFEM++
  5. Getfem++ is free software; you can redistribute it and/or modify it
  6. under the terms of the GNU Lesser General Public License as published
  7. by the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version along with the GCC Runtime Library
  9. Exception either version 3.1 or (at your option) any later version.
  10. This program is distributed in the hope that it will be useful, but
  11. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  13. License and GCC Runtime Library Exception for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program; if not, write to the Free Software Foundation,
  16. Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  17. As a special exception, you may use this file as it is a part of a free
  18. software library without restriction. Specifically, if other files
  19. instantiate templates or use macros or inline functions from this file,
  20. or you compile this file and link it with other files to produce an
  21. executable, this file does not by itself cause the resulting executable
  22. to be covered by the GNU Lesser General Public License. This exception
  23. does not however invalidate any other reasons why the executable file
  24. might be covered by the GNU Lesser General Public License.
  25. ===========================================================================*/
  26. /**@file gmm_dense_Householder.h
  27. @author Caroline Lecalvez <Caroline.Lecalvez@gmm.insa-toulouse.fr>
  28. @author Yves Renard <Yves.Renard@insa-lyon.fr>
  29. @date June 5, 2003.
  30. @brief Householder for dense matrices.
  31. */
  32. #ifndef GMM_DENSE_HOUSEHOLDER_H
  33. #define GMM_DENSE_HOUSEHOLDER_H
  34. #include "gmm_kernel.h"
  35. namespace gmm {
  36. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  37. /* ********************************************************************* */
  38. /* Rank one update (complex and real version) */
  39. /* ********************************************************************* */
  40. template <typename Matrix, typename VecX, typename VecY>
  41. inline void rank_one_update(Matrix &A, const VecX& x,
  42. const VecY& y, row_major) {
  43. typedef typename linalg_traits<Matrix>::value_type T;
  44. size_type N = mat_nrows(A);
  45. GMM_ASSERT2(N <= vect_size(x) && mat_ncols(A) <= vect_size(y),
  46. "dimensions mismatch");
  47. typename linalg_traits<VecX>::const_iterator itx = vect_const_begin(x);
  48. for (size_type i = 0; i < N; ++i, ++itx) {
  49. typedef typename linalg_traits<Matrix>::sub_row_type row_type;
  50. row_type row = mat_row(A, i);
  51. typename linalg_traits<row_type>::iterator
  52. it = vect_begin(row), ite = vect_end(row);
  53. typename linalg_traits<VecY>::const_iterator ity = vect_const_begin(y);
  54. T tx = *itx;
  55. for (; it != ite; ++it, ++ity) *it += conj_product(*ity, tx);
  56. }
  57. }
  58. template <typename Matrix, typename VecX, typename VecY>
  59. inline void rank_one_update(Matrix &A, const VecX& x,
  60. const VecY& y, col_major) {
  61. typedef typename linalg_traits<Matrix>::value_type T;
  62. size_type M = mat_ncols(A);
  63. GMM_ASSERT2(mat_nrows(A) <= vect_size(x) && M <= vect_size(y),
  64. "dimensions mismatch");
  65. typename linalg_traits<VecY>::const_iterator ity = vect_const_begin(y);
  66. for (size_type i = 0; i < M; ++i, ++ity) {
  67. typedef typename linalg_traits<Matrix>::sub_col_type col_type;
  68. col_type col = mat_col(A, i);
  69. typename linalg_traits<col_type>::iterator
  70. it = vect_begin(col), ite = vect_end(col);
  71. typename linalg_traits<VecX>::const_iterator itx = vect_const_begin(x);
  72. T ty = *ity;
  73. for (; it != ite; ++it, ++itx) *it += conj_product(ty, *itx);
  74. }
  75. }
  76. ///@endcond
  77. template <typename Matrix, typename VecX, typename VecY>
  78. inline void rank_one_update(const Matrix &AA, const VecX& x,
  79. const VecY& y) {
  80. Matrix& A = const_cast<Matrix&>(AA);
  81. rank_one_update(A, x, y, typename principal_orientation_type<typename
  82. linalg_traits<Matrix>::sub_orientation>::potype());
  83. }
  84. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  85. /* ********************************************************************* */
  86. /* Rank two update (complex and real version) */
  87. /* ********************************************************************* */
  88. template <typename Matrix, typename VecX, typename VecY>
  89. inline void rank_two_update(Matrix &A, const VecX& x,
  90. const VecY& y, row_major) {
  91. typedef typename linalg_traits<Matrix>::value_type value_type;
  92. size_type N = mat_nrows(A);
  93. GMM_ASSERT2(N <= vect_size(x) && mat_ncols(A) <= vect_size(y),
  94. "dimensions mismatch");
  95. typename linalg_traits<VecX>::const_iterator itx1 = vect_const_begin(x);
  96. typename linalg_traits<VecY>::const_iterator ity2 = vect_const_begin(y);
  97. for (size_type i = 0; i < N; ++i, ++itx1, ++ity2) {
  98. typedef typename linalg_traits<Matrix>::sub_row_type row_type;
  99. row_type row = mat_row(A, i);
  100. typename linalg_traits<row_type>::iterator
  101. it = vect_begin(row), ite = vect_end(row);
  102. typename linalg_traits<VecX>::const_iterator itx2 = vect_const_begin(x);
  103. typename linalg_traits<VecY>::const_iterator ity1 = vect_const_begin(y);
  104. value_type tx = *itx1, ty = *ity2;
  105. for (; it != ite; ++it, ++ity1, ++itx2)
  106. *it += conj_product(*ity1, tx) + conj_product(*itx2, ty);
  107. }
  108. }
  109. template <typename Matrix, typename VecX, typename VecY>
  110. inline void rank_two_update(Matrix &A, const VecX& x,
  111. const VecY& y, col_major) {
  112. typedef typename linalg_traits<Matrix>::value_type value_type;
  113. size_type M = mat_ncols(A);
  114. GMM_ASSERT2(mat_nrows(A) <= vect_size(x) && M <= vect_size(y),
  115. "dimensions mismatch");
  116. typename linalg_traits<VecX>::const_iterator itx2 = vect_const_begin(x);
  117. typename linalg_traits<VecY>::const_iterator ity1 = vect_const_begin(y);
  118. for (size_type i = 0; i < M; ++i, ++ity1, ++itx2) {
  119. typedef typename linalg_traits<Matrix>::sub_col_type col_type;
  120. col_type col = mat_col(A, i);
  121. typename linalg_traits<col_type>::iterator
  122. it = vect_begin(col), ite = vect_end(col);
  123. typename linalg_traits<VecX>::const_iterator itx1 = vect_const_begin(x);
  124. typename linalg_traits<VecY>::const_iterator ity2 = vect_const_begin(y);
  125. value_type ty = *ity1, tx = *itx2;
  126. for (; it != ite; ++it, ++itx1, ++ity2)
  127. *it += conj_product(ty, *itx1) + conj_product(tx, *ity2);
  128. }
  129. }
  130. ///@endcond
  131. template <typename Matrix, typename VecX, typename VecY>
  132. inline void rank_two_update(const Matrix &AA, const VecX& x,
  133. const VecY& y) {
  134. Matrix& A = const_cast<Matrix&>(AA);
  135. rank_two_update(A, x, y, typename principal_orientation_type<typename
  136. linalg_traits<Matrix>::sub_orientation>::potype());
  137. }
  138. ///@cond DOXY_SHOW_ALL_FUNCTIONS
  139. /* ********************************************************************* */
  140. /* Householder vector computation (complex and real version) */
  141. /* ********************************************************************* */
  142. template <typename VECT> void house_vector(const VECT &VV) {
  143. VECT &V = const_cast<VECT &>(VV);
  144. typedef typename linalg_traits<VECT>::value_type T;
  145. typedef typename number_traits<T>::magnitude_type R;
  146. R mu = vect_norm2(V), abs_v0 = gmm::abs(V[0]);
  147. if (mu != R(0))
  148. gmm::scale(V, (abs_v0 == R(0)) ? T(R(1) / mu)
  149. : (safe_divide(T(abs_v0), V[0]) / (abs_v0 + mu)));
  150. if (gmm::real(V[vect_size(V)-1]) * R(0) != R(0)) gmm::clear(V);
  151. V[0] = T(1);
  152. }
  153. template <typename VECT> void house_vector_last(const VECT &VV) {
  154. VECT &V = const_cast<VECT &>(VV);
  155. typedef typename linalg_traits<VECT>::value_type T;
  156. typedef typename number_traits<T>::magnitude_type R;
  157. size_type m = vect_size(V);
  158. R mu = vect_norm2(V), abs_v0 = gmm::abs(V[m-1]);
  159. if (mu != R(0))
  160. gmm::scale(V, (abs_v0 == R(0)) ? T(R(1) / mu)
  161. : ((abs_v0 / V[m-1]) / (abs_v0 + mu)));
  162. if (gmm::real(V[0]) * R(0) != R(0)) gmm::clear(V);
  163. V[m-1] = T(1);
  164. }
  165. /* ********************************************************************* */
  166. /* Householder updates (complex and real version) */
  167. /* ********************************************************************* */
  168. // multiply A to the left by the reflector stored in V. W is a temporary.
  169. template <typename MAT, typename VECT1, typename VECT2> inline
  170. void row_house_update(const MAT &AA, const VECT1 &V, const VECT2 &WW) {
  171. VECT2 &W = const_cast<VECT2 &>(WW); MAT &A = const_cast<MAT &>(AA);
  172. typedef typename linalg_traits<MAT>::value_type value_type;
  173. typedef typename number_traits<value_type>::magnitude_type magnitude_type;
  174. gmm::mult(conjugated(A),
  175. scaled(V, value_type(magnitude_type(-2)/vect_norm2_sqr(V))), W);
  176. rank_one_update(A, V, W);
  177. }
  178. // multiply A to the right by the reflector stored in V. W is a temporary.
  179. template <typename MAT, typename VECT1, typename VECT2> inline
  180. void col_house_update(const MAT &AA, const VECT1 &V, const VECT2 &WW) {
  181. VECT2 &W = const_cast<VECT2 &>(WW); MAT &A = const_cast<MAT &>(AA);
  182. typedef typename linalg_traits<MAT>::value_type value_type;
  183. typedef typename number_traits<value_type>::magnitude_type magnitude_type;
  184. gmm::mult(A,
  185. scaled(V, value_type(magnitude_type(-2)/vect_norm2_sqr(V))), W);
  186. rank_one_update(A, W, V);
  187. }
  188. ///@endcond
  189. /* ********************************************************************* */
  190. /* Hessemberg reduction with Householder. */
  191. /* ********************************************************************* */
  192. template <typename MAT1, typename MAT2>
  193. void Hessenberg_reduction(const MAT1& AA, const MAT2 &QQ, bool compute_Q){
  194. MAT1& A = const_cast<MAT1&>(AA); MAT2& Q = const_cast<MAT2&>(QQ);
  195. typedef typename linalg_traits<MAT1>::value_type value_type;
  196. if (compute_Q) gmm::copy(identity_matrix(), Q);
  197. size_type n = mat_nrows(A); if (n < 2) return;
  198. std::vector<value_type> v(n), w(n);
  199. sub_interval SUBK(0,n);
  200. for (size_type k = 1; k+1 < n; ++k) {
  201. sub_interval SUBI(k, n-k), SUBJ(k-1,n-k+1);
  202. v.resize(n-k);
  203. for (size_type j = k; j < n; ++j) v[j-k] = A(j, k-1);
  204. house_vector(v);
  205. row_house_update(sub_matrix(A, SUBI, SUBJ), v, sub_vector(w, SUBJ));
  206. col_house_update(sub_matrix(A, SUBK, SUBI), v, w);
  207. // is it possible to "unify" the two on the common part of the matrix?
  208. if (compute_Q) col_house_update(sub_matrix(Q, SUBK, SUBI), v, w);
  209. }
  210. }
  211. /* ********************************************************************* */
  212. /* Householder tridiagonalization for symmetric matrices */
  213. /* ********************************************************************* */
  214. template <typename MAT1, typename MAT2>
  215. void Householder_tridiagonalization(const MAT1 &AA, const MAT2 &QQ,
  216. bool compute_q) {
  217. MAT1 &A = const_cast<MAT1 &>(AA); MAT2 &Q = const_cast<MAT2 &>(QQ);
  218. typedef typename linalg_traits<MAT1>::value_type T;
  219. typedef typename number_traits<T>::magnitude_type R;
  220. size_type n = mat_nrows(A); if (n < 2) return;
  221. std::vector<T> v(n), p(n), w(n), ww(n);
  222. sub_interval SUBK(0,n);
  223. for (size_type k = 1; k+1 < n; ++k) { // not optimized ...
  224. sub_interval SUBI(k, n-k);
  225. v.resize(n-k); p.resize(n-k); w.resize(n-k);
  226. for (size_type l = k; l < n; ++l)
  227. { v[l-k] = w[l-k] = A(l, k-1); A(l, k-1) = A(k-1, l) = T(0); }
  228. house_vector(v);
  229. R norm = vect_norm2_sqr(v);
  230. A(k-1, k) = gmm::conj(A(k, k-1) = w[0] - T(2)*v[0]*vect_hp(w, v)/norm);
  231. gmm::mult(sub_matrix(A, SUBI), gmm::scaled(v, T(-2) / norm), p);
  232. gmm::add(p, gmm::scaled(v, -vect_hp(v, p) / norm), w);
  233. rank_two_update(sub_matrix(A, SUBI), v, w);
  234. // it should be possible to compute only the upper or lower part
  235. if (compute_q) col_house_update(sub_matrix(Q, SUBK, SUBI), v, ww);
  236. }
  237. }
  238. /* ********************************************************************* */
  239. /* Real and complex Givens rotations */
  240. /* ********************************************************************* */
  241. template <typename T> void Givens_rotation(T a, T b, T &c, T &s) {
  242. typedef typename number_traits<T>::magnitude_type R;
  243. R aa = gmm::abs(a), bb = gmm::abs(b);
  244. if (bb == R(0)) { c = T(1); s = T(0); return; }
  245. if (aa == R(0)) { c = T(0); s = b / bb; return; }
  246. if (bb > aa)
  247. { T t = -safe_divide(a,b); s = T(R(1) / (sqrt(R(1)+gmm::abs_sqr(t)))); c = s * t; }
  248. else
  249. { T t = -safe_divide(b,a); c = T(R(1) / (sqrt(R(1)+gmm::abs_sqr(t)))); s = c * t; }
  250. }
  251. // Apply Q* v
  252. template <typename T> inline
  253. void Apply_Givens_rotation_left(T &x, T &y, T c, T s)
  254. { T t1=x, t2=y; x = gmm::conj(c)*t1 - gmm::conj(s)*t2; y = c*t2 + s*t1; }
  255. // Apply v^T Q
  256. template <typename T> inline
  257. void Apply_Givens_rotation_right(T &x, T &y, T c, T s)
  258. { T t1=x, t2=y; x = c*t1 - s*t2; y = gmm::conj(c)*t2 + gmm::conj(s)*t1; }
  259. template <typename MAT, typename T>
  260. void row_rot(const MAT &AA, T c, T s, size_type i, size_type k) {
  261. MAT &A = const_cast<MAT &>(AA); // can be specialized for row matrices
  262. for (size_type j = 0; j < mat_ncols(A); ++j)
  263. Apply_Givens_rotation_left(A(i,j), A(k,j), c, s);
  264. }
  265. template <typename MAT, typename T>
  266. void col_rot(const MAT &AA, T c, T s, size_type i, size_type k) {
  267. MAT &A = const_cast<MAT &>(AA); // can be specialized for column matrices
  268. for (size_type j = 0; j < mat_nrows(A); ++j)
  269. Apply_Givens_rotation_right(A(j,i), A(j,k), c, s);
  270. }
  271. }
  272. #endif