bpp-core3  3.0.0
NumTools.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "Matrix/Matrix.h"
6 #include "NumTools.h"
7 
8 using namespace bpp;
9 using namespace std;
10 
11 /******************************************************************************/
12 
13 double NumTools::uniRoot(FunctionInterface& f, const std::string& param, double a, double b, double tolerance)
14 {
15  ParameterList pl;
16  pl.addParameter(Parameter(param, a));
17  double fa = f.f(pl);
18  pl[0].setValue(b);
19  double fb = f.f(pl);
20  if (fa * fb > 0.)
21  throw Exception("NumTools::uniRoot(). Initial interval values are not of opposite sign.");
22  double c = (a + b) / 2.;
23  double fc;
24  while (abs(fb - fa) > tolerance)
25  {
26  c = (a + b) / 2.; // Better use golden section here...
27  pl[0].setValue(c);
28  fc = f.f(pl);
29 
30  if (fc * fa < 0.)
31  {
32  b = c;
33  fb = fc;
34  }
35  else
36  {
37  a = c;
38  fa = fc;
39  }
40  }
41  return c;
42 }
43 
44 /******************************************************************************/
45 
46 unique_ptr<RowMatrix<double>> NumTools::computeHessianMatrix(SecondOrderDerivable& function, const ParameterList& parameters)
47 {
48  size_t n = parameters.size();
49  vector<string> variables = parameters.getParameterNames();
50  auto hessian = make_unique<RowMatrix<double>>(n, n);
51  for (unsigned int i = 0; i < n; i++)
52  {
53  for (unsigned int j = 0; j < n; j++)
54  {
55  if (j == i)
56  (*hessian)(i, j) = function.d2f(variables[i], parameters);
57  else
58  (*hessian)(i, j) = function.d2f(variables[i], variables[j], parameters);
59  }
60  }
61  return hessian;
62 }
63 
64 /******************************************************************************/
size_t size() const
Definition: ParameterList.h:56
STL namespace.
This class is designed to facilitate the manipulation of parameters.
Definition: Parameter.h:97
static std::unique_ptr< RowMatrix< double > > computeHessianMatrix(SecondOrderDerivable &function, const ParameterList &parameters)
Compute the Hessian matrix for a function at a given point.
Definition: NumTools.cpp:46
This is the function abstract class.
Definition: Functions.h:52
The parameter list object.
Definition: ParameterList.h:27
virtual std::vector< std::string > getParameterNames() const
Get all parameter names in the list.
static double uniRoot(FunctionInterface &f, const std::string &param, double a, double b, double tolerance)
Find one root of the given function.
Definition: NumTools.cpp:13
virtual void addParameter(const Parameter &param)
Add a new parameter at the end of the list.
Exception base class. Overload exception constructor (to control the exceptions mechanism). Destructor is already virtual (from std::exception)
Definition: Exceptions.h:20
virtual double f(const ParameterList &parameters)
Get the value of the function according to a given set of parameters.
Definition: Functions.h:82
This is the abstract class for second order derivable functions.
Definition: Functions.h:151