12 static bool f_calling_compute;
16 if (val) *val = this->
f(x);
17 if (g) this->
gradf(x, *g);
24 if (f_calling_compute)
25 assert(!
"vnl_cost_function: RECURSION");
27 f_calling_compute =
true;
28 this->
compute(x, &val,
nullptr);
29 f_calling_compute =
false;
36 if (f_calling_compute)
37 assert(!
"vnl_cost_function: RECURSION");
38 f_calling_compute =
true;
40 f_calling_compute =
false;
50 for (
int i = 0; i <
dim; ++i) {
51 double tplus = x[i] + h;
53 double fplus = this->
f(tx);
55 double tminus = x[i] - h;
57 double fminus = this->
f(tx);
59 gradient[i] = (fplus - fminus) / (tplus - tminus);
virtual void gradf(vnl_vector< double > const &x, vnl_vector< double > &gradient)
Calculate the gradient of f at parameter vector x.
double f(vnl_vector< double > const &x) override
The main function. Given the parameter vector x, compute the value of f(x).
void fdgradf(vnl_vector< double > const &x, vnl_vector< double > &gradient, double stepsize=1e-5)
Compute finite-difference gradient.
virtual void compute(vnl_vector< double > const &x, double *f, vnl_vector< double > *g)
Compute one or both of f and g.