GammaLib  1.7.0.dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
GDerivative.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  * GDerivative.hpp - Derivative class *
3  * ----------------------------------------------------------------------- *
4  * copyright (C) 2011-2014 by Juergen Knoedlseder *
5  * ----------------------------------------------------------------------- *
6  * *
7  * This program is free software: you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation, either version 3 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This program is distributed in the hope that it will be useful, *
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15  * GNU General Public License for more details. *
16  * *
17  * You should have received a copy of the GNU General Public License *
18  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
19  * *
20  ***************************************************************************/
21 /**
22  * @file GDerivative.hpp
23  * @brief GDerivative class interface definition.
24  * @author Juergen Knoedlseder
25  */
26 
27 #ifndef GDERIVATIVE_HPP
28 #define GDERIVATIVE_HPP
29 
30 /* __ Includes ___________________________________________________________ */
31 #include <string>
32 #include "GBase.hpp"
33 #include "GFunction.hpp"
34 
35 
36 /***********************************************************************//**
37  * @class GDerivative
38  *
39  * @brief Numerical derivatives class
40  *
41  * This class allows to compute numerical derivatives using various methods.
42  * The function to be derived is implemented by the abstract GFunction
43  * class.
44  ***************************************************************************/
45 class GDerivative : public GBase {
46 
47 public:
48 
49  // Constructors and destructors
50  GDerivative(void);
51  explicit GDerivative(GFunction* func);
52  GDerivative(const GDerivative& dx);
53  virtual ~GDerivative(void);
54 
55  // Operators
56  GDerivative& operator=(const GDerivative& dx);
57 
58  // Methods
59  void clear(void);
60  GDerivative* clone(void) const;
61  std::string classname(void) const;
62  void max_iter(const int& max_iter);
63  void eps(const double& eps);
64  void step_frac(const double& fraction);
65  void silent(const bool& silent);
66  const int& iter(void) const;
67  const int& max_iter(void) const;
68  const double& eps(void) const;
69  const double& step_frac(void) const;
70  const bool& silent(void) const;
71  void function(GFunction* func);
72  const GFunction* function(void) const;
73  double value(const double& x, const double& step = 0.0);
74  double ridder(const double& x, const double& h, double* err);
75  double minuit2(const double& x, double* err);
76  double difference(const double& x, const double& h);
77  double left_difference(const double& x, const double& h);
78  double right_difference(const double& x, const double& h);
79  double smooth_robust(const double& x, const double& h,
80  const int& degree = 2,
81  const int& length = 5);
82  std::string print(const GChatter& chatter = NORMAL) const;
83 
84 protected:
85  // Protected methods
86  void init_members(void);
87  void copy_members(const GDerivative& dx);
88  void free_members(void);
89  void set_tiny(void);
90 
91  // Tiny number computation
92  class tiny {
93  public:
94  tiny(void) : m_one(1.0) {}
95  ~tiny(void) {}
96  double one(void) const;
97  double operator()(double eps) const;
98  private:
99  double m_one;
100  };
101 
102  // Protected members
103  GFunction* m_func; //!< Pointer to function
104  double m_eps; //!< Derivative precision
105  double m_step_frac; //!< Value fraction to use for initial step
106  double m_tiny; //!< Tiny number for minuit2
107  int m_max_iter; //!< Maximum number of iterations
108  int m_iter; //!< Number of iterations used
109  bool m_silent; //!< Suppress warnings
110 };
111 
112 
113 /***********************************************************************//**
114  * @brief Return class name
115  *
116  * @return String containing the class name ("GDerivative").
117  ***************************************************************************/
118 inline
119 std::string GDerivative::classname(void) const
120 {
121  return ("GDerivative");
122 }
123 
124 
125 /***********************************************************************//**
126  * @brief Return number of iterations
127  *
128  * @return Number of iterations.
129  ***************************************************************************/
130 inline
131 const int& GDerivative::iter(void) const
132 {
133  return m_iter;
134 }
135 
136 
137 /***********************************************************************//**
138  * @brief Set maximum number of iterations
139  *
140  * @param[in] max_iter Maximum number of iterations.
141  ***************************************************************************/
142 inline
143 void GDerivative::max_iter(const int& max_iter)
144 {
146  return;
147 }
148 
149 
150 /***********************************************************************//**
151  * @brief Return maximum number of iterations
152  *
153  * @return Maximum number of iterations.
154  ***************************************************************************/
155 inline
156 const int& GDerivative::max_iter(void) const
157 {
158  return m_max_iter;
159 }
160 
161 
162 /***********************************************************************//**
163  * @brief Set precision
164  *
165  * @param[in] eps Precision.
166  ***************************************************************************/
167 inline
168 void GDerivative::eps(const double& eps)
169 {
170  m_eps = eps;
171  return;
172 }
173 
174 
175 /***********************************************************************//**
176  * @brief Get precision
177  *
178  * @return Precision.
179  ***************************************************************************/
180 inline
181 const double& GDerivative::eps(void) const
182 {
183  return m_eps;
184 }
185 
186 
187 /***********************************************************************//**
188  * @brief Set step fraction
189  *
190  * @param[in] fraction Step fraction.
191  ***************************************************************************/
192 inline
193 void GDerivative::step_frac(const double& fraction)
194 {
195  m_step_frac = fraction;
196  return;
197 }
198 
199 
200 /***********************************************************************//**
201  * @brief Get step fraction
202  *
203  * @return Step fraction.
204  ***************************************************************************/
205 inline
206 const double& GDerivative::step_frac(void) const
207 {
208  return m_step_frac;
209 }
210 
211 
212 /***********************************************************************//**
213  * @brief Set silence flag
214  *
215  * @param[in] silent Silence flag.
216  ***************************************************************************/
217 inline
218 void GDerivative::silent(const bool& silent)
219 {
220  m_silent = silent;
221  return;
222 }
223 
224 
225 /***********************************************************************//**
226  * @brief Get silence flag
227  *
228  * @return True is class is silent, false otherwise.
229  ***************************************************************************/
230 inline
231 const bool& GDerivative::silent(void) const
232 {
233  return m_silent;
234 }
235 
236 
237 /***********************************************************************//**
238  * @brief Set function
239  *
240  * @param[in] function Function.
241  *
242  * Sets the function for which the derivative should be determined.
243  ***************************************************************************/
244 inline
246 {
247  m_func = function;
248  return;
249 }
250 
251 
252 /***********************************************************************//**
253  * @brief Get function
254  *
255  * @return Function.
256  ***************************************************************************/
257 inline
259 {
260  return m_func;
261 }
262 
263 #endif /* GDERIVATIVE_HPP */
double right_difference(const double &x, const double &h)
Returns gradient computed from right-sided function difference.
double m_step_frac
Value fraction to use for initial step.
int m_max_iter
Maximum number of iterations.
double value(const double &x, const double &step=0.0)
Returns derivative.
const double & step_frac(void) const
Get step fraction.
double one(void) const
Return one.
const int & iter(void) const
Return number of iterations.
double operator()(double eps) const
Evaluate minimal difference between two floating points.
Definition of interface for all GammaLib classes.
double m_eps
Derivative precision.
double difference(const double &x, const double &h)
Returns gradient computed from symmetric function difference.
double ridder(const double &x, const double &h, double *err)
Returns derivative by Ridders&#39; method.
GDerivative * clone(void) const
Clone derivative.
GDerivative & operator=(const GDerivative &dx)
Assignment operator.
GFunction * m_func
Pointer to function.
const int & max_iter(void) const
Return maximum number of iterations.
void init_members(void)
Initialise class members.
double m_tiny
Tiny number for minuit2.
const double & eps(void) const
Get precision.
Single parameter function abstract base class definition.
double smooth_robust(const double &x, const double &h, const int &degree=2, const int &length=5)
Returns smooth noise-robust derivative.
Interface class for all GammaLib classes.
Definition: GBase.hpp:52
GChatter
Definition: GTypemaps.hpp:33
const bool & silent(void) const
Get silence flag.
void set_tiny(void)
Compute tiny number for Minuit2.
std::string classname(void) const
Return class name.
GDerivative(void)
Void constructor.
Definition: GDerivative.cpp:60
std::string print(const GChatter &chatter=NORMAL) const
Print derivative information.
const GFunction * function(void) const
Get function.
double left_difference(const double &x, const double &h)
Returns gradient computed from left-sided function difference.
void clear(void)
Clear derivative.
Numerical derivatives class.
Definition: GDerivative.hpp:45
Single parameter function abstract base class.
Definition: GFunction.hpp:44
virtual ~GDerivative(void)
Destructor.
bool m_silent
Suppress warnings.
void copy_members(const GDerivative &dx)
Copy class members.
int m_iter
Number of iterations used.
void free_members(void)
Delete class members.
double minuit2(const double &x, double *err)
Returns derivative using Minuit2 algorithm.