Memosa-FVM  0.2
PC.h
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #ifndef _PC_H_
6 #define _PC_H_
7 
8 
9 #include "NumType.h"
10 
11 #include "misc.h"
12 #include "PCSet.h"
13 
14 // helper class for convenient initialization of PolynomialChaos type
15 // variables. This class, along with the overloaded comma (,) operator
16 // in PC allows us to write statements like
17 // a = 1, 2, 3;
18 
19 template<class X, class T_iterator>
21 {
22 public:
23 
24  Initializer(T_iterator iter):
25  _iter(iter)
26  {}
27 
28  Initializer operator,(const X& x)
29  {
30  *_iter = x;
31  return Initializer(_iter+1);
32  }
33 
34 protected:
35  T_iterator _iter;
36 };
37 
38 
39 // used for compile time determination of the total size of the PC
40 // class's data member
41 
42 template <int N>
43 struct Factorial
44 {
45  enum { value = N * Factorial<N - 1>::value };
46 };
47 
48 template <>
49 struct Factorial<0>
50 {
51  enum { value = 1 };
52 };
53 
54 
55 class PCSet;
56 
57 PCSet* createPCSet(const int order, const int dim);
58 
59 
60 template<int ORDER, int DIM>
61 class PC
62 {
63 public:
64 
66 
67  typedef PC This_T;
68  typedef PC T_Scalar;
70 
72 
73  static string getTypeName()
74  {
75  return "PC";
76  }
77 
78  PC()
79  {
80  for (int i=0;i<N;i++)
81  _data[i]=0;
82  }
83 
84 
85  explicit PC(const double v)
86  {
87  _data[0] = v;
88  for (int i=1;i<N;i++)
89  _data[i]=0;
90  }
91 
92 
93  PC(const PC& o)
94  {
95  for (int i=0;i<N;i++)
96  _data[i]=o._data[i];
97  }
98 
99  ~PC()
100  {}
101 
102  double& operator[](int n) {return _data[n];}
103  const double& operator[](int n) const {return _data[n];}
104 
105  PCInitializer operator,(const double& x)
106  {
107  _data[1] = x;
108  return PCInitializer(_data+2);
109  }
110 
111 
112  PC& operator=(const PC& o)
113  {
114  if (this == &o)
115  return *this;
116  for (int i=0;i<N;i++)
117  _data[i]=o._data[i];
118  return *this;
119  }
120 
121  PC& operator=(const double& f)
122  {
123  _data[0] = f;
124  for (int i=1;i<N;i++)
125  _data[i]=0;
126  return *this;
127  }
128 
129  PC& operator+=(const PC& o)
130  {
131  for (int i=0;i<N;i++)
132  _data[i] += o._data[i];
133  return *this;
134  }
135 
136  PC& operator-=(const PC& o)
137  {
138  for (int i=0;i<N;i++)
139  _data[i] -= o._data[i];
140  return *this;
141  }
142 
143  PC& operator*=(const PC& o)
144  {
145  double pdata[N];
146  _pcSet->Prod(_data,o._data,pdata);
147  for(int i=0; i<N; i++)
148  _data[i] = pdata[i];
149  return *this;
150  }
151 
152 
153  PC& operator/=(const PC& o)
154  {
155  double pdata[N];
156  _pcSet->Div(_data,o._data,pdata);
157  for(int i=0; i<N; i++)
158  _data[i] = pdata[i];
159  return *this;
160  }
161 
162 
163 
164 
165  PC& operator+=(const double& o)
166  {
167  _data[0] += o;
168  return *this;
169  }
170 
171  PC& operator-=(const double& o)
172  {
173  _data[0] -= o;
174  return *this;
175  }
176 
177  PC& operator*=(const double& o)
178  {
179  for(int i=0; i<N; i++)
180  _data[i] *= o;
181  return *this;
182  }
183 
184  PC& operator/=(const double& o)
185  {
186  for(int i=0; i<N; i++)
187  _data[i] /= o;
188  return *this;
189  }
190 
191 #define PC_RELATIONAL_OPS(opname,_op_) \
192  bool opname(const PC& o) const \
193  { \
194  return (_data[0] _op_ o._data[0]); \
195  } \
196  bool opname(const double& o) const \
197  { \
198  return (_data[0] _op_ o); \
199  }
200 
201  PC_RELATIONAL_OPS(operator>,>);
202  PC_RELATIONAL_OPS(operator>=,>=);
203  PC_RELATIONAL_OPS(operator<,<);
204  PC_RELATIONAL_OPS(operator<=,<=);
205  PC_RELATIONAL_OPS(operator==,==);
206  PC_RELATIONAL_OPS(operator!=,!=);
207 
208 #undef PC_RELATIONAL_OPS
209 
210  void print(ostream &os) const
211  {
212  os << "[ ";
213  for(int i=0; i<N; i++)
214  os << _data[i] << " ";
215  os << "]";
216  }
217 
218  static PC getZero()
219  {
220  return PC();
221  }
222 
223  static PC getUnity()
224  {
225  PC one;
226  one._data[0] = 1.0;
227  return one;
228 
229  }
230 
232  {
233  PC m_one;
234  m_one._data[0] = -1.0;
235  return m_one;
236  }
237 
238  static double doubleMeasure(const PC& x)
239  {
241  }
242 
243  static void setFloat(PC& t, const int i, const double& val)
244  {
245  t._data[i]=val;
246  }
247  static double getFloat(const PC& t, const int i)
248  {
249  return t._data[i];
250  }
251 
252  double stdDev() const
253  {
254  return _pcSet->StDv(_data);
255  }
256 
257  // only printing the value and not the derivative
258  static void write(FILE* fp, const PC& x) {throw;}
259 
261 
262  static void getShape(int *shp) { *shp = N; NumTypeTraits<double>::getShape(shp+1);}
263 
264  static int getDataSize()
265  {
267  }
268 
269  PC fabs() const
270  {
271  PC x(*this);
272  x._data[0] = ::fabs(_data[0]);
273  return x;
274  }
275 
276 
277  static void accumulateOneNorm(PC& sum, const PC& v) { sum += v.fabs();}
278 
279  static void accumulateDotProduct(PC& sum, const PC& v0, const PC& v1)
280  {
281  sum += v0*v1;
282  }
283 
284  static void reduceSum(PC& sum, const PC& x) {sum+=x;}
285 
286  static void safeDivide(PC& x, const PC& y) {if (y._data[0]!=0) x/=y;}
287  static void normalize(PC& x, const PC& y) {if (y._data[0]!=0) x/=y._data[0];}
288 
289  static void setMax(PC& x, const PC& y) {if (y._data[0]>x._data[0]) x._data[0]=y._data[0];}
290 
291 
292  // the coefficients of expansion
293  double _data[N];
294 
295  // the PCSet object from the UQ toolkit that implements the
296  // algebraic and other operations for this particular version of PC
297  // class
298 
299  static PCSet *_pcSet;
300 };
301 
302 
303 #define PC_BINARY_OP(opname,_op_) \
304  template<int ORDER, int DIM> \
305  PC<ORDER,DIM> opname(const PC<ORDER,DIM>& a, const PC<ORDER,DIM>& b) \
306  { \
307  return PC<ORDER,DIM>(a) _op_ b; \
308  } \
309  template<int ORDER, int DIM> \
310  PC<ORDER,DIM> opname(const PC<ORDER,DIM>& a, const double& b) \
311  { \
312  return PC<ORDER,DIM>(a) _op_ b; \
313  } \
314  template<int ORDER, int DIM> \
315  PC<ORDER,DIM> opname(const double& a, const PC<ORDER,DIM>& b) \
316  { \
317  return PC<ORDER,DIM>(a) _op_ b; \
318  } \
319 
320 PC_BINARY_OP(operator+,+=);
321 PC_BINARY_OP(operator-,-=);
322 PC_BINARY_OP(operator*,*=);
323 PC_BINARY_OP(operator/,/=);
324 
325 template<int ORDER, int DIM>
327 {
328  return a;
329 }
330 
331 template<int ORDER, int DIM>
333 {
334  PC<ORDER,DIM> z;
335  return z-a;
336 }
337 
338 template<int ORDER, int DIM>
340 {
341  PC<ORDER,DIM> x(a);
342  x._data[0]=fabs(a._data[0]);
343  return x;
344 }
345 
346 template<int ORDER, int DIM>
347 inline ostream &operator<<(ostream &os, const PC<ORDER,DIM> &a)
348 {
349  a.print(os);
350  return os;
351 }
352 
353 template<int ORDER, int DIM>
356 {
357  PC<ORDER,DIM> r;
358  PC<ORDER,DIM>::_pcSet->RPow(a._data,r._data,0.5);
359  return r;
360 }
361 
362 template<int ORDER, int DIM>
363 double stdDev(const PC<ORDER,DIM>& a)
364 {
365  return PC<ORDER,DIM>::_pcSet->StDv(a._data);
366 }
367 
368 
369 
370 template<int ORDER, int DIM>
371 PCSet* PC<ORDER,DIM>::_pcSet = createPCSet(ORDER,DIM);
372 
373 template<>
374 template<int ORDER, int DIM>
375 struct ArrayScalarTraits<PC< ORDER, DIM > >
376 {
377  static void limit(PC<ORDER,DIM>& val, const double min, const double max)
378  {
379  if (val._data[0] < min)
380  val._data[0] = min;
381  else if (val._data[0] > max)
382  val._data[0] = max;
383  }
384 };
385 
386 #endif
387 
Definition: PC.h:65
PC< ORDER, DIM > sqrt(const PC< ORDER, DIM > &a)
Definition: PC.h:355
static int getDataSize()
Definition: PC.h:264
static PC getUnity()
Definition: PC.h:223
static void getShape(int *shp)
Definition: PC.h:262
Initializer operator,(const X &x)
Definition: PC.h:28
PC & operator*=(const double &o)
Definition: PC.h:177
Definition: PC.h:61
Initializer(T_iterator iter)
Definition: PC.h:24
PC This_T
Definition: PC.h:67
static void limit(PC< ORDER, DIM > &val, const double min, const double max)
Definition: PC.h:377
double max(double x, double y)
Definition: Octree.cpp:18
NumTypeTraits< double >::T_BuiltIn T_BuiltIn
Definition: PC.h:69
static void accumulateDotProduct(PC &sum, const PC &v0, const PC &v1)
Definition: PC.h:279
PC(const double v)
Definition: PC.h:85
PC T_Scalar
Definition: PC.h:68
static void setFloat(PC &t, const int i, const double &val)
Definition: PC.h:243
T_iterator _iter
Definition: PC.h:35
PCInitializer operator,(const double &x)
Definition: PC.h:105
Definition: PC.h:43
static PCSet * _pcSet
Definition: PC.h:299
PC & operator-=(const double &o)
Definition: PC.h:171
static void write(FILE *fp, const PC &x)
Definition: PC.h:258
PC(const PC &o)
Definition: PC.h:93
~PC()
Definition: PC.h:99
static PC getNegativeUnity()
Definition: PC.h:231
static string getTypeName()
Definition: PC.h:73
const double & operator[](int n) const
Definition: PC.h:103
double stdDev() const
Definition: PC.h:252
#define PC_BINARY_OP(opname, _op_)
Definition: PC.h:303
PC< ORDER, DIM > operator-(const PC< ORDER, DIM > &a)
Definition: PC.h:332
Initializer< double, double * > PCInitializer
Definition: PC.h:71
static void accumulateOneNorm(PC &sum, const PC &v)
Definition: PC.h:277
Definition: PC.h:20
static void reduceSum(PC &sum, const PC &x)
Definition: PC.h:284
PC & operator=(const PC &o)
Definition: PC.h:112
static double getFloat(const PC &t, const int i)
Definition: PC.h:247
PCSet * createPCSet(const int order, const int dim)
Definition: PC.cpp:9
static int getDimension()
Definition: PC.h:260
static PC getZero()
Definition: PC.h:218
PC_RELATIONAL_OPS(operator<,<)
static void setMax(PC &x, const PC &y)
Definition: PC.h:289
static void normalize(PC &x, const PC &y)
Definition: PC.h:287
void print(ostream &os) const
Definition: PC.h:210
double & operator[](int n)
Definition: PC.h:102
PC & operator-=(const PC &o)
Definition: PC.h:136
PC & operator=(const double &f)
Definition: PC.h:121
PC & operator*=(const PC &o)
Definition: PC.h:143
static double doubleMeasure(const PC &x)
Definition: PC.h:238
PC & operator/=(const PC &o)
Definition: PC.h:153
double min(double x, double y)
Definition: Octree.cpp:23
PC< ORDER, DIM > operator+(const PC< ORDER, DIM > &a)
Definition: PC.h:326
PC & operator/=(const double &o)
Definition: PC.h:184
PC & operator+=(const PC &o)
Definition: PC.h:129
double _data[N]
Definition: PC.h:293
PC fabs() const
Definition: PC.h:269
static void safeDivide(PC &x, const PC &y)
Definition: PC.h:286
PC & operator+=(const double &o)
Definition: PC.h:165
PC()
Definition: PC.h:78