|
bpp-core
2.1.0
|
00001 // 00002 // File: NumTools.h 00003 // Created by: Julien Dutheil 00004 // Created on: Mon Nov 10 12:06:55 2003 00005 // 00006 00007 /* 00008 Copyright or © or Copr. Bio++ Development Team, (November 17, 2004) 00009 00010 This software is a computer program whose purpose is to provide classes 00011 for numerical calculus. This file is part of the Bio++ project. 00012 00013 This software is governed by the CeCILL license under French law and 00014 abiding by the rules of distribution of free software. You can use, 00015 modify and/ or redistribute the software under the terms of the CeCILL 00016 license as circulated by CEA, CNRS and INRIA at the following URL 00017 "http://www.cecill.info". 00018 00019 As a counterpart to the access to the source code and rights to copy, 00020 modify and redistribute granted by the license, users are provided only 00021 with a limited warranty and the software's author, the holder of the 00022 economic rights, and the successive licensors have only limited 00023 liability. 00024 00025 In this respect, the user's attention is drawn to the risks associated 00026 with loading, using, modifying and/or developing or reproducing the 00027 software by the user in light of its specific status of free software, 00028 that may mean that it is complicated to manipulate, and that also 00029 therefore means that it is reserved for developers and experienced 00030 professionals having in-depth computer knowledge. Users are therefore 00031 encouraged to load and test the software's suitability as regards their 00032 requirements in conditions enabling the security of their systems and/or 00033 data to be ensured and, more generally, to use and operate it in the 00034 same conditions as regards security. 00035 00036 The fact that you are presently reading this means that you have had 00037 knowledge of the CeCILL license and that you accept its terms. 00038 */ 00039 00040 #ifndef _NUMTOOLS_H_ 00041 #define _NUMTOOLS_H_ 00042 00043 #include "Function/Functions.h" 00044 00045 namespace bpp 00046 { 00047 //Forward declaration: 00048 template<class Scalar> class RowMatrix; 00049 00053 class NumTools 00054 { 00055 public: 00056 00066 template<class T> static T abs(T a) { return a < 0 ? -a : a; } 00067 00077 template<class T> static T sign(T a) { return a < 0 ? -1 : (a == 0 ? 0 : 1); } 00078 00088 template<class T> static T max(T a, T b) { return a > b ? a : b; } 00089 00099 template<class T> static T min(T a, T b) { return a < b ? a : b; } 00100 00108 template<class T> static T sign(T a, T b) { return abs<T>(a) * sign<T>(b); } 00109 00116 template<class T> static T sqr(T a) { return a * a; } 00117 00131 template<class T> static T logsum(T lnx, T lny) { return (lny < lnx) ? 00132 lnx + log(1. + exp(lny - lnx)) : 00133 lny + log(1. + exp(lnx - lny)); 00134 } 00135 00136 /**************************************************************************/ 00137 00138 template<class T> static void swap(T & a, T & b) 00139 { 00140 T swap = a; 00141 a = b; 00142 b = swap; 00143 } 00144 00145 template<class T> static void shift(T & a, T & b, T c) 00146 { 00147 a = b; b = c; 00148 } 00149 00150 template<class T> static void shift(T & a, T & b, T & c, T d) 00151 { 00152 a = b; b = c; c = d; 00153 } 00154 00155 /**************************************************************************/ 00156 00157 template<class T> static T fact(T n) { return (n == 0) ? 1 : n * fact(n - 1); } 00158 00159 /**************************************************************************/ 00160 00161 template<class T> static T logFact(T n) { return (n == 0) ? 0 : (log(n) + logFact(n - 1)); } 00162 00163 /**************************************************************************/ 00164 00176 static double uniRoot(Function & f, const std::string & param, double a, double b, double tolerance) throw (Exception); 00177 00178 /**************************************************************************/ 00179 00196 static RowMatrix<double>* computeHessianMatrix(DerivableSecondOrder& function, const ParameterList & parameters); 00197 00198 /**************************************************************************/ 00199 00200 }; 00201 00202 } //end of namespace bpp. 00203 00204 #endif //_NUMTOOLS_H_ 00205