Debian Codegraph @

packages/stokhos/src/sacado/Sacado_PCE_OrthogPoly.hpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Stokhos Package
4 //
5 // Copyright 2009 NTESS and the Stokhos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #ifndef SACADO_PCE_ORTHOGPOLY_HPP
11 #define SACADO_PCE_ORTHOGPOLY_HPP
12
13 #include "Stokhos_ConfigDefs.h"
14
15 #ifdef HAVE_STOKHOS_SACADO
16
17 #include "Teuchos_RCP.hpp"
18
19 #include "Sacado_Traits.hpp"
20 #include "Sacado_Handle.hpp"
21 #include "Sacado_mpl_apply.hpp"
22
23 #include "Stokhos_OrthogPolyExpansion.hpp"
24 #include "Stokhos_OrthogPolyApprox.hpp"
25 #include "Stokhos_ConstantOrthogPolyExpansion.hpp"
26
27 #include <cmath>
28 #include <algorithm> // for std::min and std::max
29 #include <ostream> // for std::ostream
30
31 namespace Sacado {
32
33 //! Namespace for polynomial chaos expansion classes
34 namespace PCE {
35
36 //! Generalized polynomial chaos expansion class
37 /*!
38 * Uses a handle and a "copy-on-write" strategy for efficient copying, but
39 * no expression templating.
40 */
41 template <typename T, typename Storage >
42 class OrthogPoly {
43 public:
44
45 //! Typename of values
46 typedef T value_type;
47
48 //! Typename of scalar's (which may be different from T)
49 typedef typename ScalarType<T>::type scalar_type;
50
51 //! Typename of ordinals
52 typedef int ordinal_type;
53
54 //! Typename of storage class
55 typedef Storage storage_type;
56
57 //! Basis type
58 typedef Stokhos::OrthogPolyBasis<ordinal_type,T> basis_type;
59
60 //! Expansion type
61 typedef Stokhos::OrthogPolyExpansion<ordinal_type,T,Storage> expansion_type;
62
63 //! Stokhos approximation type
64 typedef Stokhos::OrthogPolyApprox<ordinal_type,T,Storage> approx_type;
65
66 typedef typename approx_type::pointer pointer;
67 typedef typename approx_type::const_pointer const_pointer;
68 typedef typename approx_type::reference reference;
69 typedef typename approx_type::const_reference const_reference;
70
71 //! Turn OrthogPoly into a meta-function class usable with mpl::apply
72 template <typename S>
73 struct apply {
74 typedef typename Sacado::mpl::apply<Storage,ordinal_type,S>::type storage_type;
75 typedef OrthogPoly<S,storage_type> type;
76 };
77
78 //! Default constructor
79 /*!
80 * Sets size to 1 and first coefficient to 0 (represents a constant).
81 */
82 OrthogPoly();
83
84 //! Constructor with supplied value \c x
85 /*!
86 * Sets size to 1 and first coefficient to x (represents a constant).
87 */
88 OrthogPoly(const value_type& x);
89
90 //! Constructor with expansion \c expansion (General case)
91 /*!
92 * Creates array of correct size and initializes coeffiencts to 0.
93 */
94 OrthogPoly(const Teuchos::RCP<expansion_type>& expansion);
95
96 //! Constructor with expansion \c expansion and specified size \c sz
97 /*!
98 * Creates array of size \c sz and initializes coeffiencts to 0.
99 */
100 OrthogPoly(const Teuchos::RCP<expansion_type>& expansion,
101 ordinal_type sz);
102
103 //! Copy constructor
104 OrthogPoly(const OrthogPoly& x);
105
106 //! Destructor
107 ~OrthogPoly();
108
109 //! Initialize coefficients to value
110 void init(const T& v) { th->init(v); }
111
112 //! Initialize coefficients to an array of values
113 void init(const T* v) { th->init(v); }
114
115 //! Initialize coefficients from an OrthogPoly with different storage
116 template <typename S>
117 void init(const OrthogPoly<T,S>& v) { th->init(v.getOrthogPolyApprox()); }
118
119 //! Load coefficients to an array of values
120 void load(T* v) { th->load(v); }
121
122 //! Load coefficients into an OrthogPoly with different storage
123 template <typename S>
124 void load(OrthogPoly<T,S>& v) { th->load(v.getOrthogPolyApprox()); }
125
126 //! Reset expansion
127 /*!
128 * May change size of array. Coefficients are preserved.
129 */
130 void reset(const Teuchos::RCP<expansion_type>& expansion);
131
132 //! Reset expansion and size
133 /*!
134 * Coefficients are preserved.
135 */
136 void reset(const Teuchos::RCP<expansion_type>& expansion,
137 ordinal_type sz);
138
139 //! Prepare polynomial for writing
140 /*!
141 * This method prepares the polynomial for writing through coeff() and
142 * fastAccessCoeff() member functions. It ensures the handle for the
143 * %Hermite coefficients is not shared among any other %Hermite polynomial
144 * objects. If the handle is not shared it does nothing, so there
145 * is no cost in calling this method in this case. If the handle is
146 * shared and this method is not called, any changes to the coefficients
147 * by coeff() or fastAccessCoeff() may change other polynomial objects.
148 */
149 void copyForWrite() { th.makeOwnCopy(); }
150
151 //! Evaluate polynomial approximation at a point
152 value_type evaluate(const Teuchos::Array<value_type>& point) const;
153
154 //! Evaluate polynomial approximation at a point with given basis values
155 value_type evaluate(const Teuchos::Array<value_type>& point,
156 const Teuchos::Array<value_type>& bvals) const;
157
158 //! Compute mean of expansion
159 value_type mean() const {return th->mean(); }
160
161 //! Compute standard deviation of expansion
162 value_type standard_deviation() const { return th->standard_deviation(); }
163
164 //! Compute the two-norm of expansion
165 value_type two_norm() const { return th->two_norm(); }
166
167 //! Compute the squared two-norm of expansion
168 value_type two_norm_squared() const { return th->two_norm_squared(); }
169
170 //! Compute the L2 inner product of 2 PCEs
171 value_type inner_product(const OrthogPoly& b) const {
172 return th->inner_product(b.getOrthogPolyApprox()); }
173
174 //! Print approximation in basis
175 std::ostream& print(std::ostream& os) const { return th->print(os); }
176
177 //! Returns whether two PCE objects have the same values
178 bool isEqualTo(const OrthogPoly& x) const;
179
180 /*!
181 * @name Assignment operators
182 */
183 //@{
184
185 //! Assignment operator with constant right-hand-side
186 OrthogPoly<T,Storage>& operator=(const value_type& val);
187
188 //! Assignment with OrthogPoly right-hand-side
189 OrthogPoly<T,Storage>& operator=(const OrthogPoly<T,Storage>& x);
190
191 //@}
192
193 /*!
194 * Accessor methods
195 */
196 //@{
197
198 //! Get basis
199 Teuchos::RCP<const basis_type> basis() const { return th->basis(); }
200
201 //! Get expansion
202 Teuchos::RCP<expansion_type> expansion() const { return expansion_; }
203
204 //@}
205
206 /*!
207 * @name Value accessor methods
208 */
209 //@{
210
211 //! Returns value
212 const_reference val() const { return (*th)[0]; }
213
214 //! Returns value
215 reference val() { return (*th)[0]; }
216
217 //@}
218
219 /*!
220 * @name Coefficient accessor methods
221 */
222 //@{
223
224 //! Returns size of polynomial
225 ordinal_type size() const { return th->size();}
226
227 //! Returns true if polynomial has size >= sz
228 bool hasFastAccess(ordinal_type sz) const { return th->size()>=sz;}
229
230 //! Returns Hermite coefficient array
231 const_pointer coeff() const { return th->coeff();}
232
233 //! Returns Hermite coefficient array
234 pointer coeff() { return th->coeff();}
235
236 //! Returns degree \c i term with bounds checking
237 value_type coeff(ordinal_type i) const {
238 value_type tmp= i<th->size() ? (*th)[i]:value_type(0.); return tmp;}
239
240 //! Returns degree \c i term without bounds checking
241 reference fastAccessCoeff(ordinal_type i) { return (*th)[i];}
242
243 //! Returns degree \c i term without bounds checking
244 value_type fastAccessCoeff(ordinal_type i) const { return (*th)[i];}
245
246 //! Get coefficient term for given dimension and order
247 reference term(ordinal_type dimension, ordinal_type order) {
248 return th->term(dimension, order); }
249
250 //! Get coefficient term for given dimension and order
251 const_reference term(ordinal_type dimension, ordinal_type order) const {
252 return th->term(dimension, order); }
253
254 //! Get orders for a given term
255 Teuchos::Array<ordinal_type> order(ordinal_type term) const {
256 return th->order(term); }
257
258 //@}
259
260 /*!
261 * @name Unary operators
262 */
263 //@{
264
265 //! Unary-plus operator
266 OrthogPoly<T,Storage> operator + () const;
267
268 //! Unary-minus operator
269 OrthogPoly<T,Storage> operator - () const;
270
271 //! Addition-assignment operator with constant right-hand-side
272 OrthogPoly<T,Storage>& operator += (const value_type& x);
273
274 //! Subtraction-assignment operator with constant right-hand-side
275 OrthogPoly<T,Storage>& operator -= (const value_type& x);
276
277 //! Multiplication-assignment operator with constant right-hand-side
278 OrthogPoly<T,Storage>& operator *= (const value_type& x);
279
280 //! Division-assignment operator with constant right-hand-side
281 OrthogPoly<T,Storage>& operator /= (const value_type& x);
282
283 //! Addition-assignment operator with Hermite right-hand-side
284 OrthogPoly<T,Storage>& operator += (const OrthogPoly<T,Storage>& x);
285
286 //! Subtraction-assignment operator with Hermite right-hand-side
287 OrthogPoly<T,Storage>& operator -= (const OrthogPoly<T,Storage>& x);
288
289 //! Multiplication-assignment operator with Hermite right-hand-side
290 OrthogPoly<T,Storage>& operator *= (const OrthogPoly<T,Storage>& x);
291
292 //! Division-assignment operator with Hermite right-hand-side
293 OrthogPoly<T,Storage>& operator /= (const OrthogPoly<T,Storage>& x);
294
295 //@}
296
297 //! Get underlying Stokhos::OrthogPolyApprox
298 const approx_type& getOrthogPolyApprox() const { return *th; }
299
300 //! Get underlying Stokhos::OrthogPolyApprox
301 approx_type& getOrthogPolyApprox() { return *th; }
302
303 protected:
304
305 //! Expansion class
306 Teuchos::RCP<expansion_type> expansion_;
307
308 //! Constant expansion class for constants
309 Teuchos::RCP<expansion_type> const_expansion_;
310
311 Sacado::Handle< Stokhos::OrthogPolyApprox<int,value_type,Storage> > th;
312
313 }; // class Hermite
314
315 // Operations
316 template <typename T, typename Storage> OrthogPoly<T,Storage>
317 operator+(const OrthogPoly<T,Storage>& a, const OrthogPoly<T,Storage>& b);
318
319 template <typename T, typename Storage> OrthogPoly<T,Storage>
320 operator+(const typename OrthogPoly<T,Storage>::value_type& a,
321 const OrthogPoly<T,Storage>& b);
322
323 template <typename T, typename Storage> OrthogPoly<T,Storage>
324 operator+(const OrthogPoly<T,Storage>& a,
325 const typename OrthogPoly<T,Storage>::value_type& b);
326
327 template <typename T, typename Storage> OrthogPoly<T,Storage>
328 operator-(const OrthogPoly<T,Storage>& a, const OrthogPoly<T,Storage>& b);
329
330 template <typename T, typename Storage> OrthogPoly<T,Storage>
331 operator-(const typename OrthogPoly<T,Storage>::value_type& a,
332 const OrthogPoly<T,Storage>& b);
333
334 template <typename T, typename Storage> OrthogPoly<T,Storage>
335 operator-(const OrthogPoly<T,Storage>& a,
336 const typename OrthogPoly<T,Storage>::value_type& b);
337
338 template <typename T, typename Storage> OrthogPoly<T,Storage>
339 operator*(const OrthogPoly<T,Storage>& a, const OrthogPoly<T,Storage>& b);
340
341 template <typename T, typename Storage> OrthogPoly<T,Storage>
342 operator*(const typename OrthogPoly<T,Storage>::value_type& a,
343 const OrthogPoly<T,Storage>& b);
344
345 template <typename T, typename Storage> OrthogPoly<T,Storage>
346 operator*(const OrthogPoly<T,Storage>& a,
347 const typename OrthogPoly<T,Storage>::value_type& b);
348
349 template <typename T, typename Storage> OrthogPoly<T,Storage>
350 operator/(const OrthogPoly<T,Storage>& a, const OrthogPoly<T,Storage>& b);
351
352 template <typename T, typename Storage> OrthogPoly<T,Storage>
353 operator/(const typename OrthogPoly<T,Storage>::value_type& a,
354 const OrthogPoly<T,Storage>& b);
355
356 template <typename T, typename Storage> OrthogPoly<T,Storage>
357 operator/(const OrthogPoly<T,Storage>& a,
358 const typename OrthogPoly<T,Storage>::value_type& b);
359
360 template <typename T, typename Storage> OrthogPoly<T,Storage>
361 exp(const OrthogPoly<T,Storage>& a);
362
363 template <typename T, typename Storage> OrthogPoly<T,Storage>
364 log(const OrthogPoly<T,Storage>& a);
365
366 template <typename T, typename Storage> void
367 log(OrthogPoly<T,Storage>& c, const OrthogPoly<T,Storage>& a);
368
369 template <typename T, typename Storage> OrthogPoly<T,Storage>
370 log10(const OrthogPoly<T,Storage>& a);
371
372 template <typename T, typename Storage> OrthogPoly<T,Storage>
373 sqrt(const OrthogPoly<T,Storage>& a);
374
375 template <typename T, typename Storage> OrthogPoly<T,Storage>
376 cbrt(const OrthogPoly<T,Storage>& a);
377
378 template <typename T, typename Storage> OrthogPoly<T,Storage>
379 pow(const OrthogPoly<T,Storage>& a, const OrthogPoly<T,Storage>& b);
380
381 template <typename T, typename Storage> OrthogPoly<T,Storage>
382 pow(const T& a,
383 const OrthogPoly<T,Storage>& b);
384
385 template <typename T, typename Storage> OrthogPoly<T,Storage>
386 pow(const OrthogPoly<T,Storage>& a,
387 const T& b);
388
389 template <typename T, typename Storage> OrthogPoly<T,Storage>
390 cos(const OrthogPoly<T,Storage>& a);
391
392 template <typename T, typename Storage> OrthogPoly<T,Storage>
393 sin(const OrthogPoly<T,Storage>& a);
394
395 template <typename T, typename Storage> OrthogPoly<T,Storage>
396 tan(const OrthogPoly<T,Storage>& a);
397
398 template <typename T, typename Storage> OrthogPoly<T,Storage>
399 cosh(const OrthogPoly<T,Storage>& a);
400
401 template <typename T, typename Storage> OrthogPoly<T,Storage>
402 sinh(const OrthogPoly<T,Storage>& a);
403
404 template <typename T, typename Storage> OrthogPoly<T,Storage>
405 tanh(const OrthogPoly<T,Storage>& a);
406
407 template <typename T, typename Storage> OrthogPoly<T,Storage>
408 acos(const OrthogPoly<T,Storage>& a);
409
410 template <typename T, typename Storage> OrthogPoly<T,Storage>
411 asin(const OrthogPoly<T,Storage>& a);
412
413 template <typename T, typename Storage> OrthogPoly<T,Storage>
414 atan(const OrthogPoly<T,Storage>& a);
415
416 template <typename T, typename Storage> OrthogPoly<T,Storage>
417 atan2(const OrthogPoly<T,Storage>& a, const OrthogPoly<T,Storage>& b);
418
419 template <typename T, typename Storage> OrthogPoly<T,Storage>
420 atan2(const typename OrthogPoly<T,Storage>::value_type& a,
421 const OrthogPoly<T,Storage>& b);
422
423 template <typename T, typename Storage> OrthogPoly<T,Storage>
424 atan2(const OrthogPoly<T,Storage>& a,
425 const typename OrthogPoly<T,Storage>::value_type& b);
426
427 template <typename T, typename Storage> OrthogPoly<T,Storage>
428 acosh(const OrthogPoly<T,Storage>& a);
429
430 template <typename T, typename Storage> OrthogPoly<T,Storage>
431 asinh(const OrthogPoly<T,Storage>& a);
432
433 template <typename T, typename Storage> OrthogPoly<T,Storage>
434 atanh(const OrthogPoly<T,Storage>& a);
435
436 template <typename T, typename Storage> OrthogPoly<T,Storage>
437 abs(const OrthogPoly<T,Storage>& a);
438
439 template <typename T, typename Storage> OrthogPoly<T,Storage>
440 fabs(const OrthogPoly<T,Storage>& a);
441
442 template <typename T, typename Storage> OrthogPoly<T,Storage>
443 max(const OrthogPoly<T,Storage>& a, const OrthogPoly<T,Storage>& b);
444
445 template <typename T, typename Storage> OrthogPoly<T,Storage>
446 max(const typename OrthogPoly<T,Storage>::value_type& a,
447 const OrthogPoly<T,Storage>& b);
448
449 template <typename T, typename Storage> OrthogPoly<T,Storage>
450 max(const OrthogPoly<T,Storage>& a,
451 const typename OrthogPoly<T,Storage>::value_type& b);
452
453 template <typename T, typename Storage> OrthogPoly<T,Storage>
454 min(const OrthogPoly<T,Storage>& a, const OrthogPoly<T,Storage>& b);
455
456 template <typename T, typename Storage> OrthogPoly<T,Storage>
457 min(const typename OrthogPoly<T,Storage>::value_type& a,
458 const OrthogPoly<T,Storage>& b);
459
460 template <typename T, typename Storage> OrthogPoly<T,Storage>
461 min(const OrthogPoly<T,Storage>& a,
462 const typename OrthogPoly<T,Storage>::value_type& b);
463
464 template <typename T, typename Storage> bool
465 operator==(const OrthogPoly<T,Storage>& a,
466 const OrthogPoly<T,Storage>& b);
467
468 template <typename T, typename Storage> bool
469 operator==(const typename OrthogPoly<T,Storage>::value_type& a,
470 const OrthogPoly<T,Storage>& b);
471
472 template <typename T, typename Storage> bool
473 operator==(const OrthogPoly<T,Storage>& a,
474 const typename OrthogPoly<T,Storage>::value_type& b);
475
476 template <typename T, typename Storage> bool
477 operator!=(const OrthogPoly<T,Storage>& a,
478 const OrthogPoly<T,Storage>& b);
479
480 template <typename T, typename Storage> bool
481 operator!=(const typename OrthogPoly<T,Storage>::value_type& a,
482 const OrthogPoly<T,Storage>& b);
483
484 template <typename T, typename Storage> bool
485 operator!=(const OrthogPoly<T,Storage>& a,
486 const typename OrthogPoly<T,Storage>::value_type& b);
487
488 template <typename T, typename Storage> bool
489 operator<=(const OrthogPoly<T,Storage>& a,
490 const OrthogPoly<T,Storage>& b);
491
492 template <typename T, typename Storage> bool
493 operator<=(const typename OrthogPoly<T,Storage>::value_type& a,
494 const OrthogPoly<T,Storage>& b);
495
496 template <typename T, typename Storage> bool
497 operator<=(const OrthogPoly<T,Storage>& a,
498 const typename OrthogPoly<T,Storage>::value_type& b);
499
500 template <typename T, typename Storage> bool
501 operator>=(const OrthogPoly<T,Storage>& a,
502 const OrthogPoly<T,Storage>& b);
503
504 template <typename T, typename Storage> bool
505 operator>=(const typename OrthogPoly<T,Storage>::value_type& a,
506 const OrthogPoly<T,Storage>& b);
507
508 template <typename T, typename Storage> bool
509 operator>=(const OrthogPoly<T,Storage>& a,
510 const typename OrthogPoly<T,Storage>::value_type& b);
511
512 template <typename T, typename Storage> bool
513 operator<(const OrthogPoly<T,Storage>& a,
514 const OrthogPoly<T,Storage>& b);
515
516 template <typename T, typename Storage> bool
517 operator<(const typename OrthogPoly<T,Storage>::value_type& a,
518 const OrthogPoly<T,Storage>& b);
519
520 template <typename T, typename Storage> bool
521 operator<(const OrthogPoly<T,Storage>& a,
522 const typename OrthogPoly<T,Storage>::value_type& b);
523
524 template <typename T, typename Storage> bool
525 operator>(const OrthogPoly<T,Storage>& a,
526 const OrthogPoly<T,Storage>& b);
527
528 template <typename T, typename Storage> bool
529 operator>(const typename OrthogPoly<T,Storage>::value_type& a,
530 const OrthogPoly<T,Storage>& b);
531
532 template <typename T, typename Storage> bool
533 operator>(const OrthogPoly<T,Storage>& a,
534 const typename OrthogPoly<T,Storage>::value_type& b);
535
536 template <typename T, typename Storage> std::ostream&
537 operator << (std::ostream& os, const OrthogPoly<T,Storage>& a);
538
539 template <typename T, typename Storage> std::istream&
540 operator >> (std::istream& os, OrthogPoly<T,Storage>& a);
541
542 } // namespace PCE
543
544 } // namespace Sacado
545
546 #include "Sacado_PCE_OrthogPolyTraits.hpp"
547 #include "Sacado_PCE_OrthogPolyImp.hpp"
548
549 #endif // HAVE_STOKHOS_SACADO
550
551 #endif // SACADO_PCE_ORTHOGPOLY_HPP