00001
00022
00023
00024 #ifndef __ETL__CALCULUS_H
00025 #define __ETL__CALCULUS_H
00026
00027
00028
00029 #include <functional>
00030
00031
00032
00033
00034
00035
00036
00037 #define ETL_FIXED_DERIVATIVE 1
00038
00039
00040
00041
00042
00043 _ETL_BEGIN_NAMESPACE
00044
00045 template <typename T>
00046 class derivative : public std::unary_function<typename T::argument_type,typename T::result_type>
00047 {
00048 T func;
00049 typename T::argument_type epsilon;
00050 public:
00051 explicit derivative(const T &x, const typename T::argument_type &epsilon=0.000001):func(x),epsilon(epsilon) { }
00052
00053 typename T::result_type
00054 operator()(const typename T::argument_type &x)const
00055 {
00056 #ifdef ETL_FIXED_DERIVATIVE
00057 return (func(x+epsilon)-func(x))/epsilon;
00058 #else
00059 return (func(x)-func(x+epsilon))/epsilon;
00060 #endif
00061 }
00062 };
00063
00064 template <typename T>
00065 class integral : public std::binary_function<typename T::argument_type,typename T::argument_type,typename T::result_type>
00066 {
00067 T func;
00068 int samples;
00069 public:
00070 explicit integral(const T &x, const int &samples=500):func(x),samples(samples) { }
00071
00072 typename T::result_type
00073 operator()(typename T::argument_type x,typename T::argument_type y)const
00074 {
00075 typename T::result_type ret=0;
00076 int i=samples;
00077 const typename T::argument_type increment=(y-x)/i;
00078
00079 for(;i;i--,x+=increment)
00080 ret+=(func(x)+func(x+increment))*increment/2;
00081 return ret;
00082 }
00083 };
00084
00085 _ETL_END_NAMESPACE
00086
00087
00088
00089 #endif