All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
bidirectional_function.h
1 /* Copyright (C) 2004-2016 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 _BIDIRECTIONAL_FUNCTION_H_
21 #define _BIDIRECTIONAL_FUNCTION_H_
22 
23 #include "mbsim/functions/function.h"
24 #include "mbsim/utils/utils.h"
25 
26 namespace MBSim {
27 
28  template<typename Sig> class BidirectionalFunction;
29 
30  template<typename Ret, typename Arg>
31  class BidirectionalFunction<Ret(Arg)> : public Function<Ret(Arg)> {
33  public:
34  BidirectionalFunction(Function<Ret(Arg)> *fn_=nullptr, Function<Ret(Arg)> *fp_=nullptr) : fn(fn_), fp(fp_) {
35  if(fn) fn->setParent(this);
36  if(fp) fp->setParent(this);
37  }
38  ~BidirectionalFunction() override {
39  delete fn;
40  delete fp;
41  }
42  int getArgSize() const override {
43  return fn->getArgSize();
44  }
45  std::pair<int, int> getRetSize() const override {
46  return fp->getRetSize();
47  }
48  Ret operator()(const Arg &x) override {
49  return (ToDouble<Arg>::cast(x)>=0)?(*fp)(x):(*fn)(-x);
50  }
51  typename B::DRetDArg parDer(const double &x) override {
52  return (ToDouble<Arg>::cast(x)>=0)?fp->parDer(x):fn->parDer(-x);
53  }
54  typename B::DRetDArg parDerDirDer(const Arg &xDir, const Arg &x) override {
55  return (ToDouble<Arg>::cast(x)>=0)?fp->parDerDirDer(xDir,x):fn->parDerDirDer(-xDir,-x);
56  }
57  typename B::DDRetDDArg parDerParDer(const double &x) override {
58  return (ToDouble<Arg>::cast(x)>=0)?fp->parDerParDer(x):fn->parDerParDer(-x);
59  }
60  void setNegativeDirectionalFunction(Function<Ret(Arg)> *fn_) {
61  fn = fn_;
62  fn->setParent(this);
63  fn->setName("NegativeDirectional");
64  }
65  void setPositiveDirectionalFunction(Function<Ret(Arg)> *fp_) {
66  fp = fp_;
67  fp->setParent(this);
68  fp->setName("PostiveDirectional");
69  }
70  void initializeUsingXML(xercesc::DOMElement *element) override {
71  xercesc::DOMElement *e;
72  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"negativeDirectionalFunction");
73  setNegativeDirectionalFunction(ObjectFactory::createAndInit<Function<Ret(Arg)> >(e->getFirstElementChild()));
74  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"positiveDirectionalFunction");
75  setPositiveDirectionalFunction(ObjectFactory::createAndInit<Function<Ret(Arg)> >(e->getFirstElementChild()));
76  }
77  void init(Element::InitStage stage, const InitConfigSet &config) override {
78  Function<Ret(Arg)>::init(stage, config);
79  fn->init(stage, config);
80  fp->init(stage, config);
81  }
82  private:
83  Function<Ret(Arg)> *fn, *fp;
84  };
85 
86 }
87 
88 #endif
virtual void init(InitStage stage, const InitConfigSet &config)
plots time series header
Definition: element.cc:86
Definition: utils.h:61
InitStage
The stages of the initialization.
Definition: element.h:62
Definition: planar_contour.h:30
Definition: bidirectional_function.h:28
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: bidirectional_function.h:77

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home