bpp-core3  3.0.0
TwoPointsNumericalDerivative.cpp
Go to the documentation of this file.
1 //
2 // File: TwoPointsNumericalDerivative.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2007-05-28 10:33: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 
43 
44 using namespace bpp;
45 using namespace std;
46 
48 {
49  if (computeD1_ && variables_.size() > 0)
50  {
51  if (function1_)
52  function1_->enableFirstOrderDerivatives(false);
53  if (function2_)
54  function2_->enableSecondOrderDerivatives(false);
55  function_->setParameters(parameters);
56  f1_ = function_->getValue();
57  string lastVar;
58  bool functionChanged = false;
59  bool start = true;
60  for (unsigned int i = 0; i < variables_.size(); i++)
61  {
62  string var = variables_[i];
63  if (!parameters.hasParameter(var))
64  continue;
65  ParameterList p;
66  if (!start)
67  {
68  vector<string> vars(2);
69  vars[0] = var;
70  vars[1] = lastVar;
71  lastVar = var;
72  functionChanged = true;
73  p = parameters.createSubList(vars);
74  }
75  else
76  {
77  p = parameters.createSubList(var);
78  lastVar = var;
79  functionChanged = true;
80  start = false;
81  }
82  double value = function_->getParameterValue(var);
83  double h = (1 + std::abs(value)) * h_;
84  // Compute one other point:
85  try
86  {
87  p[0].setValue(value + h);
88  function_->setParameters(p);
89  f2_ = function_->getValue();
90  }
91  catch (ConstraintException& ce1)
92  {
93  // Right limit raised, use backward approximation:
94  try
95  {
96  p[0].setValue(value - h);
97  function_->setParameters(p);
98  f2_ = function_->getValue();
99  der1_[i] = (f1_ - f2_) / h;
100  }
101  catch (ConstraintException& ce2)
102  {
103  // PB: can't compute derivative, because of a two narrow interval (lower than h)
104  throw ce2;
105  }
106  }
107  // No limit raised, use forward approximation:
108  der1_[i] = (f2_ - f1_) / h;
109  }
110  // Reset last parameter and compute analytical derivatives if any:
111  if (function1_)
112  function1_->enableFirstOrderDerivatives(computeD1_);
113  if (functionChanged)
114  function_->setParameters(parameters.createSubList(lastVar));
115  }
116  else
117  {
118  // Reset initial value and compute analytical derivatives if any.
119  if (function1_)
120  function1_->enableFirstOrderDerivatives(computeD1_);
121  if (function2_)
122  function2_->enableSecondOrderDerivatives(computeD2_);
123  // Just in case derivatives are not computed:
124  function_->setParameters(parameters);
125  f1_ = function_->getValue();
126  }
127 }
Exception thrown when a value do not match a given constraint.
The parameter list object.
Definition: ParameterList.h:65
virtual bool hasParameter(const std::string &name) const
virtual void setParameters(const ParameterList &params)
Update the parameters from params.
virtual double getParameterValue(const std::string &name) const
Get the value of the parameter with name name.
virtual ParameterList createSubList(const std::vector< std::string > &names) const
Get given parameters as a sublist.
void updateDerivatives(const ParameterList parameters)
Compute derivatives.