21 #ifndef DIFF_HESSIAN_IND_H
22 #define DIFF_HESSIAN_IND_H
47 template<
class Func,
class Model,
class Adapter>
52 const std::vector<double>& dx,
53 const Adapter& adapter
56 const size_t D = adapter.size(&x);
61 for(
size_t i = 0;
i <
D;
i++)
63 for(
size_t j = 0; j <
D; j++)
67 double yi = adapter.get(&y,
i);
70 double fx = f(y,
i,
i);
74 double fxp = f(y, i, i);
78 double fxm = f(y, i, i);
81 H(i, i) = (fxp - 2*fx + fxm) / (dx[i]*dx[i]);
84 adapter.set(&y, i, yi);
88 double yi = adapter.get(&y,
i);
89 double yj = adapter.get(&y, j);
93 double fxpp = f(y, i, j);
97 double fxpm = f(y, i, j);
101 double fxmm = f(y, i, j);
105 double fxmp = f(y, i, j);
107 H(i, j) = (fxpp - fxpm - fxmp + fxmm) / (4*dx[i]*dx[j]);
110 adapter.set(&y, i, j, yi, yj);
127 template<
class Func,
class Vec>
133 const std::vector<double>& dx
155 template<
class Func,
class Model,
class Adapter>
160 const std::vector<double>& dx,
161 const Adapter& adapter
164 const size_t D = adapter.size(&x);
169 for(
size_t i = 0;
i <
D;
i++)
171 for(
size_t j = 0; j <=
i; j++)
175 double yi = adapter.get(&y,
i);
178 double fx = f(y,
i,
i);
182 double fxp = f(y, i, i);
186 double fxm = f(y, i, i);
189 H(i, i) = (fxp - 2*fx + fxm) / (dx[i]*dx[i]);
192 adapter.set(&y, i, yi);
196 double yi = adapter.get(&y,
i);
197 double yj = adapter.get(&y, j);
201 double fxpp = f(y, i, j);
205 double fxpm = f(y, i, j);
209 double fxmm = f(y, i, j);
213 double fxmp = f(y, i, j);
215 H(i, j) = (fxpp - fxpm - fxmp + fxmm) / (4*dx[i]*dx[j]);
219 adapter.set(&y, i, j, yi, yj);
236 template<
class Func,
class Vec>
242 const std::vector<double>& dx
265 template<
class Func,
class Model,
class Adapter>
270 const std::vector<double>& dx,
271 const Adapter& adapter,
276 const size_t D = adapter.size(&x);
279 "Cannot compute Hessian diagonal; bad indices.");
284 for(
size_t i = is;
i <= ie;
i++)
286 double yi = adapter.get(&y,
i);
293 double fxp = f(y, i);
297 double fxm = f(y, i);
300 H[i - is] = (fxp - 2*fx + fxm) / (dx[i]*dx[i]);
303 adapter.
set(&y, i, yi);
318 template<
class Func,
class Vec>
324 const std::vector<double>& dx,
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
void move_param(Model &x, size_t i, double dv, const Adapter &aptr)
Helper function that moves a parameter by an amount.
Definition: diff_util.h:102
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
#define IFT(a, ex, msg)
Definition: l_exception.h:101
Matrix hessian_symmetric_ind(const Func &f, const Model &x, const std::vector< double > &dx, const Adapter &adapter)
Computes the Hessian of an "independent" function, evaluated at a point, using finite differences...
Definition: diff_hessian_ind.h:157
void move_params(Model &x, size_t i, size_t j, double dv, double dw, const Adapter &aptr)
Helper function that moves a pair of parameters by an amount.
Definition: diff_util.h:111
x
Definition: APPgetLargeConnectedEdges.m:100
Vector & set(Value_type val)
Clone of zero_out(int)
Definition: m_vector.h:707
Matrix hessian_ind(const Func &f, const Model &x, const std::vector< double > &dx, const Adapter &adapter)
Computes the Hessian of a function, evaluated at a point, using finite differences.
Definition: diff_hessian_ind.h:49
Default adapter for the hessian function.
Definition: diff_util.h:42
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
D
Definition: APPgetLargeConnectedEdges.m:106
Vector hessian_ind_diagonal(const Func &f, const Model &x, const std::vector< double > &dx, const Adapter &adapter, size_t is, size_t ie)
Computes the Hessian diagonal of a function, evaluated at a point, using finite differences.
Definition: diff_hessian_ind.h:267
Object thrown when computation fails somehow during execution.
Definition: l_exception.h:321