bpp-core3  3.0.0
NumTools.h
Go to the documentation of this file.
1 //
2 // File: NumTools.h
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 #ifndef BPP_NUMERIC_NUMTOOLS_H
42 #define BPP_NUMERIC_NUMTOOLS_H
43 
44 
45 #include "Function/Functions.h"
46 
47 namespace bpp
48 {
49 // Forward declaration:
50 template<class Scalar> class RowMatrix;
51 
55 class NumTools
56 {
57 public:
67  template<class T> static T abs(T a) { return a < 0 ? -a : a; }
68 
78  template<class T> static T sign(T a) { return a < 0 ? -1 : (a == 0 ? 0 : 1); }
79 
89  template<class T> static T max(T a, T b) { return a > b ? a : b; }
90 
100  template<class T> static T min(T a, T b) { return a < b ? a : b; }
101 
109  template<class T> static T sign(T a, T b) { return abs<T>(a) * sign<T>(b); }
110 
117  template<class T> static T sqr(T a) { return a * a; }
118 
132  template<class T> static T logsum(T lnx, T lny)
133  {
134  return (lny < lnx) ?
135  lnx + std::log(1. + exp(lny - lnx)) :
136  lny + std::log(1. + exp(lnx - lny));
137  }
138 
139  /**************************************************************************/
140 
141  template<class T> static void swap(T& a, T& b)
142  {
143  T swap = a;
144  a = b;
145  b = swap;
146  }
147 
148  template<class T> static void shift(T& a, T& b, T c)
149  {
150  a = b; b = c;
151  }
152 
153  template<class T> static void shift(T& a, T& b, T& c, T d)
154  {
155  a = b; b = c; c = d;
156  }
157 
158  /**************************************************************************/
159 
160  template<class T> static T fact(T n) { return (n == 0) ? 1 : n* fact(n - 1); }
161 
162  /**************************************************************************/
163 
164  template<class T> static T logFact(T n) { return (n == 0) ? 0 : (std::log(n) + logFact(n - 1)); }
165 
166  /**************************************************************************/
167 
179  static double uniRoot(Function& f, const std::string& param, double a, double b, double tolerance);
180 
181  /**************************************************************************/
182 
199  static RowMatrix<double>* computeHessianMatrix(DerivableSecondOrder& function, const ParameterList& parameters);
200 
201  /**************************************************************************/
202 };
203 } // end of namespace bpp.
204 #endif // BPP_NUMERIC_NUMTOOLS_H
This is the abstract class for second order derivable functions.
Definition: Functions.h:188
This is the function abstract class.
Definition: Functions.h:89
Some utilitary function for numerical calculus.
Definition: NumTools.h:56
static T sign(T a, T b)
Get the magnitude of a times the sign of b.
Definition: NumTools.h:109
static T logsum(T lnx, T lny)
Compute the logarithm of a sum from the sum of logarithms.
Definition: NumTools.h:132
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 T min(T a, T b)
Get the min between 2 values.
Definition: NumTools.h:100
static void shift(T &a, T &b, T c)
Definition: NumTools.h:148
static T sqr(T a)
Get the square of a number.
Definition: NumTools.h:117
static void swap(T &a, T &b)
Definition: NumTools.h:141
static T logFact(T n)
Definition: NumTools.h:164
static T abs(T a)
Get the magnitude of a value.
Definition: NumTools.h:67
static void shift(T &a, T &b, T &c, T d)
Definition: NumTools.h:153
static T fact(T n)
Definition: NumTools.h:160
static T sign(T a)
Get the sign of a value.
Definition: NumTools.h:78
static RowMatrix< double > * computeHessianMatrix(DerivableSecondOrder &function, const ParameterList &parameters)
Compute the Hessian matrix for a function at a given point.
Definition: NumTools.cpp:83
static T max(T a, T b)
Get the max between 2 values.
Definition: NumTools.h:89
The parameter list object.
Definition: ParameterList.h:65