cheshirekow  v0.1.0
Polynomial.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef MPBLOCKS_POLYNOMIAL_POLYNOMIAL_H_
28 #define MPBLOCKS_POLYNOMIAL_POLYNOMIAL_H_
29 
30 #include <iostream>
31 #include <cassert>
32 #include <vector>
33 #include <map>
34 
35 namespace mpblocks {
36 namespace polynomial {
37 
39 template <typename Scalar, int Degree>
40 class Polynomial :
41  public LValue< Scalar, Polynomial<Scalar,Degree> >,
42  public RValue< Scalar, Polynomial<Scalar,Degree> >
43 {
44  public:
47 
48  enum
49  {
50  DEGREE=Degree,
52  };
53 
54  struct iterator
55  {
56  int idx;
57 
59  {
60  ++idx;
61  return *this;
62  }
63 
64  bool operator()( const Polynomial_t& poly )
65  {
66  return idx < poly.size();
67  }
68  };
69 
70  protected:
71  Scalar m_data[Degree+1];
72 
73  public:
74  typedef unsigned int Size_t;
75 
76  Size_t size() const
77  { return Degree+1; }
78 
79  void resize( Size_t size ){}
80 
82  Scalar& operator[](int i)
83  {
84  return m_data[i];
85  }
86 
88  Scalar const& operator[](int i) const
89  {
90  return m_data[i];
91  }
92 
93  Scalar& operator()( const iterator iter )
94  {
95  return m_data[iter.idx];
96  }
97 
98  Scalar const& operator()( const iterator iter ) const
99  {
100  return m_data[iter.idx];
101  }
102 
105  {
106  for( int i=0; i < size(); i++)
107  (*this)[i] = 0;
108  }
109 
111  template <typename Exp>
113  {
114  for( int i=0; i < size(); i++)
115  (*this)[i] = exp[i];
116  }
117 
119  Polynomial( Scalar a0 )
120  {
121  for(int i=0; i <= Degree; i++)
122  m_data[i] = a0;
123  }
124 
125  Polynomial( Scalar a0, Scalar a1 )
126  {
127  assert( Degree == 1 );
128  m_data[0] = a0;
129  m_data[1] = a1;
130  }
131 
132  Polynomial( Scalar a0, Scalar a1, Scalar a2 )
133  {
134  assert( Degree == 2 );
135  m_data[0] = a0;
136  m_data[1] = a1;
137  m_data[2] = a2;
138  }
139 
140  Scalar eval( Scalar x )
141  {
142  Scalar x_i = 1.0;
143  Scalar r = (*this)[0];
144 
145  for(int i=1; i < size(); ++i)
146  {
147  x_i *= x;
148  r += (*this)[i]*x_i;
149  }
150 
151  return r;
152  }
153 };
154 
155 
157 template <typename Scalar>
158 class Polynomial<Scalar,Dynamic> :
159  public std::vector<Scalar>,
160  public LValue< Scalar, Polynomial<Scalar,Dynamic> >,
161  public RValue< Scalar, Polynomial<Scalar,Dynamic> >
162 {
163  public:
164  enum
165  {
167  };
168 
171 
172  struct iterator
173  {
174  int idx;
175 
176  iterator& operator++()
177  {
178  ++idx;
179  return *this;
180  }
181 
182  bool operator()( const Polynomial_t& poly )
183  {
184  return idx < poly.size();
185  }
186  };
187 
188  public:
189  typedef unsigned int Size_t;
190  typedef std::vector<Scalar> Vector_t;
191 
193  Polynomial( int size=0 ):
194  Vector_t(size)
195  {}
196 
197  template <typename Exp>
199  {
200  Vector_t::reserve(exp.size());
201  for( int i=0; i < exp.size(); i++)
202  Vector_t::push_back(exp[i]);
203  }
204 
206  Polynomial( Scalar a0 ):
207  Vector_t(1)
208  {
209  (*this)[0] = a0;
210  }
211 
212  Polynomial( Scalar a0, Scalar a1 ):
213  Vector_t(2)
214  {
215  (*this)[0] = a0;
216  (*this)[1] = a1;
217  }
218 
219  Polynomial( Scalar a0, Scalar a1, Scalar a2 ):
220  Vector_t(3)
221  {
222  (*this)[0] = a0;
223  (*this)[1] = a1;
224  (*this)[2] = a2;
225  }
226 
227  Scalar& operator()( const iterator iter )
228  {
229  return (*this)[iter.idx];
230  }
231 
232  Scalar const& operator()( const iterator iter ) const
233  {
234  return (*this)[iter.idx];
235  }
236 
237  Scalar eval( Scalar x )
238  {
239  Scalar x_i = 1.0;
240  Scalar r = (*this)[0];
241 
242  for(int i=1; i < size(); ++i)
243  {
244  x_i *= x;
245  r += (*this)[i]*x_i;
246  }
247 
248  return r;
249  }
250 
251 
252  using Vector_t::size;
253  using Vector_t::resize;
254  using Vector_t::operator[];
255 };
256 
257 
258 
259 
260 
261 
262 
263 
264 
265 } // polynomial
266 } // mpblocks
267 
268 
269 
270 
271 
272 #endif // POLYNOMIAL_H_
Polynomial(Scalar a0, Scalar a1)
Definition: Polynomial.h:125
Polynomial(Scalar a0, Scalar a1, Scalar a2)
Definition: Polynomial.h:132
Scalar const & operator[](int i) const
vector accessor
Definition: Polynomial.h:88
Scalar & operator[](int i)
vector accessor
Definition: Polynomial.h:82
expression template for rvalues
Definition: LValue.h:35
Scalar const & operator()(const iterator iter) const
Definition: Polynomial.h:98
Polynomial(int size=0)
Default constructor.
Definition: Polynomial.h:193
Scalar const & operator()(const iterator iter) const
Definition: Polynomial.h:232
Size_t size() const
return the size for a vector
Definition: LValue.h:43
bool operator()(const Polynomial_t &poly)
Definition: Polynomial.h:64
Size_t size() const
return the size for a vector
Definition: RValue.h:41
Polynomial(const RValue< Scalar, Exp > &exp)
Construct from any PolynomialExpression:
Definition: Polynomial.h:112
Polynomial()
Default constructor.
Definition: Polynomial.h:104
Polynomial< Scalar, Degree > Polynomial_t
Definition: Polynomial.h:45
expression template for rvalues
Definition: RValue.h:35
LValue< Scalar, Polynomial_t > LValue_t
Definition: Polynomial.h:46
Polynomial(Scalar a0)
fixed size construction
Definition: Polynomial.h:119
A dense, dynamically sized polynomial.
Definition: Polynomial.h:158
A dense, statically sized polynomial.
Definition: Polynomial.h:40
Scalar & operator()(const iterator iter)
Definition: Polynomial.h:93
Polynomial< Scalar, Dynamic > Polynomial_t
Definition: Polynomial.h:169
Polynomial(const RValue< Scalar, Exp > &exp)
Definition: Polynomial.h:198
Polynomial(Scalar a0, Scalar a1, Scalar a2)
Definition: Polynomial.h:219
Polynomial(Scalar a0)
fixed size construction
Definition: Polynomial.h:206