fmatvec  2.0.0
Fast Matrix/Vector Library
function.h
1 #ifndef _FMATVEC_FUNCTION_H_
2 #define _FMATVEC_FUNCTION_H_
3 
4 #include <fmatvec/fmatvec.h>
5 #include <fmatvec/atom.h>
6 #include <stdexcept>
7 
8 namespace fmatvec {
9 
13 class ErrorType {
14 #if !defined(SWIG) && !defined(MBSIM_COMPILE_SWIG)
15  ErrorType() = delete;
16 #else
17  // SWIG instantiats everything. Hence, we allow this for SWIG but throw a runtime error
18  public:
19  ErrorType() {
20  throw std::runtime_error("Impossible type.");
21  }
22 #endif
23 };
24 
29 template<typename T>
30 struct StaticSize;
31 
33 template<>
34 struct StaticSize<int> {
35  enum { size1=1, size2=1 };
36 };
37 
39 template<>
40 struct StaticSize<double> {
41  enum { size1=1, size2=1 };
42 };
43 
45 template<typename Shape, typename AT>
46 struct StaticSize<Vector<Shape, AT>> {
47  enum { size1=0, size2=1 };
48 };
49 
51 template<typename Shape, typename AT>
52 struct StaticSize<RowVector<Shape, AT>> {
53  enum { size1=1, size2=0 };
54 };
55 
57 template<int N, typename AT>
58 struct StaticSize<Vector<Fixed<N>, AT>> {
59  enum { size1=N, size2=1 };
60 };
61 
63 template<int N, typename AT>
64 struct StaticSize<RowVector<Fixed<N>, AT>> {
65  enum { size1=1, size2=N };
66 };
67 
69 template<typename Storage, typename RowShape, typename ColShape, typename AT>
70 struct StaticSize<Matrix<Storage, RowShape, ColShape, AT>> {
71  enum { size1=0, size2=0 };
72 };
73 
75 template<typename Storage, int N, int M, typename AT>
76 struct StaticSize<Matrix<Storage, Fixed<N>, Fixed<M>, AT>> {
77  enum { size1=N, size2=M };
78 };
79 
81 template<typename Storage, int N, typename ColShape, typename AT>
82 struct StaticSize<Matrix<Storage, Fixed<N>, ColShape, AT>> {
83  enum { size1=N, size2=0 };
84 };
85 
87 template<typename Storage, typename RowShape, int M, typename AT>
88 struct StaticSize<Matrix<Storage, RowShape, Fixed<M>, AT>> {
89  enum { size1=0, size2=M };
90 };
91 
93 template<int N, typename AT>
94 struct StaticSize<SquareMatrix<Fixed<N>, AT>> {
95  enum { size1=N, size2=N };
96 };
97 
99 template<typename Shape, typename AT>
100 struct StaticSize<SquareMatrix<Shape, AT>> {
101  enum { size1=0, size2=0 };
102 };
103 
107 template<typename Dep, typename Indep>
108 struct Der {
109  typedef ErrorType type;
110 };
111 
115 template<typename Dep>
116 struct Der<Dep, double> {
117  typedef Dep type;
118 };
119 
125 template<typename IndepVecShape>
126 struct Der<double, Vector<IndepVecShape, double>> {
128 };
129 
135 template<typename DepVecShape, typename IndepVecShape>
136 struct Der<Vector<DepVecShape, double>, Vector<IndepVecShape, double>> {
138 };
139 
150 template<>
151 struct Der<Matrix<Rotation, Fixed<3>, Fixed<3>, double>, double> {
152  typedef Vector<Fixed<3>, double> type;
153 };
154 
169 template<typename IndepVecShape>
170 struct Der<Matrix<Rotation, Fixed<3>, Fixed<3>, double>, Vector<IndepVecShape, double>> {
171  typedef Matrix<General, Fixed<3>, IndepVecShape, double> type;
172 };
173 
177 template<typename Dep, typename Indep>
178 struct DirDer {
179  typedef Dep type;
180 };
181 
185 template<typename Indep>
186 struct DirDer<Matrix<Rotation, Fixed<3>, Fixed<3>, double>, Indep> {
187  typedef Vector<Fixed<3>, double> type;
188 };
189 
200 template<typename Sig>
201 class Function;
202 
204 template<typename Ret, typename Arg>
205 class Function<Ret(Arg)> : virtual public Atom {
206 
207  public:
208 
209  using DRetDArg = typename Der<Ret, Arg>::type;
210  using DRetDDir = typename DirDer<Ret, Arg>::type;
211  using DDRetDDArg = typename Der<DRetDArg, Arg>::type;
212 
214  enum { retSize1 = StaticSize<Ret>::size1, retSize2 = StaticSize<Ret>::size2 };
215 
217  static constexpr int argSize = StaticSize<Arg>::size1;
218 
220  virtual std::pair<int, int> getRetSize() const { return std::make_pair(retSize1, retSize2); }
221 
223  virtual int getArgSize() const { return argSize; }
224 
226  virtual Ret operator()(const Arg &arg)=0;
227 
229  virtual DRetDArg parDer(const Arg &arg) {
230  throw std::runtime_error("parDer must be overloaded by derived class.");
231  }
232 
234  virtual DRetDDir dirDer(const Arg &argDir, const Arg &arg) {
235  throw std::runtime_error("dirDer must be overloaded by derived class.");
236  }
237 
239  virtual DDRetDDArg parDerParDer(const Arg &arg) {
240  throw std::runtime_error("parDerParDer must be overloaded by derived class.");
241  }
242 
244  virtual DRetDArg parDerDirDer(const Arg &argDir, const Arg &arg) {
245  throw std::runtime_error("parDerDirDer must be overloaded by derived class.");
246  }
247 
249  virtual DRetDDir dirDerDirDer(const Arg &argDir_1, const Arg &argDir_2, const Arg &arg) {
250  throw std::runtime_error("dirDerDirDer must be overloaded by derived class.");
251  }
252 
254  // is constant.
255  virtual bool constParDer() const { return false; }
256 };
257 
259 template<typename Ret, typename Arg1, typename Arg2>
260 class Function<Ret(Arg1, Arg2)> : virtual public Atom {
261 
262  public:
263  using DRetDArg1 = typename Der<Ret, Arg1>::type;
264  using DRetDArg2 = typename Der<Ret, Arg2>::type;
265  using DRetDDir1 = typename DirDer<Ret, Arg1>::type;
266  using DRetDDir2 = typename DirDer<Ret, Arg2>::type;
267  using DDRetDDArg1 = typename Der<DRetDArg1, Arg1>::type;
268  using DDRetDDArg2 = typename Der<DRetDArg2, Arg2>::type;
269  using DDRetDArg1DArg2 = typename Der<DRetDArg1, Arg2>::type;
270 
272  enum { retSize1 = StaticSize<Ret>::size1, retSize2 = StaticSize<Ret>::size2 };
273 
275  static constexpr int arg1Size = StaticSize<Arg1>::size1;
276 
278  static constexpr int arg2Size = StaticSize<Arg2>::size1;
279 
281  virtual std::pair<int, int> getRetSize() const { return std::make_pair(retSize1, retSize2); }
282 
284  virtual int getArg1Size() const { return arg1Size; }
286  virtual int getArg2Size() const { return arg2Size; }
287 
289  virtual Ret operator()(const Arg1 &arg1, const Arg2 &arg2)=0;
290 
292  virtual DRetDArg1 parDer1(const Arg1 &arg1, const Arg2 &arg2) {
293  throw std::runtime_error("parDer1 must be overloaded by derived class.");
294  }
295 
297  virtual DRetDDir1 dirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
298  throw std::runtime_error("dirDer1 must be overloaded by derived class.");
299  }
300 
302  virtual DRetDArg2 parDer2(const Arg1 &arg1, const Arg2 &arg2) {
303  throw std::runtime_error("parDer2 must be overloaded by derived class.");
304  }
305 
307  virtual DRetDDir2 dirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
308  throw std::runtime_error("dirDer2 must be overloaded by derived class.");
309  }
310 
312  virtual DDRetDDArg1 parDer1ParDer1(const Arg1 &arg1, const Arg2 &arg2) {
313  throw std::runtime_error("parDer1ParDer1 must be overloaded by derived class.");
314  }
315 
317  virtual DRetDArg1 parDer1DirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
318  throw std::runtime_error("parDer1DirDer1 must be overloaded by derived class.");
319  }
320 
322  virtual DRetDDir1 dirDer1DirDer1(const Arg1 &arg1Dir_1, const Arg1 &arg1Dir_2, const Arg1 &arg1, const Arg2 &arg2) {
323  throw std::runtime_error("dirDer1DirDer1 must be overloaded by derived class.");
324  }
325 
327  virtual DDRetDDArg2 parDer2ParDer2(const Arg1 &arg1, const Arg2 &arg2) {
328  throw std::runtime_error("parDer2ParDer2 must be overloaded by derived class.");
329  }
330 
332  virtual DRetDArg2 parDer2DirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
333  throw std::runtime_error("parDer2DirDer2 must be overloaded by derived class.");
334  }
335 
337  virtual DRetDDir2 dirDer2DirDer2(const Arg2 &arg2Dir_1, const Arg2 &arg2Dir_2, const Arg1 &arg1, const Arg2 &arg2) {
338  throw std::runtime_error("dirDer2DirDer2 must be overloaded by derived class.");
339  }
340 
342  virtual DDRetDArg1DArg2 parDer1ParDer2(const Arg1 &arg1, const Arg2 &arg2) {
343  throw std::runtime_error("parDer1ParDer2 must be overloaded by derived class.");
344  }
345 
347  virtual DRetDArg1 parDer1DirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
348  throw std::runtime_error("parDer1DirDer2 must be overloaded by derived class.");
349  }
350 
352  virtual DRetDDir2 dirDer2DirDer1(const Arg2 &arg2Dir, const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
353  throw std::runtime_error("dirDer2DirDer1 must be overloaded by derived class.");
354  }
355 
357  virtual DRetDArg2 parDer2DirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
358  throw std::runtime_error("parDer2DirDer1 must be overloaded by derived class.");
359  }
360 
362  // is constant.
363  virtual bool constParDer1() const { return false; }
364 
366  // is constant.
367  virtual bool constParDer2() const { return false; }
368 };
369 
370 // A function object with 3, 4, 5, ... arguments
371 // Not required till now!
372 
373 }
374 
375 #endif
virtual DDRetDArg1DArg2 parDer1ParDer2(const Arg1 &arg1, const Arg2 &arg2)
Second mixed derivative: partial derivative of parDer1 with respect to the second argument.
Definition: function.h:342
virtual DRetDDir2 dirDer2DirDer2(const Arg2 &arg2Dir_1, const Arg2 &arg2Dir_2, const Arg1 &arg1, const Arg2 &arg2)
Second derivative: directional derivative of dirDer2 with respect to the first argument.
Definition: function.h:337
Definition: fmatvec.h:38
virtual std::pair< int, int > getRetSize() const
Return the size of the return value: =0 == unknown size.
Definition: function.h:220
virtual DDRetDDArg2 parDer2ParDer2(const Arg1 &arg1, const Arg2 &arg2)
Second derivative: partial derivative of parDer2 with respect to the first argument.
Definition: function.h:327
virtual DRetDArg parDer(const Arg &arg)
First derivative: partial derivative of the function value with respect to the argument.
Definition: function.h:229
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
virtual DRetDDir1 dirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2)
First derivative: directional derivative of the function value with respect to the first argument.
Definition: function.h:297
virtual bool constParDer() const
Returns true, if the partial derivative of the function value with respect to the argument.
Definition: function.h:255
virtual DRetDArg1 parDer1DirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2)
Second derivative: directional derivative of parDer1 with respect to the first argument.
Definition: function.h:317
Definition: atom.h:21
virtual DRetDDir2 dirDer2DirDer1(const Arg2 &arg2Dir, const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2)
Second mixed derivative: directional derivative of dirDer2 with respect to the first argument.
Definition: function.h:352
Definition: fmatvec.h:50
virtual DRetDDir dirDer(const Arg &argDir, const Arg &arg)
First derivative: directional derivative of the function value with respect to the argument.
Definition: function.h:234
Shape class for rotation matrices.
Definition: types.h:156
Definition: function.h:201
Definition: function.h:13
Definition: function.h:108
virtual int getArgSize() const
Return the size of the argument: =1 == scalar; >1 == vector; =0 == unknown vector size.
Definition: function.h:223
virtual DRetDDir dirDerDirDer(const Arg &argDir_1, const Arg &argDir_2, const Arg &arg)
Second derivative: directional derivative of dirDer with respect to the argument.
Definition: function.h:249
virtual DRetDDir1 dirDer1DirDer1(const Arg1 &arg1Dir_1, const Arg1 &arg1Dir_2, const Arg1 &arg1, const Arg2 &arg2)
Second derivative: directional derivative of dirDer1 with respect to the first argument.
Definition: function.h:322
virtual int getArg2Size() const
Return the size of the second argument: =1 == scalar; >1 == vector; =0 == unknown vector size.
Definition: function.h:286
Definition: fmatvec.h:44
Definition: fmatvec.h:47
virtual DRetDArg1 parDer1DirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2)
Second mixed derivative: directional derivative of parDer1 with respect to the second argument.
Definition: function.h:347
virtual DRetDArg2 parDer2DirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2)
Second derivative: directional derivative of parDer2 with respect to the first argument.
Definition: function.h:332
virtual int getArg1Size() const
Return the size of the first argument: =1 == scalar; >1 == vector; =0 == unknown vector size.
Definition: function.h:284
Namespace fmatvec.
Definition: _memory.cc:28
virtual std::pair< int, int > getRetSize() const
Return the size of the return value: =0 == unknown size.
Definition: function.h:281
virtual DDRetDDArg1 parDer1ParDer1(const Arg1 &arg1, const Arg2 &arg2)
Second derivative: partial derivative of parDer1 with respect to the first argument.
Definition: function.h:312
Definition: function.h:30
virtual bool constParDer1() const
Returns true, if the partial derivative of the function value with respect to the first argument.
Definition: function.h:363
virtual DRetDArg parDerDirDer(const Arg &argDir, const Arg &arg)
Second derivative: directional derivative of parDer with respect to the argument.
Definition: function.h:244
virtual DRetDArg2 parDer2(const Arg1 &arg1, const Arg2 &arg2)
First derivative: partial derivative of the function value with respect to the second argument.
Definition: function.h:302
virtual DRetDDir2 dirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2)
First derivative: directional derivative of the function value with respect to the second argument.
Definition: function.h:307
virtual DDRetDDArg parDerParDer(const Arg &arg)
Second derivative: partial derivative of parDer with respect to the argument.
Definition: function.h:239
virtual bool constParDer2() const
Returns true, if the partial derivative of the function value with respect to the second argument.
Definition: function.h:367
virtual DRetDArg1 parDer1(const Arg1 &arg1, const Arg2 &arg2)
First derivative: partial derivative of the function value with respect to the first argument.
Definition: function.h:292
Definition: function.h:178
virtual DRetDArg2 parDer2DirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2)
Second mixed derivative: partial derivative of dirDer1 with respect to the second argument.
Definition: function.h:357