mbsim 4.0.0 MBSim Kernel

polynom_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 _POLYNOM_FUNCTION_H_
21 #define _POLYNOM_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 PolynomFunction;
29
30 template<typename Ret, typename Arg>
31 class PolynomFunction<Ret(Arg)> : public Function<Ret(Arg)> {
32 using B = fmatvec::Function<Ret(Arg)>;
33 public:
34 PolynomFunction() = default;
35 PolynomFunction(const fmatvec::VecV &a_) : a(a_) { }
36 void init(Element::InitStage stage, const InitConfigSet &config) override {
37 Function<Ret(Arg)>::init(stage, config);
38 if(stage == Element::preInit) {
39 ad.resize(a.size()-1);
40 add.resize(ad.size()-1);
41 for(int i=1; i<a.size(); i++)
42 ad.e(i-1) = double(i)*a.e(i);
43 for(int i=1; i<ad.size(); i++)
44 add.e(i-1) = double(i)*ad(i);
45 }
46 }
47 int getArgSize() const override { return 1; }
48 std::pair<int, int> getRetSize() const override { return std::make_pair(1,1); }
49 Ret operator()(const Arg &x_) override {
50 double x = ToDouble<Arg>::cast(x_);
51 double value=a(a.size()-1);
52 for (int i=int(a.size())-2; i>-1; i--)
53 value=value*x+a.e(i);
54 return FromDouble<Ret>::cast(value);
55 }
56 typename B::DRetDArg parDer(const Arg &x_) override {
57 double x = ToDouble<Arg>::cast(x_);
58 double value=ad(ad.size()-1);
59 for (int i=int(ad.size())-2; i>-1; i--)
60 value=value*x+ad.e(i);
61 return FromDouble<typename B::DRetDArg>::cast(value);
62 }
63 typename B::DRetDArg parDerDirDer(const Arg &xDir_, const Arg &x_) override {
64 double x = ToDouble<Arg>::cast(x_);
65 double xDir = ToDouble<Arg>::cast(xDir_);
66 double value=add(add.size()-1);
67 for (int i=int(add.size())-2; i>-1; i--)
68 value=value*x+add.e(i);
69 return FromDouble<typename B::DRetDArg>::cast(value*xDir);
70 }
71 void initializeUsingXML(xercesc::DOMElement *element) override {
72 setCoefficients(MBXMLUtils::E(MBXMLUtils::E(element)->getFirstElementChildNamed(MBSIM%"coefficients"))->getText<fmatvec::Vec>());
73 }
74 void setCoefficients(const fmatvec::VecV &a_) { a <<= a_; }
75 private:
76 fmatvec::VecV a, ad, add;
77 };
78
79 }
80
81 #endif
MBSim::PolynomFunction< Ret(Arg)>::init
void init(Element::InitStage stage, const InitConfigSet &config) override
plots time series header
Definition: polynom_function.h:36
MBSim::PolynomFunction
Definition: polynom_function.h:28
MBSim::Element::InitStage
InitStage
The stages of the initialization.
Definition: element.h:62
MBSim::Function
Definition: contact_kinematics.h:32
MBSim::Element::preInit
Definition: element.h:64
MBSim
namespace MBSim
Definition: bilateral_constraint.cc:30