00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _FUNCTION_LIBRARY_H_
00021 #define _FUNCTION_LIBRARY_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 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 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 = 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 Function<Ret(Arg)> {
00200 private:
00201 double stepTime, stepSize, stepInit;
00202 public:
00203 StepFunction() {}
00204 StepFunction(double stepTime_, double stepSize_, double stepInit_=0.) : stepTime(stepTime_), stepSize(stepSize_), stepInit(stepInit_) { }
00205 Ret operator()(const Arg &x) {
00206 return FromDouble<Ret>::cast((ToDouble<Arg>::cast(x)>=stepTime)?stepSize:stepInit);
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 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"init");
00214 stepSize=Element::getDouble(e);
00215 }
00216 };
00217
00218 template<typename Sig> class AbsoluteValueFunction;
00219
00220 template<typename Ret, typename Arg>
00221 class AbsoluteValueFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00222 public:
00223 Ret operator()(const Arg &x_) {
00224 double x = ToDouble<Arg>::cast(x_);
00225 return FromDouble<Ret>::cast(fabs(x));
00226 }
00227 };
00228
00229 template<typename Sig> class ModuloFunction;
00230
00231 template<typename Ret, typename Arg>
00232 class ModuloFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00233 private:
00234 double denom;
00235 public:
00236 void setDenominator(double denom_) { denom = denom_; }
00237 Ret operator()(const Arg &x) {
00238 return FromDouble<Ret>::cast(fmod(ToDouble<Arg>::cast(x),denom));
00239 }
00240 void initializeUsingXML(xercesc::DOMElement *element) {
00241 xercesc::DOMElement *e;
00242 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"denominator");
00243 denom=Element::getDouble(e);
00244 }
00245 };
00246
00247 template<typename Sig> class SignumFunction;
00248
00249 template<typename Ret, typename Arg>
00250 class SignumFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00251 public:
00252 Ret operator()(const Arg &x_) {
00253 double x = ToDouble<Arg>::cast(x_);
00254 return FromDouble<Ret>::cast(sign(x));
00255 }
00256 };
00257
00258 template<typename Sig> class PositiveValueFunction;
00259
00260 template<typename Ret, typename Arg>
00261 class PositiveValueFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00262 public:
00263 Ret operator()(const Arg &x_) {
00264 double x = ToDouble<Arg>::cast(x_);
00265 return FromDouble<Ret>::cast(x>=0?x:0);
00266 }
00267 };
00268
00269 template<typename Sig> class AdditionFunction;
00270
00271 template<typename Ret, typename Arg>
00272 class AdditionFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00273 public:
00274 AdditionFunction() : f1(0), f2(0) { }
00275 ~AdditionFunction() { delete f1; delete f2; }
00276 void setFirstSummand(Function<Ret(Arg)> *function) {
00277 f1 = function;
00278 f1->setParent(this);
00279 f1->setName("FirstSummand");
00280 }
00281 void setSecondSummand(Function<Ret(Arg)> *function) {
00282 f2 = function;
00283 f2->setParent(this);
00284 f2->setName("SecondSummand");
00285 }
00286 Ret operator()(const Arg &x) {
00287 return (*(f1))(x)+(*(f2))(x);
00288 }
00289 typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &x) {
00290 return f1->parDer(x)+f2->parDer(x);
00291 }
00292 typename fmatvec::Der<typename fmatvec::Der<Ret, Arg>::type, Arg>::type parDerParDer(const Arg &x) {
00293 return f1->parDerParDer(x)+f2->parDerParDer(x);
00294 }
00295 void initializeUsingXML(xercesc::DOMElement *element) {
00296 xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"firstSummand");
00297 setFirstSummand(ObjectFactory::createAndInit<Function<Ret(Arg)> >(e->getFirstElementChild()));
00298 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"secondSummand");
00299 setSecondSummand(ObjectFactory::createAndInit<Function<Ret(Arg)> >(e->getFirstElementChild()));
00300 }
00301 void init(Element::InitStage stage) {
00302 Function<Ret(Arg)>::init(stage);
00303 f1->init(stage);
00304 f2->init(stage);
00305 }
00306 private:
00307 Function<Ret(Arg)> *f1, *f2;
00308 };
00309
00310 template<typename Sig> class MultiplicationFunction;
00311
00312 template<typename Ret, typename Arg>
00313 class MultiplicationFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00314 public:
00315 MultiplicationFunction() : f1(0), f2(0) { }
00316 ~MultiplicationFunction() { delete f1; delete f2; }
00317 void setFirstFactor(Function<Ret(Arg)> *function) {
00318 f1 = function;
00319 f1->setParent(this);
00320 f1->setName("FirstFactor");
00321 }
00322 void setSecondFactor(Function<Ret(Arg)> *function) {
00323 f2 = function;
00324 f2->setParent(this);
00325 f2->setName("SecondFactor");
00326 }
00327 Ret operator()(const Arg &x) {
00328 double a1 = ToDouble<Ret>::cast((*f1)(x));
00329 double a2 = ToDouble<Ret>::cast((*f2)(x));
00330 return FromDouble<Ret>::cast(a1*a2);
00331 }
00332 typename fmatvec::Der<Ret, double>::type parDer(const double &x) {
00333 double a1 = ToDouble<Ret>::cast((*f1)(x));
00334 double a2 = ToDouble<Ret>::cast((*f2)(x));
00335 double a1d = ToDouble<typename fmatvec::Der<Ret, double>::type>::cast(f1->parDer(x));
00336 double a2d = ToDouble<typename fmatvec::Der<Ret, double>::type>::cast(f2->parDer(x));
00337 return FromDouble<typename fmatvec::Der<Ret, double>::type>::cast(a1d*a2+a1*a2d);
00338 }
00339 typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {
00340 double a1 = ToDouble<Ret>::cast((*f1)(x));
00341 double a2 = ToDouble<Ret>::cast((*f2)(x));
00342 double a1d = ToDouble<typename fmatvec::Der<Ret, double>::type>::cast(f1->parDer(x));
00343 double a2d = ToDouble<typename fmatvec::Der<Ret, double>::type>::cast(f2->parDer(x));
00344 double a1dd = ToDouble<typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type>::cast(f1->parDerParDer(x));
00345 double a2dd = ToDouble<typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type>::cast(f2->parDerParDer(x));
00346 return FromDouble<typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type>::cast(a1dd*a2 + 2*a1d*a2d + a1*a2dd);
00347 }
00348 void initializeUsingXML(xercesc::DOMElement *element) {
00349 xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"firstFactor");
00350 setFirstFactor(ObjectFactory::createAndInit<Function<Ret(Arg)> >(e->getFirstElementChild()));
00351 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"secondFactor");
00352 setSecondFactor(ObjectFactory::createAndInit<Function<Ret(Arg)> >(e->getFirstElementChild()));
00353 }
00354 void init(Element::InitStage stage) {
00355 Function<Ret(Arg)>::init(stage);
00356 f1->init(stage);
00357 f2->init(stage);
00358 }
00359 private:
00360 Function<Ret(Arg)> *f1, *f2;
00361 };
00362
00363 template<typename Sig> class VectorValuedFunction;
00364
00365
00366 template<typename Ret>
00367 class VectorValuedFunction<Ret(double)> : public Function<Ret(double)> {
00368 public:
00369 VectorValuedFunction() { }
00370 VectorValuedFunction(const std::vector<Function<double(double)> *> &component_) : component(component_) {
00371 for(std::vector<Function<double(double)> *>::iterator it=component.begin(); it!=component.end(); ++it)
00372 (*it)->setParent(this);
00373 }
00374 ~VectorValuedFunction() {
00375 for (unsigned int i=1; i<component.size(); i++)
00376 delete component[i];
00377 }
00378 void addComponent(Function<double(double)> *function) {
00379 component.push_back(function);
00380 function->setParent(this);
00381 }
00382 Ret operator()(const double &x) {
00383 Ret y(component.size(),fmatvec::NONINIT);
00384 for (unsigned int i=0; i<component.size(); i++)
00385 y(i)=(*component[i])(x);
00386 return y;
00387 }
00388 typename fmatvec::Der<Ret, double>::type parDer(const double &x) {
00389 typename fmatvec::Der<Ret, double>::type y(component.size(),fmatvec::NONINIT);
00390 for (unsigned int i=0; i<component.size(); i++)
00391 y(i)=component[i]->parDer(x);
00392 return y;
00393 }
00394 typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {
00395 typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type y(component.size(),fmatvec::NONINIT);
00396 for (unsigned int i=0; i<component.size(); i++)
00397 y(i)=component[i]->parDerParDer(x);
00398 return y;
00399 }
00400
00401 void initializeUsingXML(xercesc::DOMElement *element) {
00402 xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"components")->getFirstElementChild();
00403 while (e) {
00404 addComponent(ObjectFactory::createAndInit<Function<double(double)> >(e));
00405 e=e->getNextElementSibling();
00406 }
00407 }
00408 void init(Element::InitStage stage) {
00409 Function<Ret(double)>::init(stage);
00410 for(std::vector<Function<double(double)> *>::iterator it=component.begin(); it!=component.end(); ++it)
00411 (*it)->init(stage);
00412 }
00413 private:
00414 std::vector<Function<double(double)> *> component;
00415 };
00416
00417
00418 template<typename Ret, typename Arg>
00419 class VectorValuedFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00420 public:
00421 VectorValuedFunction() { }
00422 VectorValuedFunction(const std::vector<Function<double(Arg)> *> &component_) : component(component_) {
00423 for(typename std::vector<Function<double(Arg)>*>::iterator it=component.begin(); it!=component.end(); ++it)
00424 (*it)->setParent(this);
00425 }
00426 ~VectorValuedFunction() {
00427 for (unsigned int i=1; i<component.size(); i++)
00428 delete component[i];
00429 }
00430 void addComponent(Function<double(Arg)> *function) {
00431 component.push_back(function);
00432 function->setParent(this);
00433 }
00434 Ret operator()(const Arg &x) {
00435 Ret y(component.size(),fmatvec::NONINIT);
00436 for (unsigned int i=0; i<component.size(); i++)
00437 y(i)=(*component[i])(x);
00438 return y;
00439 }
00440 typename fmatvec::Der<Ret, Arg>::type parDer(const Arg &x) {
00441 typename fmatvec::Der<Ret, Arg>::type y(component.size(),x.size(),fmatvec::NONINIT);
00442 for (unsigned int i=0; i<component.size(); i++) {
00443 typename fmatvec::Der<double, Arg>::type row=component[i]->parDer(x);
00444 for (unsigned int j=0; j<x.size(); j++)
00445 y(i,j)=row(j);
00446 }
00447 return y;
00448 }
00449
00450 void initializeUsingXML(xercesc::DOMElement *element) {
00451 xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"components")->getFirstElementChild();
00452 while (e) {
00453 addComponent(ObjectFactory::createAndInit<Function<double(Arg)> >(e));
00454 e=e->getNextElementSibling();
00455 }
00456 }
00457 void init(Element::InitStage stage) {
00458 Function<Ret(Arg)>::init(stage);
00459 for(typename std::vector<Function<double(Arg)> *>::iterator it=component.begin(); it!=component.end(); ++it)
00460 (*it)->init(stage);
00461 }
00462 private:
00463 std::vector<Function<double(Arg)> *> component;
00464 };
00465
00466 template<typename Sig> class NestedFunction;
00467
00468
00469 template<typename Ret, typename Argo>
00470 class NestedFunction<Ret(Argo(double))> : public Function<Ret(double)> {
00471 public:
00472 NestedFunction(Function<Ret(Argo)> *fo_=0, Function<Argo(double)> *fi_=0) : fo(fo_), fi(fi_) {
00473 if(fo)
00474 fo->setParent(this);
00475 if(fi)
00476 fi->setParent(this);
00477 }
00478 ~NestedFunction() {
00479 delete fo;
00480 delete fi;
00481 }
00482 typename fmatvec::Size<double>::type getArgSize() const {
00483 return fi->getArgSize();
00484 }
00485 Ret operator()(const double &arg) {
00486 return (*fo)((*fi)(arg));
00487 }
00488 typename fmatvec::Der<Ret, double>::type parDer(const double &arg) {
00489 return fo->parDer((*fi)(arg))*fi->parDer(arg);
00490 }
00491 typename fmatvec::Der<Ret, double>::type parDerDirDer(const double &argDir, const double &arg) {
00492 return fo->parDerDirDer(fi->parDer(arg)*argDir,(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerDirDer(argDir,arg);
00493 }
00494 typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &arg) {
00495 return fo->parDerDirDer(fi->parDer(arg),(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerParDer(arg);
00496 }
00497 void setOuterFunction(Function<Ret(Argo)> *fo_) {
00498 fo = fo_;
00499 fo->setParent(this);
00500 fo->setName("Outer");
00501 }
00502 void setInnerFunction(Function<Argo(double)> *fi_) {
00503 fi = fi_;
00504 fi->setParent(this);
00505 fi->setName("Inner");
00506 }
00507 void initializeUsingXML(xercesc::DOMElement *element) {
00508 xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"outerFunction");
00509 setOuterFunction(ObjectFactory::createAndInit<Function<Ret(Argo)> >(e->getFirstElementChild()));
00510 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"innerFunction");
00511 setInnerFunction(ObjectFactory::createAndInit<Function<Argo(double)> >(e->getFirstElementChild()));
00512 }
00513 void init(Element::InitStage stage) {
00514 Function<Ret(double)>::init(stage);
00515 fo->init(stage);
00516 fi->init(stage);
00517 }
00518 xercesc::DOMElement* writeXMLFile(xercesc::DOMNode *parent) {
00519 return 0;
00520 }
00521 private:
00522 Function<Ret(Argo)> *fo;
00523 Function<Argo(double)> *fi;
00524 };
00525
00526
00527 template<typename Ret, typename Argo, typename Argi>
00528 class NestedFunction<Ret(Argo(Argi))> : public Function<Ret(Argi)> {
00529 public:
00530 NestedFunction(Function<Ret(Argo)> *fo_=0, Function<Argo(Argi)> *fi_=0) : fo(fo_), fi(fi_) {
00531 if(fo)
00532 fo->setParent(this);
00533 if(fi)
00534 fi->setParent(this);
00535 }
00536 ~NestedFunction() {
00537 delete fo;
00538 delete fi;
00539 }
00540 typename fmatvec::Size<Argi>::type getArgSize() const {
00541 return fi->getArgSize();
00542 }
00543 Ret operator()(const Argi &arg) {
00544 return (*fo)((*fi)(arg));
00545 }
00546 typename fmatvec::Der<Ret, Argi>::type parDer(const Argi &arg) {
00547 return fo->parDer((*fi)(arg))*fi->parDer(arg);
00548 }
00549 typename fmatvec::Der<Ret, Argi>::type parDerDirDer(const Argi &argDir, const Argi &arg) {
00550 return fo->parDerDirDer(fi->parDer(arg)*argDir,(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerDirDer(argDir,arg);
00551 }
00552 void setOuterFunction(Function<Ret(Argo)> *fo_) {
00553 fo = fo_;
00554 fo->setParent(this);
00555 fo->setName("Outer");
00556 }
00557 void setInnerFunction(Function<Argo(Argi)> *fi_) {
00558 fi = fi_;
00559 fi->setParent(this);
00560 fi->setName("Inner");
00561 }
00562 void initializeUsingXML(xercesc::DOMElement *element) {
00563 xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"outerFunction");
00564 setOuterFunction(ObjectFactory::createAndInit<Function<Ret(Argo)> >(e->getFirstElementChild()));
00565 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"innerFunction");
00566 setInnerFunction(ObjectFactory::createAndInit<Function<Argo(Argi)> >(e->getFirstElementChild()));
00567 }
00568 void init(Element::InitStage stage) {
00569 Function<Ret(Argi)>::init(stage);
00570 fo->init(stage);
00571 fi->init(stage);
00572 }
00573 xercesc::DOMElement* writeXMLFile(xercesc::DOMNode *parent) {
00574 return 0;
00575 }
00576 private:
00577 Function<Ret(Argo)> *fo;
00578 Function<Argo(Argi)> *fi;
00579 };
00580
00581 template<typename Sig> class LimitedFunction;
00582
00583 template <typename Ret, typename Arg>
00584 struct LimitedFunction<Ret(Arg)> {
00585 LimitedFunction(Function<Ret(Arg)> *function_, double limit_) : function(function_), limit(limit_) { }
00586 Function<Ret(Arg)> *function;
00587 double limit;
00588 };
00589
00590 template<typename Sig> class PiecewiseDefinedFunction;
00591
00592 template<typename Ret, typename Arg>
00593 class PiecewiseDefinedFunction<Ret(Arg)> : public Function<Ret(Arg)> {
00594 public:
00595 PiecewiseDefinedFunction() : contDiff(0) { a.push_back(0); }
00596 ~PiecewiseDefinedFunction() {
00597 for(unsigned int i=0; i<function.size(); i++)
00598 delete function[i];
00599 }
00600 void addLimitedFunction(const LimitedFunction<Ret(Arg)> &limitedFunction) {
00601 function.push_back(limitedFunction.function);
00602 limitedFunction.function->setParent(this);
00603 a.push_back(limitedFunction.limit);
00604 }
00605 void setContinouslyDifferentiable(Arg contDiff_) { contDiff = contDiff_; }
00606 Ret zeros(const Ret &x) { return Ret(x.size()); }
00607 Ret operator()(const Arg &x) {
00608 for(unsigned int i=0; i<a.size(); i++)
00609 if(x<=a[i+1])
00610 return (*function[i])(x-a[i]);
00611 if(contDiff==0)
00612 return yEnd;
00613 else if(contDiff==1)
00614 return yEnd+ysEnd*(x-a[a.size()-1]);
00615 else
00616 return yEnd+(ysEnd+0.5*yssEnd*(x-a[a.size()-1]))*(x-a[a.size()-1]);
00617 }
00618 typename fmatvec::Der<Ret, double>::type parDer(const double &x) {
00619 for(unsigned int i=0; i<a.size(); i++)
00620 if(x<=a[i+1])
00621 return function[i]->parDer(x-a[i]);
00622 if(contDiff==0)
00623 return zeros(yEnd);
00624 else if(contDiff==1)
00625 return ysEnd;
00626 else
00627 return ysEnd+yssEnd*(x-a[a.size()-1]);
00628 }
00629 typename fmatvec::Der<typename fmatvec::Der<Ret, double>::type, double>::type parDerParDer(const double &x) {
00630 for(unsigned int i=0; i<a.size(); i++)
00631 if(x<=a[i+1])
00632 return function[i]->parDerParDer(x-a[i]);
00633 if(contDiff==0)
00634 return zeros(yEnd);
00635 else if(contDiff==1)
00636 return zeros(yEnd);
00637 else
00638 return yssEnd;
00639 }
00640
00641 void initializeUsingXML(xercesc::DOMElement *element) {
00642 xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"limitedFunctions");
00643 xercesc::DOMElement *ee=e->getFirstElementChild();
00644 while(ee && MBXMLUtils::E(ee)->getTagName()==MBSIM%"LimitedFunction") {
00645 function.push_back(ObjectFactory::createAndInit<Function<Ret(Arg)> >(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%"function")->getFirstElementChild()));
00646 a.push_back(Element::getDouble(MBXMLUtils::E(ee)->getFirstElementChildNamed(MBSIM%"limit")));
00647 ee=ee->getNextElementSibling();
00648 }
00649 e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"continouslyDifferentiable");
00650 if(e) contDiff=Element::getDouble(e);
00651 }
00652 void init(Element::InitStage stage) {
00653 Function<Ret(Arg)>::init(stage);
00654 for(typename std::vector<Function<Ret(Arg)> *>::iterator it=function.begin(); it!=function.end(); it++)
00655 (*it)->init(stage);
00656 if(stage==Element::preInit) {
00657 yEnd = (*function[function.size()-1])(a[a.size()-1]-a[a.size()-2]);
00658 if(contDiff>0) {
00659 ysEnd = function[function.size()-1]->parDer(a[a.size()-1]-a[a.size()-2]);
00660 if(contDiff>1)
00661 yssEnd = function[function.size()-1]->parDerParDer(a[a.size()-1]-a[a.size()-2]);
00662 }
00663 }
00664 }
00665 private:
00666 std::vector<Function<Ret(Arg)> *> function;
00667 std::vector<double> a;
00668 int contDiff;
00669 Ret yEnd, ysEnd, yssEnd;
00670 };
00671
00672 template<>
00673 inline double PiecewiseDefinedFunction<double(double)>::zeros(const double &x) { return 0; }
00674
00675 }
00676
00677 #endif
00678