/** \file num_diff.hpp */ #ifndef NUMDIFF_HPP #define NUMDIFF_HPP #define OPT_HEADER #include #include #include #include #include namespace opt_utilities { /** calculate the numerical differential of a func_obj */ template rT gradient(func_obj& f,const pT& p,size_t n) { rT ep=std::sqrt(std::numeric_limits::epsilon()); rT result; pT p2; resize(p2,get_size(p)); pT p1; resize(p1,get_size(p)); for(size_t i=0;i::element_type h= std::max(get_element(p,n),rT(1))*ep; set_element(p2,n,get_element(p,n)+h); set_element(p1,n,get_element(p,n)-h); rT v2=f(p2); rT v1=f(p1); result=(v2-v1)/h/2; return result; } template rT hessian(func_obj& f,const pT& p,size_t m,size_t n) { rT ep=std::sqrt(std::numeric_limits::epsilon()); typename element_type_trait::element_type hn= std::max(get_element(p,n),rT(1))*ep; typename element_type_trait::element_type hm= std::max(get_element(p,m),rT(1))*ep; pT p11; resize(p11,get_size(p)); pT p00; resize(p00,get_size(p)); pT p10; resize(p10,get_size(p)); pT p01; resize(p01,get_size(p)); for(size_t i=0;i