1 /*! ========================================================================
2 ** Extended Template and Library
3 ** Calculus Functional Classes Implementation
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
8 ** This package is free software; you can redistribute it and/or
9 ** modify it under the terms of the GNU General Public License as
10 ** published by the Free Software Foundation; either version 2 of
11 ** the License, or (at your option) any later version.
13 ** This package is distributed in the hope that it will be useful,
14 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ** General Public License for more details.
18 ** === N O T E S ===========================================================
20 ** ========================================================================= */
22 /* === S T A R T =========================================================== */
24 #ifndef __ETL__CALCULUS_H
25 #define __ETL__CALCULUS_H
27 /* === H E A D E R S ======================================================= */
31 /* === M A C R O S ========================================================= */
34 //#define _EPSILON 0.0000001
37 /* === T Y P E D E F S ===================================================== */
39 /* === C L A S S E S & S T R U C T S ======================================= */
44 class derivative : public std::unary_function<typename T::argument_type,typename T::result_type>
47 typename T::argument_type epsilon;
49 explicit derivative(const T &x, const typename T::argument_type &epsilon=0.000001):func(x),epsilon(epsilon) { }
51 typename T::result_type
52 operator()(const typename T::argument_type &x)const
54 return (func(x+epsilon)-func(x))/epsilon;
59 class integral : public std::binary_function<typename T::argument_type,typename T::argument_type,typename T::result_type>
64 explicit integral(const T &x, const int &samples=500):func(x),samples(samples) { }
66 typename T::result_type
67 operator()(typename T::argument_type x,typename T::argument_type y)const
69 typename T::result_type ret=0;
71 const typename T::argument_type increment=(y-x)/i;
73 for(;i;i--,x+=increment)
74 ret+=(func(x)+func(x+increment))*increment/2;
81 /* === E N D =============================================================== */