All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
vector_valued_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 _VECTOR_VALUED_FUNCTIONS_H_
21 #define _VECTOR_VALUED_FUNCTIONS_H_
22 
23 #include "mbsim/functions/function.h"
24 
25 namespace MBSim {
26 
27  template<typename Sig> class VectorValuedFunction;
28 
29  // VectorValuedFunction with a double as argument (including 2nd derivative)
30  template<typename Ret>
31  class VectorValuedFunction<Ret(double)> : public Function<Ret(double)> {
33  public:
34  VectorValuedFunction() = default;
35  VectorValuedFunction(std::vector<Function<double(double)> *> component_) : component(std::move(component_)) {
36  for(auto it=component.begin(); it!=component.end(); ++it)
37  (*it)->setParent(this);
38  }
39  ~VectorValuedFunction() override {
40  for (unsigned int i=0; i<component.size(); i++)
41  delete component[i];
42  }
43  void addComponent(Function<double(double)> *function) {
44  component.push_back(function);
45  function->setParent(this);
46  }
47  int getArgSize() const override { return 1; }
48  std::pair<int, int> getRetSize() const override { return std::make_pair(component.size(),1); }
49  Ret operator()(const double &x) override {
50  Ret y(component.size(),fmatvec::NONINIT);
51  for (unsigned int i=0; i<component.size(); i++)
52  y(i)=(*component[i])(x);
53  return y;
54  }
55  typename B::DRetDArg parDer(const double &x) override {
56  typename B::DRetDArg y(component.size(),fmatvec::NONINIT);
57  for (unsigned int i=0; i<component.size(); i++)
58  y(i)=component[i]->parDer(x);
59  return y;
60  }
61  typename B::DDRetDDArg parDerParDer(const double &x) override {
62  typename B::DDRetDDArg y(component.size(),fmatvec::NONINIT);
63  for (unsigned int i=0; i<component.size(); i++)
64  y(i)=component[i]->parDerParDer(x);
65  return y;
66  }
67 
68  void initializeUsingXML(xercesc::DOMElement *element) override {
69  xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"components")->getFirstElementChild();
70  while (e) {
71  addComponent(ObjectFactory::createAndInit<Function<double(double)> >(e));
72  e=e->getNextElementSibling();
73  }
74  }
75  void init(Element::InitStage stage, const InitConfigSet &config) override {
76  Function<Ret(double)>::init(stage, config);
77  for(auto it=component.begin(); it!=component.end(); ++it)
78  (*it)->init(stage, config);
79  }
80  private:
81  std::vector<Function<double(double)> *> component;
82  };
83 
84  // VectorValuedFunction with a vector as argument (no 2nd derivative defined)
85  template<typename Ret, typename Arg>
86  class VectorValuedFunction<Ret(Arg)> : public Function<Ret(Arg)> {
88  public:
89  VectorValuedFunction() = default;
90  VectorValuedFunction(std::vector<Function<double(Arg)> *> component_) : component(std::move(component_)) {
91  for(auto it=component.begin(); it!=component.end(); ++it)
92  (*it)->setParent(this);
93  }
94  ~VectorValuedFunction() override {
95  for (unsigned int i=0; i<component.size(); i++)
96  delete component[i];
97  }
98  void addComponent(Function<double(Arg)> *function) {
99  component.push_back(function);
100  function->setParent(this);
101  }
102  int getArgSize() const override { return 1; }
103  std::pair<int, int> getRetSize() const override { return std::make_pair(component.size(),1); }
104  Ret operator()(const Arg &x) override {
105  Ret y(component.size(),fmatvec::NONINIT);
106  for (unsigned int i=0; i<component.size(); i++)
107  y(i)=(*component[i])(x);
108  return y;
109  }
110  typename B::DRetDArg parDer(const Arg &x) override {
111  typename B::DRetDArg y(component.size(),x.size(),fmatvec::NONINIT);
112  for (unsigned int i=0; i<component.size(); i++) {
113  auto row=component[i]->parDer(x);
114  for (int j=0; j<x.size(); j++)
115  y(i,j)=row(j);
116  }
117  return y;
118  }
119 
120  void initializeUsingXML(xercesc::DOMElement *element) override {
121  xercesc::DOMElement *e=MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"components")->getFirstElementChild();
122  while (e) {
123  addComponent(ObjectFactory::createAndInit<Function<double(Arg)> >(e));
124  e=e->getNextElementSibling();
125  }
126  }
127  void init(Element::InitStage stage, const InitConfigSet &config) override {
128  Function<Ret(Arg)>::init(stage, config);
129  for(auto it=component.begin(); it!=component.end(); ++it)
130  (*it)->init(stage, config);
131  }
132  private:
133  std::vector<Function<double(Arg)> *> component;
134  };
135 
136 }
137 
138 #endif
virtual void init(InitStage stage, const InitConfigSet &config)
plots time series header
Definition: element.cc:86
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: vector_valued_function.h:75
InitStage
The stages of the initialization.
Definition: element.h:62
Definition: vector_valued_function.h:27
Definition: planar_contour.h:30
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: vector_valued_function.h:127
static ContainerType * createAndInit(const xercesc::DOMElement *element)
Definition: objectfactory.h:97

Impressum / Disclaimer / Datenschutz Generated by doxygen 1.8.5 Home