MBSim

Project Page
ppolynom.h
1 /* Copyright (C) 2004-2009 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@googlemail.com
18  */
19 
20 #ifndef PPOLYNOM
21 #define PPOLYNOM
22 
23 #include "fmatvec.h"
24 #include "mbsim/utils/function.h"
25 
26 namespace MBSim {
27 
57  class PPolynom : public DifferentiableFunction1<fmatvec::Vec> {
58  public:
63 
67  virtual ~PPolynom() {}
68 
81  void setXF(const fmatvec::Vec &x, const fmatvec::Mat &f, std::string InterpolationMethod);
82 
86  std::vector<fmatvec::Mat> getCoefs();
87 
91  fmatvec::Vec getBreaks();
92 
98  void setPP(const std::vector<fmatvec::Mat> &coefs_u, const fmatvec::Vec &breaks_u);
99 
104  virtual void initializeUsingXML(TiXmlElement *element);
105 
106  protected:
110  std::vector<fmatvec::Mat> coefs;
111 
115  fmatvec::Vec breaks;
116 
120  int nPoly;
121 
125  int order;
126 
130  int index;
131 
137  void calculateSplinePeriodic(const fmatvec::Vec &x, const fmatvec::Mat &f);
138 
144  void calculateSplineNatural(const fmatvec::Vec &x, const fmatvec::Mat &f);
145 
146  /*
147  * \brief calculation of piecewise linear interpolation
148  * \param interpolated arguments
149  * \param interpolated function values
150  *
151  * the first derivative is weak and the second derivative is zero elsewhere although it should be distributionally at the corners
152  */
153  void calculatePLinear(const fmatvec::Vec &x, const fmatvec::Mat &f);
154 
158  class ZerothDerivative : public Function1<fmatvec::Vec,double> {
159  public:
160  ZerothDerivative(PPolynom *polynom) : Function1<fmatvec::Vec,double>(), parent(polynom), xSave(0), ySave(fmatvec::Vec(0)) {}
161  virtual ~ZerothDerivative() {}
162 
163  /* INHERITED INTERFACE OF FUNCTION */
164  fmatvec::Vec operator()(const double& x, const void * =NULL);
165  /***************************************************/
166 
167  private:
168  PPolynom *parent;
169  double xSave;
170  fmatvec::Vec ySave;
171  };
172 
176  class FirstDerivative : public Function1<fmatvec::Vec,double> {
177  public:
178  FirstDerivative(PPolynom *polynom) : Function1<fmatvec::Vec,double>(), parent(polynom), xSave(0), ySave(fmatvec::Vec(0)) {}
179  virtual ~FirstDerivative() {}
180 
181  /* INHERITED INTERFACE OF FUNCTION */
182  fmatvec::Vec operator()(const double& x, const void * =NULL);
183  /***************************************************/
184 
185  private:
186  PPolynom *parent;
187  double xSave;
188  fmatvec::Vec ySave;
189  };
190 
194  class SecondDerivative : public Function1<fmatvec::Vec,double> {
195  public:
196  SecondDerivative(PPolynom *polynom) : Function1<fmatvec::Vec,double>(), parent(polynom), xSave(0), ySave(fmatvec::Vec(0)) {}
197  virtual ~SecondDerivative() {}
198 
199  /* INHERITED INTERFACE OF FUNCTION */
200  fmatvec::Vec operator()(const double& x, const void * =NULL);
201  /***************************************************/
202 
203  private:
204  PPolynom *parent;
205  double xSave;
206  fmatvec::Vec ySave;
207  };
208  };
209 
210  inline std::vector<fmatvec::Mat> PPolynom::getCoefs() { return coefs; }
211  inline fmatvec::Vec PPolynom::getBreaks() { return breaks; }
212  inline void PPolynom::setPP(const std::vector<fmatvec::Mat> &coefs_u, const fmatvec::Vec &breaks_u) {
213  coefs = coefs_u;
214  breaks = breaks_u;
215  index = 0;
216  nPoly = (coefs[0]).rows();
217  order = coefs.size()-1;
218  }
219 
220 }
221 
222 #endif /* PPOLYNOM */
223