71 template<Differentiable F>
int optimize(F& func, vec& x) {
74 hist_residual.clear();
78 const auto ref_magnitude = norm(x);
83 const auto residual = func.evaluate_residual(x);
85 if(0 == counter) ninja = solve(func.evaluate_jacobian(x), residual);
89 alpha.reserve(hist_ninja.size());
91 hist_residual.back() += residual;
93 hist_factor.emplace_back(dot(hist_ninja.back(), hist_residual.back()));
98 for(
auto J =
static_cast<int>(hist_factor.size()) - 1; J >= 0; --J) {
100 alpha.emplace_back(dot(hist_ninja[J], ninja) / hist_factor[J]);
102 ninja -= alpha.back() * hist_residual[J];
105 ninja *= dot(hist_residual.back(), hist_ninja.back()) / dot(hist_residual.back(), hist_residual.back());
107 for(
size_t I = 0, J = hist_factor.size() - 1; I < hist_factor.size(); ++I, --J) ninja += (alpha[J] - dot(hist_residual[I], ninja) / hist_factor[I]) * hist_ninja[I];
111 hist_ninja.emplace_back(-ninja);
112 hist_residual.emplace_back(-residual);
114 const auto error = norm(ninja);
115 const auto ref_error = error / ref_magnitude;
116 suanpan_debug(
"Local iteration error: {:.5E}.\n", ref_error);
121 if(counter > max_hist) {
122 hist_ninja.pop_front();
123 hist_residual.pop_front();
124 hist_factor.pop_front();