30 template<
typename Change>
41 template<
typename Change>
49 template<
typename Change>
55 const fl r = 1 / (alpha * yp);
56 const sz n = p.num_floats();
59 h(i, j) += alpha * r * (minus_hy(i) * p(j)
60 + minus_hy(j) * p(i)) +
61 + alpha * alpha * (r*r * yhy + r) * p(i) * p(j);
65 template<
typename F,
typename Conf,
typename Change>
66 fl line_search(F& f,
sz n,
const Conf& x,
const Change& g,
const fl f0,
const Change& p, Conf& x_new, Change& g_new,
fl& f1) {
68 const unsigned max_trials = 10;
69 const fl multiplier = 0.5;
75 x_new = x; x_new.increment(p, alpha);
77 if(f1 - f0 < c0 * alpha * pg)
89 template<
typename Change>
95 template<
typename F,
typename Conf,
typename Change>
96 fl bfgs(F& f, Conf& x, Change& g,
const unsigned max_steps,
const fl average_required_improvement,
const sz over) {
97 sz n = g.num_floats();
111 flv f_values; f_values.reserve(max_steps+1);
112 f_values.push_back(f0);
117 const fl alpha =
line_search(f, n, x, g, f0, p, x_new, g_new, f1);
120 f_values.push_back(f1);
135 if(!(f0 <= f_orig)) {