fmatvec  0.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  if constexpr (std::is_same_v<DRetDArg, ErrorType>)
236  throw std::runtime_error("dirDer must be overloaded by derived class.");
237  else
238  return parDer(arg) * argDir;
239  }
240 
242  virtual DDRetDDArg parDerParDer(const Arg &arg) {
243  throw std::runtime_error("parDerParDer must be overloaded by derived class.");
244  }
245 
247  virtual DRetDArg parDerDirDer(const Arg &argDir, const Arg &arg) {
248  if constexpr (std::is_same_v<DDRetDDArg, ErrorType>)
249  throw std::runtime_error("parDerDirDer must be overloaded by derived class.");
250  else
251  return parDerParDer(arg) * argDir;
252  }
253 
255  virtual DRetDDir dirDerDirDer(const Arg &argDir_1, const Arg &argDir_2, const Arg &arg) {
256  if constexpr (std::is_same_v<DRetDArg, ErrorType>)
257  throw std::runtime_error("dirDerDirDer must be overloaded by derived class.");
258  else
259  return parDerDirDer(argDir_2, arg) * argDir_1;
260  }
261 
263  // is constant.
264  virtual bool constParDer() const { return false; }
265 };
266 
268 template<typename Ret, typename Arg1, typename Arg2>
269 class Function<Ret(Arg1, Arg2)> : virtual public Atom {
270 
271  public:
272  using DRetDArg1 = typename Der<Ret, Arg1>::type;
273  using DRetDArg2 = typename Der<Ret, Arg2>::type;
274  using DRetDDir1 = typename DirDer<Ret, Arg1>::type;
275  using DRetDDir2 = typename DirDer<Ret, Arg2>::type;
276  using DDRetDDArg1 = typename Der<DRetDArg1, Arg1>::type;
277  using DDRetDDArg2 = typename Der<DRetDArg2, Arg2>::type;
278  using DDRetDArg1DArg2 = typename Der<DRetDArg1, Arg2>::type;
279 
281  enum { retSize1 = StaticSize<Ret>::size1, retSize2 = StaticSize<Ret>::size2 };
282 
284  static constexpr int arg1Size = StaticSize<Arg1>::size1;
285 
287  static constexpr int arg2Size = StaticSize<Arg2>::size1;
288 
290  virtual std::pair<int, int> getRetSize() const { return std::make_pair(retSize1, retSize2); }
291 
293  virtual int getArg1Size() const { return arg1Size; }
295  virtual int getArg2Size() const { return arg2Size; }
296 
298  virtual Ret operator()(const Arg1 &arg1, const Arg2 &arg2)=0;
299 
301  virtual DRetDArg1 parDer1(const Arg1 &arg1, const Arg2 &arg2) {
302  throw std::runtime_error("parDer1 must be overloaded by derived class.");
303  }
304 
306  virtual DRetDDir1 dirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
307  if constexpr (std::is_same_v<DRetDArg1, ErrorType>)
308  throw std::runtime_error("dirDer1 must be overloaded by derived class.");
309  else
310  return parDer1(arg1, arg2) * arg1Dir;
311  }
312 
314  virtual DRetDArg2 parDer2(const Arg1 &arg1, const Arg2 &arg2) {
315  throw std::runtime_error("parDer2 must be overloaded by derived class.");
316  }
317 
319  virtual DRetDDir2 dirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
320  if constexpr (std::is_same_v<DRetDArg2, ErrorType>)
321  throw std::runtime_error("dirDer2 must be overloaded by derived class.");
322  else
323  return parDer2(arg1, arg2) * arg2Dir;
324  }
325 
327  virtual DDRetDDArg1 parDer1ParDer1(const Arg1 &arg1, const Arg2 &arg2) {
328  throw std::runtime_error("parDer1ParDer1 must be overloaded by derived class.");
329  }
330 
332  virtual DRetDArg1 parDer1DirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
333  if constexpr (std::is_same_v<DDRetDDArg1, ErrorType>)
334  throw std::runtime_error("parDer1DirDer1 must be overloaded by derived class.");
335  else
336  return parDer1ParDer1(arg1, arg2) * arg1Dir;
337  }
338 
340  virtual DRetDDir1 dirDer1DirDer1(const Arg1 &arg1Dir_1, const Arg1 &arg1Dir_2, const Arg1 &arg1, const Arg2 &arg2) {
341  if constexpr (std::is_same_v<DDRetDDArg1, ErrorType>)
342  throw std::runtime_error("dirDer1DirDer1 must be overloaded by derived class.");
343  else
344  return parDer1ParDer1(arg1, arg2) * arg1Dir_1 * arg1Dir_2;
345  }
346 
348  virtual DDRetDDArg2 parDer2ParDer2(const Arg1 &arg1, const Arg2 &arg2) {
349  throw std::runtime_error("parDer2ParDer2 must be overloaded by derived class.");
350  }
351 
353  virtual DRetDArg2 parDer2DirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
354  if constexpr (std::is_same_v<DDRetDDArg2, ErrorType>)
355  throw std::runtime_error("parDer2DirDer2 must be overloaded by derived class.");
356  else
357  return parDer2ParDer2(arg1, arg2) * arg2Dir;
358  }
359 
361  virtual DRetDDir2 dirDer2DirDer2(const Arg2 &arg2Dir_1, const Arg2 &arg2Dir_2, const Arg1 &arg1, const Arg2 &arg2) {
362  if constexpr (std::is_same_v<DDRetDDArg2, ErrorType>)
363  throw std::runtime_error("dirDer2DirDer2 must be overloaded by derived class.");
364  else
365  return parDer2ParDer2(arg1, arg2) * arg2Dir_1 * arg2Dir_2;
366  }
367 
369  virtual DDRetDArg1DArg2 parDer1ParDer2(const Arg1 &arg1, const Arg2 &arg2) {
370  throw std::runtime_error("parDer1ParDer2 must be overloaded by derived class.");
371  }
372 
374  virtual DRetDArg1 parDer1DirDer2(const Arg2 &arg2Dir, const Arg1 &arg1, const Arg2 &arg2) {
375  if constexpr (std::is_same_v<DDRetDArg1DArg2, ErrorType>)
376  throw std::runtime_error("parDer1DirDer2 must be overloaded by derived class.");
377  else
378  return parDer1ParDer2(arg1, arg2) * arg2Dir;
379  }
380 
382  virtual DRetDDir2 dirDer2DirDer1(const Arg2 &arg2Dir, const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
383  if constexpr (std::is_same_v<DDRetDArg1DArg2, ErrorType>)
384  throw std::runtime_error("dirDer2DirDer1 must be overloaded by derived class.");
385  else
386  return parDer1ParDer2(arg1, arg2) * arg2Dir * arg1Dir;
387  }
388 
390  virtual DRetDArg2 parDer2DirDer1(const Arg1 &arg1Dir, const Arg1 &arg1, const Arg2 &arg2) {
391  if constexpr (std::is_same_v<DDRetDArg1DArg2, ErrorType>)
392  throw std::runtime_error("parDer2DirDer1 must be overloaded by derived class.");
393  else
394  return parDer1ParDer2(arg1, arg2) * arg1Dir;
395  }
396 
398  // is constant.
399  virtual bool constParDer1() const { return false; }
400 
402  // is constant.
403  virtual bool constParDer2() const { return false; }
404 };
405 
406 // A function object with 3, 4, 5, ... arguments
407 // Not required till now!
408 
409 }
410 
411 #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:369
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:361
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:348
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:306
virtual bool constParDer() const
Returns true, if the partial derivative of the function value with respect to the argument.
Definition: function.h:264
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:332
Definition: atom.h:23
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:382
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:160
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:255
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:340
virtual int getArg2Size() const
Return the size of the second argument: =1 == scalar; >1 == vector; =0 == unknown vector size.
Definition: function.h:295
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:374
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:353
virtual int getArg1Size() const
Return the size of the first argument: =1 == scalar; >1 == vector; =0 == unknown vector size.
Definition: function.h:293
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:290
virtual DDRetDDArg1 parDer1ParDer1(const Arg1 &arg1, const Arg2 &arg2)
Second derivative: partial derivative of parDer1 with respect to the first argument.
Definition: function.h:327
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:399
virtual DRetDArg parDerDirDer(const Arg &argDir, const Arg &arg)
Second derivative: directional derivative of parDer with respect to the argument.
Definition: function.h:247
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:314
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:319
virtual DDRetDDArg parDerParDer(const Arg &arg)
Second derivative: partial derivative of parDer with respect to the argument.
Definition: function.h:242
virtual bool constParDer2() const
Returns true, if the partial derivative of the function value with respect to the second argument.
Definition: function.h:403
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:301
Definition: function.h:178
SymbolicExpression parDer(const SymbolicExpression &dep, const IndependentVariable &indep)
Definition: ast.cc:264
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:390