Loading...
Searching...
No Matches
SecantHessian.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#include "SecantHessian.h"
6
7#include "../ml/RgminAbi.h"
8
9#include <cstdint>
10#include <vector>
11
12// BFGS has no local update. rgmin is required, and the export has to be
13// visible. A header that does not declare it stops the build here.
14#if !defined(GPR_RGMIN_BFGS_HESSIAN) || !defined(GPR_RGMIN_SR1_HESSIAN)
15#error \
16 "gpridge requires rgmin_bfgs_hessian_update and rgmin_sr1_hessian_update. The local BFGS and SR1 updates were removed."
17#endif
18#if !__has_include(<rgsaddle.h>)
19#error \
20 "gpridge requires rgsaddle (rgsaddle_hessian_bofill). The local Bofill update was removed."
21#endif
22
23#include <rgmin.h>
24
25namespace gpr::optim {
26namespace {
27
28void packRowMajor(const Eigen::MatrixXd& matrix, std::vector<double>& packed)
29{
30 const Eigen::Index n = matrix.rows();
31 const auto nn = static_cast<std::size_t>(n);
32 packed.resize(nn * nn);
33 for (Eigen::Index col = 0; col < n; ++col) {
34 for (Eigen::Index row = 0; row < n; ++row) {
35 packed[static_cast<std::size_t>(row) * nn +
36 static_cast<std::size_t>(col)] = matrix(row, col);
37 }
38 }
39}
40
41bool finiteRowMajor(Eigen::MatrixXd& matrix, std::vector<double>& packed)
42{
43 for (double value: packed) {
44 if (!std::isfinite(value)) return false;
45 }
46 const Eigen::Index n = matrix.rows();
47 const auto nn = static_cast<std::size_t>(n);
48 for (Eigen::Index col = 0; col < n; ++col) {
49 for (Eigen::Index row = 0; row < n; ++row) {
50 matrix(row, col) = packed[static_cast<std::size_t>(row) * nn +
51 static_cast<std::size_t>(col)];
52 }
53 }
54 return true;
55}
56
57} // namespace
58
59bool SecantHessian::updateBfgs(Eigen::MatrixXd& matrix,
60 const Eigen::VectorXd& step,
61 const Eigen::VectorXd& gradient_change)
62{
63 std::vector<double> packed;
64 packRowMajor(matrix, packed);
65 const auto applied = rgmin_bfgs_hessian_update(
66 static_cast<std::int64_t>(matrix.rows()), packed.data(), step.data(),
67 gradient_change.data());
68 // ABI 1.77 reports an accepted BFGS update with identical entries as
69 // SKIPPED and reports curvature refusal separately. An accepted pair
70 // advances the reference even when the matrix entries are identical.
71 if (applied == RGMIN_HESSIAN_UPDATE_SKIPPED)
72 return rgmin_abi::libraryStamp().minor >= 77;
73 if (applied != RGMIN_SUCCESS) return false;
74 return finiteRowMajor(matrix, packed);
75}
76
77bool SecantHessian::updateSr1(Eigen::MatrixXd& matrix,
78 const Eigen::VectorXd& step,
79 const Eigen::VectorXd& gradient_change)
80{
81 std::vector<double> packed;
82 packRowMajor(matrix, packed);
83 const auto applied = rgmin_sr1_hessian_update(
84 static_cast<std::int64_t>(matrix.rows()), packed.data(), step.data(),
85 gradient_change.data());
86 // A skip leaves the Hessian and, through observe, the reference.
87 if (applied == RGMIN_HESSIAN_UPDATE_SKIPPED) return false;
88 if (applied != RGMIN_SUCCESS) return false;
89 return finiteRowMajor(matrix, packed);
90}
91
92bool SecantHessian::updateBofill(Eigen::MatrixXd& matrix,
93 const Eigen::VectorXd& step,
94 const Eigen::VectorXd& gradient_change)
95{
96 std::vector<double> packed;
97 packRowMajor(matrix, packed);
98 const int through = bofillThroughLoadedRgsaddle(
99 static_cast<std::int64_t>(matrix.rows()), packed.data(), step.data(),
100 gradient_change.data());
101 if (through < 0) throw std::runtime_error(bofillRgsaddleAbiRefusal());
102 if (through == 0) return false;
103 return finiteRowMajor(matrix, packed);
104}
105
106} // namespace gpr::optim
std::string bofillRgsaddleAbiRefusal()
int bofillThroughLoadedRgsaddle(std::int64_t n, double *hessian_row_major, const double *step, const double *gradient_change)
Stamp libraryStamp()
Definition RgminAbi.h:71