Lodestar
An integrated real-time control package in C++
AdaptiveBackwardDifference.hpp
1 //
2 // Created by Hamza El-Kebir on 6/20/21.
3 //
4 
5 #ifndef LODESTAR_ADAPTIVEBACKWARDDIFFERENCE_HPP
6 #define LODESTAR_ADAPTIVEBACKWARDDIFFERENCE_HPP
7 
8 #include "BackwardDifference.hpp"
9 #include "Lodestar/aux/ArrayStack.hpp"
10 #include <type_traits>
11 #include <array>
12 
13 namespace ls {
14  namespace primitives {
15  template<typename TType, size_t TOrder, typename TScalarType = double>
17 
18  };
19 
20  template<typename TType, typename TScalarType>
21  class AdaptiveBackwardDifference<TType, 1, TScalarType> {
22  public:
23  static const size_t kOrder = 1;
24 
25  template<size_t TSamples>
26  static typename std::enable_if<(TSamples > kOrder), TType>::type
27  compute(TScalarType h, const std::array<TType, TSamples> &a)
28  {
29  return computeImpl(h, a);
30  }
31 
32  template<size_t TSamples>
33  static typename std::enable_if<(TSamples > kOrder), TType>::type
34  compute(TScalarType h, const std::array<TType, TSamples> &a, const size_t size)
35  {
36 
37  switch (size) {
38  case 0:
39  case 1:
40  return {};
41  case 2:
42  return BackwardDifference<TType, 2, 1>::compute(a[1], a[0], h);
43  case 3:
44  return BackwardDifference<TType, 3, 1>::compute(a[2], a[1], a[0], h);
45  case 4:
46  return BackwardDifference<TType, 4, 1>::compute(a[3], a[2], a[1], a[0], h);
47  case 5:
48  return BackwardDifference<TType, 5, 1>::compute(a[4], a[3], a[2], a[1], a[0], h);
49  case 6:
50  return BackwardDifference<TType, 6, 1>::compute(a[5], a[4], a[3], a[2], a[1], a[0], h);
51  }
52 
53  return computeImpl(h, a);
54  }
55 
56  template<size_t TSamples>
57  static typename std::enable_if<(TSamples <= kOrder), TType>::type
58  compute(TScalarType h, const std::array<TType, TSamples> &a, const size_t size)
59  {
60  static_assert(TSamples > kOrder, "Number of samples must be greater than the differentiation order.");
61 
62  return TType{};
63  }
64 
65  template<size_t TSamples>
66  static typename std::enable_if<(TSamples > kOrder) && (TSamples > 7), TType>::type
67  compute(TScalarType h, const std::array<TType, TSamples> &a)
68  {
69  return BackwardDifference<TType, 7, 1>::compute(a[6], a[5], a[4], a[3], a[2], a[1], a[0], h);
70  }
71 
72  template<size_t TSamples>
73  static typename std::enable_if<(TSamples <= kOrder), TType>::type
74  compute(TScalarType h, const std::array<TType, TSamples> &a)
75  {
76  static_assert(TSamples > kOrder, "Number of samples must be greater than the differentiation order.");
77 
78  return TType{};
79  }
80 
81  protected:
82  static inline TType computeImpl(TScalarType h, const std::array<TType, 2> &a)
83  {
84  return BackwardDifference<TType, 2, 1>::compute(a[1], a[0], h);
85  }
86 
87  static inline TType computeImpl(TScalarType h, const std::array<TType, 3> &a)
88  {
89  return BackwardDifference<TType, 3, 1>::compute(a[2], a[1], a[0], h);
90  }
91 
92  static inline TType computeImpl(TScalarType h, const std::array<TType, 4> &a)
93  {
94  return BackwardDifference<TType, 4, 1>::compute(a[3], a[2], a[1], a[0], h);
95  }
96 
97  static inline TType computeImpl(TScalarType h, const std::array<TType, 5> &a)
98  {
99  return BackwardDifference<TType, 5, 1>::compute(a[4], a[3], a[2], a[1], a[0], h);
100  }
101 
102  static inline TType computeImpl(TScalarType h, const std::array<TType, 6> &a)
103  {
104  return BackwardDifference<TType, 6, 1>::compute(a[5], a[4], a[3], a[2], a[1], a[0], h);
105  }
106 
107  template<size_t TSamples>
108  static typename std::enable_if<(TSamples > kOrder) && (TSamples >= 7), TType>::type
109  computeImpl(TScalarType h, const std::array<TType, TSamples> &a)
110  {
111  return BackwardDifference<TType, 7, 1>::compute(a[6], a[5], a[4], a[3], a[2], a[1], a[0], h);
112  }
113  };
114  }
115 }
116 
117 #endif //LODESTAR_ADAPTIVEBACKWARDDIFFERENCE_HPP
ls
Main Lodestar code.
Definition: BilinearTransformation.hpp:12
ls::primitives::BackwardDifference
Definition: BackwardDifference.hpp:13
ls::primitives::AdaptiveBackwardDifference
Definition: AdaptiveBackwardDifference.hpp:16