Update/add copyright of people who modified the code.
[synfig.git] / ETL / trunk / ETL / _calculus.h
1 /*! ========================================================================
2 ** Extended Template and Library
3 ** Calculus Functional Classes Implementation
4 ** $Id$
5 **
6 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
7 ** Copyright (c) 2008 Chris Moore
8 **
9 ** This package is free software; you can redistribute it and/or
10 ** modify it under the terms of the GNU General Public License as
11 ** published by the Free Software Foundation; either version 2 of
12 ** the License, or (at your option) any later version.
13 **
14 ** This package is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ** General Public License for more details.
18 **
19 ** === N O T E S ===========================================================
20 **
21 ** ========================================================================= */
22
23 /* === S T A R T =========================================================== */
24
25 #ifndef __ETL__CALCULUS_H
26 #define __ETL__CALCULUS_H
27
28 /* === H E A D E R S ======================================================= */
29
30 #include <functional>
31
32 #include "hermite"
33
34 /* === M A C R O S ========================================================= */
35
36 //#ifndef _EPSILON
37 //#define _EPSILON              0.0000001
38 //#endif
39
40 /* === T Y P E D E F S ===================================================== */
41
42 /* === C L A S S E S & S T R U C T S ======================================= */
43
44 _ETL_BEGIN_NAMESPACE
45
46 template <typename T>
47 class derivative : public std::unary_function<typename T::argument_type,typename T::result_type>
48 {
49         T func;
50         typename T::argument_type epsilon;
51 public:
52         explicit derivative(const T &x, const typename T::argument_type &epsilon=0.000001):func(x),epsilon(epsilon) { }
53
54         typename T::result_type
55         operator()(const typename T::argument_type &x)const
56         {
57                 return (func(x+epsilon)-func(x))/epsilon;
58         }
59 };
60
61 template <typename T>
62 class derivative<hermite<T> > : public std::unary_function<typename hermite<T>::argument_type,typename hermite<T>::result_type>
63 {
64         hermite<T> func;
65 public:
66         explicit derivative(const hermite<T> &x):func(x) { }
67
68         typename hermite<T>::result_type
69         operator()(const typename hermite<T>::argument_type &x)const
70         {
71                 T a = func[0], b = func[1], c = func[2], d = func[3];
72                 typename hermite<T>::argument_type y(1-x);
73                 return ((b-a)*y*y + (c-b)*x*y*2 + (d-c)*x*x) * 3;
74         }
75 };
76
77 template <typename T>
78 class integral : public std::binary_function<typename T::argument_type,typename T::argument_type,typename T::result_type>
79 {
80         T func;
81         int samples;
82 public:
83         explicit integral(const T &x, const int &samples=500):func(x),samples(samples) { }
84
85         typename T::result_type
86         operator()(typename T::argument_type x,typename T::argument_type y)const
87         {
88                 typename T::result_type ret=0;
89                 int i=samples;
90                 const typename T::argument_type increment=(y-x)/i;
91
92                 for(;i;i--,x+=increment)
93                         ret+=(func(x)+func(x+increment))*increment/2;
94                 return ret;
95         }
96 };
97
98 _ETL_END_NAMESPACE
99
100 /* === E N D =============================================================== */
101
102 #endif