Debian Codegraph @

packages/stokhos/src/sacado/kokkos/pce/Sacado_UQ_PCE.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_UQ_PCE_HPP
11 #define SACADO_UQ_PCE_HPP
12
13 #include "Stokhos_ConfigDefs.h"
14
15 #ifdef HAVE_STOKHOS_SACADO
16
17 #include "Kokkos_Macros.hpp"
18
19 #include "Sacado_Traits.hpp"
20 #include "Sacado_mpl_apply.hpp"
21 #include "Stokhos_Is_Constant.hpp"
22
23 #include "Stokhos_CrsProductTensor.hpp"
24
25 #include <cmath>
26 #include <algorithm> // for std::min and std::max
27 #include <ostream> // for std::ostream
28 #include <initializer_list>
29
30 namespace Sacado {
31
32 // Forward declaration
33 template <typename Storage>
34 KOKKOS_INLINE_FUNCTION
35 bool is_constant(const Sacado::UQ::PCE<Storage>& x);
36
37 //! Namespace for UQ classes classes
38 namespace UQ {
39
40 //! Generalized polynomial chaos expansion class
41 /*!
42 * Uses a handle and a "copy-on-write" strategy for efficient copying, but
43 * no expression templating.
44 */
45 template <typename Storage >
46 class PCE {
47
48 template <class> friend class PCE;
49
50 public:
51
52 //! Typename of storage class
53 typedef Storage storage_type;
54
55 typedef typename storage_type::value_type value_type;
56 typedef typename storage_type::ordinal_type ordinal_type;
57 typedef typename storage_type::execution_space execution_space;
58 typedef typename storage_type::pointer pointer;
59 typedef typename storage_type::volatile_pointer volatile_pointer;
60 typedef typename storage_type::const_pointer const_pointer;
61 typedef typename storage_type::const_volatile_pointer const_volatile_pointer;
62 typedef typename storage_type::reference reference;
63 typedef typename storage_type::volatile_reference volatile_reference;
64 typedef typename storage_type::const_reference const_reference;
65 typedef typename storage_type::const_volatile_reference const_volatile_reference;
66
67 //! Typename of scalar's (which may be different from value_type)
68 typedef typename ScalarType<value_type>::type scalar_type;
69
70 //! Basis type
71 //typedef Stokhos::OrthogPolyBasis<ordinal_type,value_type> basis_type;
72
73 //! Cijk type
74 typedef Stokhos::CrsProductTensor<value_type, execution_space, Kokkos::MemoryUnmanaged> my_cijk_type;
75 typedef Stokhos::CrsProductTensor<value_type, execution_space> cijk_type;
76
77 //! Turn PCE into a meta-function class usable with mpl::apply
78 template <typename S>
79 struct apply {
80 typedef PCE<S> type;
81 };
82
83 //! Default constructor
84 /*!
85 * May not intialize the coefficient array.
86 */
87 KOKKOS_DEFAULTED_FUNCTION
88 PCE() = default;
89
90 //! Constructor with supplied value \c x
91 /*!
92 * Sets size to 1 and first coefficient to x (represents a constant).
93 */
94 KOKKOS_INLINE_FUNCTION
95 PCE(const value_type& x) : cijk_(), s_(1, x) {}
96
97 //! Constructor with Cijk \c cijk (General case)
98 /*!
99 * Creates array of correct size and initializes coeffiencts to 0.
100 */
101 template <typename M>
102 KOKKOS_INLINE_FUNCTION
103 PCE(const Stokhos::CrsProductTensor<value_type, execution_space, M>& cijkVal) :
104 cijk_(cijkVal), s_(cijk_.dimension()) {}
105
106 //! Constructor with Cijk \c cijk and specified size \c sz
107 /*!
108 * Creates array of size \c sz and initializes coeffiencts to 0.
109 */
110 template <typename M>
111 KOKKOS_INLINE_FUNCTION
112 PCE(const Stokhos::CrsProductTensor<value_type, execution_space, M>& cijkVal,
113 ordinal_type sz) : cijk_(cijkVal), s_(sz) {}
114
115 //! View constructor
116 /*!
117 * Creates PCE with pre-allocated data. Set \c owned = true
118 * if this PCE should take over management of the data.
119 */
120 template <typename M>
121 KOKKOS_INLINE_FUNCTION
122 PCE(const Stokhos::CrsProductTensor<value_type, execution_space, M>& cijkVal,
123 ordinal_type sz, pointer v, bool owned) :
124 cijk_(cijkVal), s_(sz,v,owned) {}
125
126 //! Copy constructor
127 KOKKOS_INLINE_FUNCTION
128 PCE(const PCE& x) : cijk_(x.cijk_), s_(1,x.fastAccessCoeff(0)) {
129 if (x.size() > 1 && !is_constant(x))
130 s_ = x.s_;
131 }
132
133 //! Copy constructor
134 KOKKOS_INLINE_FUNCTION
135 PCE(const volatile PCE& x) :
136 cijk_(const_cast<const my_cijk_type&>(x.cijk_)),
137 s_(1,value_type(x.fastAccessCoeff(0))) {
138 if (x.size() > 1 && !is_constant(x))
139 s_ = x.s_;
140 }
141
142 //! Intialize from initializer_list
143 /*!
144 * No KOKKOS_INLINE_FUNCTION as it is not callable from the device
145 */
146 PCE(std::initializer_list<value_type> l) :
147 cijk_(), s_(l.size(), l.begin()) {}
148
149 //! Destructor
150 KOKKOS_DEFAULTED_FUNCTION
151 ~PCE() = default;
152
153 //! Initialize coefficients to value
154 KOKKOS_INLINE_FUNCTION
155 void init(const_reference v) { s_.init(v); }
156
157 //! Initialize coefficients to value
158 KOKKOS_INLINE_FUNCTION
159 void init(const_reference v) volatile { s_.init(v); }
160
161 //! Initialize coefficients to an array of values
162 KOKKOS_INLINE_FUNCTION
163 void init(const_pointer v) { s_.init(v); }
164
165 //! Initialize coefficients to an array of values
166 KOKKOS_INLINE_FUNCTION
167 void init(const_pointer v) volatile { s_.init(v); }
168
169 //! Initialize coefficients from an PCE with different storage
170 template <typename S>
171 KOKKOS_INLINE_FUNCTION
172 void init(const PCE<S>& v) { s_.init(v.coeff()); }
173
174 //! Initialize coefficients from an PCE with different storage
175 template <typename S>
176 KOKKOS_INLINE_FUNCTION
177 void init(const PCE<S>& v) volatile { s_.init(v.coeff()); }
178
179 //! Load coefficients to an array of values
180 KOKKOS_INLINE_FUNCTION
181 void load(pointer v) { s_.load(v); }
182
183 //! Load coefficients to an array of values
184 KOKKOS_INLINE_FUNCTION
185 void load(pointer v) volatile { s_.load(v); }
186
187 //! Load coefficients into an PCE with different storage
188 template <typename S>
189 KOKKOS_INLINE_FUNCTION
190 void load(PCE<S>& v) { s_.load(v.coeff()); }
191
192 //! Load coefficients into an PCE with different storage
193 template <typename S>
194 KOKKOS_INLINE_FUNCTION
195 void load(PCE<S>& v) volatile { s_.load(v.coeff()); }
196
197 //! Reset expansion
198 /*!
199 * May change size of array. Coefficients are preserved.
200 */
201 template <typename M>
202 KOKKOS_INLINE_FUNCTION
203 void reset(const Stokhos::CrsProductTensor<value_type, execution_space, M>& cijkVal) {
204 cijk_ = cijkVal;
205 s_.resize(cijk_.dimension());
206 }
207
208 //! Reset expansion
209 /*!
210 * May change size of array. Coefficients are preserved.
211 */
212 template <typename M>
213 KOKKOS_INLINE_FUNCTION
214 void reset(const Stokhos::CrsProductTensor<value_type, execution_space, M>& cijkVal) volatile {
215 cijk_ = cijkVal;
216 s_.resize(cijk_.dimension());
217 }
218
219 //! Reset expansion and size
220 /*!
221 * Coefficients are preserved.
222 */
223 template <typename M>
224 KOKKOS_INLINE_FUNCTION
225 void reset(const Stokhos::CrsProductTensor<value_type, execution_space, M>& cijkVal, ordinal_type sz) {
226 cijk_ = cijkVal;
227 s_.resize(sz);
228 }
229
230 //! Reset expansion and size
231 /*!
232 * Coefficients are preserved.
233 */
234 template <typename M>
235 KOKKOS_INLINE_FUNCTION
236 void reset(const Stokhos::CrsProductTensor<value_type, execution_space, M>& cijkVal, ordinal_type sz) volatile {
237 cijk_ = cijkVal;
238 s_.resize(sz);
239 }
240
241 //! Prepare polynomial for writing
242 /*!
243 * This method prepares the polynomial for writing through coeff() and
244 * fastAccessCoeff() member functions. It ensures the handle for the
245 * %PCE coefficients is not shared among any other %PCE polynomial
246 * objects. If the handle is not shared it does nothing, so there
247 * is no cost in calling this method in this case. If the handle is
248 * shared and this method is not called, any changes to the coefficients
249 * by coeff() or fastAccessCoeff() may change other polynomial objects.
250 */
251 KOKKOS_INLINE_FUNCTION
252 void copyForWrite() { }
253
254 /*
255 //! Evaluate polynomial approximation at a point
256 KOKKOS_INLINE_FUNCTION
257 value_type evaluate(const Teuchos::Array<value_type>& point) const;
258
259 //! Evaluate polynomial approximation at a point with given basis values
260 KOKKOS_INLINE_FUNCTION
261 value_type evaluate(const Teuchos::Array<value_type>& point,
262 const Teuchos::Array<value_type>& bvals) const;
263 */
264
265 //! Compute mean of expansion
266 KOKKOS_INLINE_FUNCTION
267 value_type mean() const { return this->fastAccessCoeff(0); }
268
269 //! Compute standard deviation of expansion
270 KOKKOS_INLINE_FUNCTION
271 value_type standard_deviation() const;
272
273 //! Compute the two-norm of expansion
274 KOKKOS_INLINE_FUNCTION
275 value_type two_norm() const {
276 return std::sqrt(this->two_norm_squared());
277 }
278
279 //! Compute the squared two-norm of expansion
280 KOKKOS_INLINE_FUNCTION
281 value_type two_norm_squared() const;
282
283 //! Compute the L2 inner product of 2 PCEs
284 KOKKOS_INLINE_FUNCTION
285 value_type inner_product(const PCE& x) const;
286
287 //! Returns whether two PCE objects have the same values
288 KOKKOS_INLINE_FUNCTION
289 bool isEqualTo(const PCE& x) const;
290
291 //! Returns whether two PCE objects have the same values
292 KOKKOS_INLINE_FUNCTION
293 bool isEqualTo(const PCE& x) const volatile;
294
295 /*!
296 * @name Assignment operators
297 */
298 //@{
299
300 //! Assignment operator with constant right-hand-side
301 KOKKOS_INLINE_FUNCTION
302 PCE<Storage>& operator=(const value_type val);
303
304 //! Assignment operator with constant right-hand-side
305 KOKKOS_INLINE_FUNCTION
306 /*volatile*/ PCE<Storage>& operator=(const value_type val) volatile;
307
308 //! Assignment with PCE right-hand-side
309 KOKKOS_INLINE_FUNCTION
310 PCE<Storage>& operator=(const PCE<Storage>& x);
311
312 //! Assignment with PCE right-hand-side
313 KOKKOS_INLINE_FUNCTION
314 PCE<Storage>& operator=(const volatile PCE<Storage>& x);
315
316 //! Assignment with PCE right-hand-side
317 KOKKOS_INLINE_FUNCTION
318 /*volatile*/ PCE<Storage>& operator=(const PCE<Storage>& x) volatile;
319
320 //! Assignment with PCE right-hand-side
321 KOKKOS_INLINE_FUNCTION
322 /*volatile*/ PCE<Storage>& operator=(const volatile PCE<Storage>& x) volatile;
323
324 //! Assignment with PCE right-hand-side and different storage
325 template <typename S>
326 KOKKOS_INLINE_FUNCTION
327 PCE<Storage>& operator=(const PCE<S>& x) {
328 // Don't copy cijk as it may be on a different device
329 const ordinal_type sz_new = x.size();
330 s_.resize(sz_new);
331 for (ordinal_type i=0; i<sz_new; i++)
332 s_[i] = x.s_[i];
333
334 // For DyamicStorage as a view (is_owned=false), we need to set
335 // the trailing entries when assigning a constant vector (because
336 // the copy constructor in this case doesn't reset the size of this)
337 const ordinal_type sz = s_.size();
338 if (sz > sz_new)
339 for (ordinal_type i=sz_new; i<sz; i++)
340 s_[i] = value_type(0);
341
342 return *this;
343 }
344
345 //! Assignment from initializer_list
346 /*!
347 * No KOKKOS_INLINE_FUNCTION as it is not callable from the device
348 */
349 PCE& operator=(std::initializer_list<value_type> l) {
350 const ordinal_type lsz = l.size();
351 if (lsz != s_.size())
352 s_.resize(lsz);
353 s_.init(l.begin(), lsz);
354 return *this;
355 }
356
357 //! Assignment from initializer_list
358 /*!
359 * No KOKKOS_INLINE_FUNCTION as it is not callable from the device
360 */
361 /*volatile*/ PCE&
362 operator=(std::initializer_list<value_type> l) volatile {
363 const ordinal_type lsz = l.size();
364 if (lsz != s_.size())
365 s_.resize(lsz);
366 s_.init(l.begin(), lsz);
367 return const_cast<PCE&>(*this);
368 }
369
370 //@}
371
372 /*!
373 * Accessor methods
374 */
375 //@{
376
377 /*
378 //! Get basis
379 KOKKOS_INLINE_FUNCTION
380 Teuchos::RCP<const basis_type> basis() const { return s_.basis(); }
381 */
382
383 //! Get cijk
384 KOKKOS_INLINE_FUNCTION
385 my_cijk_type cijk() const { return cijk_; }
386
387 //! Get cijk
388 KOKKOS_INLINE_FUNCTION
389 my_cijk_type cijk() const volatile {
390 return const_cast<const my_cijk_type&>(cijk_);
391 }
392
393 //@}
394
395 /*!
396 * @name Value accessor methods
397 */
398 //@{
399
400 //! Returns value
401 KOKKOS_INLINE_FUNCTION
402 const_volatile_reference val() const volatile { return s_[0]; }
403
404 //! Returns value
405 KOKKOS_INLINE_FUNCTION
406 const_reference val() const { return s_[0]; }
407
408 //! Returns value
409 KOKKOS_INLINE_FUNCTION
410 volatile_reference val() volatile { return s_[0]; }
411
412 //! Returns value
413 KOKKOS_INLINE_FUNCTION
414 reference val() { return s_[0]; }
415
416 //@}
417
418 /*!
419 * @name Coefficient accessor methods
420 */
421 //@{
422
423 //! Returns size of polynomial
424 KOKKOS_INLINE_FUNCTION
425 ordinal_type size() const { return s_.size();}
426
427 //! Returns size of polynomial
428 KOKKOS_INLINE_FUNCTION
429 ordinal_type size() const volatile { return s_.size();}
430
431 //! Returns true if polynomial has size >= sz
432 KOKKOS_INLINE_FUNCTION
433 bool hasFastAccess(ordinal_type sz) const { return s_.size()>=sz;}
434
435 //! Returns true if polynomial has size >= sz
436 KOKKOS_INLINE_FUNCTION
437 bool hasFastAccess(ordinal_type sz) const volatile {
438 return s_.size()>=sz;
439 }
440
441 //! Returns coefficient array
442 KOKKOS_INLINE_FUNCTION
443 const_pointer coeff() const { return s_.coeff();}
444
445 //! Returns coefficient array
446 KOKKOS_INLINE_FUNCTION
447 const_volatile_pointer coeff() const volatile { return s_.coeff();}
448
449 //! Returns coefficient array
450 KOKKOS_INLINE_FUNCTION
451 volatile_pointer coeff() volatile { return s_.coeff();}
452
453 //! Returns coefficient array
454 KOKKOS_INLINE_FUNCTION
455 pointer coeff() { return s_.coeff();}
456
457 //! Returns degree \c i term with bounds checking
458 KOKKOS_INLINE_FUNCTION
459 value_type coeff(ordinal_type i) const {
460 value_type tmp= i<s_.size() ? s_[i]:value_type(0.); return tmp;}
461
462 //! Returns degree \c i term with bounds checking
463 KOKKOS_INLINE_FUNCTION
464 value_type coeff(ordinal_type i) const volatile {
465 value_type tmp= i<s_.size() ? s_[i]:value_type(0.); return tmp;}
466
467 //! Returns degree \c i term without bounds checking
468 KOKKOS_INLINE_FUNCTION
469 const_volatile_reference fastAccessCoeff(ordinal_type i) const volatile {
470 return s_[i];}
471
472 //! Returns degree \c i term without bounds checking
473 KOKKOS_INLINE_FUNCTION
474 const_reference fastAccessCoeff(ordinal_type i) const {
475 return s_[i];}
476
477 //! Returns degree \c i term without bounds checking
478 KOKKOS_INLINE_FUNCTION
479 volatile_reference fastAccessCoeff(ordinal_type i) volatile {
480 return s_[i];}
481
482 //! Returns degree \c i term without bounds checking
483 KOKKOS_INLINE_FUNCTION
484 reference fastAccessCoeff(ordinal_type i) {
485 return s_[i];}
486
487 /*
488 //! Get coefficient term for given dimension and order
489 KOKKOS_INLINE_FUNCTION
490 reference term(ordinal_type dimension, ordinal_type order) {
491 return s_.term(dimension, order); }
492
493 //! Get coefficient term for given dimension and order
494 KOKKOS_INLINE_FUNCTION
495 const_reference term(ordinal_type dimension, ordinal_type order) const {
496 return s_.term(dimension, order); }
497
498 //! Get orders for a given term
499 KOKKOS_INLINE_FUNCTION
500 Teuchos::Array<ordinal_type> order(ordinal_type term) const {
501 return s_.order(term); }
502 */
503
504 //! Return iterator to first element of coefficient array
505 KOKKOS_INLINE_FUNCTION
506 pointer begin() { return s_.coeff(); }
507
508 //! Return iterator to first element of coefficient array
509 KOKKOS_INLINE_FUNCTION
510 const_pointer begin() const { return s_.coeff(); }
511
512 //! Return iterator to first element of coefficient array
513 KOKKOS_INLINE_FUNCTION
514 volatile_pointer begin() volatile { return s_.coeff(); }
515
516 //! Return iterator to first element of coefficient array
517 KOKKOS_INLINE_FUNCTION
518 const_volatile_pointer begin() const volatile { return s_.coeff(); }
519
520 //! Return iterator to first element of coefficient array
521 KOKKOS_INLINE_FUNCTION
522 const_pointer cbegin() const { return s_.coeff(); }
523
524 //! Return iterator to first element of coefficient array
525 KOKKOS_INLINE_FUNCTION
526 const_volatile_pointer cbegin() const volatile { return s_.coeff(); }
527
528 //! Return iterator following last element of coefficient array
529 KOKKOS_INLINE_FUNCTION
530 pointer end() { return s_.coeff() + s_.size(); }
531
532 //! Return iterator following last element of coefficient array
533 KOKKOS_INLINE_FUNCTION
534 const_pointer end() const { return s_.coeff() + s_.size(); }
535
536 //! Return iterator following last element of coefficient array
537 KOKKOS_INLINE_FUNCTION
538 volatile_pointer end() volatile { return s_.coeff() + s_.size(); }
539
540 //! Return iterator following last element of coefficient array
541 KOKKOS_INLINE_FUNCTION
542 const_volatile_pointer end() const volatile { return s_.coeff() + s_.size(); }
543
544 //! Return iterator following last element of coefficient array
545 KOKKOS_INLINE_FUNCTION
546 const_pointer cend() const { return s_.coeff()+ s_.size(); }
547
548 //! Return iterator following last element of coefficient array
549 KOKKOS_INLINE_FUNCTION
550 const_volatile_pointer cend() const volatile { return s_.coeff()+ s_.size(); }
551
552 //@}
553
554 /*!
555 * @name Unary operators
556 */
557 //@{
558
559 //! Unary-plus operator
560 KOKKOS_INLINE_FUNCTION
561 PCE operator + () const { return *this; }
562
563 //! Unary-plus operator
564 KOKKOS_INLINE_FUNCTION
565 PCE operator + () const volatile { return *this; }
566
567 //! Unary-minus operator
568 KOKKOS_INLINE_FUNCTION
569 PCE operator - () const;
570
571 //! Unary-minus operator
572 KOKKOS_INLINE_FUNCTION
573 PCE operator - () const volatile;
574
575 //! Addition-assignment operator with constant right-hand-side
576 KOKKOS_INLINE_FUNCTION
577 PCE& operator += (const value_type x) {
578 s_[0] += x;
579 return *this;
580 }
581
582 //! Addition-assignment operator with constant right-hand-side
583 KOKKOS_INLINE_FUNCTION
584 /*volatile*/ PCE& operator += (const value_type x) volatile {
585 s_[0] += x;
586 return const_cast<PCE&>(*this);
587 }
588
589 //! Subtraction-assignment operator with constant right-hand-side
590 KOKKOS_INLINE_FUNCTION
591 PCE& operator -= (const value_type x) {
592 s_[0] -= x;
593 return *this;
594 }
595
596 //! Subtraction-assignment operator with constant right-hand-side
597 KOKKOS_INLINE_FUNCTION
598 /*volatile*/ PCE& operator -= (const value_type x) volatile {
599 s_[0] -= x;
600 return const_cast<PCE&>(*this);
601 }
602
603 //! Multiplication-assignment operator with constant right-hand-side
604 KOKKOS_INLINE_FUNCTION
605 PCE& operator *= (const value_type x);
606
607 //! Multiplication-assignment operator with constant right-hand-side
608 KOKKOS_INLINE_FUNCTION
609 /*volatile*/ PCE& operator *= (const value_type x) volatile;
610
611 //! Division-assignment operator with constant right-hand-side
612 KOKKOS_INLINE_FUNCTION
613 PCE& operator /= (const value_type x);
614
615 //! Division-assignment operator with constant right-hand-side
616 KOKKOS_INLINE_FUNCTION
617 /*volatile*/ PCE& operator /= (const value_type x) volatile;
618
619 //! Addition-assignment operator with PCE right-hand-side
620 KOKKOS_INLINE_FUNCTION
621 PCE& operator += (const PCE& x);
622
623 //! Addition-assignment operator with PCE right-hand-side
624 KOKKOS_INLINE_FUNCTION
625 PCE& operator += (const volatile PCE& x) {
626 return *this += const_cast<const PCE&>(x);
627 }
628
629 //! Addition-assignment operator with PCE right-hand-side
630 KOKKOS_INLINE_FUNCTION
631 /*volatile*/ PCE& operator += (const PCE& x) volatile {
632 return const_cast<PCE&>(*this) += x;
633 }
634
635 //! Addition-assignment operator with PCE right-hand-side
636 KOKKOS_INLINE_FUNCTION
637 /*volatile*/ PCE& operator += (const volatile PCE& x) volatile {
638 return const_cast<PCE&>(*this) += const_cast<const PCE&>(x);
639 }
640
641 //! Subtraction-assignment operator with PCE right-hand-side
642 KOKKOS_INLINE_FUNCTION
643 PCE& operator -= (const PCE& x);
644
645 //! Subtraction-assignment operator with PCE right-hand-side
646 KOKKOS_INLINE_FUNCTION
647 PCE& operator -= (const volatile PCE& x) {
648 return *this -= const_cast<const PCE&>(x);
649 }
650
651 //! Subtraction-assignment operator with PCE right-hand-side
652 KOKKOS_INLINE_FUNCTION
653 /*volatile*/ PCE& operator -= (const PCE& x) volatile {
654 return const_cast<PCE&>(*this) -= x;
655 }
656
657 //! Subtraction-assignment operator with PCE right-hand-side
658 KOKKOS_INLINE_FUNCTION
659 /*volatile*/ PCE& operator -= (const volatile PCE& x) volatile {
660 return const_cast<PCE&>(*this) -= const_cast<const PCE&>(x);
661 }
662
663 //! Multiplication-assignment operator with PCE right-hand-side
664 KOKKOS_INLINE_FUNCTION
665 PCE& operator *= (const PCE& x);
666
667 //! Multiplication-assignment operator with PCE right-hand-side
668 KOKKOS_INLINE_FUNCTION
669 PCE& operator *= (const volatile PCE& x) {
670 return *this *= const_cast<const PCE&>(x);
671 }
672
673 //! Multiplication-assignment operator with PCE right-hand-side
674 KOKKOS_INLINE_FUNCTION
675 /*volatile*/ PCE& operator *= (const PCE& x) volatile {
676 return const_cast<PCE&>(*this) *= x;
677 }
678
679 //! Multiplication-assignment operator with PCE right-hand-side
680 KOKKOS_INLINE_FUNCTION
681 /*volatile*/ PCE& operator *= (const volatile PCE& x) volatile {
682 return const_cast<PCE&>(*this) *= const_cast<const PCE&>(x);
683 }
684
685 //! Division-assignment operator with PCE right-hand-side
686 KOKKOS_INLINE_FUNCTION
687 PCE& operator /= (const PCE& x);
688
689 //! Division-assignment operator with PCE right-hand-side
690 KOKKOS_INLINE_FUNCTION
691 PCE& operator /= (const volatile PCE& x) {
692 return *this /= const_cast<const PCE&>(x);
693 }
694
695 //! Division-assignment operator with PCE right-hand-side
696 KOKKOS_INLINE_FUNCTION
697 /*volatile*/ PCE& operator /= (const PCE& x) volatile {
698 return const_cast<PCE&>(*this) /= x;
699 }
700
701 //! Division-assignment operator with PCE right-hand-side
702 KOKKOS_INLINE_FUNCTION
703 /*volatile*/ PCE& operator /= (const volatile PCE& x) volatile {
704 return const_cast<PCE&>(*this) /= const_cast<const PCE&>(x);
705 }
706
707 //! Prefix ++
708 KOKKOS_INLINE_FUNCTION
709 PCE& operator++() {
710 ++(s_[0]);
711 return *this;
712 }
713
714 //! Prefix ++
715 KOKKOS_INLINE_FUNCTION
716 volatile PCE& operator++() volatile {
717 ++(s_[0]);
718 return *this;
719 }
720
721 //! Postfix ++
722 KOKKOS_INLINE_FUNCTION
723 PCE operator++(int) {
724 PCE tmp(*this);
725 ++(*this);
726 return tmp;
727 }
728
729 //! Postfix ++
730 KOKKOS_INLINE_FUNCTION
731 PCE operator++(int) volatile {
732 PCE tmp(*this);
733 ++(*this);
734 return tmp;
735 }
736
737 //! Prefix --
738 KOKKOS_INLINE_FUNCTION
739 PCE& operator--() {
740 --(s_[0]);
741 return *this;
742 }
743
744 //! Prefix --
745 KOKKOS_INLINE_FUNCTION
746 volatile PCE& operator--() volatile {
747 --(s_[0]);
748 return *this;
749 }
750
751 //! Postfix --
752 KOKKOS_INLINE_FUNCTION
753 PCE operator--(int) {
754 PCE tmp(*this);
755 --(*this);
756 return tmp;
757 }
758
759 //! Postfix --
760 KOKKOS_INLINE_FUNCTION
761 PCE operator--(int) volatile {
762 PCE tmp(*this);
763 --(*this);
764 return tmp;
765 }
766
767
768
769 //@}
770
771 protected:
772
773 typedef typename my_cijk_type::size_type cijk_size_type;
774
775 //! Cijk class
776 my_cijk_type cijk_;
777
778 Storage s_;
779
780 private:
781
782 //PCE division using CG with diagonal preconditioning
783 // KOKKOS_INLINE_FUNCTION
784 // void CG_divide(const PCE& a, const PCE& b, PCE& c);
785
786
787 }; // class PCE
788
789 template <typename Storage>
790 KOKKOS_INLINE_FUNCTION
791 void CG_divide(const PCE<Storage>& a, const PCE<Storage>& b, PCE<Storage>& c);
792
793 // Operations
794 template <typename Storage>
795 KOKKOS_INLINE_FUNCTION
796 PCE<Storage>
797 operator+(const PCE<Storage>& a, const PCE<Storage>& b);
798
799 template <typename Storage>
800 KOKKOS_INLINE_FUNCTION
801 PCE<Storage>
802 operator+(const volatile PCE<Storage>& a, const PCE<Storage>& b) {
803 return const_cast<const PCE<Storage>&>(a) + b;
804 }
805
806 template <typename Storage>
807 KOKKOS_INLINE_FUNCTION
808 PCE<Storage>
809 operator+(const PCE<Storage>& a, const volatile PCE<Storage>& b) {
810 return a + const_cast<const PCE<Storage>&>(b);
811 }
812
813 template <typename Storage>
814 KOKKOS_INLINE_FUNCTION
815 PCE<Storage>
816 operator+(const volatile PCE<Storage>& a, const volatile PCE<Storage>& b) {
817 return const_cast<const PCE<Storage>&>(a) +
818 const_cast<const PCE<Storage>&>(b);
819 }
820
821 template <typename Storage>
822 KOKKOS_INLINE_FUNCTION
823 PCE<Storage>
824 operator+(const typename PCE<Storage>::value_type& a,
825 const PCE<Storage>& b);
826
827 template <typename Storage>
828 KOKKOS_INLINE_FUNCTION
829 PCE<Storage>
830 operator+(const PCE<Storage>& a,
831 const typename PCE<Storage>::value_type& b);
832
833 template <typename Storage>
834 KOKKOS_INLINE_FUNCTION
835 PCE<Storage>
836 operator-(const PCE<Storage>& a, const PCE<Storage>& b);
837
838 template <typename Storage> PCE<Storage>
839 KOKKOS_INLINE_FUNCTION
840 operator-(const typename PCE<Storage>::value_type& a,
841 const PCE<Storage>& b);
842
843 template <typename Storage>
844 KOKKOS_INLINE_FUNCTION
845 PCE<Storage>
846 operator-(const PCE<Storage>& a,
847 const typename PCE<Storage>::value_type& b);
848
849 template <typename Storage>
850 KOKKOS_INLINE_FUNCTION
851 PCE<Storage>
852 operator*(const PCE<Storage>& a, const PCE<Storage>& b);
853
854 template <typename Storage>
855 KOKKOS_INLINE_FUNCTION
856 PCE<Storage>
857 operator*(const typename PCE<Storage>::value_type& a,
858 const PCE<Storage>& b);
859
860 template <typename Storage> PCE<Storage>
861 KOKKOS_INLINE_FUNCTION
862 operator*(const PCE<Storage>& a,
863 const typename PCE<Storage>::value_type& b);
864
865 template <typename Storage>
866 KOKKOS_INLINE_FUNCTION
867 PCE<Storage>
868 operator/(const PCE<Storage>& a, const PCE<Storage>& b);
869
870 template <typename Storage>
871 KOKKOS_INLINE_FUNCTION
872 PCE<Storage>
873 operator/(const typename PCE<Storage>::value_type& a,
874 const PCE<Storage>& b);
875
876 template <typename Storage>
877 KOKKOS_INLINE_FUNCTION
878 PCE<Storage>
879 operator/(const PCE<Storage>& a,
880 const typename PCE<Storage>::value_type& b);
881
882 template <typename Storage>
883 KOKKOS_INLINE_FUNCTION
884 PCE<Storage>
885 exp(const PCE<Storage>& a);
886
887 template <typename Storage>
888 KOKKOS_INLINE_FUNCTION
889 PCE<Storage>
890 log(const PCE<Storage>& a);
891
892 template <typename Storage>
893 KOKKOS_INLINE_FUNCTION
894 void
895 log(PCE<Storage>& c, const PCE<Storage>& a);
896
897 template <typename Storage>
898 KOKKOS_INLINE_FUNCTION
899 PCE<Storage>
900 log10(const PCE<Storage>& a);
901
902 template <typename Storage>
903 KOKKOS_INLINE_FUNCTION
904 PCE<Storage>
905 sqrt(const PCE<Storage>& a);
906
907 template <typename Storage>
908 KOKKOS_INLINE_FUNCTION
909 PCE<Storage>
910 cbrt(const PCE<Storage>& a);
911
912 template <typename Storage>
913 KOKKOS_INLINE_FUNCTION
914 PCE<Storage>
915 pow(const PCE<Storage>& a, const PCE<Storage>& b);
916
917 template <typename Storage>
918 KOKKOS_INLINE_FUNCTION
919 PCE<Storage>
920 pow(const typename PCE<Storage>::value_type& a, const PCE<Storage>& b);
921
922 template <typename Storage> PCE<Storage>
923 KOKKOS_INLINE_FUNCTION
924 pow(const PCE<Storage>& a,
925 const typename PCE<Storage>::value_type& b);
926
927 template <typename Storage>
928 KOKKOS_INLINE_FUNCTION
929 PCE<Storage>
930 cos(const PCE<Storage>& a);
931
932 template <typename Storage>
933 KOKKOS_INLINE_FUNCTION
934 PCE<Storage>
935 sin(const PCE<Storage>& a);
936
937 template <typename Storage>
938 KOKKOS_INLINE_FUNCTION
939 PCE<Storage>
940 tan(const PCE<Storage>& a);
941
942 template <typename Storage>
943 KOKKOS_INLINE_FUNCTION
944 PCE<Storage>
945 cosh(const PCE<Storage>& a);
946
947 template <typename Storage>
948 KOKKOS_INLINE_FUNCTION
949 PCE<Storage>
950 sinh(const PCE<Storage>& a);
951
952 template <typename Storage>
953 KOKKOS_INLINE_FUNCTION
954 PCE<Storage>
955 tanh(const PCE<Storage>& a);
956
957 template <typename Storage>
958 KOKKOS_INLINE_FUNCTION
959 PCE<Storage>
960 acos(const PCE<Storage>& a);
961
962 template <typename Storage>
963 KOKKOS_INLINE_FUNCTION
964 PCE<Storage>
965 asin(const PCE<Storage>& a);
966
967 template <typename Storage>
968 KOKKOS_INLINE_FUNCTION
969 PCE<Storage>
970 atan(const PCE<Storage>& a);
971
972 template <typename Storage>
973 KOKKOS_INLINE_FUNCTION
974 PCE<Storage>
975 atan2(const PCE<Storage>& a, const PCE<Storage>& b);
976
977 template <typename Storage>
978 KOKKOS_INLINE_FUNCTION
979 PCE<Storage>
980 atan2(const typename PCE<Storage>::value_type& a,
981 const PCE<Storage>& b);
982
983 template <typename Storage>
984 KOKKOS_INLINE_FUNCTION
985 PCE<Storage>
986 atan2(const PCE<Storage>& a,
987 const typename PCE<Storage>::value_type& b);
988
989 template <typename Storage>
990 KOKKOS_INLINE_FUNCTION
991 PCE<Storage>
992 acosh(const PCE<Storage>& a);
993
994 template <typename Storage>
995 KOKKOS_INLINE_FUNCTION
996 PCE<Storage>
997 asinh(const PCE<Storage>& a);
998
999 template <typename Storage>
1000 KOKKOS_INLINE_FUNCTION
1001 PCE<Storage>
1002 atanh(const PCE<Storage>& a);
1003
1004 template <typename Storage>
1005 KOKKOS_INLINE_FUNCTION
1006 PCE<Storage>
1007 abs(const PCE<Storage>& a);
1008
1009 template <typename Storage>
1010 KOKKOS_INLINE_FUNCTION
1011 PCE<Storage>
1012 fabs(const PCE<Storage>& a);
1013
1014 template <typename Storage>
1015 KOKKOS_INLINE_FUNCTION
1016 PCE<Storage>
1017 ceil(const PCE<Storage>& a);
1018
1019 // template <typename Storage>
1020 // KOKKOS_INLINE_FUNCTION
1021 // PCE<Storage>
1022 // max(const PCE<Storage>& a, const PCE<Storage>& b);
1023
1024 template <typename Storage>
1025 KOKKOS_INLINE_FUNCTION
1026 PCE<Storage>
1027 max(const typename PCE<Storage>::value_type& a,
1028 const PCE<Storage>& b);
1029
1030 template <typename Storage>
1031 KOKKOS_INLINE_FUNCTION
1032 PCE<Storage>
1033 max(const PCE<Storage>& a,
1034 const typename PCE<Storage>::value_type& b);
1035
1036 // template <typename Storage>
1037 // KOKKOS_INLINE_FUNCTION
1038 // PCE<Storage>
1039 // min(const PCE<Storage>& a, const PCE<Storage>& b);
1040
1041 template <typename Storage>
1042 KOKKOS_INLINE_FUNCTION
1043 PCE<Storage>
1044 min(const typename PCE<Storage>::value_type& a,
1045 const PCE<Storage>& b);
1046
1047 template <typename Storage>
1048 KOKKOS_INLINE_FUNCTION
1049 PCE<Storage>
1050 min(const PCE<Storage>& a,
1051 const typename PCE<Storage>::value_type& b);
1052
1053 template <typename Storage>
1054 KOKKOS_INLINE_FUNCTION
1055 bool
1056 operator==(const PCE<Storage>& a, const PCE<Storage>& b);
1057
1058 template <typename Storage>
1059 KOKKOS_INLINE_FUNCTION
1060 bool
1061 operator==(const typename PCE<Storage>::value_type& a,
1062 const PCE<Storage>& b);
1063
1064 template <typename Storage>
1065 KOKKOS_INLINE_FUNCTION
1066 bool
1067 operator==(const PCE<Storage>& a,
1068 const typename PCE<Storage>::value_type& b);
1069
1070 template <typename Storage>
1071 KOKKOS_INLINE_FUNCTION
1072 bool
1073 operator!=(const PCE<Storage>& a, const PCE<Storage>& b);
1074
1075 template <typename Storage>
1076 KOKKOS_INLINE_FUNCTION
1077 bool
1078 operator!=(const typename PCE<Storage>::value_type& a,
1079 const PCE<Storage>& b);
1080
1081 template <typename Storage>
1082 KOKKOS_INLINE_FUNCTION
1083 bool
1084 operator!=(const PCE<Storage>& a,
1085 const typename PCE<Storage>::value_type& b);
1086
1087 template <typename Storage>
1088 KOKKOS_INLINE_FUNCTION
1089 bool
1090 operator<=(const PCE<Storage>& a,
1091 const PCE<Storage>& b);
1092
1093 template <typename Storage>
1094 KOKKOS_INLINE_FUNCTION
1095 bool
1096 operator<=(const typename PCE<Storage>::value_type& a,
1097 const PCE<Storage>& b);
1098
1099 template <typename Storage>
1100 KOKKOS_INLINE_FUNCTION
1101 bool
1102 operator<=(const PCE<Storage>& a,
1103 const typename PCE<Storage>::value_type& b);
1104
1105 template <typename Storage>
1106 KOKKOS_INLINE_FUNCTION
1107 bool
1108 operator>=(const PCE<Storage>& a, const PCE<Storage>& b);
1109
1110 template <typename Storage>
1111 KOKKOS_INLINE_FUNCTION
1112 bool
1113 operator>=(const typename PCE<Storage>::value_type& a,
1114 const PCE<Storage>& b);
1115
1116 template <typename Storage>
1117 KOKKOS_INLINE_FUNCTION
1118 bool
1119 operator>=(const PCE<Storage>& a,
1120 const typename PCE<Storage>::value_type& b);
1121
1122 template <typename Storage>
1123 KOKKOS_INLINE_FUNCTION
1124 bool
1125 operator<(const PCE<Storage>& a, const PCE<Storage>& b);
1126
1127 template <typename Storage>
1128 KOKKOS_INLINE_FUNCTION
1129 bool
1130 operator<(const typename PCE<Storage>::value_type& a,
1131 const PCE<Storage>& b);
1132
1133 template <typename Storage>
1134 KOKKOS_INLINE_FUNCTION
1135 bool
1136 operator<(const PCE<Storage>& a,
1137 const typename PCE<Storage>::value_type& b);
1138
1139 template <typename Storage>
1140 KOKKOS_INLINE_FUNCTION
1141 bool
1142 operator>(const PCE<Storage>& a, const PCE<Storage>& b);
1143
1144 template <typename Storage>
1145 KOKKOS_INLINE_FUNCTION
1146 bool
1147 operator>(const typename PCE<Storage>::value_type& a,
1148 const PCE<Storage>& b);
1149
1150 template <typename Storage>
1151 KOKKOS_INLINE_FUNCTION
1152 bool
1153 operator>(const PCE<Storage>& a,
1154 const typename PCE<Storage>::value_type& b);
1155
1156 template <typename Storage>
1157 std::ostream&
1158 operator << (std::ostream& os, const PCE<Storage>& a);
1159
1160 template <typename Storage>
1161 std::istream&
1162 operator >> (std::istream& os, PCE<Storage>& a);
1163
1164 /**\brief Define a partition of a View of Sacado::UQ::PCE type */
1165 struct PCEPartition {
1166 unsigned begin ;
1167 unsigned end ;
1168
1169 template< typename iType0 , typename iType1 >
1170 KOKKOS_INLINE_FUNCTION
1171 PCEPartition( const iType0 & i0 , const iType1 & i1 ) :
1172 begin(i0), end(i1) {}
1173 };
1174
1175 /* template <typename Storage>
1176 KOKKOS_INLINE_FUNCTION
1177 void
1178 PCE<Storage>::
1179 CG_divide(const PCE<Storage>& a, const PCE<Storage>& b, PCE<Storage>& c) {
1180 // typedef typename PCE<Storage>::ordinal_type ordinal_type;
1181 // typedef typename PCE<Storage>::value_type value_type;
1182
1183 const ordinal_type size = c.size();
1184 //Needed scalars
1185 value_type alpha, beta, rTz, rTz_old, resid;
1186
1187 //Needed temporary PCEs
1188 PCE<Storage> r(a.cijk(),size);
1189 PCE<Storage> p(a.cijk(),size);
1190 PCE<Storage> bp(a.cijk(),size);
1191 PCE<Storage> z(a.cijk(),size);
1192
1193 //compute residual = a - b*c
1194 r = a - b*c;
1195 z = r/b.coeff(0);
1196 p = z;
1197 resid = r.two_norm();
1198 //Compute <r,z>=rTz (L2 inner product)
1199 rTz = r.inner_product(z);
1200 ordinal_type k = 0;
1201 value_type tol = 1e-6;
1202 while ( resid > tol && k < 100){
1203 //Compute b*p
1204 bp = b*p;
1205
1206 //Compute alpha = <r,z>/<p,b*p>
1207 alpha = rTz/p.inner_product(bp);
1208
1209 //Check alpha is positive!
1210 //Update the solution c = c + alpha*p
1211 c = c + alpha*p;
1212 rTz_old = rTz;
1213
1214 //Compute the new residual r = r - alpha*b*p
1215 r = r - alpha*bp;
1216 resid = r.two_norm();
1217
1218 //Compute beta = rTz_new/ rTz_old and new p
1219 z = r/b.coeff(0);
1220 rTz = r.inner_product(z);
1221 beta = rTz/rTz_old;
1222 p = z + beta*p;
1223 k++;
1224 }
1225 // return c;
1226 }
1227 */
1228
1229 template <typename S>
1230 void memcpy(PCE<S>* dst, const PCE<S>* src, const size_t sz) {
1231 const size_t n = sz / sizeof(PCE<S>);
1232 for (size_t i=0; i<n; ++i)
1233 dst[i] = src[i];
1234 }
1235
1236 } // namespace PCE
1237
1238 //! Trait class to determine if a scalar type is a PCE
1239 template <typename T> struct is_uq_pce {
1240 static const bool value = false;
1241 };
1242 template <typename S> struct is_uq_pce< UQ::PCE<S> > {
1243 static const bool value = true;
1244 };
1245 template <typename T> struct is_uq_pce< const T > {
1246 static const bool value = is_uq_pce<T>::value;
1247 };
1248 template <typename T> struct is_uq_pce< T* > {
1249 static const bool value = is_uq_pce<T>::value;
1250 };
1251 template <typename T> struct is_uq_pce< T[] > {
1252 static const bool value = is_uq_pce<T>::value;
1253 };
1254 template <typename T, unsigned N> struct is_uq_pce< T[N] > {
1255 static const bool value = is_uq_pce<T>::value;
1256 };
1257
1258 // Utility function to see if a PCE is really a constant
1259 template <typename Storage>
1260 KOKKOS_INLINE_FUNCTION
1261 bool is_constant(const Sacado::UQ::PCE<Storage>& x)
1262 {
1263 typedef typename Storage::ordinal_type ordinal_type;
1264 typedef typename Storage::value_type value_type;
1265
1266 // All size-1 expansions are constants
1267 const ordinal_type sz = x.size();
1268 if (sz == 1) return true;
1269
1270 // Maybe use a tolerance????
1271 const value_type zero = 0;
1272 for (ordinal_type i=1; i<sz; ++i)
1273 if (x.fastAccessCoeff(i) != zero) return false;
1274
1275 return true;
1276 }
1277
1278 } // namespace Sacado
1279
1280 #include "Sacado_UQ_PCE_Traits.hpp"
1281 #include "Sacado_UQ_PCE_Imp.hpp"
1282
1283 #include "Kokkos_NumericTraits.hpp"
1284
1285 namespace Kokkos {
1286
1287 template <typename Storage>
1288 struct reduction_identity< Sacado::UQ::PCE<Storage> > {
1289 typedef Sacado::UQ::PCE<Storage> pce;
1290 typedef typename Storage::value_type scalar;
1291 typedef reduction_identity<scalar> RIS;
1292 KOKKOS_FORCEINLINE_FUNCTION constexpr static pce sum() {
1293 return pce(RIS::sum());
1294 }
1295 KOKKOS_FORCEINLINE_FUNCTION constexpr static pce prod() {
1296 return pce(RIS::prod());
1297 }
1298 KOKKOS_FORCEINLINE_FUNCTION constexpr static pce max() {
1299 return pce(RIS::max());
1300 }
1301 KOKKOS_FORCEINLINE_FUNCTION constexpr static pce min() {
1302 return pce(RIS::min());
1303 }
1304 };
1305
1306 namespace Impl {
1307 template <typename Storage>
1308 struct promote<Sacado::UQ::PCE<Storage>,false> {
1309 using type = typename Sacado::UQ::PCE<Storage>;
1310 };
1311 }
1312
1313 }
1314
1315 #endif // HAVE_STOKHOS_SACADO
1316
1317 #endif // SACADO_PCE_ORTHOGPOLY_HPP