GetFEM  5.6
gmm_dense_qr.h
Go to the documentation of this file.
1 /* -*- c++ -*- (enables emacs c++ mode) */
2 /*===========================================================================
3 
4  Copyright (C) 2003-2026 Yves Renard
5 
6  This file is a part of GetFEM
7 
8  GetFEM is free software; you can redistribute it and/or modify it
9  under the terms of the GNU Lesser General Public License as published
10  by the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version along with the GCC Runtime Library
12  Exception either version 3.1 or (at your option) any later version.
13  This program is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
16  License and GCC Runtime Library Exception for more details.
17  You should have received a copy of the GNU Lesser General Public License
18  along with this program. If not, see https://www.gnu.org/licenses/.
19 
20  As a special exception, you may use this file as it is a part of a free
21  software library without restriction. Specifically, if other files
22  instantiate templates or use macros or inline functions from this file,
23  or you compile this file and link it with other files to produce an
24  executable, this file does not by itself cause the resulting executable
25  to be covered by the GNU Lesser General Public License. This exception
26  does not however invalidate any other reasons why the executable file
27  might be covered by the GNU Lesser General Public License.
28 
29 ===========================================================================*/
30 
31 /**@file gmm_dense_qr.h
32  @author Caroline Lecalvez, Caroline.Lecalvez@gmm.insa-tlse.fr, Yves Renard <Yves.Renard@insa-lyon.fr>
33  @date September 12, 2003.
34  @brief Dense QR factorization.
35 */
36 #ifndef GMM_DENSE_QR_H
37 #define GMM_DENSE_QR_H
38 
39 #include "gmm_dense_Householder.h"
40 
41 namespace gmm {
42 
43 
44  /**
45  QR factorization using Householder method (complex and real version).
46  */
47  template <typename MAT>
48  void qr_factor(const MAT &A_) {
49  MAT &A = const_cast<MAT &>(A_);
50  typedef typename linalg_traits<MAT>::value_type T;
51  typedef typename number_traits<T>::magnitude_type R;
52 
53  const size_type m = mat_nrows(A), n = mat_ncols(A);
54  GMM_ASSERT2(m >= n, "dimensions mismatch");
55  if (n == 0) return;
56 
57  std::vector<T> W(m), V(m);
58  // skip the last reflection for *real* square matrices (m-1 limit)
59  // lapack does the same in {sd}larfg.f but not in {cz}larfg.f
60  const size_type jmax = (m==n && !is_complex(T())) ? m-1
61  : n;
62  for (size_type j = 0; j < jmax; ++j) {
63  sub_interval SUBI(j, m-j), SUBJ(j, n-j);
64  V.resize(m-j); W.resize(n-j);
65 
66  for (size_type i = j; i < m; ++i) V[i-j] = A(i, j);
67  house_vector(V);
68 
69  R Vnorm2 = vect_norm2_sqr(V);
70  if (vect_norminf(sub_vector(V, sub_interval(1, m-j-1))) > R(0)
71  || gmm::imag(A(j, j)) != R(0)) { // compatible with {sdcz}larfg.f
72  auto subA = sub_matrix(A, SUBI, SUBJ);
73  gmm::mult(conjugated(subA),
74  scaled(V, T(R(-2)/Vnorm2)), W);
75  rank_one_update(subA, V, W);
76  for (size_type i = j+1; i < m; ++i) A(i, j) = V[i-j];
77  }
78  }
79  }
80 
81 
82  // QR comes from QR_factor(QR) where the upper triangular part stands for R
83  // and the lower part contains the Householder reflectors.
84  // A <- AQ
85  template <typename MAT1, typename MAT2>
86  void apply_house_right(const MAT1 &QR, const MAT2 &A_) {
87  MAT2 &A = const_cast<MAT2 &>(A_);
88  typedef typename linalg_traits<MAT1>::value_type T;
89  typedef typename number_traits<T>::magnitude_type R;
90 
91  const size_type m = mat_nrows(QR), n = mat_ncols(QR);
92  GMM_ASSERT2(m == mat_ncols(A), "dimensions mismatch");
93  if (m == 0) return;
94  std::vector<T> V(m), W(mat_nrows(A));
95  V[0] = T(1);
96  // assume that the last reflection was skipped for *real* square matrices
97  const size_type jmax = (m==n && !is_complex(T())) ? m-1
98  : n;
99  for (size_type j = 0; j < jmax; ++j) {
100  V.resize(m-j);
101  for (size_type i = j+1; i < m; ++i)
102  V[i-j] = QR(i, j);
103  R Vnorm2 = vect_norm2_sqr(V);
104  if (vect_norminf(sub_vector(V, sub_interval(1, m-j-1))) > R(0)
105  || gmm::imag(QR(j, j)) != R(0)) { // compatible with {sdcz}larfg.f
106  auto subA = sub_matrix(A, sub_interval(0, mat_nrows(A)),
107  sub_interval(j, m-j));
108  gmm::mult(subA, scaled(V, T(R(-2)/Vnorm2)), W);
109  rank_one_update(subA, W, V);
110  }
111  }
112  }
113 
114  // QR comes from QR_factor(QR) where the upper triangular part stands for R
115  // and the lower part contains the Householder reflectors.
116  // A <- Q*A
117  template <typename MAT1, typename MAT2>
118  void apply_house_left(const MAT1 &QR, const MAT2 &A_) {
119  MAT2 &A = const_cast<MAT2 &>(A_);
120  typedef typename linalg_traits<MAT1>::value_type T;
121  typedef typename number_traits<T>::magnitude_type R;
122 
123  const size_type m = mat_nrows(QR), n = mat_ncols(QR);
124  GMM_ASSERT2(m == mat_nrows(A), "dimensions mismatch");
125  if (m == 0) return;
126  std::vector<T> V(m), W(mat_ncols(A));
127  V[0] = T(1);
128  // assume that the last reflection was skipped for *real* square matrices
129  const size_type jmax = (m==n && !is_complex(T())) ? m-1
130  : n;
131  for (size_type j = 0; j < jmax; ++j) {
132  V.resize(m-j);
133  for (size_type i = j+1; i < m; ++i)
134  V[i-j] = QR(i, j);
135  R Vnorm2 = vect_norm2_sqr(V);
136  if (vect_norminf(sub_vector(V, sub_interval(1, m-j-1))) > R(0)
137  || gmm::imag(QR(j, j)) != R(0)) { // compatible with {sdcz}larfg.f
138  auto subA = sub_matrix(A, sub_interval(j, m-j),
139  sub_interval(0, mat_ncols(A)));
140  gmm::mult(conjugated(subA),
141  scaled(V, T(R(-2)/Vnorm2)), W);
142  rank_one_update(subA, V, W);
143  }
144  }
145  }
146 
147  /** Compute the QR factorization, where Q is assembled. */
148  template <typename MAT1, typename MAT2, typename MAT3>
149  void qr_factor(const MAT1 &A, const MAT2 &QQ, const MAT3 &RR) {
150  MAT2 &Q = const_cast<MAT2 &>(QQ); MAT3 &R = const_cast<MAT3 &>(RR);
151  typedef typename linalg_traits<MAT1>::value_type T;
152 
153  const size_type m = mat_nrows(A), n = mat_ncols(A);
154  GMM_ASSERT2(m >= n, "dimensions mismatch");
155  if (n == 0) return;
156  gmm::copy(A, Q);
157 
158  std::vector<T> W(m);
159  dense_matrix<T> VV(m, n);
160  // skip the last reflection for *real* square matrices (m-1 limit)
161  // lapack does the same in dlarfg.f but not in zlarfg.f
162  const size_type jmax = (m==n && !is_complex(T())) ? m-1
163  : n;
164  for (size_type j = 0; j < jmax; ++j) {
165  sub_interval SUBI(j, m-j), SUBJ(j, n-j);
166 
167  for (size_type i = j; i < m; ++i) VV(i,j) = Q(i, j);
168  house_vector(sub_vector(mat_col(VV,j), SUBI));
169 
170  row_house_update(sub_matrix(Q, SUBI, SUBJ),
171  sub_vector(mat_col(VV,j), SUBI), sub_vector(W, SUBJ));
172  }
173 
174  gmm::copy(sub_matrix(Q, sub_interval(0, n), sub_interval(0, n)), R);
175  gmm::copy(identity_matrix(), Q);
176  // assume that the last reflection was skipped for *real* square matrices
177  const size_type jstart = (m==n && !is_complex(T())) ? m-2
178  : n-1;
179  for (size_type j = jstart; j != size_type(-1); --j) {
180  sub_interval SUBI(j, m-j), SUBJ(j, n-j);
181  row_house_update(sub_matrix(Q, SUBI, SUBJ),
182  sub_vector(mat_col(VV,j), SUBI), sub_vector(W, SUBJ));
183  }
184  }
185 
186  ///@cond DOXY_SHOW_ALL_FUNCTIONS
187  template <typename TA, typename TV, typename Ttol,
188  typename MAT, typename VECT>
189  void extract_eig(const MAT &A, VECT &V, Ttol tol, TA, TV) {
190  size_type n = mat_nrows(A);
191  if (n == 0) return;
192  tol *= Ttol(2);
193  Ttol tol_i = tol * gmm::abs(A(0,0)), tol_cplx = tol_i;
194  for (size_type i = 0; i < n; ++i) {
195  if (i < n-1) {
196  tol_i = (gmm::abs(A(i,i))+gmm::abs(A(i+1,i+1)))*tol;
197  tol_cplx = std::max(tol_cplx, tol_i);
198  }
199  if ((i < n-1) && gmm::abs(A(i+1,i)) >= tol_i) {
200  TA tr = A(i,i) + A(i+1, i+1);
201  TA det = A(i,i)*A(i+1, i+1) - A(i,i+1)*A(i+1, i);
202  TA delta = tr*tr - TA(4) * det;
203  if (delta < -tol_cplx) {
204  GMM_WARNING1("A complex eigenvalue has been detected : "
205  << std::complex<TA>(tr/TA(2), gmm::sqrt(-delta)/TA(2)));
206  V[i] = V[i+1] = tr / TA(2);
207  }
208  else {
209  delta = std::max(TA(0), delta);
210  V[i ] = TA(tr + gmm::sqrt(delta))/ TA(2);
211  V[i+1] = TA(tr - gmm::sqrt(delta))/ TA(2);
212  }
213  ++i;
214  }
215  else
216  V[i] = TV(A(i,i));
217  }
218  }
219 
220  template <typename TA, typename TV, typename Ttol,
221  typename MAT, typename VECT>
222  void extract_eig(const MAT &A, VECT &V, Ttol tol, TA, std::complex<TV>) {
223  size_type n = mat_nrows(A);
224  tol *= Ttol(2);
225  for (size_type i = 0; i < n; ++i)
226  if ((i == n-1) ||
227  gmm::abs(A(i+1,i)) < (gmm::abs(A(i,i))+gmm::abs(A(i+1,i+1)))*tol)
228  V[i] = std::complex<TV>(A(i,i));
229  else {
230  TA tr = A(i,i) + A(i+1, i+1);
231  TA det = A(i,i)*A(i+1, i+1) - A(i,i+1)*A(i+1, i);
232  TA delta = tr*tr - TA(4) * det;
233  if (delta < TA(0)) {
234  V[i] = std::complex<TV>(tr / TA(2), gmm::sqrt(-delta) / TA(2));
235  V[i+1] = std::complex<TV>(tr / TA(2), -gmm::sqrt(-delta)/ TA(2));
236  }
237  else {
238  V[i ] = TA(tr + gmm::sqrt(delta)) / TA(2);
239  V[i+1] = TA(tr - gmm::sqrt(delta)) / TA(2);
240  }
241  ++i;
242  }
243  }
244 
245  template <typename TA, typename TV, typename Ttol,
246  typename MAT, typename VECT>
247  void extract_eig(const MAT &A, VECT &V, Ttol tol, std::complex<TA>, TV) {
248  typedef std::complex<TA> T;
249  size_type n = mat_nrows(A);
250  if (n == 0) return;
251  tol *= Ttol(2);
252  Ttol tol_i = tol * gmm::abs(A(0,0)), tol_cplx = tol_i;
253  for (size_type i = 0; i < n; ++i) {
254  if (i < n-1) {
255  tol_i = (gmm::abs(A(i,i))+gmm::abs(A(i+1,i+1)))*tol;
256  tol_cplx = std::max(tol_cplx, tol_i);
257  }
258  if ((i == n-1) || gmm::abs(A(i+1,i)) < tol_i) {
259  if (gmm::abs(std::imag(A(i,i))) > tol_cplx)
260  GMM_WARNING1("A complex eigenvalue has been detected : "
261  << T(A(i,i)) << " : " << gmm::abs(std::imag(A(i,i)))
262  / gmm::abs(std::real(A(i,i))) << " : " << tol_cplx);
263  V[i] = std::real(A(i,i));
264  }
265  else {
266  T tr = A(i,i) + A(i+1, i+1);
267  T det = A(i,i)*A(i+1, i+1) - A(i,i+1)*A(i+1, i);
268  T delta = tr*tr - TA(4) * det;
269  T a1 = (tr + gmm::sqrt(delta)) / TA(2);
270  T a2 = (tr - gmm::sqrt(delta)) / TA(2);
271  if (gmm::abs(std::imag(a1)) > tol_cplx)
272  GMM_WARNING1("A complex eigenvalue has been detected : " << a1);
273  if (gmm::abs(std::imag(a2)) > tol_cplx)
274  GMM_WARNING1("A complex eigenvalue has been detected : " << a2);
275 
276  V[i] = std::real(a1); V[i+1] = std::real(a2);
277  ++i;
278  }
279  }
280  }
281 
282  template <typename TA, typename TV, typename Ttol,
283  typename MAT, typename VECT>
284  void extract_eig(const MAT &A, VECT &V, Ttol tol,
285  std::complex<TA>, std::complex<TV>) {
286  size_type n = mat_nrows(A);
287  tol *= Ttol(2);
288  for (size_type i = 0; i < n; ++i)
289  if ((i == n-1) ||
290  gmm::abs(A(i+1,i)) < (gmm::abs(A(i,i))+gmm::abs(A(i+1,i+1)))*tol)
291  V[i] = std::complex<TV>(A(i,i));
292  else {
293  std::complex<TA> tr = A(i,i) + A(i+1, i+1);
294  std::complex<TA> det = A(i,i)*A(i+1, i+1) - A(i,i+1)*A(i+1, i);
295  std::complex<TA> delta = tr*tr - TA(4) * det;
296  V[i] = (tr + gmm::sqrt(delta)) / TA(2);
297  V[i+1] = (tr - gmm::sqrt(delta)) / TA(2);
298  ++i;
299  }
300  }
301 
302  ///@endcond
303  /**
304  Compute eigenvalue vector.
305  */
306  template <typename MAT, typename Ttol, typename VECT> inline
307  void extract_eig(const MAT &A, const VECT &V, Ttol tol) {
308  extract_eig(A, const_cast<VECT&>(V), tol,
309  typename linalg_traits<MAT>::value_type(),
310  typename linalg_traits<VECT>::value_type());
311  }
312 
313  /* ********************************************************************* */
314  /* Stop criterion for QR algorithms */
315  /* ********************************************************************* */
316 
317  template <typename MAT, typename Ttol>
318  void qr_stop_criterion(MAT &A, size_type &p, size_type &q, Ttol tol) {
319  typedef typename linalg_traits<MAT>::value_type T;
320  typedef typename number_traits<T>::magnitude_type R;
321  R rmin = default_min(R()) * R(2);
322  size_type n = mat_nrows(A);
323  if (n <= 2) { q = n; p = 0; }
324  else {
325  for (size_type i = 1; i < n-q; ++i)
326  if (gmm::abs(A(i,i-1)) < (gmm::abs(A(i,i))+ gmm::abs(A(i-1,i-1)))*tol
327  || gmm::abs(A(i,i-1)) < rmin)
328  A(i,i-1) = T(0);
329 
330  while ((q < n-1 && A(n-1-q, n-2-q) == T(0)) ||
331  (q < n-2 && A(n-2-q, n-3-q) == T(0))) ++q;
332  if (q >= n-2) q = n;
333  p = n-q; if (p) --p; if (p) --p;
334  while (p > 0 && A(p,p-1) != T(0)) --p;
335  }
336  }
337 
338  template <typename MAT, typename Ttol> inline
339  void symmetric_qr_stop_criterion(const MAT &AA, size_type &p, size_type &q,
340  Ttol tol) {
341  typedef typename linalg_traits<MAT>::value_type T;
342  typedef typename number_traits<T>::magnitude_type R;
343  R rmin = default_min(R()) * R(2);
344  MAT& A = const_cast<MAT&>(AA);
345  size_type n = mat_nrows(A);
346  if (n <= 1) { q = n; p = 0; }
347  else {
348  for (size_type i = 1; i < n-q; ++i)
349  if (gmm::abs(A(i,i-1)) < (gmm::abs(A(i,i))+ gmm::abs(A(i-1,i-1)))*tol
350  || gmm::abs(A(i,i-1)) < rmin)
351  A(i,i-1) = T(0);
352 
353  while (q < n-1 && A(n-1-q, n-2-q) == T(0)) ++q;
354  if (q >= n-1) q = n;
355  p = n-q; if (p) --p; if (p) --p;
356  while (p > 0 && A(p,p-1) != T(0)) --p;
357  }
358  }
359 
360  template <typename VECT1, typename VECT2, typename Ttol> inline
361  void symmetric_qr_stop_criterion(const VECT1 &diag, const VECT2 &sdiag_,
362  size_type &p, size_type &q, Ttol tol) {
363  typedef typename linalg_traits<VECT2>::value_type T;
364  typedef typename number_traits<T>::magnitude_type R;
365  R rmin = default_min(R()) * R(2);
366  VECT2 &sdiag = const_cast<VECT2 &>(sdiag_);
367  size_type n = vect_size(diag);
368  if (n <= 1) { q = n; p = 0; return; }
369  for (size_type i = 1; i < n-q; ++i)
370  if (gmm::abs(sdiag[i-1]) < (gmm::abs(diag[i])+ gmm::abs(diag[i-1]))*tol
371  || gmm::abs(sdiag[i-1]) < rmin)
372  sdiag[i-1] = T(0);
373  while (q < n-1 && sdiag[n-2-q] == T(0)) ++q;
374  if (q >= n-1) q = n;
375  p = n-q; if (p) --p; if (p) --p;
376  while (p > 0 && sdiag[p-1] != T(0)) --p;
377  }
378 
379  /* ********************************************************************* */
380  /* Reduction of the 2x2 diagonal blocks of a Schur form */
381  /* ********************************************************************* */
382 
383  template <typename MATH, typename MATQ, typename Ttol>
384  void block2x2_reduction(MATH &H, MATQ &Q, Ttol tol) {
385  typedef typename linalg_traits<MATH>::value_type T;
386  typedef typename number_traits<T>::magnitude_type R;
387 
388  size_type n = mat_nrows(H), nq = mat_nrows(Q);
389  if (n < 2) return;
390  sub_interval SUBQ(0, nq);
391  std::vector<T> v(2), w(std::max(n, nq)); v[0] = T(1);
392  tol *= Ttol(2);
393  Ttol tol_i = tol * gmm::abs(H(0,0)), tol_cplx = tol_i;
394  for (size_type i = 0; i < n-1; ++i) {
395  tol_i = (gmm::abs(H(i,i))+gmm::abs(H(i+1,i+1)))*tol;
396  tol_cplx = std::max(tol_cplx, tol_i);
397 
398  if (gmm::abs(H(i+1,i)) > tol_i) { // 2x2 block detected
399  T tr = (H(i+1, i+1) - H(i,i)) / T(2);
400  T delta = tr*tr + H(i,i+1)*H(i+1, i);
401 
402  // For real T, blocks with complex eigenvalues are left unreduced.
403  if (is_complex(T()) || gmm::real(delta) >= R(0)) {
404  sub_interval SUBI(i, 2);
405  T theta = (tr - gmm::sqrt(delta)) / H(i+1,i);
406  R a = gmm::abs(theta);
407  v[1] = (a == R(0)) ? T(-1)
408  : gmm::conj(theta) * (R(1) - gmm::sqrt(a*a + R(1)) / a);
409  sub_interval SUBR(i, n-i), SUBC(0, i+2);
410  row_house_update(sub_matrix(H, SUBI, SUBR), v,
411  sub_vector(w, sub_interval(0, n-i)));
412  col_house_update(sub_matrix(H, SUBC, SUBI), v,
413  sub_vector(w, SUBC));
414  col_house_update(sub_matrix(Q, SUBQ, SUBI), v, sub_vector(w, SUBQ));
415  }
416  ++i;
417  }
418  }
419  }
420 
421  /* ********************************************************************* */
422  /* Basic qr algorithm. */
423  /* ********************************************************************* */
424 
425  #define tol_type_for_qr typename number_traits<typename \
426  linalg_traits<MAT1>::value_type>::magnitude_type
427  #define default_tol_for_qr \
428  (gmm::default_tol(tol_type_for_qr()) * tol_type_for_qr(3))
429 
430  // QR method for real or complex square matrices based on QR factorisation.
431  // eigval has to be a complex vector if A has complex eigeinvalues.
432  // Very slow method. Use implicit_qr_method instead.
433  template <typename MAT1, typename VECT, typename MAT2>
434  void rudimentary_qr_algorithm(const MAT1 &A, const VECT &eigval_,
435  const MAT2 &eigvect_,
436  tol_type_for_qr tol = default_tol_for_qr,
437  bool compvect = true) {
438  VECT &eigval = const_cast<VECT &>(eigval_);
439  MAT2 &eigvect = const_cast<MAT2 &>(eigvect_);
440 
441  typedef typename linalg_traits<MAT1>::value_type value_type;
442 
443  size_type n = mat_nrows(A), p, q = 0, ite = 0;
444  dense_matrix<value_type> Q(n, n), R(n,n), A1(n,n);
445  gmm::copy(A, A1);
446 
447  Hessenberg_reduction(A1, eigvect, compvect);
448  qr_stop_criterion(A1, p, q, tol);
449 
450  while (q < n) {
451  qr_factor(A1, Q, R);
452  gmm::mult(R, Q, A1);
453  if (compvect) { gmm::mult(eigvect, Q, R); gmm::copy(R, eigvect); }
454 
455  qr_stop_criterion(A1, p, q, tol);
456  ++ite;
457  GMM_ASSERT1(ite < n*1000, "QR algorithm failed");
458  }
459  if (compvect) block2x2_reduction(A1, Q, tol);
460  extract_eig(A1, eigval, tol);
461  }
462 
463  template <typename MAT1, typename VECT>
464  void rudimentary_qr_algorithm(const MAT1 &a, VECT &eigval,
465  tol_type_for_qr tol = default_tol_for_qr) {
466  dense_matrix<typename linalg_traits<MAT1>::value_type> m(0,0);
467  rudimentary_qr_algorithm(a, eigval, m, tol, false);
468  }
469 
470  /* ********************************************************************* */
471  /* Francis QR step. */
472  /* ********************************************************************* */
473 
474  template <typename MAT1, typename MAT2>
475  void Francis_qr_step(const MAT1& HH, const MAT2 &QQ, bool compute_Q) {
476  MAT1& H = const_cast<MAT1&>(HH); MAT2& Q = const_cast<MAT2&>(QQ);
477  typedef typename linalg_traits<MAT1>::value_type value_type;
478  size_type n = mat_nrows(H), nq = mat_nrows(Q);
479 
480  std::vector<value_type> v(3), w(std::max(n, nq));
481 
482  value_type s = H(n-2, n-2) + H(n-1, n-1);
483  value_type t = H(n-2, n-2) * H(n-1, n-1) - H(n-2, n-1) * H(n-1, n-2);
484  value_type x = H(0, 0) * H(0, 0) + H(0,1) * H(1, 0) - s * H(0,0) + t;
485  value_type y = H(1, 0) * (H(0,0) + H(1,1) - s);
486  value_type z = H(1, 0) * H(2, 1);
487 
488  sub_interval SUBQ(0, nq);
489 
490  for (size_type k = 0; k < n - 2; ++k) {
491  v[0] = x; v[1] = y; v[2] = z;
492  house_vector(v);
493  size_type r = std::min(k+4, n), q = (k==0) ? 0 : k-1;
494  sub_interval SUBI(k, 3), SUBJ(0, r), SUBK(q, n-q);
495 
496  row_house_update(sub_matrix(H, SUBI, SUBK), v, sub_vector(w, SUBK));
497  col_house_update(sub_matrix(H, SUBJ, SUBI), v, sub_vector(w, SUBJ));
498 
499  if (compute_Q)
500  col_house_update(sub_matrix(Q, SUBQ, SUBI), v, sub_vector(w, SUBQ));
501 
502  x = H(k+1, k); y = H(k+2, k);
503  if (k < n-3) z = H(k+3, k);
504  }
505  sub_interval SUBI(n-2,2), SUBJ(0, n), SUBK(n-3,3), SUBL(0, 3);
506  v.resize(2);
507  v[0] = x; v[1] = y;
508  house_vector(v);
509  row_house_update(sub_matrix(H, SUBI, SUBK), v, sub_vector(w, SUBL));
510  col_house_update(sub_matrix(H, SUBJ, SUBI), v, sub_vector(w, SUBJ));
511  if (compute_Q)
512  col_house_update(sub_matrix(Q, SUBQ, SUBI), v, sub_vector(w, SUBQ));
513  }
514 
515  /* ********************************************************************* */
516  /* Wilkinson Double shift QR step (from Lapack). */
517  /* ********************************************************************* */
518 
519  template <typename MAT1, typename MAT2, typename Ttol>
520  void Wilkinson_double_shift_qr_step(const MAT1& HH, const MAT2 &QQ,
521  Ttol tol, bool exc, bool compute_Q) {
522  MAT1& H = const_cast<MAT1&>(HH); MAT2& Q = const_cast<MAT2&>(QQ);
523  typedef typename linalg_traits<MAT1>::value_type T;
524  typedef typename number_traits<T>::magnitude_type R;
525 
526  size_type n = mat_nrows(H), nq = mat_nrows(Q), m;
527  std::vector<T> v(3), w(std::max(n, nq));
528  const R dat1(0.75), dat2(-0.4375);
529  T h33, h44, h43h34, v1(0), v2(0), v3(0);
530 
531  if (exc) { /* Exceptional shift. */
532  R s = gmm::abs(H(n-1, n-2)) + gmm::abs(H(n-2, n-3));
533  h33 = h44 = dat1 * s;
534  h43h34 = dat2*s*s;
535  }
536  else { /* Wilkinson double shift. */
537  h44 = H(n-1,n-1); h33 = H(n-2, n-2);
538  h43h34 = H(n-1, n-2) * H(n-2, n-1);
539  }
540 
541  /* Look for two consecutive small subdiagonal elements. */
542  /* Determine the effect of starting the double-shift QR iteration at */
543  /* row m, and see if this would make H(m-1, m-2) negligible. */
544  for (m = n-2; m != 0; --m) {
545  T h11 = H(m-1, m-1), h22 = H(m, m);
546  T h21 = H(m, m-1), h12 = H(m-1, m);
547  T h44s = h44 - h11, h33s = h33 - h11;
548  v1 = (h33s*h44s-h43h34) / h21 + h12;
549  v2 = h22 - h11 - h33s - h44s;
550  v3 = H(m+1, m);
551  R s = gmm::abs(v1) + gmm::abs(v2) + gmm::abs(v3);
552  v1 /= s; v2 /= s; v3 /= s;
553  if (m == 1) break;
554  T h00 = H(m-2, m-2);
555  T h10 = H(m-1, m-2);
556  R tst1 = gmm::abs(v1)*(gmm::abs(h00)+gmm::abs(h11)+gmm::abs(h22));
557  if (gmm::abs(h10)*(gmm::abs(v2)+gmm::abs(v3)) <= tol * tst1) break;
558  }
559 
560  /* Double shift QR step. */
561  sub_interval SUBQ(0, nq);
562  for (size_type k = (m == 0) ? 0 : m-1; k < n-2; ++k) {
563  v[0] = v1; v[1] = v2; v[2] = v3;
564  house_vector(v);
565  size_type r = std::min(k+4, n), q = (k==0) ? 0 : k-1;
566  sub_interval SUBI(k, 3), SUBJ(0, r), SUBK(q, n-q);
567 
568  row_house_update(sub_matrix(H, SUBI, SUBK), v, sub_vector(w, SUBK));
569  col_house_update(sub_matrix(H, SUBJ, SUBI), v, sub_vector(w, SUBJ));
570  if (k > m-1) { H(k+1, k-1) = T(0); if (k < n-3) H(k+2, k-1) = T(0); }
571 
572  if (compute_Q)
573  col_house_update(sub_matrix(Q, SUBQ, SUBI), v, sub_vector(w, SUBQ));
574 
575  v1 = H(k+1, k); v2 = H(k+2, k);
576  if (k < n-3) v3 = H(k+3, k);
577  }
578  sub_interval SUBI(n-2,2), SUBJ(0, n), SUBK(n-3,3), SUBL(0, 3);
579  v.resize(2); v[0] = v1; v[1] = v2;
580  house_vector(v);
581  row_house_update(sub_matrix(H, SUBI, SUBK), v, sub_vector(w, SUBL));
582  col_house_update(sub_matrix(H, SUBJ, SUBI), v, sub_vector(w, SUBJ));
583  if (compute_Q)
584  col_house_update(sub_matrix(Q, SUBQ, SUBI), v, sub_vector(w, SUBQ));
585  }
586 
587  /* ********************************************************************* */
588  /* Implicit QR algorithm. */
589  /* ********************************************************************* */
590 
591  // QR method for real or complex square matrices based on an
592  // implicit QR factorisation. eigval has to be a complex vector
593  // if A has complex eigenvalues. Complexity about 10n^3, 25n^3 if
594  // eigenvectors are computed
595  template <typename MAT1, typename VECT, typename MAT2>
596  void implicit_qr_algorithm(const MAT1 &A, const VECT &eigval_,
597  const MAT2 &Q_,
598  tol_type_for_qr tol = default_tol_for_qr,
599  bool compvect = true) {
600  VECT &eigval = const_cast<VECT &>(eigval_);
601  MAT2 &Q = const_cast<MAT2 &>(Q_);
602  typedef typename linalg_traits<MAT1>::value_type T;
603  typedef typename number_traits<T>::magnitude_type R;
604 
605  size_type n(mat_nrows(A)), q(0), p(0);
606  dense_matrix<T> H(n,n);
607  gmm::copy(A, H);
608 
609  Hessenberg_reduction(H, Q, compvect);
610  qr_stop_criterion(H, p, q, tol);
611 
612  size_type ite(0), its(0);
613  sub_interval SUBK(0,0);
614  while (q < n) {
615  sub_interval SUBI(p, n-p-q), SUBJ(0, mat_ncols(Q));
616  if (compvect) SUBK = SUBI;
617 // Francis_qr_step(sub_matrix(H, SUBI),
618 // sub_matrix(Q, SUBJ, SUBK), compvect);
619  Wilkinson_double_shift_qr_step(sub_matrix(H, SUBI),
620  sub_matrix(Q, SUBJ, SUBK),
621  tol, (its == 10 || its == 20), compvect);
622  size_type q_old(q);
623  qr_stop_criterion(H, p, q, tol*R(2));
624  if (q != q_old) its = 0;
625  ++its; ++ite;
626  GMM_ASSERT1(ite < n*100, "QR algorithm failed");
627  }
628  if (compvect) block2x2_reduction(H, Q, tol);
629  extract_eig(H, eigval, tol);
630  }
631 
632  template <typename MAT1, typename VECT>
633  void implicit_qr_algorithm(const MAT1 &a, VECT &eigval,
634  tol_type_for_qr tol = default_tol_for_qr) {
635  dense_matrix<typename linalg_traits<MAT1>::value_type> m(1,1);
636  implicit_qr_algorithm(a, eigval, m, tol, false);
637  }
638 
639  /* ********************************************************************* */
640  /* Implicit symmetric QR step with Wilkinson Shift. */
641  /* ********************************************************************* */
642 
643  template <typename MAT1, typename MAT2>
644  void symmetric_Wilkinson_qr_step(const MAT1& MM, const MAT2 &ZZ,
645  bool compute_z) {
646  MAT1& M = const_cast<MAT1&>(MM); MAT2& Z = const_cast<MAT2&>(ZZ);
647  typedef typename linalg_traits<MAT1>::value_type T;
648  typedef typename number_traits<T>::magnitude_type R;
649  size_type n = mat_nrows(M);
650 
651  for (size_type i = 0; i < n; ++i) {
652  M(i, i) = T(gmm::real(M(i, i)));
653  if (i > 0) {
654  T a = (M(i, i-1) + gmm::conj(M(i-1, i)))/R(2);
655  M(i, i-1) = a; M(i-1, i) = gmm::conj(a);
656  }
657  }
658 
659  R d = gmm::real(M(n-2, n-2) - M(n-1, n-1)) / R(2);
660  R e = gmm::abs_sqr(M(n-1, n-2));
661  R nu = d + gmm::sgn(d)*gmm::sqrt(d*d+e);
662  if (nu == R(0)) { M(n-1, n-2) = T(0); return; }
663  R mu = gmm::real(M(n-1, n-1)) - e / nu;
664  T x = M(0,0) - T(mu), z = M(1, 0), c, s;
665 
666  for (size_type k = 1; k < n; ++k) {
667  Givens_rotation(x, z, c, s);
668 
669  if (k > 1) Apply_Givens_rotation_left(M(k-1,k-2), M(k,k-2), c, s);
670  Apply_Givens_rotation_left(M(k-1,k-1), M(k,k-1), c, s);
671  Apply_Givens_rotation_left(M(k-1,k ), M(k,k ), c, s);
672  if (k < n-1) Apply_Givens_rotation_left(M(k-1,k+1), M(k,k+1), c, s);
673  if (k > 1) Apply_Givens_rotation_right(M(k-2,k-1), M(k-2,k), c, s);
674  Apply_Givens_rotation_right(M(k-1,k-1), M(k-1,k), c, s);
675  Apply_Givens_rotation_right(M(k ,k-1), M(k,k) , c, s);
676  if (k < n-1) Apply_Givens_rotation_right(M(k+1,k-1), M(k+1,k), c, s);
677 
678  if (compute_z) col_rot(Z, c, s, k-1, k);
679  if (k < n-1) { x = M(k, k-1); z = M(k+1, k-1); }
680  }
681  }
682 
683  template <typename VECT1, typename VECT2, typename MAT>
684  void symmetric_Wilkinson_qr_step(const VECT1& diag_, const VECT2& sdiag_,
685  const MAT &ZZ, bool compute_z) {
686  VECT1& diag = const_cast<VECT1&>(diag_);
687  VECT2& sdiag = const_cast<VECT2&>(sdiag_);
688  MAT& Z = const_cast<MAT&>(ZZ);
689  typedef typename linalg_traits<VECT2>::value_type T;
690  typedef typename number_traits<T>::magnitude_type R;
691 
692  size_type n = vect_size(diag);
693  R d = (diag[n-2] - diag[n-1]) / R(2);
694  R e = gmm::abs_sqr(sdiag[n-2]);
695  R nu = d + gmm::sgn(d)*gmm::sqrt(d*d+e);
696  if (nu == R(0)) { sdiag[n-2] = T(0); return; }
697  R mu = diag[n-1] - e / nu;
698  T x = diag[0] - T(mu), z = sdiag[0], c, s;
699 
700  T a01(0), a02(0);
701  T a10(0), a11(diag[0]), a12(gmm::conj(sdiag[0])), a13(0);
702  T a20(0), a21(sdiag[0]), a22(diag[1]), a23(gmm::conj(sdiag[1]));
703  T a31(0), a32(sdiag[1]);
704 
705  for (size_type k = 1; k < n; ++k) {
706  Givens_rotation(x, z, c, s);
707 
708  if (k > 1) Apply_Givens_rotation_left(a10, a20, c, s);
709  Apply_Givens_rotation_left(a11, a21, c, s);
710  Apply_Givens_rotation_left(a12, a22, c, s);
711  if (k < n-1) Apply_Givens_rotation_left(a13, a23, c, s);
712 
713  if (k > 1) Apply_Givens_rotation_right(a01, a02, c, s);
714  Apply_Givens_rotation_right(a11, a12, c, s);
715  Apply_Givens_rotation_right(a21, a22, c, s);
716  if (k < n-1) Apply_Givens_rotation_right(a31, a32, c, s);
717 
718  if (compute_z) col_rot(Z, c, s, k-1, k);
719 
720  diag[k-1] = gmm::real(a11);
721  diag[k] = gmm::real(a22);
722  if (k > 1) sdiag[k-2] = (gmm::conj(a01) + a10) / R(2);
723  sdiag[k-1] = (gmm::conj(a12) + a21) / R(2);
724 
725  x = sdiag[k-1]; z = (gmm::conj(a13) + a31) / R(2);
726 
727  a01 = a12; a02 = a13;
728  a10 = a21; a11 = a22; a12 = a23; a13 = T(0);
729  a20 = a31; a21 = a32; a31 = T(0);
730 
731  if (k < n-1) {
732  sdiag[k] = (gmm::conj(a23) + a32) / R(2);
733  a22 = T(diag[k+1]); a32 = sdiag[k+1]; a23 = gmm::conj(a32);
734  }
735  }
736  }
737 
738  /* ********************************************************************* */
739  /* Implicit QR algorithm for symmetric or hermitian matrices. */
740  /* ********************************************************************* */
741 
742  // implicit QR method for real square symmetric matrices or complex
743  // hermitian matrices.
744  // eigval has to be a complex vector if A has complex eigeinvalues.
745  // complexity about 4n^3/3, 9n^3 if eigenvectors are computed
746  template <typename MAT1, typename VECT, typename MAT2>
747  void symmetric_qr_algorithm_old(const MAT1 &A, const VECT &eigval_,
748  const MAT2 &eigvect_,
749  tol_type_for_qr tol = default_tol_for_qr,
750  bool compvect = true) {
751  VECT &eigval = const_cast<VECT &>(eigval_);
752  MAT2 &eigvect = const_cast<MAT2 &>(eigvect_);
753  typedef typename linalg_traits<MAT1>::value_type T;
754  typedef typename number_traits<T>::magnitude_type R;
755 
756  size_type n(mat_nrows(A)), q(0), p(0), ite(0);
757  dense_matrix<T> Tri(n, n);
758  gmm::copy(A, Tri);
759 
760  if (compvect) gmm::copy(identity_matrix(), eigvect);
761  Householder_tridiagonalization(Tri, eigvect, compvect);
762  symmetric_qr_stop_criterion(Tri, p, q, tol);
763 
764  while (q < n) {
765  sub_interval SUBI(p, n-p-q), SUBJ(0, mat_ncols(eigvect)), SUBK(p, n-p-q);
766  if (!compvect) SUBK = sub_interval(0,0);
767  symmetric_Wilkinson_qr_step(sub_matrix(Tri, SUBI),
768  sub_matrix(eigvect, SUBJ, SUBK), compvect);
769 
770  symmetric_qr_stop_criterion(Tri, p, q, tol*R(2));
771  ++ite;
772  GMM_ASSERT1(ite < n*100, "QR algorithm failed. Probably, your matrix"
773  " is not real symmetric or complex hermitian");
774  }
775 
776  extract_eig(Tri, eigval, tol);
777  }
778 
779  template <typename MAT1, typename VECT, typename MAT2>
780  void symmetric_qr_algorithm(const MAT1 &A, const VECT &eigval_,
781  const MAT2 &eigvect_,
782  tol_type_for_qr tol = default_tol_for_qr,
783  bool compvect = true) {
784  VECT &eigval = const_cast<VECT &>(eigval_);
785  MAT2 &eigvect = const_cast<MAT2 &>(eigvect_);
786  typedef typename linalg_traits<MAT1>::value_type T;
787  typedef typename number_traits<T>::magnitude_type R;
788 
789  size_type n = mat_nrows(A), q = 0, p, ite = 0;
790  if (compvect) gmm::copy(identity_matrix(), eigvect);
791  if (n == 0) return;
792  if (n == 1) { eigval[0]=gmm::real(A(0,0)); return; }
793  dense_matrix<T> Tri(n, n);
794  gmm::copy(A, Tri);
795 
796  Householder_tridiagonalization(Tri, eigvect, compvect);
797 
798  std::vector<R> diag(n);
799  std::vector<T> sdiag(n);
800  for (size_type i = 0; i < n; ++i)
801  { diag[i] = gmm::real(Tri(i, i)); if (i+1 < n) sdiag[i] = Tri(i+1, i); }
802 
803  symmetric_qr_stop_criterion(diag, sdiag, p, q, tol);
804 
805  while (q < n) {
806  sub_interval SUBI(p, n-p-q), SUBJ(0, mat_ncols(eigvect)), SUBK(p, n-p-q);
807  if (!compvect) SUBK = sub_interval(0,0);
808 
809  symmetric_Wilkinson_qr_step(sub_vector(diag, SUBI),
810  sub_vector(sdiag, SUBI),
811  sub_matrix(eigvect, SUBJ, SUBK), compvect);
812 
813  symmetric_qr_stop_criterion(diag, sdiag, p, q, tol*R(3));
814  ++ite;
815  GMM_ASSERT1(ite < n*100, "QR algorithm failed.");
816  }
817 
818  gmm::copy(diag, eigval);
819  }
820 
821 
822  template <typename MAT1, typename VECT>
823  void symmetric_qr_algorithm(const MAT1 &a, VECT &eigval,
824  tol_type_for_qr tol = default_tol_for_qr) {
825  dense_matrix<typename linalg_traits<MAT1>::value_type> m(0,0);
826  symmetric_qr_algorithm(a, eigval, m, tol, false);
827  }
828 
829 
830 }
831 
832 #endif
833 
void copy(const L1 &l1, L2 &l2)
*‍/
Definition: gmm_blas.h:976
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norm2_sqr(const V &v)
squared Euclidean norm of a vector.
Definition: gmm_blas.h:543
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norminf(const V &v)
Infinity norm of a vector.
Definition: gmm_blas.h:692
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*‍/
Definition: gmm_blas.h:1663
conjugated_return< L >::return_type conjugated(const L &v)
return a conjugated view of the input matrix or vector.
Householder for dense matrices.
void qr_factor(const MAT &A_)
QR factorization using Householder method (complex and real version).
Definition: gmm_dense_qr.h:48
void extract_eig(const MAT &A, const VECT &V, Ttol tol)
Compute eigenvalue vector.
Definition: gmm_dense_qr.h:307
size_t size_type
used as the common size type in the library
Definition: bgeot_poly.h:48