46 template <
typename T> compare_vp {
47 bool operator()(
const std::pair<T, size_type> &a,
48 const std::pair<T, size_type> &b)
const
49 {
return (gmm::abs(a.first) > gmm::abs(b.first)); }
52 struct idgmres_state {
53 size_type m, tb_deb, tb_def, p, k, nb_want, nb_unwant;
54 size_type nb_nolong, tb_deftot, tb_defwant, conv, nb_un, fin;
58 : m(mm), tb_deb(1), tb_def(0), p(pp), k(kk), nb_want(0),
59 nb_unwant(0), nb_nolong(0), tb_deftot(0), tb_defwant(0),
60 conv(0), nb_un(0), fin(0), ok(false); {}
64 : m(mm), tb_deb(1), tb_def(0), p(pp), k(kk), nb_want(0),
65 nb_unwant(0), nb_nolong(0), tb_deftot(0), tb_defwant(0),
66 conv(0), nb_un(0), fin(0), ok(false); {}
69 template <
typename CONT,
typename IND>
70 apply_permutation(CONT &cont,
const IND &ind) {
72 std::vector<bool> sorted(m,
false);
75 if (!sorted[l] && ind[l] != l) {
77 typeid(cont[0]) aux = cont[l];
82 for(k2 = ind[k]; k2 != l; k2 = ind[k]) {
110 template <
typename Mat,
typename Vec,
typename VecB,
typename Precond,
112 void idgmres(
const Mat &A, Vec &x,
const VecB &b,
const Precond &M,
114 iteration &outer, Basis& KS) {
116 typedef typename linalg_traits<Mat>::value_type T;
117 typedef typename number_traits<T>::magnitude_type R;
120 idgmres_state st(m, p, k);
122 std::vector<T> w(vect_size(x)), r(vect_size(x)), u(vect_size(x));
123 std::vector<T> c_rot(m+1), s_rot(m+1), s(m+1);
124 std::vector<T> y(m+1), ztest(m+1), gam(m+1);
125 std::vector<T> gamma(m+1);
126 gmm::dense_matrix<T> H(m+1, m), Hess(m+1, m),
127 Hobl(m+1, m), W(vect_size(x), m+1);
130 outer.set_rhsnorm(gmm::vect_norm2(b));
131 if (outer.get_rhsnorm() == 0.0) {
clear(x);
return; }
133 mult(A, scaled(x, -1.0), b, w);
137 iteration inner = outer;
138 inner.reduce_noisy();
139 inner.set_maxiter(m);
140 inner.set_name(
"GMRes inner iter");
142 while (! outer.finished(beta)) {
144 gmm::copy(gmm::scaled(r, 1.0/beta), KS[0]);
149 inner.set_maxiter(m - st.tb_deb + 1);
150 size_type i = st.tb_deb - 1; inner.init();
155 orthogonalize_with_refinment(KS, mat_col(H, i), i);
157 gmm::scale(KS[i+1], R(1) / a);
159 gmm::copy(mat_col(H, i), mat_col(Hess, i));
160 gmm::copy(mat_col(H, i), mat_col(Hobl, i));
163 Apply_Givens_rotation_left(H(l,i), H(l+1,i), c_rot[l], s_rot[l]);
165 Givens_rotation(H(i,i), H(i+1,i), c_rot[i], s_rot[i]);
166 Apply_Givens_rotation_left(H(i,i), H(i+1,i), c_rot[i], s_rot[i]);
168 Apply_Givens_rotation_left(s[i], s[i+1], c_rot[i], s_rot[i]);
170 ++inner, ++outer, ++i;
171 }
while (! inner.finished(gmm::abs(s[i])));
173 if (inner.converged()) {
175 upper_tri_solve(H, y, i,
false);
176 combine(KS, y, x, i);
177 mult(A, gmm::scaled(x, T(-1)), b, w);
185 Apply_Givens_rotation_left(gam[l-1], gam[l], gmm::conj(c_rot[l-1]),
188 mult(KS.mat(), gam, r);
191 mult(Hess, scaled(y, T(-1)), gamma, ztest);
197 T nss = H(m,m-1) / ztest[m];
198 nss /= gmm::abs(nss);
202 sub_interval SUBI(0, m);
203 add(scaled(sub_vector(ztest, SUBI), -Hobl(m, m-1) / ztest[m]),
204 sub_vector(mat_col(Hobl, m-1), SUBI));
205 Hobl(m, m-1) *= nss * beta / ztest[m];
212 std::vector<std::complex<R> > eval(m);
213 dense_matrix<T> YB(m-st.tb_def, m-st.tb_def);
214 std::vector<char> pure(m-st.tb_def, 0);
217 select_eval(Hobl, eval, YB, pure, st);
222 T
alpha = Lock(W, Hobl,
223 sub_matrix(YB, sub_interval(0, m-st.tb_def)),
224 sub_interval(st.tb_def, m-st.tb_def),
225 (st.tb_defwant < p));
232 for (
size_type j = st.tb_def; j < st.tb_deftot; ++j) {
233 if ( pure[j-st.tb_def] == 0)
234 gmm::clear(sub_vector(mat_col(Hobl,j), sub_interval(j+1,m-j)));
235 else if (pure[j-st.tb_def] == 1) {
236 gmm::clear(sub_matrix(Hobl, sub_interval(j+2,m-j-1),
237 sub_interval(j, 2)));
240 else GMM_ASSERT3(
false,
"internal error");
245 size_type mm = std::min(k+st.nb_unwant+st.nb_nolong, m-1);
247 if (eval_sort[m-mm-1].second != R(0)
248 && eval_sort[m-mm-1].second == -eval_sort[m-mm].second)
251 std::vector<complex<R> > shifts(m-mm);
253 shifts[i] = eval_sort[i].second;
255 apply_shift_to_Arnoldi_factorization(W, Hobl, shifts, mm,
261 st.fin = st.tb_deftot;
268 if (st.nb_nolong + st.nb_unwant > 0) {
270 std::vector<std::complex<R> > eval(m);
271 dense_matrix<T> YB(st.fin, st.tb_deftot);
272 std::vector<char> pure(st.tb_deftot, 0);
274 st.nb_un = st.nb_nolong + st.nb_unwant;
276 select_eval_for_purging(Hobl, eval, YB, pure, st);
278 T
alpha = Lock(W, Hobl, YB, sub_interval(0, st.fin), ok);
282 for (
size_type j = 0; j < st.tb_deftot; ++j) {
284 gmm::clear(sub_vector(mat_col(Hobl,j), sub_interval(j+1,m-j)));
285 else if (pure[j] == 1) {
286 gmm::clear(sub_matrix(Hobl, sub_interval(j+2,m-j-1),
287 sub_interval(j, 2)));
290 else GMM_ASSERT3(
false,
"internal error");
293 gmm::dense_matrix<T> z(st.nb_un, st.fin - st.nb_un);
294 sub_interval SUBI(0, st.nb_un), SUBJ(st.nb_un, st.fin - st.nb_un);
295 sylvester(sub_matrix(Hobl, SUBI),
296 sub_matrix(Hobl, SUBJ),
297 sub_matrix(gmm::scaled(Hobl, -T(1)), SUBI, SUBJ), z);
305 template <
typename Mat,
typename Vec,
typename VecB,
typename Precond >
306 void idgmres(
const Mat &A, Vec &x,
const VecB &b,
307 const Precond &M,
size_type m, iteration& outer) {
308 typedef typename linalg_traits<Mat>::value_type T;
309 modified_gram_schmidt<T> orth(m, vect_size(x));
310 gmres(A, x, b, M, m, outer, orth);
322 template <
typename T,
typename MATYB>
323 void Lock(gmm::dense_matrix<T> &W, gmm::dense_matrix<T> &H,
324 const MATYB &YB,
const sub_interval SUB,
325 bool restore, T &ns) {
327 size_type n = mat_nrows(W), m = mat_ncols(W) - 1;
328 size_type ncols = mat_ncols(YB), nrows = mat_nrows(YB);
329 size_type begin = min(SUB); end = max(SUB) - 1;
330 sub_interval SUBR(0, nrows), SUBC(0, ncols);
333 GMM_ASSERT2(((end-begin) == ncols) && (m == mat_nrows(H))
334 && (m+1 == mat_ncols(H)),
"dimensions mismatch");
338 dense_matrix<T> QR(n_rows, n_rows);
339 gmmm::copy(YB, sub_matrix(QR, SUBR, SUBC));
340 gmm::clear(submatrix(QR, SUBR, sub_interval(ncols, nrows-ncols)));
343 apply_house_left(QR, sub_matrix(H, SUB));
344 apply_house_right(QR, sub_matrix(H, SUBR, SUB));
345 apply_house_right(QR, sub_matrix(W, sub_interval(0, n), SUB));
352 gmm::dense_matrix tab_p(end - st.tb_deftot, end - st.tb_deftot);
355 for (
size_type j = end-1; j >= st.tb_deftot+2; --j) {
358 std::vector<T> v(jm - st.tb_deftot);
359 sub_interval SUBtot(st.tb_deftot, jm - st.tb_deftot);
360 sub_interval SUBtot2(st.tb_deftot, end - st.tb_deftot);
361 gmm::copy(sub_vector(mat_row(H, j), SUBtot), v);
362 house_vector_last(v);
364 col_house_update(sub_matrix(H, SUBI, SUBtot), v, w);
365 w.resize(end - st.tb_deftot);
366 row_house_update(sub_matrix(H, SUBtot, SUBtot2), v, w);
368 sub_interval(st.tb_deftot, j-1-st.tb_deftot)));
369 w.resize(end - st.tb_deftot);
370 col_house_update(sub_matrix(tab_p, sub_interval(0, end-st.tb_deftot),
371 sub_interval(0, jm-st.tb_deftot)), v, w);
373 col_house_update(sub_matrix(W, sub_interval(0, n), SUBtot), v, w);
378 std::vector<T> d(fin-st.tb_deftot); d[0] = T(1);
383 for (
size_type j = 0; j+1 < end-st.tb_deftot; ++j) {
384 T e = H(st.tb_deftot+j, st.tb_deftot+j-1);
385 d[j+1] = (e == T(0)) ? T(1) : d[j] * gmm::abs(e) / e;
386 scale(sub_vector(mat_row(H, st.tb_deftot+j+1),
387 sub_interval(st.tb_deftot, m-st.tb_deftot)), d[j+1]);
388 scale(mat_col(H, st.tb_deftot+j+1), T(1) / d[j+1]);
389 scale(mat_col(W, st.tb_deftot+j+1), T(1) / d[j+1]);
392 alpha = tab_p(end-st.tb_deftot-1, end-st.tb_deftot-1) / d[end-st.tb_deftot-1];
393 alpha /= gmm::abs(alpha);
394 scale(mat_col(W, m), alpha);
408 template<
typename T,
typename C>
409 apply_shift_to_Arnoldi_factorization(dense_matrix<T> V, dense_matrix<T> H,
413 size_type k1 = 0, num = 0, kend = k+p, kp1 = k + 1;
417 dense_matrix<T> q(1, kend);
419 std::vector<T> hv(3), w(std::max(kend, mat_nrows(V)));
425 if (abs(Lambda[jj].real()) == 0.0) {
428 for (
size_type k1 = 0, k2 = 0; k2 != kend-1; k1 = k2+1) {
430 while (h(k2+1, k2) != T(0) && k2 < kend-1)
433 Givens_rotation(H(k1, k1) - Lambda[jj], H(k1+1, k1), c, s);
435 for (i = k1; i <= k2; ++i) {
437 Givens_rotation(H(i, i-1), H(i+1, i-1), c, s);
443 row_rot(sub_matrix(H, sub_interval(i,2), sub_interval(i, kend-i)),
448 col_rot(sub_matrix(H, sub_interval(0, ip2), sub_interval(i, 2))
452 col_rot(V, c, s, i, i+1);
455 Apply_Givens_rotation_left(q(0,i), q(0,i+1), c, s);
475 for (
size_type k1 = 0, k3 = 0; k3 != kend-2; k1 = k3+1) {
477 while (h(k3+1, k3) != T(0) && k3 < kend-2)
482 x = H(k1,k1) * H(k1,k1) + H(k1,k2) * H(k2,k1)
483 - 2.0*Lambda[jj].real() * H(k1,k1) + gmm::abs_sqr(Lambda[jj]);
484 y = H(k2,k1) * (H(k1,k1) + H(k2,k2) - 2.0*Lambda[jj].real());
485 z = H(k2+1,k2) * H(k2,k1);
496 hv[0] = x; hv[1] = y; hv[2] = z;
503 row_house_update(sub_matrix(H, sub_interval(i, 2),
504 sub_interval(i, kend-i)),
511 col_house_update(sub_matrix(H, sub_interval(0, ip3),
517 w.resize(mat_nrows(V));
518 col_house_update(sub_matrix(V, sub_interval(0, mat_nrows(V)),
524 col_house_update(sub_matrix(q, sub_interval(0,1),
536 Givens_rotation(H(i, i-1), H(i+1, i-1), c, s);
542 row_rot(sub_matrix(H, sub_interval(i,2), sub_interval(i, kend-i)),
547 col_rot(sub_matrix(H, sub_interval(0, ip2), sub_interval(i, 2))
551 col_rot(V, c, s, i, i+1);
554 Apply_Givens_rotation_left(q(0,i), q(0,i+1), c, s);
562 scale(mat_col(V, kend), q(0, k));
564 if (k < mat_nrows(H)) {
566 gmm::copy(mat_col(V, kend), mat_col(V, k));
570 gmm::add(scaled(mat_col(V, kend), H(kend, kend-1)),
571 scaled(mat_col(V, k), H(k, k-1)), mat_col(V, k));
575 scale(mat_col(V, kend), T(1) / H(k, k-1));
580 template<
typename MAT,
typename EVAL,
typename PURE>
581 void select_eval(
const MAT &Hobl, EVAL &eval, MAT &YB, PURE &pure,
584 typedef typename linalg_traits<MAT>::value_type T;
585 typedef typename number_traits<T>::magnitude_type R;
590 col_matrix< std::vector<T> > evect(m-st.tb_def, m-st.tb_def);
592 std::vector<R> ritznew(m, T(-1));
596 sub_interval SUB1(st.tb_def, m-st.tb_def);
597 implicit_qr_algorithm(sub_matrix(Hobl, SUB1),
598 sub_vector(eval, SUB1), evect);
599 sub_interval SUB2(0, st.tb_def);
600 implicit_qr_algorithm(sub_matrix(Hobl, SUB2),
601 sub_vector(eval, SUB2), );
603 for (
size_type l = st.tb_def; l < m; ++l)
604 ritznew[l] = gmm::abs(evect(m-st.tb_def-1, l-st.tb_def) * Hobl(m, m-1));
606 std::vector< std::pair<T, size_type> > eval_sort(m);
608 eval_sort[l] = std::pair<T, size_type>(eval[l], l);
609 std::sort(eval_sort.begin(), eval_sort.end(), compare_vp());
611 std::vector<size_type> index(m);
612 for (
size_type l = 0; l < m; ++l) index[l] = eval_sort[l].second;
614 std::vector<bool> kept(m,
false);
615 std::fill(kept.begin(), kept.begin()+st.tb_def,
true);
617 apply_permutation(eval, index);
618 apply_permutation(evect, index);
619 apply_permutation(ritznew, index);
620 apply_permutation(kept, index);
639 st.nb_want = 0, st.nb_unwant = 0, st.nb_nolong = 0;
642 for (j = 0, ind = 0; j < m-p; ++j) {
643 if (ritznew[j] == R(-1)) {
644 if (std::imag(eval[j]) != R(0)) {
645 st.nb_nolong += 2; ++j;
649 if (ritznew[j] < tol_vp * gmm::abs(eval[j])) {
651 for (
size_type l = 0, l < m-st.tb_def; ++l)
652 YB(l, ind) = std::real(evect(l, j));
654 ++j; ++st.nb_unwant; ind++;
656 if (std::imag(eval[j]) != R(0)) {
657 for (
size_type l = 0, l < m-st.tb_def; ++l)
658 YB(l, ind) = std::imag(evect(l, j));
673 if (ritznew[j] != R(-1)) {
675 for (
size_type l = 0, l < m-st.tb_def; ++l)
676 YB(l, ind) = std::real(evect(l, j));
682 if (ritznew[j] < tol_vp * gmm::abs(eval[j])) {
683 for (
size_type l = 0, l < m-st.tb_def; ++l)
684 YB(l, ind) = std::imag(evect(l, j));
696 std::vector<T> shift(m - st.tb_def - st.nb_want - st.nb_unwant);
699 shift[i++] = eval[j];
706 size_type st.tb_deftot = st.tb_def + st.conv;
707 size_type st.tb_defwant = st.tb_def + st.nb_want - st.nb_nolong;
709 sub_interval SUBYB(0, st.conv);
711 if ( st.tb_defwant >= p ) {
714 st.nb_want = p + st.nb_nolong - st.tb_def;
717 if ( pure[st.conv - st.nb_want + 1] == 2 ) {
718 ++st.nb_want; st.tb_defwant = ++p;
721 SUBYB = sub_interval(st.conv - st.nb_want, st.nb_want);
724 st.conv = st.nb_want;
725 st.tb_deftot = st.tb_def + st.conv;
732 template<
typename MAT,
typename EVAL,
typename PURE>
733 void select_eval_for_purging(
const MAT &Hobl, EVAL &eval, MAT &YB,
734 PURE &pure, idgmres_state &st) {
736 typedef typename linalg_traits<MAT>::value_type T;
737 typedef typename number_traits<T>::magnitude_type R;
742 col_matrix< std::vector<T> > evect(st.tb_deftot, st.tb_deftot);
744 sub_interval SUB1(0, st.tb_deftot);
745 implicit_qr_algorithm(sub_matrix(Hobl, SUB1),
746 sub_vector(eval, SUB1), evect);
747 std::fill(eval.begin() + st.tb_deftot, eval.end(), std::complex<R>(0));
749 std::vector< std::pair<T, size_type> > eval_sort(m);
751 eval_sort[l] = std::pair<T, size_type>(eval[l], l);
752 std::sort(eval_sort.begin(), eval_sort.end(), compare_vp());
754 std::vector<bool> sorted(m);
755 std::fill(sorted.begin(), sorted.end(),
false);
757 std::vector<size_type> ind(m);
758 for (
size_type l = 0; l < m; ++l) ind[l] = eval_sort[l].second;
760 std::vector<bool> kept(m,
false);
761 std::fill(kept.begin(), kept.begin()+st.tb_def,
true);
763 apply_permutation(eval, ind);
764 apply_permutation(evect, ind);
767 for (j = 0; j < st.tb_deftot; ++j) {
769 for (
size_type l = 0, l < st.tb_deftot; ++l)
770 YB(l, j) = std::real(evect(l, j));
772 if (std::imag(eval[j]) != R(0)) {
773 for (
size_type l = 0, l < m-st.tb_def; ++l)
774 YB(l, j+1) = std::imag(evect(l, j));
void copy(const L1 &l1, L2 &l2)
*/
number_traits< typename linalg_traits< V >::value_type >::magnitude_type vect_norm2(const V &v)
Euclidean norm of a vector.
void clear(L &l)
clear (fill with zeros) a vector or matrix.
void mult(const L1 &l1, const L2 &l2, L3 &l3)
*/
void add(const L1 &l1, L2 &l2)
*/
void qr_factor(const MAT &A_)
QR factorization using Householder method (complex and real version).
Sylvester equation solver.
Include the base gmm files.
void gmres(const Mat &A, Vec &x, const VecB &b, const Precond &M, int restart, iteration &outer, Basis &KS)
Generalized Minimum Residual.
size_t size_type
used as the common size type in the library
size_type alpha(short_type n, short_type d)
Return the value of which is the number of monomials of a polynomial of variables and degree .