-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcghs.hh
More file actions
45 lines (37 loc) · 1.09 KB
/
cghs.hh
File metadata and controls
45 lines (37 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// -*- mode: c++; coding: utf-8 -*-
// ra-ra/examples - Conjugate gradient after Hestenes und Stiefel.
// (c) Daniel Llorens - 2017
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option) any
// later version.
// Adapted from lsolver/cghs.cc by Christian Badura, 1998/05.
#pragma once
#include "ra/ra.hh"
template <class A, class B, class X, class W>
inline int
cghs(A & a, B const & b, X & x, W & work, double eps)
{
using ra::sqr;
work = 0.;
auto g = work(0);
auto r = work(1);
auto p = work(2);
double err = sqr(eps)*reduce_sqrm(b);
mult(a, x, g);
g = b-g;
r = g;
int i;
for (i=0; reduce_sqrm(g)>err; ++i) {
mult(a, r, p);
double rho = reduce_sqrm(p);
double sig = dot(r, p);
double tau = dot(g, r);
double t = tau/sig;
double gam = (sqr(t)*rho-tau)/tau;
x += t*r;
g -= t*p;
r = r*gam + g;
}
return i;
}