88 {
89 if (position.size() == 0 || gradient.size() != position.size() ||
90 !position.allFinite() || !gradient.allFinite())
91 throw std::invalid_argument(
92 "secant observation requires finite matching position and "
93 "gradient");
94 if (previous_position_.size() == 0) {
95 previous_position_ = position;
96 previous_gradient_ = gradient;
97 hessian_ = Eigen::MatrixXd::Zero(position.size(), position.size());
98 return false;
99 }
100 if (position.size() != previous_position_.size())
101 throw std::invalid_argument("secant observation dimension changed");
102
103 const Eigen::VectorXd step = position - previous_position_;
104 const Eigen::VectorXd gradient_change = gradient - previous_gradient_;
105 if (!(step.squaredNorm() > 0.0) || !step.allFinite() ||
106 !gradient_change.allFinite())
107 return false;
108
109 Eigen::MatrixXd candidate = hessian_;
111 const double ss = step.squaredNorm();
112 const double ys = gradient_change.dot(step);
113 if (!bfgsCurvatureResolved(gradient_change, step, ys) ||
114 !std::isfinite(ss))
115 return false;
116 candidate.diagonal().setConstant(ys / ss);
117 }
118
119
120
121
122
123 const Eigen::VectorXd action = candidate * step;
124 const Eigen::VectorXd residual = gradient_change - action;
125 const double action_scale =
126 std::max(gradient_change.norm(), action.norm());
127 const double resolution =
128 std::sqrt(std::numeric_limits<double>::epsilon()) * action_scale;
129 const double ys = gradient_change.dot(step);
130 if (pairs_ > 0 && residual.norm() <= resolution &&
132 (ys > 0.0 && std::isfinite(ys)))) {
133 previous_position_ = position;
134 previous_gradient_ = gradient;
135 last_step_ = step;
136 last_gradient_change_ = gradient_change;
137 appendMeasuredStep(step);
138 ++pairs_;
139 return true;
140 }
141
142 const bool updated =
update(candidate, method_, step, gradient_change);
143 if (!updated || !candidate.allFinite()) return false;
144 hessian_ = (0.5 * (candidate + candidate.transpose())).eval();
145 previous_position_ = position;
146 previous_gradient_ = gradient;
147 last_step_ = step;
148 last_gradient_change_ = gradient_change;
149 appendMeasuredStep(step);
150 ++pairs_;
151 return true;
152 }
static bool update(Eigen::MatrixXd &matrix, SecantHessianMethod method, const Eigen::VectorXd &step, const Eigen::VectorXd &gradient_change)