11template <Config Conf,
class Storage>
15 if (sᵀs <=
params.min_abs_s)
17 if (not std::isfinite(yᵀs))
19 real_t a_yᵀs =
params.force_pos_def ? yᵀs : std::abs(yᵀs);
20 if (a_yᵀs <=
params.min_div_fac * sᵀs)
28 bool cbfgs_cond = a_yᵀs >= sᵀs * ϵ * std::pow(pᵀp,
α / 2);
36template <Config Conf,
class Storage>
38 real_t pₙₑₓₜᵀpₙₑₓₜ,
bool forced) {
59template <Config Conf,
class Storage>
65template <Config Conf,
class Storage>
67 Sign sign,
bool forced) {
68 const auto s = xₙₑₓₜ - xₖ;
70 real_t pₙₑₓₜᵀpₙₑₓₜ =
params.cbfgs ? pₙₑₓₜ.squaredNorm() : 0;
74template <Config Conf,
class Storage>
83 real_t yᵀy =
y(new_idx).squaredNorm();
84 γ = 1 / (
ρ(new_idx) * yᵀy);
88 α(i) =
ρ(i) *
s(i).dot(q);
96 real_t β = ρ(i) * y(i).dot(q);
97 q -= (β - α(i)) * s(i);
103template <Config Conf,
class Storage>
105 const auto &J)
const {
109 const bool fullJ = q.size() ==
static_cast<index_t>(J.size());
116 throw std::invalid_argument(
"CBFGS check not supported when using "
117 "masked version of LBFGS::apply_masked()");
127 const auto dotJ = [&J, fullJ](
const auto &a,
const auto &b) {
138 const auto axmyJ = [&J, fullJ](
real_t a,
const auto &x,
auto &
y) {
147 const auto scalJ = [&J, fullJ](
real_t a,
auto &x) {
169 α(i) =
ρ(i) * dotJ(
s(i), q);
170 axmyJ(
α(i),
y(i), q);
175 γ = 1 / (
ρ(i) * yᵀy);
187 if (std::isnan(
ρ(i)))
191 axmyJ(β -
α(i),
s(i), q);
197template <Config Conf,
class Storage>
202template <Config Conf,
class Storage>
204 const std::vector<index_t> &J)
const {
208template <Config Conf,
class Storage>
214template <Config Conf,
class Storage>
217 throw std::invalid_argument(
"LBFGS::Params::memory must be >= 1");
222template <Config Conf>
227template <Config Conf,
class Storage>
void foreach_rev(const F &fun) const
Iterate over the indices in the history buffer, newest first.
bool apply_masked(rvec q, real_t γ, crindexvec J) const
Apply the inverse Hessian approximation to the given vector q, applying only the columns and rows of ...
LBFGSParams< config_t > Params
index_t succ(index_t i) const
Get the next index in the circular buffer of previous s and y vectors.
index_t pred(index_t i) const
Get the previous index in the circular buffer of s and y vectors.
length_t n() const
Get the size of the s and y vectors in the buffer.
length_t history() const
Get the number of previous vectors s and y stored in the buffer.
void foreach_fwd(const F &fun) const
Iterate over the indices in the history buffer, oldest first.
bool update_sy(crvec s, crvec y, real_t pₙₑₓₜᵀpₙₑₓₜ, bool forced=false)
Update the inverse Hessian approximation using the new vectors sₖ = xₙₑₓₜ - xₖ and yₖ = pₙₑₓₜ - pₖ.
static bool update_valid(const Params ¶ms, real_t yᵀs, real_t sᵀs, real_t pᵀp)
Check if the new vectors s and y allow for a valid BFGS update that preserves the positive definitene...
bool apply_masked_impl(rvec q, real_t γ, const auto &J) const
Apply the inverse Hessian approximation to the given vector q, applying only the columns and rows of ...
void resize(length_t n)
Re-allocate storage for a problem with a different size.
void reset()
Throw away the approximation and all previous vectors s and y.
bool update_sy_impl(const auto &s, const auto &y, real_t pₙₑₓₜᵀpₙₑₓₜ, bool forced=false)
Sign
The sign of the vectors passed to the update method.
bool apply(rvec q, real_t γ=-1) const
Apply the inverse Hessian approximation to the given vector q.
void scale_y(real_t factor)
Scale the stored y vectors by the given factor.
bool update(crvec xₖ, crvec xₙₑₓₜ, crvec pₖ, crvec pₙₑₓₜ, Sign sign=Sign::Positive, bool forced=false)
Update the inverse Hessian approximation using the new vectors xₙₑₓₜ and pₙₑₓₜ.
typename Conf::real_t real_t
typename Conf::index_t index_t
typename Conf::length_t length_t
typename Conf::crvec crvec
@ BasedOnCurvature
Initial inverse Hessian approximation is set to .
typename Conf::crindexvec crindexvec
void resize(length_t n, length_t history)
Re-allocate storage for a problem with a different size.
length_t n() const
Get the size of the s and y vectors in the buffer.
length_t history() const
Get the number of previous vectors s and y stored in the buffer.