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_=nullptr, Function<Argo(double)> *fi_=nullptr) : fo(fo_), fi(fi_) {
35  if(fo)
36  fo->setParent(this);
37  if(fi)
38  fi->setParent(this);
39  }
40  ~CompositeFunction() override {
41  delete fo;
42  delete fi;
43  }
44  int getArgSize() const override {
45  return fi->getArgSize();
46  }
47  std::pair<int, int> getRetSize() const override {
48  return fo->getRetSize();
49  }
50  Ret operator()(const double &arg) override {
51  return (*fo)((*fi)(arg));
52  }
53  typename B::DRetDArg parDer(const double &arg) override {
54  return fo->parDer((*fi)(arg))*fi->parDer(arg);
55  }
56  typename B::DRetDArg parDerDirDer(const double &argDir, const double &arg) override {
57  return fo->parDerDirDer(fi->parDer(arg)*argDir,(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerDirDer(argDir,arg);
58  }
59  typename B::DDRetDDArg parDerParDer(const double &arg) override {
60  return fo->parDerDirDer(fi->parDer(arg),(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerParDer(arg);
61  }
62  void setOuterFunction(Function<Ret(Argo)> *fo_) {
63  fo = fo_;
64  fo->setParent(this);
65  fo->setName("Outer");
66  }
67  void setInnerFunction(Function<Argo(double)> *fi_) {
68  fi = fi_;
69  fi->setParent(this);
70  fi->setName("Inner");
71  }
72  void initializeUsingXML(xercesc::DOMElement *element) override {
73  xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"outerFunction");
74  setOuterFunction(ObjectFactory::createAndInit<Function<Ret(Argo)> >(e->getFirstElementChild()));
75  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"innerFunction");
76  setInnerFunction(ObjectFactory::createAndInit<Function<Argo(double)> >(e->getFirstElementChild()));
77  }
78  void init(Element::InitStage stage, const InitConfigSet &config) override {
79  Function<Ret(double)>::init(stage, config);
80  fo->init(stage, config);
81  fi->init(stage, config);
82  }
83  private:
86  };
87 
88  // VectorValuedFunction with a vector as inner argument (no 2nd derivative defined)
89  template<typename Ret, typename Argo, typename Argi>
90  class CompositeFunction<Ret(Argo(Argi))> : public Function<Ret(Argi)> {
91  using B = Function<Ret(Argi)>;
92  public:
93  CompositeFunction(Function<Ret(Argo)> *fo_=nullptr, Function<Argo(Argi)> *fi_=nullptr) : fo(fo_), fi(fi_) {
94  if(fo)
95  fo->setParent(this);
96  if(fi)
97  fi->setParent(this);
98  }
99  ~CompositeFunction() override {
100  delete fo;
101  delete fi;
102  }
103  int getArgSize() const override {
104  return fi->getArgSize();
105  }
106  std::pair<int, int> getRetSize() const override {
107  return fo->getRetSize();
108  }
109  Ret operator()(const Argi &arg) override {
110  return (*fo)((*fi)(arg));
111  }
112  typename B::DRetDArg parDer(const Argi &arg) override {
113  return fo->parDer((*fi)(arg))*fi->parDer(arg);
114  }
115  typename B::DRetDArg parDerDirDer(const Argi &argDir, const Argi &arg) override {
116  return fo->parDerDirDer(fi->parDer(arg)*argDir,(*fi)(arg))*fi->parDer(arg) + fo->parDer((*fi)(arg))*fi->parDerDirDer(argDir,arg);
117  }
118  void setOuterFunction(Function<Ret(Argo)> *fo_) {
119  fo = fo_;
120  fo->setParent(this);
121  fo->setName("Outer");
122  }
123  void setInnerFunction(Function<Argo(Argi)> *fi_) {
124  fi = fi_;
125  fi->setParent(this);
126  fi->setName("Inner");
127  }
128  void initializeUsingXML(xercesc::DOMElement *element) override {
129  xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"outerFunction");
130  setOuterFunction(ObjectFactory::createAndInit<Function<Ret(Argo)> >(e->getFirstElementChild()));
131  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"innerFunction");
132  setInnerFunction(ObjectFactory::createAndInit<Function<Argo(Argi)> >(e->getFirstElementChild()));
133  }
134  void init(Element::InitStage stage, const InitConfigSet &config) override {
135  Function<Ret(Argi)>::init(stage, config);
136  fo->init(stage, config);
137  fi->init(stage, config);
138  }
139  private:
142  };
143 }
144 
145 #endif
virtual void init(InitStage stage, const InitConfigSet &config)
plots time series header
Definition: element.cc:86
Definition: composite_function.h:27
InitStage
The stages of the initialization.
Definition: element.h:62
Definition: planar_contour.h:30
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: composite_function.h:134
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:97
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: composite_function.h:78

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home