MBSim

Project Page
nonlinear_algebra.h
00001 /* Copyright (C) 2004-2009 MBSim Development Team
00002  *
00003  * This library is free software; you can redistribute it and/or 
00004  * modify it under the terms of the GNU Lesser General Public 
00005  * License as published by the Free Software Foundation; either 
00006  * version 2.1 of the License, or (at your option) any later version. 
00007  *  
00008  * This library is distributed in the hope that it will be useful, 
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
00011  * Lesser General Public License for more details. 
00012  *  
00013  * You should have received a copy of the GNU Lesser General Public 
00014  * License along with this library; if not, write to the Free Software 
00015  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
00016  *
00017  * Contact: martin.o.foerg@googlemail.com
00018  */
00019 
00020 #ifndef NONLINEAR_ALGEBRA_H_
00021 #define NONLINEAR_ALGEBRA_H_
00022 
00023 #include "mbsim/utils/function.h"
00024 #include "fmatvec/fmatvec.h"
00025 
00026 namespace MBSim {
00027 
00028   enum SolverType {
00029     RegulaFalsiSolver, FixPointIterationSolver, NewtonMethodSolver
00030   };
00031 
00037   class RegulaFalsi {
00038     public:
00039       /*
00040        * \brief constructor
00041        * \param root function
00042        */
00043       RegulaFalsi(Function1<double,double> *f);
00044 
00045       /* GETTER / SETTER */
00046       int getNumberOfIterations() const { return it; };
00047       int getInfo() const { return info; };
00048       void setMaximumNumberOfIterations(int itmax_) { itmax = itmax_; }
00049       void setTolerance(double tol_) { tol = tol_; }
00050       /***************************************************/
00051 
00057       double solve(double a, double b);
00058 
00059     private:
00063       Function1<double,double> *func;
00064 
00068       int itmax, it, info;
00069 
00073       double tol;
00074   };
00075 
00081   class MultiDimFixPointIteration {
00082     public:
00083       /*
00084        * \brief constructor
00085        * \param fct pointer to used fix-point-function
00086        */
00087       MultiDimFixPointIteration(Function1<fmatvec::Vec, fmatvec::Vec> *function_);
00088 
00089       /* GETTER / SETTER */
00090       /*
00091        * \brief returns info of iteration progress
00092        * info == 0 :  a solution has been found
00093        * info == -1:  no converge
00094        * info == 1:   process (seems to) converge but hasn't finished
00095        */
00096       int getInfo() {
00097         return info;
00098       }
00099       std::vector<double> getNorms() {
00100         return norms;
00101       }
00102       double getNumberOfIterations() {
00103         return iter;
00104       }
00105       double getNumberOfMaximalIterations() {
00106         return itermax;
00107       }
00108       void setNumberOfMaximalIterations(int itermax_) {
00109         itermax = itermax_;
00110       }
00111       double getTolerance() {
00112         return tol;
00113       }
00114       void setTolerance(double tol_) {
00115         tol = tol_;
00116       }
00117       /*******************/
00118 
00119       fmatvec::Vec solve(const fmatvec::Vec &initialGuess);
00120 
00121     private:
00125       Function1<fmatvec::Vec, fmatvec::Vec> *function;
00126 
00130       double tol;
00131 
00135       int iter;
00136 
00140       double itermax;
00141 
00145       std::vector<double> norms;
00146 
00150       int info;
00151   };
00152 
00158   class NewtonMethod {
00159     public:
00160       /*
00161        * \brief constructor
00162        * \param root function
00163        * \param Jacobian matrix
00164        */
00165       NewtonMethod(Function1<double,double> *fct_, Function1<double,double> *jac_=0);
00166 
00167       /* GETTER / SETTER */
00168       int getNumberOfIterations() const { return iter; }
00169       int getInfo() const { return info; }
00170       void setMaximumNumberOfIterations(int itmax_) { itmax = itmax_; }
00171       void setMaximumDampingSteps(int kmax_) { kmax = kmax_; }
00172       void setTolerance(double tol_) { tol = tol_; }
00173       /***************************************************/
00174 
00179       double solve(const double &x);
00180 
00181     private:
00185       Function1<double,double> *fct;
00186 
00190       Function1<double,double> *jac;
00191 
00195       int itmax, iter, kmax, info;
00196 
00200       double tol;
00201   };
00202 
00208   class MultiDimNewtonMethod {
00209     public:
00210       /*
00211        * \brief constructor
00212        * \param root function
00213        * \param Jacobian matrix
00214        */
00215       MultiDimNewtonMethod(Function1<fmatvec::Vec,fmatvec::Vec> *fct_, Function1<fmatvec::SqrMat,fmatvec::Vec> *jac_=0);
00216 
00217       /* GETTER / SETTER */
00218       int getNumberOfIterations() const { return iter; }
00219       int getNumberOfMaximalIterations() const { return itmax; }
00220       int getInfo() const { return info; }
00221       std::vector<double> getNorms() { return norms; }
00222       void setMaximumNumberOfIterations(int itmax_) { itmax = itmax_; }
00223       void setMaximumDampingSteps(int kmax_) { kmax = kmax_; }
00224       void setTolerance(double tol_) { tol = tol_; }
00225       /***************************************************/
00226 
00231       fmatvec::Vec solve(const fmatvec::Vec &x);
00232 
00233     private:
00237       Function1<fmatvec::Vec,fmatvec::Vec> *fct;
00238 
00242       Function1<fmatvec::SqrMat,fmatvec::Vec> *jac;
00243 
00247       int itmax, iter, kmax, info;
00248 
00252       std::vector<double> norms;
00253 
00257       double tol;
00258   };
00259   
00260 }
00261 
00262 #endif /* NONLINEAR_ALGEBRA_H_ */
00263