virtual void gradient(real& grad, const real& x) const { real eps = finiteDifferenceEpsilon(), fp, fm; real xx = x; xx += eps; fp = this->operator()(xx); xx -= 2.0*eps; fm = this->operator()(xx); grad = 0.5 * (fp - fm) / eps; }
//! method to overload to compute grad_f, the first derivative of // the cost function with respect to x virtual void gradient(Array& grad, const Array& x) const { Real eps = finiteDifferenceEpsilon(), fp, fm; Array xx(x); for (Size i=0; i<x.size(); i++) { xx[i] += eps; fp = value(xx); xx[i] -= 2.0*eps; fm = value(xx); grad[i] = 0.5*(fp - fm)/eps; xx[i] = x[i]; } }
//! method to overload to compute J_f, the jacobian of // the cost function with respect to x virtual void jacobian(Matrix &jac, const Array &x) const { Real eps = finiteDifferenceEpsilon(); Array xx(x), fp, fm; for(Size i=0; i<x.size(); ++i) { xx[i] += eps; fp = values(xx); xx[i] -= 2.0*eps; fm = values(xx); for(Size j=0; j<fp.size(); ++j) { jac[j][i] = 0.5*(fp[j]-fm[j])/eps; } xx[i] = x[i]; } }