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