bpp-core3  3.0.0
NumTools.cpp
Go to the documentation of this file.
1 //
2 // File: NumTools.cpp
3 // Authors:
4 // Julien Dutheil
5 // Created: 2003-11-10 12:06:55
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. This file is part of the Bio++ project.
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 
42 #include "Matrix/Matrix.h"
43 #include "NumTools.h"
44 
45 using namespace bpp;
46 using namespace std;
47 
48 /******************************************************************************/
49 
50 double NumTools::uniRoot(Function& f, const std::string& param, double a, double b, double tolerance)
51 {
52  ParameterList pl;
53  pl.addParameter(Parameter(param, a));
54  double fa = f.f(pl);
55  pl[0].setValue(b);
56  double fb = f.f(pl);
57  if (fa * fb > 0.)
58  throw Exception("NumTools::uniRoot(). Initial interval values are not of opposite sign.");
59  double c = (a + b) / 2.;
60  double fc;
61  while (abs(fb - fa) > tolerance)
62  {
63  c = (a + b) / 2.; // Better use golden section here...
64  pl[0].setValue(c);
65  fc = f.f(pl);
66 
67  if (fc * fa < 0.)
68  {
69  b = c;
70  fb = fc;
71  }
72  else
73  {
74  a = c;
75  fa = fc;
76  }
77  }
78  return c;
79 }
80 
81 /******************************************************************************/
82 
84 {
85  size_t n = parameters.size();
86  vector<string> variables = parameters.getParameterNames();
87  RowMatrix<double>* hessian = new RowMatrix<double>(n, n);
88  for (unsigned int i = 0; i < n; i++)
89  {
90  for (unsigned int j = 0; j < n; j++)
91  {
92  if (j == i)
93  (*hessian)(i, j) = function.d2f(variables[i], parameters);
94  else
95  (*hessian)(i, j) = function.d2f(variables[i], variables[j], parameters);
96  }
97  }
98  return hessian;
99 }
100 
101 /******************************************************************************/
This is the abstract class for second order derivable functions.
Definition: Functions.h:188
Exception base class. Overload exception constructor (to control the exceptions mechanism)....
Definition: Exceptions.h:59
This is the function abstract class.
Definition: Functions.h:89
virtual double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
Definition: Functions.h:117
static double uniRoot(Function &f, const std::string &param, double a, double b, double tolerance)
Find one root of the given function.
Definition: NumTools.cpp:50
static RowMatrix< double > * computeHessianMatrix(DerivableSecondOrder &function, const ParameterList &parameters)
Compute the Hessian matrix for a function at a given point.
Definition: NumTools.cpp:83
The parameter list object.
Definition: ParameterList.h:65
size_t size() const
Definition: ParameterList.h:92
virtual std::vector< std::string > getParameterNames() const
Get all parameter names in the list.
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:135