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