bpp-core  2.1.0
NumTools.cpp
Go to the documentation of this file.
00001 //
00002 // File: NumTools.cpp
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 #include "NumTools.h"
00041 #include "Matrix/Matrix.h"
00042 
00043 using namespace bpp;
00044 using namespace std;
00045 
00046 /******************************************************************************/
00047   
00048 double NumTools::uniRoot(Function& f, const std::string& param, double a, double b, double tolerance) throw (Exception)
00049 {
00050   ParameterList pl;
00051   pl.addParameter(Parameter(param, a));
00052   double fa = f.f(pl);
00053   pl[0].setValue(b);
00054   double fb = f.f(pl);
00055   if(fa * fb > 0.) throw Exception("NumTools::uniRoot(). Initial interval values are not of opposite sign.");
00056   double c = (a + b) / 2.;
00057   double fc;
00058   while(abs(fb - fa) > tolerance)
00059   {
00060     c = (a + b) / 2.; //Better use golden section here...
00061     pl[0].setValue(c);
00062     fc = f.f(pl);
00063     
00064     if(fc * fa < 0.)
00065     {
00066       b = c;
00067       fb = fc;
00068     }
00069     else
00070     {
00071       a = c;
00072       fa = fc;
00073     }
00074   }
00075   return c;
00076 }
00077  
00078 /******************************************************************************/
00079 
00080 RowMatrix<double>* NumTools::computeHessianMatrix(DerivableSecondOrder& function, const ParameterList& parameters)
00081 {
00082   size_t n = parameters.size();
00083   vector<string> variables = parameters.getParameterNames();
00084   RowMatrix<double>* hessian = new RowMatrix<double>(n, n);
00085   for(unsigned int i = 0; i < n; i++)
00086     for(unsigned int j = 0; j < n; j++)
00087       if(j == i)
00088         (*hessian)(i,j) = function.d2f(variables[i], parameters);
00089       else
00090         (*hessian)(i,j) = function.d2f(variables[i], variables[j], parameters);
00091   return hessian;
00092 }
00093 
00094 /******************************************************************************/
00095 
 All Classes Namespaces Files Functions Variables Typedefs Friends