MBSim

Project Page
basic_functions.h
00001 /* Copyright (C) 2004-2014 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@gmail.com
00018  */
00019 
00020 #ifndef _BASIC_FUNCTIONS_H_
00021 #define _BASIC_FUNCTIONS_H_
00022 
00023 #include "mbsim/functions/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 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 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 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 Function<Ret(Arg)> {
00109     public:
00110       PolynomFunction() { }
00111       PolynomFunction(const fmatvec::VecV &a_) : a(a_) { }
00112       void init(Element::InitStage stage) {
00113         Function<Ret(Arg)>::init(stage);
00114         if(stage == Element::preInit) {
00115           ad.resize(a.size()-1);
00116           add.resize(ad.size()-1);
00117           for(unsigned int i=1; i<a.size(); i++)
00118             ad.e(i-1) = double(i)*a.e(i);
00119           for(unsigned int i=1; i<ad.size(); i++)
00120             add.e(i-1) = double(i)*ad(i);
00121         }
00122       }
00123       Ret operator()(const Arg &x_) {
00124         double x = ToDouble<Arg>::cast(x_);
00125         double value=a(a.size()-1);
00126         for (int i=int(a.size())-2; i>-1; i--)
00127           value=value*x+a.e(i);
00128         return FromDouble<Ret>::cast(value);
00129       }
00130       typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &x_) {  
00131         double x = ToDouble<Arg>::cast(x_);
00132         double value=ad(ad.size()-1);
00133         for (int i=int(ad.size())-2; i>-1; i--)
00134           value=value*x+ad.e(i);
00135         return FromDouble<Ret>::cast(value);
00136       }
00137       typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {  
00138         double value=add(add.size()-1);
00139         for (int i=int(add.size())-2; i>-1; i--)
00140           value=value*x+add.e(i);
00141         return FromDouble<Ret>::cast(value);
00142       }
00143 
00144       void initializeUsingXML(xercesc::DOMElement *element) {
00145         a = Element::getVec(MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"coefficients"));
00146       }
00147 
00148     private:
00149       fmatvec::VecV a, ad, add;
00150   };
00151 
00152   template<typename Sig> class SinusoidalFunction; 
00153 
00154   template<typename Ret, typename Arg>
00155   class SinusoidalFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00156     protected:
00157       double A, f, phi0, y0;
00158     public:
00159       SinusoidalFunction(double A_=0, double f_=0, double phi0_=0, double y0_=0) : A(A_), f(f_), phi0(phi0_), y0(y0_) { }
00160       Ret operator()(const Arg &x) {  
00161         return FromDouble<Ret>::cast(y0+A*sin(2.*M_PI*f*ToDouble<Arg>::cast(x)+phi0));
00162       }
00163       typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &x) {  
00164         double om = 2.*M_PI*f;
00165         return FromDouble<Ret>::cast(A*om*cos(om*ToDouble<Arg>::cast(x)+phi0));
00166       }
00167       typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {  
00168         double om = 2.*M_PI*f;
00169         return FromDouble<Ret>::cast(-A*om*om*sin(om*x+phi0));
00170       }
00171       void initializeUsingXML(xercesc::DOMElement *element) {
00172         xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"amplitude");
00173         A=Element::getDouble(e);
00174         e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"frequency");
00175         f=Element::getDouble(e);
00176         e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"phase");
00177         if(e) phi0=Element::getDouble(e);
00178         e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"offset");
00179         if(e) y0=Element::getDouble(e);
00180       }
00181       xercesc::DOMElement* writeXMLFile(xercesc::DOMNode *parent) {
00182         xercesc::DOMElement *ele0 = Function<Ret(Arg)>::writeXMLFile(parent);
00183         addElementText(ele0,MBSIM%"amplitude",A);
00184         addElementText(ele0,MBSIM%"frequency",f);
00185         addElementText(ele0,MBSIM%"phase",phi0);
00186         addElementText(ele0,MBSIM%"offset",y0);
00187         return ele0;
00188       }
00189   };
00190 
00191   template<typename Sig> class StepFunction; 
00192 
00193   template<typename Ret, typename Arg>
00194   class StepFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00195     private:
00196       double stepTime, stepSize, stepInit;
00197     public:
00198       StepFunction() {}
00199       StepFunction(double stepTime_, double stepSize_, double stepInit_=0.) : stepTime(stepTime_), stepSize(stepSize_), stepInit(stepInit_) { }
00200       Ret operator()(const Arg &x) {
00201         return FromDouble<Ret>::cast((ToDouble<Arg>::cast(x)>=stepTime)?stepSize:stepInit);
00202       }
00203       void initializeUsingXML(xercesc::DOMElement *element) {
00204         xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"time");
00205         stepTime=Element::getDouble(e);
00206         e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"size");
00207         stepSize=Element::getDouble(e);
00208         e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"init");
00209         stepSize=Element::getDouble(e);
00210       }
00211   };
00212 
00213   template<typename Sig> class AbsoluteValueFunction; 
00214 
00215   template<typename Ret, typename Arg>
00216   class AbsoluteValueFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00217     public:
00218       Ret operator()(const Arg &x_) {
00219         double x = ToDouble<Arg>::cast(x_);
00220         return FromDouble<Ret>::cast(fabs(x));
00221       }
00222   };
00223 
00224   template<typename Sig> class ModuloFunction;
00225 
00226   template<typename Ret, typename Arg>
00227     class ModuloFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00228       private:
00229         double denom;
00230       public:
00231         void setDenominator(double denom_) { denom = denom_; }
00232         Ret operator()(const Arg &x_) {
00233           double x = ToDouble<Arg>::cast(x_);
00234           return FromDouble<Ret>::cast(x-denom*floor(x/denom));
00235         }
00236         void initializeUsingXML(xercesc::DOMElement *element) {
00237           xercesc::DOMElement *e;
00238           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"denominator");
00239           denom=Element::getDouble(e);
00240         }
00241     };
00242 
00243   template<typename Sig> class SignumFunction; 
00244 
00245   template<typename Ret, typename Arg>
00246   class SignumFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00247     public:
00248       Ret operator()(const Arg &x_) {
00249         double x = ToDouble<Arg>::cast(x_);
00250         return FromDouble<Ret>::cast(sign(x));
00251       }
00252   };
00253 
00254   template<typename Sig> class PositiveValueFunction; 
00255 
00256   template<typename Ret, typename Arg>
00257   class PositiveValueFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00258     public:
00259       Ret operator()(const Arg &x_) {
00260         double x = ToDouble<Arg>::cast(x_);
00261         return FromDouble<Ret>::cast(x>=0?x:0);
00262       }
00263   };
00264 
00265   template<typename Sig> class AdditionFunction; 
00266 
00267   template<typename Ret, typename Arg>
00268     class AdditionFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00269       public:
00270         AdditionFunction() : f1(0), f2(0) { }
00271         ~AdditionFunction() { delete f1; delete f2; }
00272         void setFirstSummand(Function<Ret(Arg)> *function) {
00273           f1 = function;
00274           f1->setParent(this);
00275           f1->setName("FirstSummand");
00276         }
00277         void setSecondSummand(Function<Ret(Arg)> *function) {
00278           f2 = function;
00279           f2->setParent(this);
00280           f2->setName("SecondSummand");
00281         }
00282         Ret operator()(const Arg &x) {
00283           return (*(f1))(x)+(*(f2))(x);
00284         }
00285         typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &x) {  
00286           return f1->parDer(x)+f2->parDer(x);
00287         }
00288         typename fmatvec::Der<typename fmatvec::Der<Ret, Arg>::type, Arg>::type parDerParDer(const Arg &x) {  
00289           return f1->parDerParDer(x)+f2->parDerParDer(x);
00290         }
00291         void initializeUsingXML(xercesc::DOMElement *element) {
00292           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"firstSummand");
00293           setFirstSummand(ObjectFactory::createAndInit<Function<Ret(Arg)> >(e->getFirstElementChild()));
00294           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"secondSummand");
00295           setSecondSummand(ObjectFactory::createAndInit<Function<Ret(Arg)> >(e->getFirstElementChild()));
00296         }
00297         void init(Element::InitStage stage) {
00298           Function<Ret(Arg)>::init(stage);
00299           f1->init(stage);
00300           f2->init(stage);
00301         }
00302       private:
00303         Function<Ret(Arg)> *f1, *f2;
00304     };
00305 
00306   template<typename Sig> class MultiplicationFunction; 
00307 
00308   template<typename Ret, typename Arg>
00309     class MultiplicationFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00310       public:
00311         MultiplicationFunction() : f1(0), f2(0) { }
00312         ~MultiplicationFunction() { delete f1; delete f2; }
00313         void setFirstFactor(Function<Ret(Arg)> *function) {
00314           f1 = function;
00315           f1->setParent(this);
00316           f1->setName("FirstFactor");
00317         }
00318         void setSecondFactor(Function<Ret(Arg)> *function) {
00319           f2 = function;
00320           f2->setParent(this);
00321           f2->setName("SecondFactor");
00322         }
00323         Ret operator()(const Arg &x) {
00324           double a1 = ToDouble<Ret>::cast((*f1)(x));
00325           double a2 = ToDouble<Ret>::cast((*f2)(x));
00326           return FromDouble<Ret>::cast(a1*a2);
00327         }
00328         typename fmatvec::Der<Ret, double>::type parDer(const double &x) {  
00329           double a1 = ToDouble<Ret>::cast((*f1)(x));
00330           double a2 = ToDouble<Ret>::cast((*f2)(x));
00331           double a1d = ToDouble<typename fmatvec::Der<Ret, double>::type>::cast(f1->parDer(x));
00332           double a2d = ToDouble<typename fmatvec::Der<Ret, double>::type>::cast(f2->parDer(x));
00333           return FromDouble<typename fmatvec::Der<Ret, double>::type>::cast(a1d*a2+a1*a2d);
00334         }
00335         typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {  
00336           double a1 = ToDouble<Ret>::cast((*f1)(x));
00337           double a2 = ToDouble<Ret>::cast((*f2)(x));
00338           double a1d = ToDouble<typename fmatvec::Der<Ret, double>::type>::cast(f1->parDer(x));
00339           double a2d = ToDouble<typename fmatvec::Der<Ret, double>::type>::cast(f2->parDer(x));
00340           double a1dd = ToDouble<typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type>::cast(f1->parDerParDer(x));
00341           double a2dd = ToDouble<typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type>::cast(f2->parDerParDer(x));
00342           return FromDouble<typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type>::cast(a1dd*a2 + 2*a1d*a2d + a1*a2dd);
00343         }
00344         void initializeUsingXML(xercesc::DOMElement *element) {
00345           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"firstFactor");
00346           setFirstFactor(ObjectFactory::createAndInit<Function<Ret(Arg)> >(e->getFirstElementChild()));
00347           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"secondFactor");
00348           setSecondFactor(ObjectFactory::createAndInit<Function<Ret(Arg)> >(e->getFirstElementChild()));
00349         }
00350         void init(Element::InitStage stage) {
00351           Function<Ret(Arg)>::init(stage);
00352           f1->init(stage);
00353           f2->init(stage);
00354         }
00355       private:
00356         Function<Ret(Arg)> *f1, *f2;
00357     };
00358 
00359   template<typename Sig> class VectorValuedFunction; 
00360 
00361   // VectorValuedFunction with a double as argument (including 2nd derivative)
00362   template<typename Ret>
00363     class VectorValuedFunction<Ret(double)> : public Function<Ret(double)> {
00364       public:
00365         VectorValuedFunction() { }
00366         VectorValuedFunction(const std::vector<Function<double(double)> *> &component_) : component(component_) {
00367           for(std::vector<Function<double(double)> *>::iterator it=component.begin(); it!=component.end(); ++it)
00368             (*it)->setParent(this);
00369         }
00370         ~VectorValuedFunction() { 
00371           for (unsigned int i=1; i<component.size(); i++)
00372             delete component[i]; 
00373         }
00374         void addComponent(Function<double(double)> *function) {
00375           component.push_back(function);
00376           function->setParent(this);
00377         }
00378         Ret operator()(const double &x) {
00379           Ret y(component.size(),fmatvec::NONINIT);
00380           for (unsigned int i=0; i<component.size(); i++)
00381             y(i)=(*component[i])(x);
00382           return y;
00383         }
00384         typename fmatvec::Der<Ret, double>::type parDer(const double &x) {  
00385           typename fmatvec::Der<Ret, double>::type y(component.size(),fmatvec::NONINIT);
00386           for (unsigned int i=0; i<component.size(); i++)
00387             y(i)=component[i]->parDer(x);
00388           return y;
00389         }
00390         typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {  
00391           typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type y(component.size(),fmatvec::NONINIT);
00392           for (unsigned int i=0; i<component.size(); i++)
00393             y(i)=component[i]->parDerParDer(x);
00394           return y;
00395         }
00396 
00397         void initializeUsingXML(xercesc::DOMElement *element) {
00398           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"components")->getFirstElementChild();
00399           while (e) {
00400             addComponent(ObjectFactory::createAndInit<Function<double(double)> >(e));
00401             e=e->getNextElementSibling();
00402           }
00403         }
00404         void init(Element::InitStage stage) {
00405           Function<Ret(double)>::init(stage);
00406           for(std::vector<Function<double(double)> *>::iterator it=component.begin(); it!=component.end(); ++it)
00407             (*it)->init(stage);
00408         }
00409       private:
00410         std::vector<Function<double(double)> *> component;
00411     };
00412 
00413   // VectorValuedFunction with a vector as argument (no 2nd derivative defined)
00414   template<typename Ret, typename Arg>
00415     class VectorValuedFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00416       public:
00417         VectorValuedFunction() { }
00418         VectorValuedFunction(const std::vector<Function<double(Arg)> *> &component_) : component(component_) {
00419           for(typename std::vector<Function<double(Arg)>*>::iterator it=component.begin(); it!=component.end(); ++it)
00420             (*it)->setParent(this);
00421         }
00422         ~VectorValuedFunction() { 
00423           for (unsigned int i=1; i<component.size(); i++)
00424             delete component[i]; 
00425         }
00426         void addComponent(Function<double(Arg)> *function) {
00427           component.push_back(function);
00428           function->setParent(this);
00429         }
00430         Ret operator()(const Arg &x) {
00431           Ret y(component.size(),fmatvec::NONINIT);
00432           for (unsigned int i=0; i<component.size(); i++)
00433             y(i)=(*component[i])(x);
00434           return y;
00435         }
00436         typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &x) {  
00437           typename fmatvec::Der<Ret, Arg>::type y(component.size(),x.size(),fmatvec::NONINIT);
00438           for (unsigned int i=0; i<component.size(); i++) {
00439             typename fmatvec::Der<double, Arg>::type row=component[i]->parDer(x);
00440             for (unsigned int j=0; j<x.size(); j++)
00441               y(i,j)=row(j);
00442           }
00443           return y;
00444         }
00445 
00446         void initializeUsingXML(xercesc::DOMElement *element) {
00447           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"components")->getFirstElementChild();
00448           while (e) {
00449             addComponent(ObjectFactory::createAndInit<Function<double(Arg)> >(e));
00450             e=e->getNextElementSibling();
00451           }
00452         }
00453         void init(Element::InitStage stage) {
00454           Function<Ret(Arg)>::init(stage);
00455           for(typename std::vector<Function<double(Arg)> *>::iterator it=component.begin(); it!=component.end(); ++it)
00456             (*it)->init(stage);
00457         }
00458       private:
00459         std::vector<Function<double(Arg)> *> component;
00460     };
00461 
00462   template<typename Sig> class NestedFunction; 
00463 
00464   // NestedFunction with a double as inner argument (including 2nd derivative)
00465   template<typename Ret, typename Argo> 
00466     class NestedFunction<Ret(Argo(double))> : public Function<Ret(double)> {
00467       public:
00468         NestedFunction(Function<Ret(Argo)> *fo_=0, Function<Argo(double)> *fi_=0) : fo(fo_), fi(fi_) {
00469           if(fo)
00470             fo->setParent(this);
00471           if(fi)
00472             fi->setParent(this);
00473         }
00474         ~NestedFunction() {
00475           delete fo;
00476           delete fi;
00477         }
00478         typename fmatvec::Size<double>::type getArgSize() const {
00479           return fi->getArgSize();
00480         }
00481         Ret operator()(const double &arg) {
00482           return (*fo)((*fi)(arg));
00483         }
00484         typename fmatvec::Der<Ret, double>::type parDer(const double &arg) {
00485           return fo->parDer((*fi)(arg))*fi->parDer(arg);
00486         }
00487         typename fmatvec::Der<Ret, double>::type parDerDirDer(const double &argDir, const double &arg) {
00488           return fo->parDerDirDer(fi->parDer(arg)*argDir,(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerDirDer(argDir,arg);
00489         }
00490         typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &arg) {
00491           return fo->parDerDirDer(fi->parDer(arg),(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerParDer(arg);
00492         }
00493         void setOuterFunction(Function<Ret(Argo)> *fo_) {
00494           fo = fo_;
00495           fo->setParent(this);
00496           fo->setName("Outer");
00497         }
00498         void setInnerFunction(Function<Argo(double)> *fi_) {
00499           fi = fi_;
00500           fi->setParent(this);
00501           fi->setName("Inner");
00502         }
00503         void initializeUsingXML(xercesc::DOMElement *element) {
00504           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"outerFunction");
00505           setOuterFunction(ObjectFactory::createAndInit<Function<Ret(Argo)> >(e->getFirstElementChild()));
00506           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"innerFunction");
00507           setInnerFunction(ObjectFactory::createAndInit<Function<Argo(double)> >(e->getFirstElementChild()));
00508         }
00509         void init(Element::InitStage stage) {
00510           Function<Ret(double)>::init(stage);
00511           fo->init(stage);
00512           fi->init(stage);
00513         }
00514         xercesc::DOMElement* writeXMLFile(xercesc::DOMNode *parent) {
00515           return 0;
00516         } 
00517       private:
00518         Function<Ret(Argo)> *fo;
00519         Function<Argo(double)> *fi;
00520     };
00521 
00522   // VectorValuedFunction with a vector as inner argument (no 2nd derivative defined)
00523   template<typename Ret, typename Argo, typename Argi> 
00524     class NestedFunction<Ret(Argo(Argi))> : public Function<Ret(Argi)> {
00525       public:
00526         NestedFunction(Function<Ret(Argo)> *fo_=0, Function<Argo(Argi)> *fi_=0) : fo(fo_), fi(fi_) {
00527           if(fo)
00528             fo->setParent(this);
00529           if(fi)
00530             fi->setParent(this);
00531         }
00532         ~NestedFunction() {
00533           delete fo;
00534           delete fi;
00535         }
00536         typename fmatvec::Size<Argi>::type getArgSize() const {
00537           return fi->getArgSize();
00538         }
00539         Ret operator()(const Argi &arg) {
00540           return (*fo)((*fi)(arg));
00541         }
00542         typename fmatvec::Der<Ret, Argi>::type parDer(const Argi &arg) {
00543           return fo->parDer((*fi)(arg))*fi->parDer(arg);
00544         }
00545         typename fmatvec::Der<Ret, Argi>::type parDerDirDer(const Argi &argDir, const Argi &arg) {
00546           return fo->parDerDirDer(fi->parDer(arg)*argDir,(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerDirDer(argDir,arg);
00547         }
00548         void setOuterFunction(Function<Ret(Argo)> *fo_) {
00549           fo = fo_;
00550           fo->setParent(this);
00551           fo->setName("Outer");
00552         }
00553         void setInnerFunction(Function<Argo(Argi)> *fi_) {
00554           fi = fi_;
00555           fi->setParent(this);
00556           fi->setName("Inner");
00557         }
00558         void initializeUsingXML(xercesc::DOMElement *element) {
00559           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"outerFunction");
00560           setOuterFunction(ObjectFactory::createAndInit<Function<Ret(Argo)> >(e->getFirstElementChild()));
00561           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"innerFunction");
00562           setInnerFunction(ObjectFactory::createAndInit<Function<Argo(Argi)> >(e->getFirstElementChild()));
00563         }
00564         void init(Element::InitStage stage) {
00565           Function<Ret(Argi)>::init(stage);
00566           fo->init(stage);
00567           fi->init(stage);
00568         }
00569         xercesc::DOMElement* writeXMLFile(xercesc::DOMNode *parent) {
00570           return 0;
00571         } 
00572       private:
00573         Function<Ret(Argo)> *fo;
00574         Function<Argo(Argi)> *fi;
00575     };
00576 
00577   template<typename Sig> class LimitedFunction; 
00578 
00579   template <typename Ret, typename Arg>
00580   struct LimitedFunction<Ret(Arg)> {
00581     LimitedFunction(Function<Ret(Arg)> *function_, double limit_) : function(function_), limit(limit_) { }
00582     Function<Ret(Arg)> *function;
00583     double limit;
00584   };
00585 
00586   template<typename Sig> class PiecewiseDefinedFunction; 
00587 
00588   template<typename Ret, typename Arg>
00589     class PiecewiseDefinedFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00590       public:
00591         PiecewiseDefinedFunction() : contDiff(0) { a.push_back(0); }
00592         ~PiecewiseDefinedFunction() { 
00593           for(unsigned int i=0; i<function.size(); i++)
00594             delete function[i];
00595         }
00596         void addLimitedFunction(const LimitedFunction<Ret(Arg)> &limitedFunction) { 
00597           function.push_back(limitedFunction.function); 
00598           limitedFunction.function->setParent(this);
00599           a.push_back(limitedFunction.limit); 
00600         }
00601         void setContinouslyDifferentiable(Arg contDiff_) { contDiff = contDiff_; }
00602         Ret zeros(const Ret &x) { return Ret(x.size()); }
00603         Ret operator()(const Arg &x) {
00604           for(unsigned int i=0; i<a.size(); i++)
00605             if(x<=a[i+1])
00606               return (*function[i])(x-a[i]);
00607           if(contDiff==0)
00608             return yEnd;
00609           else if(contDiff==1)
00610             return yEnd+ysEnd*(x-a[a.size()-1]);
00611           else 
00612             return yEnd+(ysEnd+0.5*yssEnd*(x-a[a.size()-1]))*(x-a[a.size()-1]);
00613         }
00614         typename fmatvec::Der<Ret, double>::type parDer(const double &x) {  
00615           for(unsigned int i=0; i<a.size(); i++)
00616             if(x<=a[i+1])
00617               return function[i]->parDer(x-a[i]);
00618           if(contDiff==0)
00619             return zeros(yEnd);
00620           else if(contDiff==1)
00621             return ysEnd;
00622           else 
00623             return ysEnd+yssEnd*(x-a[a.size()-1]);
00624         }
00625         typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {  
00626           for(unsigned int i=0; i<a.size(); i++)
00627             if(x<=a[i+1])
00628               return function[i]->parDerParDer(x-a[i]);
00629           if(contDiff==0)
00630             return zeros(yEnd);
00631           else if(contDiff==1)
00632             return zeros(yEnd);
00633           else 
00634             return yssEnd;
00635         }
00636 
00637         void initializeUsingXML(xercesc::DOMElement *element) {
00638           xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"limitedFunctions");
00639           xercesc::DOMElement *ee=e->getFirstElementChild();
00640           while(ee && MBXMLUtils::E(ee)->getTagName()==MBSIM%"LimitedFunction") {
00641             addLimitedFunction(LimitedFunction<Ret(Arg)>(ObjectFactory::createAndInit<Function<Ret(Arg)> >(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%"function")->getFirstElementChild()),Element::getDouble(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%"limit"))));
00642             ee=ee->getNextElementSibling();
00643           }
00644           e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"continouslyDifferentiable");
00645           if(e) contDiff=Element::getDouble(e);
00646         }
00647         void init(Element::InitStage stage) {
00648           Function<Ret(Arg)>::init(stage);
00649           for(typename std::vector<Function<Ret(Arg)> *>::iterator it=function.begin(); it!=function.end(); it++)
00650             (*it)->init(stage);
00651           if(stage==Element::preInit) {
00652             yEnd = (*function[function.size()-1])(a[a.size()-1]-a[a.size()-2]); 
00653             if(contDiff>0) {
00654               ysEnd = function[function.size()-1]->parDer(a[a.size()-1]-a[a.size()-2]); 
00655               if(contDiff>1)
00656                 yssEnd = function[function.size()-1]->parDerParDer(a[a.size()-1]-a[a.size()-2]); 
00657             }
00658           }
00659         }
00660       private:
00661         std::vector<Function<Ret(Arg)> *> function;
00662         std::vector<double> a;
00663         int contDiff;
00664         Ret yEnd, ysEnd, yssEnd;
00665     };
00666 
00667   template<>
00668     inline double PiecewiseDefinedFunction<double(double)>::zeros(const double &x) { return 0; } 
00669 
00670 }
00671 
00672 #endif /* _FUNCTION_LIBRARY_H_ */
00673