Loading...
Searching...
No Matches
gpr::derivatives::DerivativeProvider Class Reference

A type-erased Hessian and Hessian-vector-product provider. More...

#include <gpr/derivatives/DerivativeProvider.h>

Public Types

using HessianFunction = std::function<bool(const Vector&, Matrix&)>
using HessianVectorFunction

Public Member Functions

 DerivativeProvider ()=default
 DerivativeProvider (HessianFunction hessian, HessianVectorFunction hessian_vector)
 operator bool () const noexcept
bool hessian (const Vector &position, Matrix &result, std::string *reason=nullptr) const noexcept
bool hessianVector (const Vector &position, const Vector &direction, Vector &result, std::string *reason=nullptr) const noexcept

Detailed Description

A type-erased Hessian and Hessian-vector-product provider.

False means that the derivative is unavailable or that an input/evaluation was invalid; callback exceptions are caught at this boundary and reported through the optional reason string. Implementations return the Hessian of the energy; force-based providers therefore negate the force Jacobian.

Definition at line 32 of file DerivativeProvider.h.

Member Typedef Documentation

◆ HessianFunction

Definition at line 34 of file DerivativeProvider.h.

◆ HessianVectorFunction

Initial value:
std::function<bool(const Vector&, const Vector&, Vector&)>
Eigen::VectorXd Vector

Definition at line 35 of file DerivativeProvider.h.

Constructor & Destructor Documentation

◆ DerivativeProvider() [1/2]

gpr::derivatives::DerivativeProvider::DerivativeProvider ( )
default

◆ DerivativeProvider() [2/2]

gpr::derivatives::DerivativeProvider::DerivativeProvider ( HessianFunction hessian,
HessianVectorFunction hessian_vector )
inline

Definition at line 40 of file DerivativeProvider.h.

42 : hessian_(std::move(hessian)),
43 hessian_vector_(std::move(hessian_vector))
44 {
45 }
bool hessian(const Vector &position, Matrix &result, std::string *reason=nullptr) const noexcept

Member Function Documentation

◆ hessian()

bool gpr::derivatives::DerivativeProvider::hessian ( const Vector & position,
Matrix & result,
std::string * reason = nullptr ) const
inlinenodiscardnoexcept

Definition at line 53 of file DerivativeProvider.h.

55 {
56 result.resize(0, 0);
57 if (!hessian_) {
58 setReason(reason, "derivative provider is unavailable");
59 return false;
60 }
61 if (position.size() == 0 || !position.allFinite()) {
62 setReason(reason, "position must be non-empty and finite");
63 return false;
64 }
65 try {
66 if (!hessian_(position, result)) {
67 result.resize(0, 0);
68 setReason(reason, "hessian callback returned false");
69 return false;
70 }
71 } catch (const std::exception& error) {
72 result.resize(0, 0);
73 setReason(reason, error.what());
74 return false;
75 } catch (...) {
76 result.resize(0, 0);
77 setReason(reason, "hessian callback threw non-standard exception");
78 return false;
79 }
80 if (result.rows() != position.size() ||
81 result.cols() != position.size()) {
82 result.resize(0, 0);
83 setReason(reason, "hessian output shape mismatch");
84 return false;
85 }
86 if (!result.allFinite()) {
87 result.resize(0, 0);
88 setReason(reason, "hessian output contains non-finite entries");
89 return false;
90 }
91 clearReason(reason);
92 return true;
93 }

◆ hessianVector()

bool gpr::derivatives::DerivativeProvider::hessianVector ( const Vector & position,
const Vector & direction,
Vector & result,
std::string * reason = nullptr ) const
inlinenodiscardnoexcept

Definition at line 95 of file DerivativeProvider.h.

98 {
99 result.resize(0);
100 if (!hessian_vector_) {
101 setReason(reason, "derivative provider is unavailable");
102 return false;
103 }
104 if (position.size() == 0 || !position.allFinite()) {
105 setReason(reason, "position must be non-empty and finite");
106 return false;
107 }
108 if (direction.size() != position.size() || !direction.allFinite()) {
109 setReason(reason,
110 "direction must match position size and be finite");
111 return false;
112 }
113 if (direction.cwiseAbs().maxCoeff() == 0.0) {
114 setReason(reason, "direction must be non-zero");
115 return false;
116 }
117 try {
118 if (!hessian_vector_(position, direction, result)) {
119 result.resize(0);
120 setReason(reason, "hessian-vector callback returned false");
121 return false;
122 }
123 } catch (const std::exception& error) {
124 result.resize(0);
125 setReason(reason, error.what());
126 return false;
127 } catch (...) {
128 result.resize(0);
129 setReason(reason,
130 "hessian-vector callback threw non-standard exception");
131 return false;
132 }
133 if (result.size() != position.size()) {
134 result.resize(0);
135 setReason(reason, "hessian-vector output size mismatch");
136 return false;
137 }
138 if (!result.allFinite()) {
139 result.resize(0);
140 setReason(reason,
141 "hessian-vector output contains non-finite entries");
142 return false;
143 }
144 clearReason(reason);
145 return true;
146 }

◆ operator bool()

gpr::derivatives::DerivativeProvider::operator bool ( ) const
inlineexplicitnodiscardnoexcept

Definition at line 47 of file DerivativeProvider.h.

48 {
49 return static_cast<bool>(hessian_) &&
50 static_cast<bool>(hessian_vector_);
51 }

The documentation for this class was generated from the following file: