bpp-core  2.2.0
AbstractNumericalDerivative.h
Go to the documentation of this file.
1 //
2 // File: AbstractNumericalDerivative.h
3 // Created by: Julien Dutheil
4 // Created on: Thu Aug 17 15:00 2006
5 //
6 
7 /*
8 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004)
9 
10 This software is a computer program whose purpose is to provide classes
11 for numerical calculus.
12 
13 This software is governed by the CeCILL license under French law and
14 abiding by the rules of distribution of free software. You can use,
15 modify and/ or redistribute the software under the terms of the CeCILL
16 license as circulated by CEA, CNRS and INRIA at the following URL
17 "http://www.cecill.info".
18 
19 As a counterpart to the access to the source code and rights to copy,
20 modify and redistribute granted by the license, users are provided only
21 with a limited warranty and the software's author, the holder of the
22 economic rights, and the successive licensors have only limited
23 liability.
24 
25 In this respect, the user's attention is drawn to the risks associated
26 with loading, using, modifying and/or developing or reproducing the
27 software by the user in light of its specific status of free software,
28 that may mean that it is complicated to manipulate, and that also
29 therefore means that it is reserved for developers and experienced
30 professionals having in-depth computer knowledge. Users are therefore
31 encouraged to load and test the software's suitability as regards their
32 requirements in conditions enabling the security of their systems and/or
33 data to be ensured and, more generally, to use and operate it in the
34 same conditions as regards security.
35 
36 The fact that you are presently reading this means that you have had
37 knowledge of the CeCILL license and that you accept its terms.
38 */
39 
40 #ifndef _ABSTRACTNUMERICALDERIVATIVE_H_
41 #define _ABSTRACTNUMERICALDERIVATIVE_H_
42 
43 #include "Functions.h"
44 #include "../Matrix/Matrix.h"
45 
46 //From the STL:
47 #include <map>
48 #include <vector>
49 #include <string>
50 
51 namespace bpp
52 {
53 
67  public DerivableSecondOrder,
68  public FunctionWrapper
69 {
70  protected:
73  double h_;
74  std::vector<std::string> variables_;
75  mutable std::map<std::string, size_t> index_; //Store positions in array corresponding to variable names.
76  std::vector<double> der1_;
77  std::vector<double> der2_;
80 
81  public:
83  FunctionWrapper(function), function1_(0), function2_(0),
84  h_(0.0001), variables_(), index_(), der1_(), der2_(), crossDer2_(),
85  computeD1_(true), computeD2_(true), computeCrossD2_(false) {}
86 
88  FunctionWrapper(function), function1_(function), function2_(0),
89  h_(0.0001), variables_(), index_(), der1_(), der2_(), crossDer2_(),
90  computeD1_(true), computeD2_(true), computeCrossD2_(false) {}
91 
93  FunctionWrapper(function), function1_(function), function2_(function),
94  h_(0.0001), variables_(), index_(), der1_(), der2_(), crossDer2_(),
95  computeD1_(true), computeD2_(true), computeCrossD2_(false) {}
96 
101 
103  {
105  function1_ = ad.function1_;
106  function2_ = ad.function2_;
107  h_ = ad.h_;
108  variables_ = ad.variables_;
109  index_ = ad.index_;
110  der1_ = ad.der1_;
111  der2_ = ad.der2_;
112  crossDer2_ = ad.crossDer2_;
113  computeD1_ = ad.computeD1_;
114  computeD2_ = ad.computeD2_;
116  return *this;
117  }
118 
120 
121  AbstractNumericalDerivative* clone() const = 0;
122 
123  public:
131  void setInterval(double h) { h_ = h; }
132 
136  double getInterval() const { return h_; }
137 
143  void setParametersToDerivate(const std::vector<std::string>& variables)
144  {
145  variables_ = variables;
146  index_.clear();
147  for(size_t i = 0; i < variables_.size(); i++)
148  index_[variables_[i]] = i;
149  der1_.resize(variables_.size());
150  der2_.resize(variables_.size());
151  crossDer2_.resize(variables_.size(), variables_.size());
152  }
153 
159  void enableFirstOrderDerivatives(bool yn) { computeD1_ = yn; }
160  bool enableFirstOrderDerivatives() const { return computeD1_; }
161 
162  double getFirstOrderDerivative(const std::string& variable) const
163  throw (Exception)
164  {
165  if (function1_)
166  {
167  try
168  {
169  return function1_->getFirstOrderDerivative(variable);
170  }
171  catch (Exception& e) {}
172  }
173  std::map<std::string, size_t>::iterator it = index_.find(variable);
174  if (computeD1_ && it != index_.end()) return der1_[it->second];
175  else throw Exception("First order derivative not computed for variable " + variable + ".");
176  }
185  void enableSecondOrderDerivatives(bool yn) { computeD2_ = yn; }
186  bool enableSecondOrderDerivatives() const { return computeD2_; }
187 
188  double getSecondOrderDerivative(const std::string& variable) const
189  throw (Exception)
190  {
191  if (function2_)
192  {
193  try
194  {
195  return function2_->getSecondOrderDerivative(variable);
196  }
197  catch(Exception & e) {}
198  }
199  std::map<std::string, size_t>::iterator it = index_.find(variable);
200  if(computeD2_ && it != index_.end()) return der2_[it->second];
201  else throw Exception("Second order derivative not computed for variable " + variable + ".");
202  }
203 
204  double getSecondOrderDerivative(const std::string& variable1, const std::string& variable2) const
205  throw (Exception)
206  {
207  if (function2_)
208  {
209  try
210  {
211  return function2_->getSecondOrderDerivative(variable1, variable2);
212  }
213  catch(Exception & e) {}
214  }
215  std::map<std::string, size_t>::iterator it1 = index_.find(variable1);
216  std::map<std::string, size_t>::iterator it2 = index_.find(variable2);
217  if(computeCrossD2_ && it1 != index_.end() && it2 != index_.end()) return crossDer2_(it1->second, it2->second);
218  else throw Exception("Cross second order derivative not computed for variables " + variable1 + " and " + variable2 + ".");
219  }
227  double f(const ParameterList& parameters) throw (Exception)
228  {
229  setParameters(parameters);
230  return getValue();
231  }
232  void setParameters(const ParameterList& parameters)
234  {
235  function_->setParameters(parameters);
236  updateDerivatives(parameters);
237  }
238  void setAllParametersValues(const ParameterList& parameters)
240  {
241  function_->setAllParametersValues(parameters);
242  updateDerivatives(parameters);
243  }
244 
245  void setParameterValue(const std::string& name, double value)
247  {
248  function_->setParameterValue(name, value);
250  }
251 
252  void setParametersValues(const ParameterList& parameters)
254  {
255  function_->setParametersValues(parameters);
256  updateDerivatives(parameters);
257  }
258 
259  bool matchParametersValues(const ParameterList& parameters)
260  throw (ConstraintException)
261  {
262  bool test = function_->matchParametersValues(parameters);
263  updateDerivatives(parameters);
264  return test;
265  }
270 
271  protected:
278  virtual void updateDerivatives(const ParameterList parameters) = 0;
279 
280 };
281 
282 } //end of namespace bpp.
283 
284 #endif //_ABSTRACTNUMERICALDERIVATIVE_H_
285 
AbstractNumericalDerivative * clone() const =0
Create a copy of this object and send a pointer to it.
virtual bool matchParametersValues(const ParameterList &parameters)=0
Update the parameters from parameters.
Exception thrown when a parameter is not found, for instance in a ParameterList.
double getFirstOrderDerivative(const std::string &variable) const
Get the derivative of the function at the current point.
double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
virtual ParameterList subList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
virtual void setParameters(const ParameterList &parameters)=0
Set the point where the function must be computed.
void setInterval(double h)
Set the interval value used in numerical approximation.
virtual void setParameterValue(const std::string &name, double value)=0
Set the value of parameter with name name to be equal to value.
void setParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
This class allows to perform a correspondence analysis.
AbstractNumericalDerivative(DerivableFirstOrder *function)
This is the function abstract class.
Definition: Functions.h:86
Function * function_
Definition: Functions.h:278
double getValue() const
Get the value of the function at the current point.
Definition: Functions.h:311
virtual const ParameterList & getParameters() const =0
Get all parameters available.
virtual void setParametersValues(const ParameterList &parameters)=0
Update the parameters from 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...
virtual double getSecondOrderDerivative(const std::string &variable) const =0
Get the second order derivative of the function at the current point.
Numerical derivative function wrapper, partial implementation.
virtual double getFirstOrderDerivative(const std::string &variable) const =0
Get the derivative of the function at the current point.
The parameter list object.
Definition: ParameterList.h:61
AbstractNumericalDerivative(const AbstractNumericalDerivative &ad)
bool matchParametersValues(const ParameterList &parameters)
Update the parameters from parameters.
void enableFirstOrderDerivatives(bool yn)
Tell if derivatives must be computed.
void enableSecondOrderDerivatives(bool yn)
Tell if derivatives must be computed.
void resize(size_t nRows, size_t nCols)
Resize the matrix.
Definition: Matrix.h:208
bool enableSecondOrderDerivatives() const
Tell if derivatives must be computed.
virtual void setAllParametersValues(const ParameterList &parameters)=0
Set the parameters values to be equals to those of parameters.
General class that wraps a function into another one. This class is meant to be derivated and just pr...
Definition: Functions.h:274
void setParameterValue(const std::string &name, double value)
Set the value of parameter with name name to be equal to value.
std::map< std::string, size_t > index_
AbstractNumericalDerivative(DerivableSecondOrder *function)
void setAllParametersValues(const ParameterList &parameters)
Set the parameters values to be equals to those of parameters.
void setParametersToDerivate(const std::vector< std::string > &variables)
Set the list of parameters to derivate.
virtual void updateDerivatives(const ParameterList parameters)=0
Compute derivatives.
Exception base class.
Definition: Exceptions.h:57
This is the abstract class for second order derivable functions.
Definition: Functions.h:189
This is the abstract class for first order derivable functions.
Definition: Functions.h:129
AbstractNumericalDerivative & operator=(const AbstractNumericalDerivative &ad)
Exception thrown when a value do not match a given constraint.
void setParameters(const ParameterList &parameters)
Set the point where the function must be computed.
bool enableFirstOrderDerivatives() const
Tell if derivatives must be computed.
FunctionWrapper & operator=(const FunctionWrapper &fw)
Definition: Functions.h:283
double getSecondOrderDerivative(const std::string &variable) const
Get the second order derivative of the function at the current point.