Conjugate gradient minimization

From stats++ wiki
Jump to: navigation, search

stats++

In stats++, conjugate gradient minimization is accomplished with the ConjugateGradient class:

class ConjugateGradient : public LineSearchMethod
{
 
public:
 
    std::string method;
    double ftol;
    double gtol;
    double tol;
    int iter_max;
 
 
    ConjugateGradient();
    ~ConjugateGradient();
 
    std::tuple<int,
               Vector<double>,
               double> minimize(std::function<double(const Vector<double> &)> f,
                                std::function<Vector<double>(const Vector<double> &)> df,
                                Vector<double> x);
 
private:
 
    bool converged(const double f, const double fi, const Vector<double> &g, const Vector<double> &pi);
 
};

In code, it functions as follows:

ConjugateGradient cg;
 
int info;
Vector<double> x = ...;
double fx;
std::tie(info, x, fx) = cg.minimize(f, df, x);

Example

IGNORE