bpp-core3  3.0.0
FivePointsNumericalDerivative.cpp
Go to the documentation of this file.
1 //
2 // File: FivePointsNumericalDerivative.cpp
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 
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  f3_ = function_->getValue();
57  string lastVar;
58  bool functionChanged = false;
59  ParameterList p;
60  bool start = true;
61  for (unsigned int i = 0; i < variables_.size(); i++)
62  {
63  string var = variables_[i];
64  if (!parameters.hasParameter(var))
65  continue;
66  if (!start)
67  {
68  vector<string> vars(2);
69  vars[0] = var;
70  vars[1] = lastVar;
71  p = parameters.createSubList(vars);
72  }
73  else
74  {
75  p = parameters.createSubList(var);
76  start = true;
77  }
78  lastVar = var;
79  functionChanged = true;
80  double value = function_->getParameterValue(var);
81  double h = (1. + std::abs(value)) * h_;
82  // Compute four other points:
83  try
84  {
85  p[0].setValue(value - 2 * h);
86  function_->setParameters(p);
87  f1_ = function_->getValue();
88  try
89  {
90  p[0].setValue(value + 2 * h);
91  function_->setParameters(p);
92  f5_ = function_->getValue();
93  // No limit raised, use central approximation:
94  p[0].setValue(value - h);
95  function_->setParameters(p);
96  f2_ = function_->getValue();
97  p[0].setValue(value + h);
98  function_->setParameters(p);
99  f4_ = function_->getValue();
100  der1_[i] = (f1_ - 8. * f2_ + 8. * f4_ - f5_) / (12. * h);
101  der2_[i] = (-f1_ + 16. * f2_ - 30. * f3_ + 16. * f4_ - f5_) / (12. * h * h);
102  }
103  catch (ConstraintException& ce)
104  {
105  // Right limit raised, use backward approximation:
106  p[0].setValue(value - h);
107  function_->setParameters(p);
108  f2_ = function_->getValue();
109  p[0].setValue(value - 2 * h);
110  function_->setParameters(p);
111  f1_ = function_->getValue();
112  der1_[i] = (f3_ - f2_) / h;
113  der2_[i] = (f3_ - 2. * f2_ + f1_) / (h * h);
114  }
115  }
116  catch (ConstraintException& ce)
117  {
118  // Left limit raised, use forward approximation:
119  p[0].setValue(value + h);
120  function_->setParameters(p);
121  f4_ = function_->getValue();
122  p[0].setValue(value + 2 * h);
123  function_->setParameters(p);
124  f5_ = function_->getValue();
125  der1_[i] = (f4_ - f3_) / h;
126  der2_[i] = (f5_ - 2. * f4_ + f3_) / (h * h);
127  }
128  }
129  // Reset last parameter and compute analytical derivatives if any.
130  if (function1_)
131  function1_->enableFirstOrderDerivatives(computeD1_);
132  if (function2_)
133  function2_->enableSecondOrderDerivatives(computeD2_);
134  if (functionChanged)
135  function_->setParameters(parameters.createSubList(lastVar));
136  }
137  else
138  {
139  // Reset initial value and compute analytical derivatives if any.
140  if (function1_)
141  function1_->enableFirstOrderDerivatives(computeD1_);
142  if (function2_)
143  function2_->enableSecondOrderDerivatives(computeD2_);
144  function_->setParameters(parameters);
145  // Just in case derivatives are not computed:
146  f3_ = function_->getValue();
147  }
148 }
Exception thrown when a value do not match a given constraint.
void updateDerivatives(const ParameterList parameters)
Compute derivatives.
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.