23 #include <boost/shared_ptr.hpp> 29 template<
class S,
class V,
class E>
44 parameters_(parameters),k(0),steepest(steep) {
62 std::cout <<
"iteration = " << k << std::endl;
65 std::cout <<
"dotg = " << gamma << std::endl;
89 if (k % parameters_.
reset() == 0) g = Ab.gradient(x);
91 else Ab.transposeMultiplyAdd(alpha, Ad, g);
94 double new_gamma =
dot(g, g);
97 std::cout <<
"iteration " << k <<
": alpha = " << alpha
98 <<
", dotg = " << new_gamma
101 if (new_gamma < threshold)
return true;
106 double beta = new_gamma /
gamma;
115 Ab.multiplyInPlace(d, Ad);
124 template<
class S,
class V,
class E>
130 std::cout <<
"CG: epsilon = " << parameters.
epsilon()
132 <<
", ||g0||^2 = " << state.
gamma 138 std::cout <<
"||g0||^2 < threshold, exiting immediately !" << std::endl;
144 while (!state.
step(Ab, x)) {}
void print(const Matrix &A, const string &s, ostream &stream)
size_t maxIterations() const
double dot(const V1 &a, const V2 &b)
Iterative methods, implementation.
V conjugateGradients(const S &Ab, V x, const ConjugateGradientParameters ¶meters, bool steepest)
double epsilon_abs() const
Implementation of Conjugate Gradient solver for a linear system.
bool steepest
flag to indicate we are doing steepest descent
bool step(const S &Ab, V &x)
CGState(const S &Ab, const V &x, const Parameters ¶meters, bool steep)
ConjugateGradientParameters Parameters
void axpy(double alpha, const V1 &x, V2 &y)
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)