MBSim

Project Page
basic_functions.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: markus.ms.schneider@gmail.com
00018  */
00019 
00020 #ifndef _FUNCTION_LIBRARY_H_
00021 #define _FUNCTION_LIBRARY_H_
00022 
00023 #include "fmatvec/function.h"
00024 #include "mbsim/objectfactory.h"
00025 #include "mbsim/element.h"
00026 #include "mbsim/utils/eps.h"
00027 #include "mbsim/utils/utils.h"
00028 
00029 namespace MBSim {
00030 
00031   template<typename Sig> class ConstantFunction; 
00032 
00033   template<typename Ret, typename Arg>
00034     class ConstantFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00035       protected:
00036         double a0;
00037       public:
00038         ConstantFunction(double a0_=0) : a0(a0_) {}
00039         typename fmatvec::Size<double>::type getArgSize() const { return 1; }
00040         Ret operator()(const Arg &x) { return FromDouble<Ret>::cast(a0); }
00041         typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &x) { return FromDouble<Ret>::cast(0); }
00042         typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) { return FromDouble<Ret>::cast(0); }
00043         void initializeUsingXML(xercesc::DOMElement *element) {
00044           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"a0");
00045           a0=Element::getDouble(e);
00046         }
00047         xercesc::DOMElement* writeXMLFile(xercesc::DOMNode *parent) { return 0; } 
00048     };
00049 
00050   template<typename Sig> class LinearFunction; 
00051 
00052   template<typename Ret, typename Arg>
00053     class LinearFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00054       private:
00055         double a0, a1;
00056       public:
00057         LinearFunction(double a1_=0) : a0(0), a1(a1_) { }
00058         LinearFunction(double a0_, double a1_) : a0(a0_), a1(a1_) { }
00059         typename fmatvec::Size<double>::type getArgSize() const { return 1; }
00060         Ret operator()(const Arg &x) { return FromDouble<Ret>::cast(a1*ToDouble<Arg>::cast(x)+a0); }
00061         typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &x) { return FromDouble<Ret>::cast(a1); }
00062         typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) { return FromDouble<Ret>::cast(0); }
00063         void initializeUsingXML(xercesc::DOMElement *element) {
00064           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"a0");
00065           if(e) a0=Element::getDouble(e);
00066           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"a1");
00067           a1=Element::getDouble(e);
00068         }
00069         xercesc::DOMElement* writeXMLFile(xercesc::DOMNode *parent) { return 0; } 
00070         void seta0(double a0_) { a0 = a0_; }
00071         void seta1(double a1_) { a1 = a1_; }
00072     };
00073 
00074   template<typename Sig> class QuadraticFunction; 
00075 
00076   template<typename Ret, typename Arg>
00077     class QuadraticFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00078       private:
00079         double a0, a1, a2;
00080       public:
00081         QuadraticFunction(double a2_=0) : a0(0), a1(0), a2(a2_) { }
00082         QuadraticFunction(double a1_, double a2_) : a0(0), a1(a1_), a2(a2_) { }
00083         QuadraticFunction(double a0_, double a1_, double a2_) : a0(a0_), a1(a1_), a2(a2_) { }
00084         Ret operator()(const Arg &x_) {  
00085           double x = ToDouble<Arg>::cast(x_);
00086           return FromDouble<Ret>::cast(a0+(a1+a2*x)*x);
00087         }
00088         typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &x_) {  
00089           double x = ToDouble<Arg>::cast(x_);
00090           return FromDouble<Ret>::cast(a1+2.*a2*x);
00091         }
00092         typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {  
00093           return FromDouble<Ret>::cast(2.*a2);
00094         }
00095         void initializeUsingXML(xercesc::DOMElement *element) {
00096           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"a0");
00097           if(e) a0=Element::getDouble(e);
00098           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"a1");
00099           if(e) a1=Element::getDouble(e);
00100           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"a2");
00101           a2=Element::getDouble(e);
00102         }
00103     };
00104 
00105   template<typename Sig> class PolynomFunction; 
00106 
00107   template<typename Ret, typename Arg>
00108   class PolynomFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00109     public:
00110       PolynomFunction() { }
00111       PolynomFunction(const std::vector<double> &a_) : a(a_) { init(); }
00112       void init() {
00113         for(unsigned int i=1; i<a.size(); i++)
00114           ad.push_back(double(i)*a[i]);
00115         for(unsigned int i=1; i<ad.size(); i++)
00116           add.push_back(double(i)*ad[i]);
00117       }
00118 
00119       Ret operator()(const Arg &x_) {
00120         double x = ToDouble<Arg>::cast(x_);
00121         double value=a[a.size()-1];
00122         for (int i=int(a.size())-2; i>-1; i--)
00123           value=value*x+a[i];
00124         return FromDouble<Ret>::cast(value);
00125       }
00126       typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &x_) {  
00127         double x = ToDouble<Arg>::cast(x_);
00128         double value=ad[ad.size()-1];
00129         for (int i=int(ad.size())-2; i>-1; i--)
00130           value=value*x+ad[i];
00131         return FromDouble<Ret>::cast(value);
00132       }
00133       typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {  
00134         double value=add[add.size()-1];
00135         for (int i=int(add.size())-2; i>-1; i--)
00136           value=value*x+add[i];
00137         return FromDouble<Ret>::cast(value);
00138       }
00139 
00140       void addCoefficient(double c) {
00141         a.push_back(c);
00142       }
00143 
00144       void initializeUsingXML(xercesc::DOMElement *element) {
00145         xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"coefficients")->getFirstElementChild();
00146         while (e) {
00147           addCoefficient(Element::getDouble(e));
00148           e=e->getNextElementSibling();
00149         }
00150         init();
00151       }
00152 
00153     private:
00154       std::vector<double> a, ad, add;
00155   };
00156 
00157   template<typename Sig> class SinusoidalFunction; 
00158 
00159   template<typename Ret, typename Arg>
00160   class SinusoidalFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00161     protected:
00162       double A, f, phi0, y0;
00163     public:
00164       SinusoidalFunction(double A_=0, double f_=0, double phi0_=0, double y0_=0) : A(A_), f(f_), phi0(phi0_), y0(y0_) { }
00165       Ret operator()(const Arg &x) {  
00166         return FromDouble<Ret>::cast(y0+A*sin(2.*M_PI*f*ToDouble<Arg>::cast(x)+phi0));
00167       }
00168       typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &x) {  
00169         double om = 2.*M_PI*f;
00170         return FromDouble<Ret>::cast(A*om*cos(om*ToDouble<Arg>::cast(x)+phi0));
00171       }
00172       typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {  
00173         double om = 2.*M_PI*f;
00174         return FromDouble<Ret>::cast(-A*om*om*sin(om*x+phi0));
00175       }
00176       void initializeUsingXML(xercesc::DOMElement *element) {
00177         xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"amplitude");
00178         A=Element::getDouble(e);
00179         e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"frequency");
00180         f=Element::getDouble(e);
00181         e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"phase");
00182         if(e) phi0=Element::getDouble(e);
00183         e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"offset");
00184         if(e) y0=Element::getDouble(e);
00185       }
00186       xercesc::DOMElement* writeXMLFile(xercesc::DOMNode *parent) {
00187         xercesc::DOMElement *ele0 = fmatvec::Function<Ret(Arg)>::writeXMLFile(parent);
00188         addElementText(ele0,MBSIM%"amplitude",A);
00189         addElementText(ele0,MBSIM%"frequency",f);
00190         addElementText(ele0,MBSIM%"phase",phi0);
00191         addElementText(ele0,MBSIM%"offset",y0);
00192         return ele0;
00193       }
00194   };
00195 
00196   template<typename Sig> class StepFunction; 
00197 
00198   template<typename Ret, typename Arg>
00199   class StepFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00200     private:
00201       double stepTime, stepSize;
00202     public:
00203       StepFunction() {}
00204       StepFunction(double stepTime_, double stepSize_) : stepTime(stepTime_), stepSize(stepSize_) { }
00205       Ret operator()(const Arg &x) {
00206         return FromDouble<Ret>::cast((ToDouble<Arg>::cast(x)>=stepTime)?stepSize:0);
00207       }
00208       void initializeUsingXML(xercesc::DOMElement *element) {
00209         xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"time");
00210         stepTime=Element::getDouble(e);
00211         e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"size");
00212         stepSize=Element::getDouble(e);
00213       }
00214   };
00215 
00216   template<typename Sig> class AbsoluteValueFunction; 
00217 
00218   template<typename Ret, typename Arg>
00219   class AbsoluteValueFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00220     public:
00221       Ret operator()(const Arg &x_) {
00222         double x = ToDouble<Arg>::cast(x_);
00223         return FromDouble<Ret>::cast(fabs(x));
00224       }
00225   };
00226 
00227   template<typename Sig> class ModuloFunction;
00228 
00229   template<typename Ret, typename Arg>
00230     class ModuloFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00231       private:
00232         double denom;
00233       public:
00234         void setDenominator(double denom_) { denom = denom_; }
00235         Ret operator()(const Arg &x) {
00236           return FromDouble<Ret>::cast(fmod(ToDouble<Arg>::cast(x),denom));
00237         }
00238         void initializeUsingXML(xercesc::DOMElement *element) {
00239           xercesc::DOMElement *e;
00240           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"denominator");
00241           denom=Element::getDouble(e);
00242         }
00243     };
00244 
00245   template<typename Sig> class SignumFunction; 
00246 
00247   template<typename Ret, typename Arg>
00248   class SignumFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00249     public:
00250       Ret operator()(const Arg &x_) {
00251         double x = ToDouble<Arg>::cast(x_);
00252         return FromDouble<Ret>::cast(sign(x));
00253       }
00254   };
00255 
00256   template<typename Sig> class PositiveValueFunction; 
00257 
00258   template<typename Ret, typename Arg>
00259   class PositiveValueFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00260     public:
00261       Ret operator()(const Arg &x_) {
00262         double x = ToDouble<Arg>::cast(x_);
00263         return FromDouble<Ret>::cast(x>=0?x:0);
00264       }
00265   };
00266 
00267   template<typename Sig> class AdditionFunction; 
00268 
00269   template<typename Ret, typename Arg>
00270     class AdditionFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00271       public:
00272         AdditionFunction() : f1(0), f2(0) { }
00273         ~AdditionFunction() { delete f1; delete f2; }
00274         void setFirstSummand(fmatvec::Function<Ret(Arg)> *function) { f1 = function; }
00275         void setSecondSummand(fmatvec::Function<Ret(Arg)> *function) { f2 = function; }
00276         Ret operator()(const Arg &x) {
00277           return (*(f1))(x)+(*(f2))(x);
00278         }
00279         typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &x) {  
00280           return f1->parDer(x)+f2->parDer(x);
00281         }
00282         typename fmatvec::Der<typename fmatvec::Der<Ret, Arg>::type, Arg>::type parDerParDer(const Arg &x) {  
00283           return f1->parDerParDer(x)+f2->parDerParDer(x);
00284         }
00285         void initializeUsingXML(xercesc::DOMElement *element) {
00286           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"firstSummand");
00287           f1=ObjectFactory::createAndInit<fmatvec::Function<Ret(Arg)> >(e->getFirstElementChild());
00288           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"secondSummand");
00289           f2=ObjectFactory::createAndInit<fmatvec::Function<Ret(Arg)> >(e->getFirstElementChild());
00290         }
00291       private:
00292         fmatvec::Function<Ret(Arg)> *f1, *f2;
00293     };
00294 
00295   template<typename Sig> class MultiplicationFunction; 
00296 
00297   template<typename Ret, typename Arg>
00298     class MultiplicationFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00299       public:
00300         MultiplicationFunction() : f1(0), f2(0) { }
00301         ~MultiplicationFunction() { delete f1; delete f2; }
00302         void setFirstFactor(fmatvec::Function<Ret(Arg)> *function) { f1 = function; }
00303         void setSecondFactor(fmatvec::Function<Ret(Arg)> *function) { f2 = function; }
00304         Ret operator()(const Arg &x) {
00305           double a1 = ToDouble<Ret>::cast((*f1)(x));
00306           double a2 = ToDouble<Ret>::cast((*f2)(x));
00307           return FromDouble<Ret>::cast(a1*a2);
00308         }
00309         typename fmatvec::Der<Ret, double>::type parDer(const double &x) {  
00310           double a1 = ToDouble<Ret>::cast((*f1)(x));
00311           double a2 = ToDouble<Ret>::cast((*f2)(x));
00312           double a1d = ToDouble<typename fmatvec::Der<Ret, double>::type>::cast(f1->parDer(x));
00313           double a2d = ToDouble<typename fmatvec::Der<Ret, double>::type>::cast(f2->parDer(x));
00314           return FromDouble<typename fmatvec::Der<Ret, double>::type>::cast(a1d*a2+a1*a2d);
00315         }
00316         typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {  
00317           double a1 = ToDouble<Ret>::cast((*f1)(x));
00318           double a2 = ToDouble<Ret>::cast((*f2)(x));
00319           double a1d = ToDouble<typename fmatvec::Der<Ret, double>::type>::cast(f1->parDer(x));
00320           double a2d = ToDouble<typename fmatvec::Der<Ret, double>::type>::cast(f2->parDer(x));
00321           double a1dd = ToDouble<typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type>::cast(f1->parDerParDer(x));
00322           double a2dd = ToDouble<typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type>::cast(f2->parDerParDer(x));
00323           return FromDouble<typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type>::cast(a1dd*a2 + 2*a1d*a2d + a1*a2dd);
00324         }
00325         void initializeUsingXML(xercesc::DOMElement *element) {
00326           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"firstFactor");
00327           f1=ObjectFactory::createAndInit<fmatvec::Function<Ret(Arg)> >(e->getFirstElementChild());
00328           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"secondFactor");
00329           f2=ObjectFactory::createAndInit<fmatvec::Function<Ret(Arg)> >(e->getFirstElementChild());
00330         }
00331       private:
00332         fmatvec::Function<Ret(Arg)> *f1, *f2;
00333     };
00334 
00335   template<typename Sig> class VectorValuedFunction; 
00336 
00337   template<typename Ret, typename Arg>
00338     class VectorValuedFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00339       public:
00340         VectorValuedFunction() { }
00341         VectorValuedFunction(const std::vector<fmatvec::Function<double(Arg)> *> &component_) : component(component_) { }
00342         ~VectorValuedFunction() { 
00343           for (unsigned int i=1; i<component.size(); i++)
00344             delete component[i]; 
00345         }
00346         void addComponent(fmatvec::Function<double(Arg)> *function) { component.push_back(function); }
00347         Ret operator()(const Arg &x) {
00348           Ret y(component.size(),fmatvec::NONINIT);
00349           for (unsigned int i=0; i<component.size(); i++)
00350             y(i)=(*component[i])(x);
00351           return y;
00352         }
00353         typename fmatvec::Der<Ret, double>::type parDer(const double &x) {  
00354           typename fmatvec::Der<Ret, double>::type y(component.size(),fmatvec::NONINIT);
00355           for (unsigned int i=0; i<component.size(); i++)
00356             y(i)=component[i]->parDer(x);
00357           return y;
00358         }
00359         typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {  
00360           typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type y(component.size(),fmatvec::NONINIT);
00361           for (unsigned int i=0; i<component.size(); i++)
00362             y(i)=component[i]->parDerParDer(x);
00363           return y;
00364         }
00365 
00366         void initializeUsingXML(xercesc::DOMElement *element) {
00367           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"components")->getFirstElementChild();
00368           while (e) {
00369             addComponent(ObjectFactory::createAndInit<fmatvec::Function<double(Arg)> >(e));
00370             e=e->getNextElementSibling();
00371           }
00372         }
00373       private:
00374         std::vector<fmatvec::Function<double(Arg)> *> component;
00375     };
00376 
00377   template<typename Sig> class NestedFunction; 
00378 
00379   template<typename Ret, typename Argo, typename Argi> 
00380     class NestedFunction<Ret(Argo(Argi))> : public fmatvec::Function<Ret(Argi)> {
00381       public:
00382        NestedFunction(fmatvec::Function<Ret(Argo)> *fo_=0, fmatvec::Function<Argo(Argi)> *fi_=0) : fo(fo_), fi(fi_) { }
00383         ~NestedFunction() { delete fo; delete fi; }
00384         typename fmatvec::Size<Argi>::type getArgSize() const { return fi->getArgSize();}
00385         Ret operator()(const Argi &arg) {return (*fo)((*fi)(arg));}
00386         typename fmatvec::Der<Ret, Argi>::type parDer(const Argi &arg) { return fo->parDer((*fi)(arg))*fi->parDer(arg); }
00387         typename fmatvec::Der<Ret, Argi>::type parDerDirDer(const Argi &argDir, const Argi &arg) { return fo->parDerDirDer(fi->parDer(arg)*argDir,(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerDirDer(argDir,arg); }
00388         typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &arg) { return fo->parDerDirDer(fi->parDer(arg),(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerParDer(arg); }
00389         void setOuterFunction(fmatvec::Function<Ret(Argo)> *fo_) { fo = fo_; }
00390         void setInnerFunction(fmatvec::Function<Argo(Argi)> *fi_) { fi = fi_; }
00391         void initializeUsingXML(xercesc::DOMElement *element) {
00392           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"outerFunction");
00393           fo=ObjectFactory::createAndInit<fmatvec::Function<Ret(Argo)> >(e->getFirstElementChild());
00394           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"innerFunction");
00395           fi=ObjectFactory::createAndInit<fmatvec::Function<Argo(Argi)> >(e->getFirstElementChild());
00396         }
00397         xercesc::DOMElement* writeXMLFile(xercesc::DOMNode *parent) { return 0; } 
00398       private:
00399         fmatvec::Function<Ret(Argo)> *fo;
00400         fmatvec::Function<Argo(Argi)> *fi;
00401     };
00402 
00403   template<typename Sig> class LimitedFunction; 
00404 
00405   template <typename Ret, typename Arg>
00406   struct LimitedFunction<Ret(Arg)> {
00407     LimitedFunction(fmatvec::Function<Ret(Arg)> *function_, double limit_) : function(function_), limit(limit_) { }
00408     fmatvec::Function<Ret(Arg)> *function;
00409     double limit;
00410   };
00411 
00412   template<typename Sig> class PiecewiseDefinedFunction; 
00413 
00414   template<typename Ret, typename Arg>
00415     class PiecewiseDefinedFunction<Ret(Arg)> : public fmatvec::Function<Ret(Arg)> {
00416       public:
00417         PiecewiseDefinedFunction() : contDiff(0) { a.push_back(0); }
00418         ~PiecewiseDefinedFunction() { 
00419           for(unsigned int i=0; i<function.size(); i++)
00420             delete function[i];
00421         }
00422         void addLimitedFunction(const LimitedFunction<Ret(Arg)> &limitedFunction) { 
00423           function.push_back(limitedFunction.function); 
00424           a.push_back(limitedFunction.limit); 
00425           init();
00426         }
00427         void setContinouslyDifferentiable(Arg contDiff_) { contDiff = contDiff_; }
00428         Ret zeros(const Ret &x) { return Ret(x.size()); }
00429         Ret operator()(const Arg &x) {
00430           for(unsigned int i=0; i<a.size(); i++)
00431             if(x<=a[i+1])
00432               return (*function[i])(x-a[i]);
00433           if(contDiff==0)
00434             return yEnd;
00435           else if(contDiff==1)
00436             return yEnd+ysEnd*(x-a[a.size()-1]);
00437           else 
00438             return yEnd+(ysEnd+0.5*yssEnd*(x-a[a.size()-1]))*(x-a[a.size()-1]);
00439         }
00440         typename fmatvec::Der<Ret, double>::type parDer(const double &x) {  
00441           for(unsigned int i=0; i<a.size(); i++)
00442             if(x<=a[i+1])
00443               return function[i]->parDer(x-a[i]);
00444           if(contDiff==0)
00445             return zeros(yEnd);
00446           else if(contDiff==1)
00447             return ysEnd;
00448           else 
00449             return ysEnd+yssEnd*(x-a[a.size()-1]);
00450         }
00451         typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {  
00452           for(unsigned int i=0; i<a.size(); i++)
00453             if(x<=a[i+1])
00454               return function[i]->parDerParDer(x-a[i]);
00455           if(contDiff==0)
00456             return zeros(yEnd);
00457           else if(contDiff==1)
00458             return zeros(yEnd);
00459           else 
00460             return yssEnd;
00461         }
00462 
00463         void initializeUsingXML(xercesc::DOMElement *element) {
00464           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"limitedFunctions");
00465           xercesc::DOMElement *ee=e->getFirstElementChild();
00466           while(ee && MBXMLUtils::E(ee)->getTagName()==MBSIM%"LimitedFunction") {
00467             function.push_back(ObjectFactory::createAndInit<fmatvec::Function<Ret(Arg)> >(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%"function")->getFirstElementChild()));
00468             a.push_back(Element::getDouble(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%"limit")));
00469             ee=ee->getNextElementSibling();
00470           }
00471           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"continouslyDifferentiable");
00472           if(e) contDiff=Element::getDouble(e);
00473           init();
00474         }
00475       private:
00476         std::vector<fmatvec::Function<Ret(Arg)> *> function;
00477         std::vector<double> a;
00478         int contDiff;
00479         Ret yEnd, ysEnd, yssEnd;
00480         void init() {
00481           yEnd = (*function[function.size()-1])(a[a.size()-1]-a[a.size()-2]); 
00482           if(contDiff>0) {
00483             ysEnd = function[function.size()-1]->parDer(a[a.size()-1]-a[a.size()-2]); 
00484             if(contDiff>1)
00485               yssEnd = function[function.size()-1]->parDerParDer(a[a.size()-1]-a[a.size()-2]); 
00486           }
00487         }
00488     };
00489 
00490   template<>
00491     inline double PiecewiseDefinedFunction<double(double)>::zeros(const double &x) { return 0; } 
00492 
00493 }
00494 
00495 #endif /* _FUNCTION_LIBRARY_H_ */
00496