bpp-core3  3.0.0
AbstractNumericalDerivative.h
Go to the documentation of this file.
1 //
2 // File: AbstractNumericalDerivative.h
3 // Authors:
4 // Julien Dutheil
5 // Created: 2006-08-17 15:00:00
6 //
7 
8 /*
9  Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
10 
11  This software is a computer program whose purpose is to provide classes
12  for numerical calculus.
13 
14  This software is governed by the CeCILL license under French law and
15  abiding by the rules of distribution of free software. You can use,
16  modify and/ or redistribute the software under the terms of the CeCILL
17  license as circulated by CEA, CNRS and INRIA at the following URL
18  "http://www.cecill.info".
19 
20  As a counterpart to the access to the source code and rights to copy,
21  modify and redistribute granted by the license, users are provided only
22  with a limited warranty and the software's author, the holder of the
23  economic rights, and the successive licensors have only limited
24  liability.
25 
26  In this respect, the user's attention is drawn to the risks associated
27  with loading, using, modifying and/or developing or reproducing the
28  software by the user in light of its specific status of free software,
29  that may mean that it is complicated to manipulate, and that also
30  therefore means that it is reserved for developers and experienced
31  professionals having in-depth computer knowledge. Users are therefore
32  encouraged to load and test the software's suitability as regards their
33  requirements in conditions enabling the security of their systems and/or
34  data to be ensured and, more generally, to use and operate it in the
35  same conditions as regards security.
36 
37  The fact that you are presently reading this means that you have had
38  knowledge of the CeCILL license and that you accept its terms.
39 */
40 
41 #ifndef BPP_NUMERIC_FUNCTION_ABSTRACTNUMERICALDERIVATIVE_H
42 #define BPP_NUMERIC_FUNCTION_ABSTRACTNUMERICALDERIVATIVE_H
43 
44 
45 #include "../Matrix/Matrix.h"
46 #include "Functions.h"
47 
48 // From the STL:
49 #include <map>
50 #include <vector>
51 #include <string>
52 
53 namespace bpp
54 {
68  public DerivableSecondOrder,
69  public FunctionWrapper
70 {
71 protected:
74  double h_;
75  std::vector<std::string> variables_;
76  mutable std::map<std::string, size_t> index_; // Store positions in array corresponding to variable names.
77  std::vector<double> der1_;
78  std::vector<double> der2_;
81 
82 public:
84  FunctionWrapper(function), function1_(0), function2_(0),
85  h_(0.0001), variables_(), index_(), der1_(), der2_(), crossDer2_(),
86  computeD1_(true), computeD2_(true), computeCrossD2_(false) {}
87 
89  FunctionWrapper(function), function1_(function), function2_(0),
90  h_(0.0001), variables_(), index_(), der1_(), der2_(), crossDer2_(),
91  computeD1_(true), computeD2_(true), computeCrossD2_(false) {}
92 
94  FunctionWrapper(function), function1_(function), function2_(function),
95  h_(0.0001), variables_(), index_(), der1_(), der2_(), crossDer2_(),
96  computeD1_(true), computeD2_(true), computeCrossD2_(false) {}
97 
102 
104  {
106  function1_ = ad.function1_;
107  function2_ = ad.function2_;
108  h_ = ad.h_;
109  variables_ = ad.variables_;
110  index_ = ad.index_;
111  der1_ = ad.der1_;
112  der2_ = ad.der2_;
113  crossDer2_ = ad.crossDer2_;
114  computeD1_ = ad.computeD1_;
115  computeD2_ = ad.computeD2_;
117  return *this;
118  }
119 
121 
123 
124 public:
132  void setInterval(double h) { h_ = h; }
133 
137  double getInterval() const { return h_; }
138 
144  void setParametersToDerivate(const std::vector<std::string>& variables)
145  {
146  variables_ = variables;
147  index_.clear();
148  for (size_t i = 0; i < variables_.size(); i++)
149  {
150  index_[variables_[i]] = i;
151  }
152  der1_.resize(variables_.size());
153  der2_.resize(variables_.size());
154  crossDer2_.resize(variables_.size(), variables_.size());
155  }
156 
162  void enableFirstOrderDerivatives(bool yn) { computeD1_ = yn; }
163  bool enableFirstOrderDerivatives() const { return computeD1_; }
164 
165  double getFirstOrderDerivative(const std::string& variable) const
166  {
167  std::map<std::string, size_t>::iterator it = index_.find(variable);
168  if (computeD1_ && it != index_.end())
169  return der1_[it->second];
170 
171  if (function1_)
172  return function1_->getFirstOrderDerivative(variable);
173 
174  throw Exception("First order derivative not computed for variable " + variable + ".");
175  }
183  void enableSecondOrderDerivatives(bool yn) { computeD2_ = yn; }
184  bool enableSecondOrderDerivatives() const { return computeD2_; }
185 
186  double getSecondOrderDerivative(const std::string& variable) const
187  {
188  std::map<std::string, size_t>::iterator it = index_.find(variable);
189  if (computeD2_ && it != index_.end())
190  return der2_[it->second];
191 
192  if (function2_)
193  return function2_->getSecondOrderDerivative(variable);
194 
195  throw Exception("Second order derivative not computed for variable " + variable + ".");
196  }
197 
198  double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const
199  {
200  std::map<std::string, size_t>::iterator it1 = index_.find(variable1);
201  std::map<std::string, size_t>::iterator it2 = index_.find(variable2);
202  if (computeCrossD2_ && it1 != index_.end() && it2 != index_.end()) return crossDer2_(it1->second, it2->second);
203 
204  if (function2_)
205  return function2_->getSecondOrderDerivative(variable1, variable2);
206 
207  throw Exception("Cross second order derivative not computed for variables " + variable1 + " and " + variable2 + ".");
208  }
209 
217  double f(const ParameterList& parameters)
218  {
219  setParameters(parameters);
220  return getValue();
221  }
222  void setParameters(const ParameterList& parameters)
223  {
224  function_->setParameters(parameters);
225  updateDerivatives(parameters);
226  }
227  void setAllParametersValues(const ParameterList& parameters)
228  {
229  function_->setAllParametersValues(parameters);
230  updateDerivatives(parameters);
231  }
232 
233  void setParameterValue(const std::string& name, double value)
234  {
235  function_->setParameterValue(name, value);
237  }
238 
239  void setParametersValues(const ParameterList& parameters)
240  {
241  function_->setParametersValues(parameters);
242  updateDerivatives(parameters);
243  }
244 
245  bool matchParametersValues(const ParameterList& parameters)
246  {
247  bool test = function_->matchParametersValues(parameters);
248  updateDerivatives(parameters);
249  return test;
250  }
255 
256 protected:
263  virtual void updateDerivatives(const ParameterList parameters) = 0;
264 };
265 } // end of namespace bpp.
266 #endif // BPP_NUMERIC_FUNCTION_ABSTRACTNUMERICALDERIVATIVE_H
Numerical derivative function wrapper, partial implementation.
void setParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
AbstractNumericalDerivative(DerivableFirstOrder *function)
AbstractNumericalDerivative(const AbstractNumericalDerivative &ad)
AbstractNumericalDerivative & operator=(const AbstractNumericalDerivative &ad)
void enableSecondOrderDerivatives(bool yn)
Tell if derivatives must be computed.
void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
void setParametersToDerivate(const std::vector< std::string > &variables)
Set the list of parameters to derivate numerically.
bool enableSecondOrderDerivatives() const
Tell if derivatives must be computed.
void setParameters(const ParameterList &parameters)
Set the point where the function must be computed.
void setInterval(double h)
Set the interval value used in numerical approximation.
AbstractNumericalDerivative(DerivableSecondOrder *function)
void setAllParametersValues(const ParameterList &parameters)
Set the parameters values to be equals to those of parameters.
AbstractNumericalDerivative * clone() const =0
Create a copy of this object and send a pointer to it.
void enableFirstOrderDerivatives(bool yn)
Tell if derivatives must be computed.
double getFirstOrderDerivative(const std::string &variable) const
Get the derivative of the function at the current point.
std::map< std::string, size_t > index_
double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
double getSecondOrderDerivative(const std::string &variable1, const std::string &variable2) const
Get the value of the cross derivative of the function according to a given set of parameters.
bool enableFirstOrderDerivatives() const
Tell if derivatives must be computed.
double getSecondOrderDerivative(const std::string &variable) const
Get the second order derivative of the function at the current point.
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
virtual void updateDerivatives(const ParameterList parameters)=0
Compute derivatives.
This is the abstract class for first order derivable functions.
Definition: Functions.h:133
virtual double getFirstOrderDerivative(const std::string &variable) const =0
Get the derivative of the function at the current point.
This is the abstract class for second order derivable functions.
Definition: Functions.h:188
virtual double getSecondOrderDerivative(const std::string &variable) const =0
Get the second order derivative of the function at the current point.
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
General class that wraps a function into another one. This class is meant to be derivated and just pr...
Definition: Functions.h:268
FunctionWrapper & operator=(const FunctionWrapper &fw)
Definition: Functions.h:275
Function * function_
Definition: Functions.h:270
double getValue() const
Get the value of the function at the current point.
Definition: Functions.h:302
This is the function abstract class.
Definition: Functions.h:89
virtual void setParameters(const ParameterList &parameters)=0
Set the point where the function must be computed.
The parameter list object.
Definition: ParameterList.h:65
virtual ParameterList createSubList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
virtual bool matchParametersValues(const ParameterList &parameters)=0
Update the parameters from parameters.
virtual void setAllParametersValues(const ParameterList &parameters)=0
Set the parameters values to be equals to those of parameters.
virtual const ParameterList & getParameters() const =0
Get all parameters available.
virtual void setParameterValue(const std::string &name, double value)=0
Set the value of parameter with name name to be equal to value.
virtual void setParametersValues(const ParameterList &parameters)=0
Update the parameters from parameters.
void resize(size_t nRows, size_t nCols)
Resize the matrix.
Definition: Matrix.h:213