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 
25 namespace MBSim {
26 
27  template<typename Sig> class BidirectionalFunction;
28 
29  template<typename Ret, typename Arg>
30  class BidirectionalFunction<Ret(Arg)> : public Function<Ret(Arg)> {
32  public:
33  BidirectionalFunction(Function<Ret(Arg)> *fn_=0, Function<Ret(Arg)> *fp_=0) : fn(fn_), fp(fp_) {
34  if(fn) fn->setParent(this);
35  if(fp) fp->setParent(this);
36  }
38  delete fn;
39  delete fp;
40  }
41  int getArgSize() const {
42  return fn->getArgSize();
43  }
44  Ret operator()(const Arg &x) {
45  return (ToDouble<Arg>::cast(x)>=0)?(*fp)(x):(*fn)(-x);
46  }
47  typename B::DRetDArg parDer(const double &x) {
48  return (ToDouble<Arg>::cast(x)>=0)?fp->parDer(x):fn->parDer(-x);
49  }
50  typename B::DRetDArg parDerDirDer(const Arg &xDir, const Arg &x) {
51  return (ToDouble<Arg>::cast(x)>=0)?fp->parDerDirDer(xDir,x):fn->parDerDirDer(-xDir,-x);
52  }
53  typename B::DDRetDDArg parDerParDer(const double &x) {
54  return (ToDouble<Arg>::cast(x)>=0)?fp->parDerParDer(x):fn->parDerParDer(-x);
55  }
56  void setNegativeDirectionalFunction(Function<Ret(Arg)> *fn_) {
57  fn = fn_;
58  fn->setParent(this);
59  fn->setName("NegativeDirectional");
60  }
61  void setPositiveDirectionalFunction(Function<Ret(Arg)> *fp_) {
62  fp = fp_;
63  fp->setParent(this);
64  fp->setName("PostiveDirectional");
65  }
66  void initializeUsingXML(xercesc::DOMElement *element) {
67  xercesc::DOMElement *e;
68  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"negativeDirectionalFunction");
69  setNegativeDirectionalFunction(ObjectFactory::createAndInit<Function<Ret(Arg)> >(e->getFirstElementChild()));
70  e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"positiveDirectionalFunction");
71  setPositiveDirectionalFunction(ObjectFactory::createAndInit<Function<Ret(Arg)> >(e->getFirstElementChild()));
72  }
73  void init(Element::InitStage stage) {
75  fn->init(stage);
76  fp->init(stage);
77  }
78  private:
79  Function<Ret(Arg)> *fn, *fp;
80  };
81 
82 }
83 
84 #endif
Definition: utils.h:153
InitStage
The stages of the initialization.
Definition: element.h:97
Definition: planar_contour.h:31
void init(Element::InitStage stage)
plots time series header
Definition: bidirectional_function.h:73
virtual void init(InitStage stage)
plots time series header
Definition: element.cc:70
Definition: bidirectional_function.h:27
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:87

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Valid HTML