| 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 |
#include "Sacado_DynamicArrayTraits.hpp" |
| 11 |
#include "Teuchos_TimeMonitor.hpp" |
| 12 |
#include "Stokhos_ConstantOrthogPolyExpansion.hpp" |
| 13 |
|
| 14 |
namespace Sacado { |
| 15 |
namespace ETPCE { |
| 16 |
|
| 17 |
template <int NN, typename ExprT> |
| 18 |
struct ExprQuadFuncWrapper { |
| 19 |
static const int N = NN; |
| 20 |
typedef typename ExprT::value_type value_type; |
| 21 |
const ExprT& expr_; |
| 22 |
ExprQuadFuncWrapper(const ExprT& expr) : expr_(expr) {} |
| 23 |
template <typename tuple_type> |
| 24 |
KERNEL_PREFIX value_type operator() (tuple_type x) const { |
| 25 |
return expr_.template eval_sample<0>(x); |
| 26 |
} |
| 27 |
}; |
| 28 |
|
| 29 |
template <typename T, typename Storage> |
| 30 |
template <typename S> |
| 31 |
void |
| 32 |
OrthogPolyImpl<T,Storage>:: |
| 33 |
expressionCopy(const Expr<S>& x) |
| 34 |
{ |
| 35 |
#ifdef STOKHOS_TEUCHOS_TIME_MONITOR |
| 36 |
TEUCHOS_FUNC_TIME_MONITOR("ETPCE ExpressionCopy(" << x.name() << ")"); |
| 37 |
#endif |
| 38 |
int p = x.order(); |
| 39 |
if (p == 0) { |
| 40 |
(*th_)[0] = x.val(); |
| 41 |
} |
| 42 |
else if (p <= 2) { |
| 43 |
int sz = th_->size(); |
| 44 |
approx_type* tc = th_.get(); |
| 45 |
bool on_rhs = false; |
| 46 |
|
| 47 |
// Check if *this is on the right-hand-side of the expression |
| 48 |
if (p == 2) { |
| 49 |
const int N = Expr<S>::num_args; |
| 50 |
for (int i=0; i<N; i++) { |
| 51 |
const approx_type* opa = &(x.getArg(i)); |
| 52 |
if (opa == &(*th_)) { |
| 53 |
on_rhs = true; |
| 54 |
break; |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// If we are on the RHS, we have to put the results in a temporary |
| 60 |
if (on_rhs) |
| 61 |
tc = new approx_type(expansion_->getBasis(), sz); |
| 62 |
|
| 63 |
(*tc)[0] = x.val(); |
| 64 |
if (x.has_fast_access(sz)) { |
| 65 |
for (int i=1; i<sz; i++) |
| 66 |
(*tc)[i] = x.fast_higher_order_coeff(i); |
| 67 |
} |
| 68 |
else { |
| 69 |
for (int i=1; i<sz; i++) |
| 70 |
(*tc)[i] = x.higher_order_coeff(i); |
| 71 |
} |
| 72 |
|
| 73 |
// Set underlying OPA if we created a temporary |
| 74 |
if (on_rhs) { |
| 75 |
th_ = Sacado::Handle<approx_type>(tc); |
| 76 |
} |
| 77 |
} |
| 78 |
else { |
| 79 |
const int N = Expr<S>::num_args; |
| 80 |
const approx_type* opas[N]; |
| 81 |
for (int i=0; i<N; i++) |
| 82 |
opas[i] = &(x.getArg(i)); |
| 83 |
ExprQuadFuncWrapper< N, Expr<S> > func(x); |
| 84 |
quad_expansion_->nary_op(func, *th_, opas); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
template <typename T, typename Storage> |
| 89 |
OrthogPolyImpl<T,Storage>:: |
| 90 |
OrthogPolyImpl() : |
| 91 |
expansion_(), |
| 92 |
quad_expansion_(), |
| 93 |
th_(new Stokhos::OrthogPolyApprox<int,value_type,Storage>) |
| 94 |
{ |
| 95 |
const_expansion_ = Teuchos::rcp(new Stokhos::ConstantOrthogPolyExpansion<int,T>); |
| 96 |
expansion_ = const_expansion_; |
| 97 |
} |
| 98 |
|
| 99 |
template <typename T, typename Storage> |
| 100 |
OrthogPolyImpl<T,Storage>:: |
| 101 |
OrthogPolyImpl(const typename OrthogPolyImpl<T,Storage>::value_type& x) : |
| 102 |
expansion_(), |
| 103 |
quad_expansion_(), |
| 104 |
th_(new Stokhos::OrthogPolyApprox<int,value_type,Storage>(Teuchos::null, 1, &x)) |
| 105 |
{ |
| 106 |
const_expansion_ = Teuchos::rcp(new Stokhos::ConstantOrthogPolyExpansion<int,T>); |
| 107 |
expansion_ = const_expansion_; |
| 108 |
} |
| 109 |
|
| 110 |
template <typename T, typename Storage> |
| 111 |
OrthogPolyImpl<T,Storage>:: |
| 112 |
OrthogPolyImpl(const Teuchos::RCP<expansion_type>& expansion) : |
| 113 |
expansion_(expansion), |
| 114 |
quad_expansion_(Teuchos::rcp_dynamic_cast<quad_expansion_type>(expansion_)), |
| 115 |
th_(new Stokhos::OrthogPolyApprox<int,value_type,Storage>(expansion_->getBasis())) |
| 116 |
{ |
| 117 |
const_expansion_ = Teuchos::rcp(new Stokhos::ConstantOrthogPolyExpansion<int,T>); |
| 118 |
} |
| 119 |
|
| 120 |
template <typename T, typename Storage> |
| 121 |
OrthogPolyImpl<T,Storage>:: |
| 122 |
OrthogPolyImpl(const Teuchos::RCP<expansion_type>& expansion, |
| 123 |
ordinal_type sz) : |
| 124 |
expansion_(expansion), |
| 125 |
quad_expansion_(Teuchos::rcp_dynamic_cast<quad_expansion_type>(expansion_)), |
| 126 |
th_(new Stokhos::OrthogPolyApprox<int,value_type,Storage>(expansion_->getBasis(), sz)) |
| 127 |
{ |
| 128 |
const_expansion_ = Teuchos::rcp(new Stokhos::ConstantOrthogPolyExpansion<int,T>); |
| 129 |
} |
| 130 |
|
| 131 |
template <typename T, typename Storage> |
| 132 |
OrthogPolyImpl<T,Storage>:: |
| 133 |
OrthogPolyImpl(const OrthogPolyImpl<T,Storage>& x) : |
| 134 |
expansion_(x.expansion_), |
| 135 |
quad_expansion_(x.quad_expansion_), |
| 136 |
th_(x.th_) |
| 137 |
{ |
| 138 |
const_expansion_ = Teuchos::rcp(new Stokhos::ConstantOrthogPolyExpansion<int,T>); |
| 139 |
} |
| 140 |
|
| 141 |
template <typename T, typename Storage> |
| 142 |
template <typename S> |
| 143 |
OrthogPolyImpl<T,Storage>:: |
| 144 |
OrthogPolyImpl(const Expr<S>& x) : |
| 145 |
expansion_(x.expansion()), |
| 146 |
quad_expansion_(Teuchos::rcp_dynamic_cast<quad_expansion_type>(expansion_)), |
| 147 |
th_(new Stokhos::OrthogPolyApprox<int,value_type,Storage>(expansion_->getBasis(), x.size())) |
| 148 |
{ |
| 149 |
const_expansion_ = Teuchos::rcp(new Stokhos::ConstantOrthogPolyExpansion<int,T>); |
| 150 |
expressionCopy(x); |
| 151 |
} |
| 152 |
|
| 153 |
template <typename T, typename Storage> |
| 154 |
void |
| 155 |
OrthogPolyImpl<T,Storage>:: |
| 156 |
reset(const Teuchos::RCP<expansion_type>& expansion) |
| 157 |
{ |
| 158 |
expansion_ = expansion; |
| 159 |
quad_expansion_ = Teuchos::rcp_dynamic_cast<quad_expansion_type>(expansion_); |
| 160 |
th_->reset(expansion_->getBasis()); |
| 161 |
} |
| 162 |
|
| 163 |
template <typename T, typename Storage> |
| 164 |
void |
| 165 |
OrthogPolyImpl<T,Storage>:: |
| 166 |
reset(const Teuchos::RCP<expansion_type>& expansion, ordinal_type sz) |
| 167 |
{ |
| 168 |
expansion_ = expansion; |
| 169 |
quad_expansion_ = Teuchos::rcp_dynamic_cast<quad_expansion_type>(expansion_); |
| 170 |
th_->reset(expansion_->getBasis(), sz); |
| 171 |
} |
| 172 |
|
| 173 |
template <typename T, typename Storage> |
| 174 |
typename OrthogPolyImpl<T,Storage>::value_type |
| 175 |
OrthogPolyImpl<T,Storage>:: |
| 176 |
evaluate(const Teuchos::Array<typename OrthogPolyImpl<T,Storage>::value_type>& point) const |
| 177 |
{ |
| 178 |
return th_->evaluate(point); |
| 179 |
} |
| 180 |
|
| 181 |
template <typename T, typename Storage> |
| 182 |
typename OrthogPolyImpl<T,Storage>::value_type |
| 183 |
OrthogPolyImpl<T,Storage>:: |
| 184 |
evaluate( |
| 185 |
const Teuchos::Array<typename OrthogPolyImpl<T,Storage>::value_type>& point, |
| 186 |
const Teuchos::Array<typename OrthogPolyImpl<T,Storage>::value_type>& bvals) const |
| 187 |
{ |
| 188 |
return th_->evaluate(point, bvals); |
| 189 |
} |
| 190 |
|
| 191 |
template <typename T, typename Storage> |
| 192 |
template <typename S> |
| 193 |
bool |
| 194 |
OrthogPolyImpl<T,Storage>:: |
| 195 |
isEqualTo(const Expr<S>& x) const { |
| 196 |
typedef IsEqual<value_type> IE; |
| 197 |
if (x.size() != this->size()) return false; |
| 198 |
// Allow expansions to be different if their size is 1 and one |
| 199 |
// of them is a constant expansion |
| 200 |
if (expansion_ != x.expansion_) { |
| 201 |
if (x.size() != 1) |
| 202 |
return false; |
| 203 |
if ((expansion_ != const_expansion_) && |
| 204 |
(x.expansion_ != x.const_expansion_)) |
| 205 |
return false; |
| 206 |
} |
| 207 |
bool eq = true; |
| 208 |
for (int i=0; i<this->size(); i++) |
| 209 |
eq = eq && IE::eval(x.coeff(i), this->coeff(i)); |
| 210 |
return eq; |
| 211 |
} |
| 212 |
|
| 213 |
template <typename T, typename Storage> |
| 214 |
OrthogPolyImpl<T,Storage>& |
| 215 |
OrthogPolyImpl<T,Storage>:: |
| 216 |
operator=(const typename OrthogPolyImpl<T,Storage>::value_type& v) |
| 217 |
{ |
| 218 |
th_.makeOwnCopy(); |
| 219 |
*th_ = v; |
| 220 |
return *this; |
| 221 |
} |
| 222 |
|
| 223 |
template <typename T, typename Storage> |
| 224 |
OrthogPolyImpl<T,Storage>& |
| 225 |
OrthogPolyImpl<T,Storage>:: |
| 226 |
operator=(const OrthogPolyImpl<T,Storage>& x) |
| 227 |
{ |
| 228 |
expansion_ = x.expansion_; |
| 229 |
quad_expansion_ = x.quad_expansion_; |
| 230 |
th_ = x.th_; |
| 231 |
return *this; |
| 232 |
} |
| 233 |
|
| 234 |
template <typename T, typename Storage> |
| 235 |
template <typename S> |
| 236 |
OrthogPolyImpl<T,Storage>& |
| 237 |
OrthogPolyImpl<T,Storage>:: |
| 238 |
operator=(const Expr<S>& x) |
| 239 |
{ |
| 240 |
th_.makeOwnCopy(); |
| 241 |
expansion_ = x.expansion(); |
| 242 |
quad_expansion_ = Teuchos::rcp_dynamic_cast<quad_expansion_type>(expansion_); |
| 243 |
th_->reset(expansion_->getBasis(), x.size()); |
| 244 |
expressionCopy(x); |
| 245 |
return *this; |
| 246 |
} |
| 247 |
|
| 248 |
template <typename T, typename Storage> |
| 249 |
OrthogPolyImpl<T,Storage>& |
| 250 |
OrthogPolyImpl<T,Storage>:: |
| 251 |
operator+=(const typename OrthogPolyImpl<T,Storage>::value_type& v) |
| 252 |
{ |
| 253 |
th_.makeOwnCopy(); |
| 254 |
expansion_->plusEqual(*th_, v); |
| 255 |
return *this; |
| 256 |
} |
| 257 |
|
| 258 |
template <typename T, typename Storage> |
| 259 |
OrthogPolyImpl<T,Storage>& |
| 260 |
OrthogPolyImpl<T,Storage>:: |
| 261 |
operator-=(const typename OrthogPolyImpl<T,Storage>::value_type& v) |
| 262 |
{ |
| 263 |
th_.makeOwnCopy(); |
| 264 |
expansion_->minusEqual(*th_, v); |
| 265 |
return *this; |
| 266 |
} |
| 267 |
|
| 268 |
template <typename T, typename Storage> |
| 269 |
OrthogPolyImpl<T,Storage>& |
| 270 |
OrthogPolyImpl<T,Storage>:: |
| 271 |
operator*=(const typename OrthogPolyImpl<T,Storage>::value_type& v) |
| 272 |
{ |
| 273 |
th_.makeOwnCopy(); |
| 274 |
expansion_->timesEqual(*th_, v); |
| 275 |
return *this; |
| 276 |
} |
| 277 |
|
| 278 |
template <typename T, typename Storage> |
| 279 |
OrthogPolyImpl<T,Storage>& |
| 280 |
OrthogPolyImpl<T,Storage>:: |
| 281 |
operator/=(const typename OrthogPolyImpl<T,Storage>::value_type& v) |
| 282 |
{ |
| 283 |
th_.makeOwnCopy(); |
| 284 |
expansion_->divideEqual(*th_, v); |
| 285 |
return *this; |
| 286 |
} |
| 287 |
|
| 288 |
template <typename T, typename Storage> |
| 289 |
std::ostream& |
| 290 |
operator << (std::ostream& os, const OrthogPoly<T,Storage>& a) |
| 291 |
{ |
| 292 |
typedef typename OrthogPoly<T,Storage>::ordinal_type ordinal_type; |
| 293 |
|
| 294 |
os << "[ "; |
| 295 |
|
| 296 |
for (ordinal_type i=0; i<a.size(); i++) { |
| 297 |
os << a.coeff(i) << " "; |
| 298 |
} |
| 299 |
|
| 300 |
os << "]\n"; |
| 301 |
return os; |
| 302 |
} |
| 303 |
|
| 304 |
template <typename T, typename Storage> |
| 305 |
std::istream& |
| 306 |
operator >> (std::istream& is, OrthogPoly<T,Storage>& a) |
| 307 |
{ |
| 308 |
typedef typename OrthogPoly<T,Storage>::ordinal_type ordinal_type; |
| 309 |
|
| 310 |
// Read in the opening "[" |
| 311 |
char bracket; |
| 312 |
is >> bracket; |
| 313 |
|
| 314 |
for (ordinal_type i=0; i<a.size(); i++) { |
| 315 |
is >> a.fastAccessCoeff(i); |
| 316 |
} |
| 317 |
|
| 318 |
// Read in the closing "]" |
| 319 |
|
| 320 |
is >> bracket; |
| 321 |
return is; |
| 322 |
} |
| 323 |
|
| 324 |
} // namespace PCE |
| 325 |
} // namespace Sacado |