From bfcbc4e9a3754eb36d5f657a5130bafc2b727afd Mon Sep 17 00:00:00 2001 From: astrojhgu Date: Tue, 21 Apr 2009 08:07:37 +0000 Subject: git-svn-id: file:///home/svn/opt_utilities@26 ed2142bd-67ad-457f-ba7c-d818d4011675 --- core/num_diff.hpp | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ core/numdiff.hpp | 84 ------------------------------------------------------- 2 files changed, 84 insertions(+), 84 deletions(-) create mode 100644 core/num_diff.hpp delete mode 100644 core/numdiff.hpp (limited to 'core') diff --git a/core/num_diff.hpp b/core/num_diff.hpp new file mode 100644 index 0000000..f9cf520 --- /dev/null +++ b/core/num_diff.hpp @@ -0,0 +1,84 @@ +#ifndef NUMDIFF_HPP +#define NUMDIFF_HPP + +#include +#include +#include +#include +#include + +namespace opt_utilities +{ + template + class dfunc_obj + :public func_obj + { + private: + virtual pT do_diff(const pT& p)=0; + + public: + pT diff(const pT& p) + { + return do_diff(p); + } + }; + + class underivable + :public opt_exception + { + public: + underivable() + :opt_exception("underivable") + {} + }; + + template + pT numdiff(func_obj& f,const pT& p) + { + rT ep=std::sqrt(std::numeric_limits::epsilon()); + + pT result; + resize(result,get_size(p)); + 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,i),rT(1))*ep; + + set_element(p2,i,get_element(p,i)+h); + set_element(p1,i,get_element(p,i)-h); + + rT v2=f(p2); + rT v1=f(p1); + set_element(result,i, + (v2-v1)/h/2 + ); + set_element(p2,i,get_element(p,i)); + set_element(p1,i,get_element(p,i)); + } + return result; + } + + template + pT diff(func_obj& f,const pT& p) + { + dfunc_obj* pdf=dynamic_cast*>(&f); + if(pdf) + { + return pdf->diff(p); + } + return numdiff(f,p); + + } +} + +#endif diff --git a/core/numdiff.hpp b/core/numdiff.hpp deleted file mode 100644 index f9cf520..0000000 --- a/core/numdiff.hpp +++ /dev/null @@ -1,84 +0,0 @@ -#ifndef NUMDIFF_HPP -#define NUMDIFF_HPP - -#include -#include -#include -#include -#include - -namespace opt_utilities -{ - template - class dfunc_obj - :public func_obj - { - private: - virtual pT do_diff(const pT& p)=0; - - public: - pT diff(const pT& p) - { - return do_diff(p); - } - }; - - class underivable - :public opt_exception - { - public: - underivable() - :opt_exception("underivable") - {} - }; - - template - pT numdiff(func_obj& f,const pT& p) - { - rT ep=std::sqrt(std::numeric_limits::epsilon()); - - pT result; - resize(result,get_size(p)); - 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,i),rT(1))*ep; - - set_element(p2,i,get_element(p,i)+h); - set_element(p1,i,get_element(p,i)-h); - - rT v2=f(p2); - rT v1=f(p1); - set_element(result,i, - (v2-v1)/h/2 - ); - set_element(p2,i,get_element(p,i)); - set_element(p1,i,get_element(p,i)); - } - return result; - } - - template - pT diff(func_obj& f,const pT& p) - { - dfunc_obj* pdf=dynamic_cast*>(&f); - if(pdf) - { - return pdf->diff(p); - } - return numdiff(f,p); - - } -} - -#endif -- cgit v1.2.2