EEROS  0.4.1.0
API for the EEROS Real-Time Robotics Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Fraction.hpp
Go to the documentation of this file.
1 #ifndef ORG_EEROS_MATH_FRACTION_HPP_
2 #define ORG_EEROS_MATH_FRACTION_HPP_
3 
4 #include <vector>
5 
6 namespace eeros {
7  namespace math {
8 
9  template < int ORDER >
10  class Polynome;
11 
12  template < int ORDER >
13  class Fraction {
14  public:
15  Fraction() { }
16 
17  explicit Fraction(const std::vector<double> n) :
18  numerator(n) { }
19 
20  Fraction(const std::vector<double>& n, const std::vector<double>& d) :
21  numerator(n), denominator(d) { }
22 
23  template < int RORDER >
26  p.numerator = numerator * right.numerator;
28  return p;
29  }
30 
33  f.numerator = numerator*k;
35  return f;
36  }
37 
38  template < int RORDER >
41  p.numerator = numerator * right.denominator + denominator * right.numerator;
43  return p;
44  }
45 
46  Fraction<ORDER> operator +(const double right) {
48  p.numerator = numerator + denominator * right;
50  return p;
51  }
52 
55  };
56 
57  template < int ORDER >
58  class Polynome {
59  public:
60  static constexpr int N = (ORDER + 1);
61  static constexpr int MAX(int x, int y) { return (x > y) ? x : y; }
62 
64  for (int i = 0; i < N; i++)
65  this->c[i] = 0;
66  }
67 
68  explicit Polynome(const std::vector<double>& c) {
69  int n = c.size();
70 
71  for (int i = 0; i < N; i++)
72  if (i < n)
73  this->c[i] = c[i];
74  else
75  this->c[i] = 0;
76  }
77 
78  template < int RORDER >
79  explicit Polynome(const Polynome<RORDER>& copy) {
80  int n = (RORDER+1);
81 
82  for (int i = 0; i < N; i++)
83  if (i < n)
84  this->c[i] = copy.c[i];
85  else
86  this->c[i] = 0;
87  }
88 
89  template < int RORDER >
91  constexpr int M = Polynome<RORDER>::N;
93 
94  for (int i = 0; i < N; i++)
95  for (int j = 0; j < M; j++)
96  p.c[i+j] += c[i]*right.c[j];
97 
98  return p;
99  }
100 
102  Polynome<ORDER> p;
103 
104  for (int i = 0; i < N; i++)
105  p.c[i] += c[i]*k;
106 
107  return p;
108  }
109 
110  template < int RORDER >
112  constexpr int M = Polynome<RORDER>::N;
113  constexpr int n = Polynome<MAX(ORDER,RORDER)>::N;
115 
116  for (int i = 0; i < n; i++) {
117  if (i < M && i < N)
118  p.c[i] = c[i] + right.c[i];
119  else if (i < N)
120  p.c[i] = c[i];
121  else if (i < M)
122  p.c[i] = right.c[i];
123  }
124 
125  return p;
126  }
127 
128  Polynome<ORDER> operator +(double right) {
129  Polynome<ORDER> p = *this;
130  p.c[0] = c[0] + right;
131  return p;
132  }
133 
134  template < int RORDER >
139  return p;
140  }
141 
142  double c[N];
143  };
144  }
145 }
146 
147 
148 #endif // ORG_EEROS_MATH_FRACTION_HPP_
Definition: Fraction.hpp:10
Polynome< ORDER > denominator
Definition: Fraction.hpp:54
Polynome< MAX(ORDER, RORDER)> operator+(const Polynome< RORDER > right)
Definition: Fraction.hpp:111
Fraction(const std::vector< double > n)
Definition: Fraction.hpp:17
Polynome< ORDER > numerator
Definition: Fraction.hpp:53
Polynome()
Definition: Fraction.hpp:63
Fraction()
Definition: Fraction.hpp:15
Polynome< ORDER+RORDER > operator*(const Polynome< RORDER > right)
Definition: Fraction.hpp:90
static constexpr int N
Definition: Fraction.hpp:60
Fraction< ORDER+RORDER > operator+(const Fraction< RORDER > right)
Definition: Fraction.hpp:39
Fraction< ORDER+RORDER > operator*(const Fraction< RORDER > right)
Definition: Fraction.hpp:24
Definition: Fraction.hpp:13
Polynome(const std::vector< double > &c)
Definition: Fraction.hpp:68
int j
Definition: RingBufferTest.cpp:13
int i
Definition: RingBufferTest.cpp:12
Fraction(const std::vector< double > &n, const std::vector< double > &d)
Definition: Fraction.hpp:20
static constexpr int MAX(int x, int y)
Definition: Fraction.hpp:61
Polynome(const Polynome< RORDER > &copy)
Definition: Fraction.hpp:79
Fraction< MAX(ORDER, RORDER)> operator/(const Polynome< RORDER > right)
Definition: Fraction.hpp:135
double c[N]
Definition: Fraction.hpp:142