diff options
author | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2010-07-26 14:20:25 +0000 |
---|---|---|
committer | astrojhgu <astrojhgu@ed2142bd-67ad-457f-ba7c-d818d4011675> | 2010-07-26 14:20:25 +0000 |
commit | 967219fc1991d466ecd9ede8fa00083b46ebf2ed (patch) | |
tree | 4cc9a4d7965626805b1451670eac4cda4536e59b /math | |
parent | a152f015339205e449a54f4512d600bcbb510674 (diff) | |
download | opt-utilities-967219fc1991d466ecd9ede8fa00083b46ebf2ed.tar.bz2 |
git-svn-id: file:///home/svn/opt_utilities@127 ed2142bd-67ad-457f-ba7c-d818d4011675
Diffstat (limited to 'math')
-rw-r--r-- | math/num_diff.hpp | 70 | ||||
-rw-r--r-- | math/vector_operation.hpp | 26 |
2 files changed, 68 insertions, 28 deletions
diff --git a/math/num_diff.hpp b/math/num_diff.hpp index 0bd2937..2d2e67d 100644 --- a/math/num_diff.hpp +++ b/math/num_diff.hpp @@ -17,35 +17,60 @@ namespace opt_utilities /** calculate the numerical differential of a func_obj */ + template<typename rT,typename pT> + class diff_func_obj + :public func_obj<rT,pT> + { + private: + virtual pT do_gradient(const pT& p)=0; + public: + pT gradient(const pT& p) + { + return do_gradient(p); + } + }; + + + template <typename rT,typename pT> - rT gradient(func_obj<rT,pT>& f,const pT& p,size_t n) + rT gradient(func_obj<rT,pT>& f,pT& p,size_t n) { rT ep=std::sqrt(std::numeric_limits<rT>::epsilon()); rT result; - pT p2; - resize(p2,get_size(p)); - pT p1; - resize(p1,get_size(p)); + pT p_tmp; + + typename element_type_trait<pT>::element_type old_value=get_element(p,n); - for(size_t i=0;i<get_size(p);++i) - { - set_element(p2,i,get_element(p,i)); - set_element(p1,i,get_element(p,i)); - } typename element_type_trait<pT>::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); - + set_element(p,n,old_value+h); + rT v2=f(p); + set_element(p,n,old_value-h); + rT v1=f(p_tmp); + set_element(p,n,old_value); result=(v2-v1)/h/2; return result; } + template <typename rT,typename pT> + pT gradient(func_obj<rT,pT>& f,pT& p) + { + diff_func_obj<rT,pT>* pdfo=0; + if(pdfo=dynamic_cast<diff_func_obj<rT,pT>*>(&f)) + { + return pdfo->gradient(p); + } + pT result; + resize(result,get_size(p)); + for(int i=0;i<get_size(p);++i) + { + set_element(result,i,gradient(f,p,i)); + } + return result; + } + template <typename rT,typename pT> rT hessian(func_obj<rT,pT>& f,const pT& p,size_t m,size_t n) { @@ -83,6 +108,19 @@ namespace opt_utilities rT result=(f(p11)+f(p00)-f(p01)-f(p10))/(4*hm*hn); return result; } + + + template<typename rT,typename pT> + rT div(func_obj<rT,pT>& f,const pT& p) + { + rT result=0; + for(int i=0;i<get_size(p);++i) + { + result+=hessian(f,p,i,i); + } + return result; + } + } #endif diff --git a/math/vector_operation.hpp b/math/vector_operation.hpp index 481fdef..3f5532c 100644 --- a/math/vector_operation.hpp +++ b/math/vector_operation.hpp @@ -1,18 +1,20 @@ #ifndef VECTOR_OPERATION_HPP #define VECTOR_OPERATION_HPP #include <core/opt_traits.hpp> - -template <typename pT> -typename element_type_trait<pT>::element_type -inner_product(const pT& v1,const pT& v2) +namespace opt_utilities { - typename element_type_trait<pT>::element_type result; - for(int i=0;i<get_size(v1);++i) - { - result+=get_element(v1,i)*get_element(v2,i); - } - return result; -} - + template <typename pT> + typename element_type_trait<pT>::element_type + inner_product(const pT& v1,const pT& v2) + { + typename element_type_trait<pT>::element_type result(0); + for(int i=0;i<get_size(v1);++i) + { + result+=get_element(v1,i)*get_element(v2,i); + } + return result; + } + +} #endif |