28 template<
class S,
class V,
class E>
43 parameters_(parameters),k(0),steepest(steep) {
61 std::cout <<
"iteration = " << k << std::endl;
64 std::cout <<
"dotg = " << gamma << std::endl;
88 if (k % parameters_.
reset() == 0) g = Ab.gradient(x);
90 else Ab.transposeMultiplyAdd(alpha, Ad, g);
93 double new_gamma =
dot(g, g);
96 std::cout <<
"iteration " << k <<
": alpha = " << alpha
97 <<
", dotg = " << new_gamma
100 if (new_gamma < threshold)
return true;
105 double beta = new_gamma /
gamma;
114 Ab.multiplyInPlace(d, Ad);
123 template<
class S,
class V,
class E>
129 std::cout <<
"CG: epsilon = " << parameters.
epsilon()
131 <<
", ||g0||^2 = " << state.
gamma 137 std::cout <<
"||g0||^2 < threshold, exiting immediately !" << std::endl;
143 while (!state.
step(Ab, x)) {}
void print(const Matrix &A, const string &s, ostream &stream)
double dot(const V1 &a, const V2 &b)
Iterative methods, implementation.
V conjugateGradients(const S &Ab, V x, const ConjugateGradientParameters ¶meters, bool steepest)
Implementation of Conjugate Gradient solver for a linear system.
double epsilon_abs() const
bool steepest
flag to indicate we are doing steepest descent
size_t maxIterations() const
bool step(const S &Ab, V &x)
CGState(const S &Ab, const V &x, const Parameters ¶meters, bool steep)
ConjugateGradientParameters Parameters
Verbosity verbosity() const
static ConjugateGradientParameters parameters
const Parameters & parameters_
double threshold
gamma (squared L2 norm of g) and convergence threshold
V d
gradient g and search direction d for CG
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
double takeOptimalStep(V &x)