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_CUDA_POLYNOMIAL_POLYNOMIAL_H_
28 #define MPBLOCKS_CUDA_POLYNOMIAL_POLYNOMIAL_H_
29 
30 #include <cassert>
31 #include <iostream>
32 
37 
38 
39 namespace mpblocks {
40 namespace cuda {
41 namespace polynomial {
42 
43 
44 
45 
47 template <typename Scalar, int Idx>
49 {
50  Scalar value;
51 };
52 
55 template <typename Scalar, class IntList>
56 struct Storage;
57 
58 template <typename Scalar, int SpecHead, class SpecTail>
59 struct Storage<Scalar, IntList<SpecHead,SpecTail> >:
60  Coefficient<Scalar,SpecHead>,
61  Storage<Scalar,SpecTail>
62 {
65 // template <typename ParamHead, typename... ParamTail>
66 // void init( ParamHead pHead, ParamTail... pTail )
67 // {
68 // Coefficient<Scalar,SpecHead>::value = pHead;
69 // Storage<Scalar,SpecTail>::init( pTail );
70 // }
71 
74  void assign( const Scalar& v0 )
76 
78  void assign( const Scalar& v0, const Scalar& v1 )
79  {
80  assign(v0);
82  }
83 
85  void assign( const Scalar& v0, const Scalar& v1, const Scalar& v2 )
86  {
87  assign(v0);
89  }
90 
92  void assign( const Scalar& v0, const Scalar& v1, const Scalar& v2, const Scalar& v3 )
93  {
94  assign(v0);
96  }
97 
99  void assign( const Scalar& v0, const Scalar& v1, const Scalar& v2, const Scalar& v3, const Scalar& v4 )
100  {
101  assign(v0);
103  }
104 
106  void assign( const Scalar& v0, const Scalar& v1, const Scalar& v2, const Scalar& v3, const Scalar& v4, const Scalar& v5 )
107  {
108  assign(v0);
109  Storage<Scalar,SpecTail>::assign(v1,v2,v3,v4,v5);
110  }
111 
113  template < class Exp, class Spec2 >
115  void assign( const RValue<Scalar,Exp,Spec2>& exp )
116  {
117 // if( intlist::contains<Spec2,SpecHead>::value )
118 // Coefficient<Scalar,SpecHead>::value =
119 // get<SpecHead>(static_cast<const Exp&>(exp) );
120 // else
121 // Coefficient<Scalar,SpecHead>::value = Scalar(0);
123  get<SpecHead>(static_cast<const Exp&>(exp) );
125  }
126 
128  template <typename T>
130  void fill( T value )
131  {
134  }
135 
137  template< int idx >
139  Scalar eval( const Scalar& s, Scalar sn = Scalar(1.0) )
140  {
141  if( idx < SpecHead )
142  return eval<idx+1>( s, sn*s );
143  else
145  + Storage<Scalar,SpecTail>::eval<idx+1>(s,sn*s);
146  }
147 
152  Scalar dyn_get( int i )
153  {
154  return i == 0 ?
157  }
158 
163  void dyn_set( int i, const Scalar& value )
164  {
165  if( i == 0 )
167  else
169  }
170 };
171 
172 template <typename Scalar, int SpecTail>
173 struct Storage<Scalar, IntList<SpecTail,intlist::Terminal> >:
174  Coefficient<Scalar,SpecTail>
175 {
176 // template <typename ParamTail>
177 // void init( ParamTail pTail )
178 // {
179 // Coefficient<Scalar,SpecTail>::value = pTail;
180 // }
182  void assign( const Scalar& v0 )
184 
186  template < class Exp, class Spec2 >
188  void assign( const RValue<Scalar,Exp,Spec2>& exp )
189  {
190 // if( intlist::contains<Spec2,SpecTail>::value )
191  Coefficient<Scalar,SpecTail>::value = get<SpecTail>(
192  static_cast<const Exp&>(exp) );
193  }
194 
195 
196  template <typename T>
198  void fill( T value )
199  {
201  }
202 
203  template< int idx >
205  Scalar eval( const Scalar& s, Scalar sn = Scalar(1.0) )
206  {
207  if( idx < SpecTail )
208  return eval<idx+1>( s, sn*s );
209  else
211  }
212 
213  __host__
214  Scalar dyn_get( int i )
215  {
216  assert( i == 0 );
218  }
219 
220  __host__
221  void dyn_set( int i, const Scalar& value )
222  {
223  assert( i == 0 );
225  }
226 };
227 
228 
229 template <int idx>
230 struct CoefficientKey{};
231 
232 namespace coefficient_key
233 {
244 }
245 
246 namespace device_coefficient_key
247 {
258 }
259 
260 
261 
262 
263 
265 template <typename Scalar, class Spec>
266 struct Polynomial:
267  Storage<Scalar,Spec>,
268  RValue< Scalar, Polynomial<Scalar,Spec>, Spec >,
269  LValue< Scalar, Polynomial<Scalar,Spec> >
270 {
274 
276  template <typename Exp, class Spec2>
279  {
281 // AssignmentHelper<Scalar, Polynomial<Scalar,Spec>, Exp, Spec2 >
282 // ::assign(*this, static_cast<const Exp&>(exp));
283  }
284 
285  template <typename Exp, class Spec2>
288  {
290 // AssignmentHelper<Scalar, Polynomial<Scalar,Spec>, Exp, Spec >
291 // ::assign(*this, static_cast<const Exp&>(exp));
292  }
293 
294  template <int n, class Exp2, class Spec2>
297  {
298  differentiate<n>( surrogate.exp, *this );
299  }
300 
301  template <int n, class Exp2, class Spec2>
304  {
305  differentiate( surrogate.exp, *this );
306  }
307 
309  Polynomial( const Scalar& v0 )
311 
313  Polynomial( const Scalar& v0, const Scalar& v1 )
314  { Storage<Scalar,Spec>::assign(v0,v1); }
315 
317  Polynomial( const Scalar& v0, const Scalar& v1, const Scalar& v2 )
318  { Storage<Scalar,Spec>::assign(v0,v1,v2); }
319 
321  Polynomial( const Scalar& v0, const Scalar& v1, const Scalar& v2, const Scalar& v3 )
322  { Storage<Scalar,Spec>::assign(v0,v1,v2,v3); }
323 
325  Polynomial( const Scalar& v0, const Scalar& v1, const Scalar& v2, const Scalar& v3, const Scalar& v4 )
326  { Storage<Scalar,Spec>::assign(v0,v1,v2,v3,v4); }
327 
329  Polynomial( const Scalar& v0, const Scalar& v1, const Scalar& v2, const Scalar& v3, const Scalar& v4, const Scalar& v5 )
330  { Storage<Scalar,Spec>::assign(v0,v1,v2,v3,v4,v5); }
331 
332 
333 
336  Scalar eval( Scalar x )
337  {
338  return Storage<Scalar,Spec>::eval(x,1);
339  }
340 };
341 
342 
343 
344 template< bool HasCoefficient, int idx, typename Scalar, class Spec >
345 struct GetHelper
346 {
348  static Scalar get( const Polynomial<Scalar,Spec>& poly )
349  {
350  return 0;
351  }
352 };
353 
354 template< int idx, typename Scalar, class Spec >
355 struct GetHelper<true,idx,Scalar,Spec>
356 {
358  static Scalar get( const Polynomial<Scalar,Spec>& poly )
359  {
360  return poly.Coefficient<Scalar,idx>::value;
361  }
362 };
363 
364 
365 
366 
367 
368 template <int idx, typename Scalar, class Spec>
370 Scalar get( const Polynomial<Scalar,Spec>& poly )
371 {
372  return GetHelper< intlist::contains<Spec,idx>::value, idx,Scalar,Spec >
373  ::get(poly);
374 }
375 
376 template <int idx, typename Scalar, class Spec>
378 Scalar& set( Polynomial<Scalar,Spec>& poly )
379 {
380  return static_cast< Coefficient<Scalar,idx>& >(poly).value;
381 }
382 
383 template <int idx, typename Scalar, class Spec>
386 {
387  return static_cast<
389 }
390 
391 
392 
393 
394 template< class Scalar, class Spec >
395 struct get_spec< Polynomial<Scalar,Spec> >
396 {
397  typedef Spec result;
398 };
399 
400 } // polynomial
401 } // cuda
402 } // mpblocks
403 
404 
405 
406 
407 
408 #endif // POLYNOMIAL_H_
A sparse, statically sized polynomial.
Definition: Polynomial.h:266
__host__ __device__ __forceinline__ Polynomial(const Scalar &v0, const Scalar &v1)
Definition: Polynomial.h:313
__host__ __device__ __forceinline__ Scalar eval(const Scalar &s, Scalar sn=Scalar(1.0))
evaluate the polynomial
Definition: Polynomial.h:139
__host__ __device__ __forceinline__ void assign(const Scalar &v0)
Definition: Polynomial.h:182
__device__ const CoefficientKey< 5 > _5
Definition: Polynomial.h:253
__host__ __device__ __forceinline__ void assign(const Scalar &v0, const Scalar &v1, const Scalar &v2, const Scalar &v3, const Scalar &v4, const Scalar &v5)
Definition: Polynomial.h:106
__device__ const CoefficientKey< 8 > _8
Definition: Polynomial.h:256
__host__ __device__ __forceinline__ void assign(const Scalar &v0, const Scalar &v1, const Scalar &v2, const Scalar &v3, const Scalar &v4)
Definition: Polynomial.h:99
__host__ __device__ __forceinline__ Polynomial(const Scalar &v0, const Scalar &v1, const Scalar &v2)
Definition: Polynomial.h:317
__host__ __device__ __forceinline__ void operator=(const DerivativeSurrogate< n, Scalar, Exp2, Spec2 > &surrogate)
Definition: Polynomial.h:303
#define __forceinline__
Definition: fakecuda.h:38
__device__ const CoefficientKey< 4 > _4
Definition: Polynomial.h:252
__host__ __device__ void differentiate(const RValue< Scalar, Exp1, InSpec > &in, LValue< Scalar, Exp2 > &out)
evaluate a polynomial
__host__ __device__ __forceinline__ void assign(const Scalar &v0, const Scalar &v1, const Scalar &v2)
Definition: Polynomial.h:85
__host__ __device__ Scalar & set(Exp1 &)
__host__ __device__ __forceinline__ Polynomial()
Default constructor.
Definition: Polynomial.h:273
__host__ __device__ __forceinline__ void operator=(const RValue< Scalar, Exp, Spec2 > &exp)
Definition: Polynomial.h:287
__host__ __device__ __forceinline__ void assign(const Scalar &v0, const Scalar &v1)
Definition: Polynomial.h:78
__host__ __device__ __forceinline__ Polynomial(const RValue< Scalar, Exp, Spec2 > &exp)
Construct from any PolynomialExpression:
Definition: Polynomial.h:278
expression template for rvalues
Definition: RValue.h:40
__host__ __device__ __forceinline__ void assign(const RValue< Scalar, Exp, Spec2 > &exp)
assignment by rvalue
Definition: Polynomial.h:115
__host__ __device__ __forceinline__ Polynomial(const Scalar &v0, const Scalar &v1, const Scalar &v2, const Scalar &v3)
Definition: Polynomial.h:321
__host__ __device__ __forceinline__ Polynomial(const DerivativeSurrogate< n, Scalar, Exp2, Spec2 > &surrogate)
Definition: Polynomial.h:296
__host__ __device__ __forceinline__ Scalar eval(const Scalar &s, Scalar sn=Scalar(1.0))
Definition: Polynomial.h:205
expression template for lvalues
Definition: LValue.h:85
__device__ const CoefficientKey< 1 > _1
Definition: Polynomial.h:249
__device__ const CoefficientKey< 9 > _9
Definition: Polynomial.h:257
__device__ const CoefficientKey< 7 > _7
Definition: Polynomial.h:255
__host__ __device__ __forceinline__ void assign(const Scalar &v0, const Scalar &v1, const Scalar &v2, const Scalar &v3)
Definition: Polynomial.h:92
__host__ __device__ __forceinline__ void assign(const Scalar &v0)
variadic initializer, recursively sets all coefficients with a function call
Definition: Polynomial.h:74
__host__ __device__ __forceinline__ Polynomial(const Scalar &v0, const Scalar &v1, const Scalar &v2, const Scalar &v3, const Scalar &v4)
Definition: Polynomial.h:325
__device__ const CoefficientKey< 0 > _0
Definition: Polynomial.h:248
__device__ const CoefficientKey< 6 > _6
Definition: Polynomial.h:254
__host__ __device__ __forceinline__ void assign(const RValue< Scalar, Exp, Spec2 > &exp)
assignment by rvalue
Definition: Polynomial.h:188
intermediate object which allows LValue assignment operator to call differntiate expression template ...
Definition: LValue.h:73
__device__ const CoefficientKey< 2 > _2
Definition: Polynomial.h:250
#define __device__
Definition: fakecuda.h:34
__host__ __device__ __forceinline__ Polynomial(const Scalar &v0)
Definition: Polynomial.h:309
class actually providing storage for a coefficient
Definition: Polynomial.h:48
__host__ __device__ __forceinline__ Polynomial(const Scalar &v0, const Scalar &v1, const Scalar &v2, const Scalar &v3, const Scalar &v4, const Scalar &v5)
Definition: Polynomial.h:329
__host__ __device__ __forceinline__ Scalar dyn_get(int i)
runtime indexing of coefficients, i is the array offset (i.e. the i'th nonzero coefficient, not the coefficient of the i'th power of the polynomial parameter )
Definition: Polynomial.h:152
#define __host__
Definition: fakecuda.h:37
__device__ const CoefficientKey< 3 > _3
Definition: Polynomial.h:251
__host__ __device__ Scalar get(const Derivative< Scalar, Exp, Spec > &exp)
Definition: Derivative.h:94
__host__ __device__ __forceinline__ Scalar eval(Scalar x)
evaluate the polynomial at a particular value
Definition: Polynomial.h:336
__host__ __device__ __forceinline__ void dyn_set(int i, const Scalar &value)
runtime indexing of coefficients, i is the array offset (i.e. the i'th nonzero coefficient, not the coefficient of the i'th power of the polynomial parameter )
Definition: Polynomial.h:163
__host__ __device__ __forceinline__ Scalar & set_storage(Polynomial< Scalar, Spec > &poly)
Definition: Polynomial.h:385
__host__ __device__ __forceinline__ void fill(T value)
assignment of all coefficients
Definition: Polynomial.h:130
compile time integer list (because cuda doesn't understand variadic templates )
Definition: IntList.h:50
recursive inheritance tree which provides storage for each of the required coefficients ...
Definition: Polynomial.h:56
__host__ __device__ __forceinline__ void assign(Polynomial< Scalar, Spec > &p, const Exp &exp)
Definition: assign.h:55