All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
composite_function.h
1 /* Copyright (C) 2004-2014 MBSim Development Team
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact: martin.o.foerg@gmail.com
18  */
19 
20 #ifndef _NESTED_FUNCTION_H_
21 #define _NESTED_FUNCTION_H_
22 
23 #include "mbsim/functions/function.h"
24 
25 namespace MBSim {
26 
27  template<typename Sig> class CompositeFunction;
28 
29  // CompositeFunction with a double as inner argument (including 2nd derivative)
30  template<typename Ret, typename Argo>
31  class CompositeFunction<Ret(Argo(double))> : public Function<Ret(double)> {
32  using B = Function<Ret(double)>;
33  public:
34  CompositeFunction(Function<Ret(Argo)> *fo_=0, Function<Argo(double)> *fi_=0) : fo(fo_), fi(fi_) {
35  if(fo)
36  fo->setParent(this);
37  if(fi)
38  fi->setParent(this);
39  }
41  delete fo;
42  delete fi;
43  }
44  int getArgSize() const {
45  return fi->getArgSize();
46  }
47  Ret operator()(const double &arg) {
48  return (*fo)((*fi)(arg));
49  }
50  typename B::DRetDArg parDer(const double &arg) {
51  return fo->parDer((*fi)(arg))*fi->parDer(arg);
52  }
53  typename B::DRetDArg parDerDirDer(const double &argDir, const double &arg) {
54  return fo->parDerDirDer(fi->parDer(arg)*argDir,(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerDirDer(argDir,arg);
55  }
56  typename B::DDRetDDArg parDerParDer(const double &arg) {
57  return fo->parDerDirDer(fi->parDer(arg),(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerParDer(arg);
58  }
59  void setOuterFunction(Function<Ret(Argo)> *fo_) {
60  fo = fo_;
61  fo->setParent(this);
62  fo->setName("Outer");
63  }
64  void setInnerFunction(Function<Argo(double)> *fi_) {
65  fi = fi_;
66  fi->setParent(this);
67  fi->setName("Inner");
68  }
69  void initializeUsingXML(xercesc::DOMElement *element) {
70  xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"outerFunction");
71  setOuterFunction(ObjectFactory::createAndInit<Function<Ret(Argo)> >(e->getFirstElementChild()));
72  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"innerFunction");
73  setInnerFunction(ObjectFactory::createAndInit<Function<Argo(double)> >(e->getFirstElementChild()));
74  }
75  void init(Element::InitStage stage) {
77  fo->init(stage);
78  fi->init(stage);
79  }
80  private:
83  };
84 
85  // VectorValuedFunction with a vector as inner argument (no 2nd derivative defined)
86  template<typename Ret, typename Argo, typename Argi>
87  class CompositeFunction<Ret(Argo(Argi))> : public Function<Ret(Argi)> {
88  using B = Function<Ret(Argi)>;
89  public:
90  CompositeFunction(Function<Ret(Argo)> *fo_=0, Function<Argo(Argi)> *fi_=0) : fo(fo_), fi(fi_) {
91  if(fo)
92  fo->setParent(this);
93  if(fi)
94  fi->setParent(this);
95  }
97  delete fo;
98  delete fi;
99  }
100  int getArgSize() const {
101  return fi->getArgSize();
102  }
103  Ret operator()(const Argi &arg) {
104  return (*fo)((*fi)(arg));
105  }
106  typename B::DRetDArg parDer(const Argi &arg) {
107  return fo->parDer((*fi)(arg))*fi->parDer(arg);
108  }
109  typename B::DRetDArg parDerDirDer(const Argi &argDir, const Argi &arg) {
110  return fo->parDerDirDer(fi->parDer(arg)*argDir,(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerDirDer(argDir,arg);
111  }
112  void setOuterFunction(Function<Ret(Argo)> *fo_) {
113  fo = fo_;
114  fo->setParent(this);
115  fo->setName("Outer");
116  }
117  void setInnerFunction(Function<Argo(Argi)> *fi_) {
118  fi = fi_;
119  fi->setParent(this);
120  fi->setName("Inner");
121  }
122  void initializeUsingXML(xercesc::DOMElement *element) {
123  xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"outerFunction");
124  setOuterFunction(ObjectFactory::createAndInit<Function<Ret(Argo)> >(e->getFirstElementChild()));
125  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"innerFunction");
126  setInnerFunction(ObjectFactory::createAndInit<Function<Argo(Argi)> >(e->getFirstElementChild()));
127  }
128  void init(Element::InitStage stage) {
130  fo->init(stage);
131  fi->init(stage);
132  }
133  private:
136  };
137 }
138 
139 #endif
void init(Element::InitStage stage)
plots time series header
Definition: composite_function.h:75
Definition: composite_function.h:27
InitStage
The stages of the initialization.
Definition: element.h:97
Definition: planar_contour.h:31
virtual void init(InitStage stage)
plots time series header
Definition: element.cc:70
void init(Element::InitStage stage)
plots time series header
Definition: composite_function.h:128
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:87

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML