bpp-core  2.2.0
NumTools.h
Go to the documentation of this file.
1 //
2 // File: NumTools.h
3 // Created by: Julien Dutheil
4 // Created on: Mon Nov 10 12:06:55 2003
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. This file is part of the Bio++ project.
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 
40 #ifndef _NUMTOOLS_H_
41 #define _NUMTOOLS_H_
42 
43 #include "Function/Functions.h"
44 
45 namespace bpp
46 {
47 //Forward declaration:
48 template<class Scalar> class RowMatrix;
49 
53 class NumTools
54 {
55 public:
56 
66  template<class T> static T abs(T a) { return a < 0 ? -a : a; }
67 
77  template<class T> static T sign(T a) { return a < 0 ? -1 : (a == 0 ? 0 : 1); }
78 
88  template<class T> static T max(T a, T b) { return a > b ? a : b; }
89 
99  template<class T> static T min(T a, T b) { return a < b ? a : b; }
100 
108  template<class T> static T sign(T a, T b) { return abs<T>(a) * sign<T>(b); }
109 
116  template<class T> static T sqr(T a) { return a * a; }
117 
131  template<class T> static T logsum(T lnx, T lny) { return (lny < lnx) ?
132  lnx + log(1. + exp(lny - lnx)) :
133  lny + log(1. + exp(lnx - lny));
134  }
135 
136  /**************************************************************************/
137 
138  template<class T> static void swap(T & a, T & b)
139  {
140  T swap = a;
141  a = b;
142  b = swap;
143  }
144 
145  template<class T> static void shift(T & a, T & b, T c)
146  {
147  a = b; b = c;
148  }
149 
150  template<class T> static void shift(T & a, T & b, T & c, T d)
151  {
152  a = b; b = c; c = d;
153  }
154 
155  /**************************************************************************/
156 
157  template<class T> static T fact(T n) { return (n == 0) ? 1 : n * fact(n - 1); }
158 
159  /**************************************************************************/
160 
161  template<class T> static T logFact(T n) { return (n == 0) ? 0 : (log(n) + logFact(n - 1)); }
162 
163  /**************************************************************************/
164 
176  static double uniRoot(Function & f, const std::string & param, double a, double b, double tolerance) throw (Exception);
177 
178  /**************************************************************************/
179 
196  static RowMatrix<double>* computeHessianMatrix(DerivableSecondOrder& function, const ParameterList & parameters);
197 
198  /**************************************************************************/
199 
200 };
201 
202 } //end of namespace bpp.
203 
204 #endif //_NUMTOOLS_H_
205 
static T sign(T a)
Get the sign of a value.
Definition: NumTools.h:77
This class allows to perform a correspondence analysis.
This is the function abstract class.
Definition: Functions.h:86
static T min(T a, T b)
Get the min between 2 values.
Definition: NumTools.h:99
static T logsum(T lnx, T lny)
Compute the logarithm of a sum from the sum of logarithms.
Definition: NumTools.h:131
static T logFact(T n)
Definition: NumTools.h:161
The parameter list object.
Definition: ParameterList.h:61
Some utilitary function for numerical calculus.
Definition: NumTools.h:53
static T max(T a, T b)
Get the max between 2 values.
Definition: NumTools.h:88
static void shift(T &a, T &b, T &c, T d)
Definition: NumTools.h:150
static void swap(T &a, T &b)
Definition: NumTools.h:138
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:48
Exception base class.
Definition: Exceptions.h:57
This is the abstract class for second order derivable functions.
Definition: Functions.h:189
static T sign(T a, T b)
Get the magnitude of a times the sign of b.
Definition: NumTools.h:108
static RowMatrix< double > * computeHessianMatrix(DerivableSecondOrder &function, const ParameterList &parameters)
Compute the Hessian matrix for a function at a given point.
Definition: NumTools.cpp:80
static T fact(T n)
Definition: NumTools.h:157
static void shift(T &a, T &b, T c)
Definition: NumTools.h:145
static T sqr(T a)
Get the square of a number.
Definition: NumTools.h:116
static T abs(T a)
Get the magnitude of a value.
Definition: NumTools.h:66