All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
deriv.h
Go to the documentation of this file.
1 /*
2  -------------------------------------------------------------------
3 
4  Copyright (C) 2006-2014, Andrew W. Steiner
5 
6  This file is part of O2scl.
7 
8  O2scl is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 3 of the License, or
11  (at your option) any later version.
12 
13  O2scl 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
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with O2scl. If not, see <http://www.gnu.org/licenses/>.
20 
21  -------------------------------------------------------------------
22 */
23 #ifndef O2SCL_DERIV_H
24 #define O2SCL_DERIV_H
25 
26 /** \file deriv.h
27  \brief File defining \ref o2scl::deriv_base
28 */
29 
30 #include <iostream>
31 #include <cmath>
32 #include <o2scl/funct.h>
33 
34 #ifndef DOXYGEN_NO_O2NS
35 namespace o2scl {
36 #endif
37 
38  /** \brief Numerical differentiation base [abstract base]
39 
40  This base class does not perform any actual differentiation.
41  Use one of the children cern_deriv, gsl_deriv, or eqi_deriv
42  instead.
43 
44  This base class contains some code to automatically apply
45  the first derivative routines to compute second or third
46  derivatives. The error estimates for these will likely
47  be underestimated.
48 
49  \note Because this class template aims to automatically provide
50  second and third derivatives, one must overload either both
51  calc() and calc_int() or both calc_err() and calc_err_int().
52 
53  \future Improve the methods for second and third derivatives
54  */
55  template<class func_t=funct11> class deriv_base {
56 
57 #ifndef DOXYGEN_INTERNAL
58 
59  protected:
60 
61  /** \brief A structure for passing the function to second and
62  third derivatives [protected]
63  */
64  typedef struct {
65 
66  public:
67 
68  /// The pointer to the function
69  func_t *func;
70 
71  } dpars;
72 
73  /// Avoids infinite loops in case the user calls the base class version
74  bool from_deriv;
75 
76 #endif
77 
78  public:
79 
80  deriv_base() {
81  verbose=0;
82  from_deriv=false;
83  err_nonconv=true;
84  }
85 
86  virtual ~deriv_base() {}
87 
88  /// If true, call the error handler if the routine does not "converge"
90 
91  /** \brief Calculate the first derivative of \c func w.r.t. x
92 
93  After calling deriv(), the error may be obtained from
94  \ref get_err().
95  */
96  virtual double deriv(double x, func_t &func) {
97  double dx;
98  from_deriv=true;
99  deriv_err(x,func,dx,derr);
100  from_deriv=false;
101  return dx;
102  }
103 
104  /** \brief Calculate the second derivative of \c func w.r.t. x
105  */
106  virtual double deriv2(double x, func_t &func) {
107  double val;
108 
109  funct11 mf=
110  std::bind(std::mem_fn<double(double,func_t *)>(&deriv_base::derivfun),
111  this,std::placeholders::_1,&func);
112 
113  val=deriv_int(x,mf);
114  // The error estimate is unavailable, so we set it to zero
115  derr=0.0;
116  return val;
117  }
118 
119  /** \brief Calculate the third derivative of \c func w.r.t. x
120  */
121  virtual double deriv3(double x, func_t &func) {
122  double val;
123 
124  funct11 mf=
125  std::bind(std::mem_fn<double(double,func_t *)>(&deriv_base::derivfun2),
126  this,std::placeholders::_1,&func);
127 
128  val=deriv_int(x,mf);
129  // The error estimate is unavailable, so we set it to zero
130  derr=0.0;
131  return val;
132  }
133 
134  /** \brief Get uncertainty of last calculation
135  */
136  virtual double get_err() {
137  return derr;
138  }
139 
140  /** \brief Output control
141  */
142  int verbose;
143 
144  /** \brief Calculate the first derivative of \c func w.r.t. x and the
145  uncertainty
146  */
147  virtual int deriv_err(double x, func_t &func, double &dfdx,
148  double &err)=0;
149 
150  /** \brief Calculate the second derivative of \c func w.r.t. x and the
151  uncertainty
152  */
153  virtual int deriv2_err(double x, func_t &func,
154  double &d2fdx2, double &err) {
155  int ret;
156 
157  funct11 mf=
158  std::bind(std::mem_fn<double(double,func_t *)>(&deriv_base::derivfun),
159  this,std::placeholders::_1,&func);
160 
161  ret=deriv_err_int(x,mf,d2fdx2,err);
162  // The error estimate is unavailable, so we set it to zero
163  err=0.0;
164  return 0;
165  }
166 
167  /** \brief Calculate the third derivative of \c func w.r.t. x and the
168  uncertainty
169  */
170  virtual int deriv3_err(double x, func_t &func,
171  double &d3fdx3, double &err) {
172  int ret;
173 
174  funct11 mf=
175  std::bind(std::mem_fn<double(double,func_t *)>(&deriv_base::derivfun2),
176  this,std::placeholders::_1,&func);
177 
178  ret=deriv_err_int(x,mf,d3fdx3,err);
179  // The error estimate is unavailable, so we set it to zero
180  err=0.0;
181  return 0;
182  }
183 
184 #ifdef O2SCL_NEVER_DEFINED
185  }{
186 #endif
187 
188  /// Return string denoting type ("deriv")
189  virtual const char *type() { return "deriv"; }
190 
191  protected:
192 
193 #ifndef DOXYGEN_INTERNAL
194 
195  /** \brief Calculate the first derivative of \c func w.r.t. x
196 
197  This is an internal version of deriv() which is used in
198  computing second and third derivatives
199  */
200  virtual double deriv_int(double x, funct11 &func) {
201  double dx;
202  from_deriv=true;
203  deriv_err_int(x,func,dx,derr);
204  from_deriv=false;
205  return dx;
206  }
207 
208  /** \brief Calculate the first derivative of \c func w.r.t. x and the
209  uncertainty
210 
211  This is an internal version of deriv_err() which is used in
212  computing second and third derivatives
213  */
214  virtual int deriv_err_int(double x, funct11 &func,
215  double &dfdx, double &err)=0;
216 
217  /// The uncertainity in the most recent derivative computation
218  double derr;
219 
220  /// The function for the second derivative
221  double derivfun(double x, func_t *fp) {
222  return deriv(x,*fp);
223  }
224 
225  /// The function for the third derivative
226  double derivfun2(double x, func_t *fp) {
227  funct11 mf=
228  std::bind(std::mem_fn<double(double,func_t *)>(&deriv_base::derivfun),
229  this,std::placeholders::_1,fp);
230  double val=deriv_int(x,mf);
231  return val;
232  }
233 
234 #endif
235 
236  };
237 
238 #ifndef DOXYGEN_NO_O2NS
239 }
240 #endif
241 
242 #endif
double derr
The uncertainity in the most recent derivative computation.
Definition: deriv.h:218
virtual int deriv_err(double x, func_t &func, double &dfdx, double &err)=0
Calculate the first derivative of func w.r.t. x and the uncertainty.
std::function< double(double)> funct11
One-dimensional function typedef.
Definition: funct.h:44
virtual const char * type()
Return string denoting type ("deriv")
Definition: deriv.h:189
bool from_deriv
Avoids infinite loops in case the user calls the base class version.
Definition: deriv.h:74
virtual double deriv(double x, func_t &func)
Calculate the first derivative of func w.r.t. x.
Definition: deriv.h:96
virtual int deriv_err_int(double x, funct11 &func, double &dfdx, double &err)=0
Calculate the first derivative of func w.r.t. x and the uncertainty.
virtual double deriv3(double x, func_t &func)
Calculate the third derivative of func w.r.t. x.
Definition: deriv.h:121
virtual int deriv3_err(double x, func_t &func, double &d3fdx3, double &err)
Calculate the third derivative of func w.r.t. x and the uncertainty.
Definition: deriv.h:170
func_t * func
The pointer to the function.
Definition: deriv.h:69
virtual double deriv2(double x, func_t &func)
Calculate the second derivative of func w.r.t. x.
Definition: deriv.h:106
Numerical differentiation base [abstract base].
Definition: deriv.h:55
virtual double deriv_int(double x, funct11 &func)
Calculate the first derivative of func w.r.t. x.
Definition: deriv.h:200
bool err_nonconv
If true, call the error handler if the routine does not "converge".
Definition: deriv.h:89
virtual int deriv2_err(double x, func_t &func, double &d2fdx2, double &err)
Calculate the second derivative of func w.r.t. x and the uncertainty.
Definition: deriv.h:153
double derivfun(double x, func_t *fp)
The function for the second derivative.
Definition: deriv.h:221
int verbose
Output control.
Definition: deriv.h:142
A structure for passing the function to second and third derivatives [protected]. ...
Definition: deriv.h:64
virtual double get_err()
Get uncertainty of last calculation.
Definition: deriv.h:136
double derivfun2(double x, func_t *fp)
The function for the third derivative.
Definition: deriv.h:226

Documentation generated with Doxygen. Provided under the GNU Free Documentation License (see License Information).
Hosted at Get Object-oriented Scientific Computing
Lib at SourceForge.net. Fast, secure and Free Open Source software
downloads..