bpp-core3  3.0.0
NumTools.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_NUMERIC_NUMTOOLS_H
6 #define BPP_NUMERIC_NUMTOOLS_H
7 
8 
9 #include "Function/Functions.h"
10 
11 namespace bpp
12 {
13 // Forward declaration:
14 template<class Scalar> class RowMatrix;
15 
19 class NumTools
20 {
21 public:
31  template<class T> static T abs(T a) { return a < 0 ? -a : a; }
32 
42  template<class T> static T sign(T a) { return a < 0 ? -1 : (a == 0 ? 0 : 1); }
43 
53  template<class T> static T max(T a, T b) { return a > b ? a : b; }
54 
64  template<class T> static T min(T a, T b) { return a < b ? a : b; }
65 
73  template<class T> static T sign(T a, T b) { return abs<T>(a) * sign<T>(b); }
74 
81  template<class T> static T sqr(T a) { return a * a; }
82 
96  template<class T> static T logsum(T lnx, T lny)
97  {
98  return (lny < lnx) ?
99  lnx + std::log(1. + exp(lny - lnx)) :
100  lny + std::log(1. + exp(lnx - lny));
101  }
102 
103  /**************************************************************************/
104 
105  template<class T> static void swap(T& a, T& b)
106  {
107  T swap = a;
108  a = b;
109  b = swap;
110  }
111 
112  template<class T> static void shift(T& a, T& b, T c)
113  {
114  a = b; b = c;
115  }
116 
117  template<class T> static void shift(T& a, T& b, T& c, T d)
118  {
119  a = b; b = c; c = d;
120  }
121 
122  /**************************************************************************/
123 
124  template<class T> static T fact(T n) { return (n == 0) ? 1 : n* fact(n - 1); }
125 
126  /**************************************************************************/
127 
128  template<class T> static T logFact(T n) { return (n == 0) ? 0 : (std::log(n) + logFact(n - 1)); }
129 
130  /**************************************************************************/
131 
143  static double uniRoot(FunctionInterface& f, const std::string& param, double a, double b, double tolerance);
144 
145  /**************************************************************************/
146 
163  static std::unique_ptr<RowMatrix<double>> computeHessianMatrix(SecondOrderDerivable& function, const ParameterList& parameters);
164 
165  /**************************************************************************/
166 };
167 } // end of namespace bpp.
168 #endif // BPP_NUMERIC_NUMTOOLS_H
static T sign(T a)
Get the sign of a value.
Definition: NumTools.h:42
static T min(T a, T b)
Get the min between 2 values.
Definition: NumTools.h:64
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
static T logsum(T lnx, T lny)
Compute the logarithm of a sum from the sum of logarithms.
Definition: NumTools.h:96
This is the function abstract class.
Definition: Functions.h:52
static T logFact(T n)
Definition: NumTools.h:128
The parameter list object.
Definition: ParameterList.h:27
Some utilitary function for numerical calculus.
Definition: NumTools.h:19
static T max(T a, T b)
Get the max between 2 values.
Definition: NumTools.h:53
static void shift(T &a, T &b, T &c, T d)
Definition: NumTools.h:117
static void swap(T &a, T &b)
Definition: NumTools.h:105
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
static T sign(T a, T b)
Get the magnitude of a times the sign of b.
Definition: NumTools.h:73
static T fact(T n)
Definition: NumTools.h:124
static void shift(T &a, T &b, T c)
Definition: NumTools.h:112
static T sqr(T a)
Get the square of a number.
Definition: NumTools.h:81
This is the abstract class for second order derivable functions.
Definition: Functions.h:151
static T abs(T a)
Get the magnitude of a value.
Definition: NumTools.h:31