Stokhos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_ETPCE_OrthogPolyImp.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Stokhos Package
5 // Copyright (2009) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Eric T. Phipps (etphipp@sandia.gov).
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include "Sacado_DynamicArrayTraits.hpp"
43 #include "Teuchos_TimeMonitor.hpp"
45 
46 namespace Sacado {
47 namespace ETPCE {
48 
49 template <int NN, typename ExprT>
51  static const int N = NN;
52  typedef typename ExprT::value_type value_type;
53  const ExprT& expr_;
54  ExprQuadFuncWrapper(const ExprT& expr) : expr_(expr) {}
55  template <typename tuple_type>
56  KERNEL_PREFIX value_type operator() (tuple_type x) const {
57  return expr_.template eval_sample<0>(x);
58  }
59 };
60 
61 template <typename T, typename Storage>
62 template <typename S>
63 void
64 OrthogPolyImpl<T,Storage>::
65 expressionCopy(const Expr<S>& x)
66 {
67 #ifdef STOKHOS_TEUCHOS_TIME_MONITOR
68  TEUCHOS_FUNC_TIME_MONITOR("ETPCE ExpressionCopy(" << x.name() << ")");
69 #endif
70  int p = x.order();
71  if (p == 0) {
72  (*th_)[0] = x.val();
73  }
74  else if (p <= 2) {
75  int sz = th_->size();
76  approx_type* tc = th_.get();
77  bool on_rhs = false;
78 
79  // Check if *this is on the right-hand-side of the expression
80  if (p == 2) {
81  const int N = Expr<S>::num_args;
82  for (int i=0; i<N; i++) {
83  const approx_type* opa = &(x.getArg(i));
84  if (opa == &(*th_)) {
85  on_rhs = true;
86  break;
87  }
88  }
89  }
90 
91  // If we are on the RHS, we have to put the results in a temporary
92  if (on_rhs)
93  tc = new approx_type(expansion_->getBasis(), sz);
94 
95  (*tc)[0] = x.val();
96  if (x.has_fast_access(sz)) {
97  for (int i=1; i<sz; i++)
98  (*tc)[i] = x.fast_higher_order_coeff(i);
99  }
100  else {
101  for (int i=1; i<sz; i++)
102  (*tc)[i] = x.higher_order_coeff(i);
103  }
104 
105  // Set underlying OPA if we created a temporary
106  if (on_rhs) {
107  th_ = Sacado::Handle<approx_type>(tc);
108  }
109  }
110  else {
111  const int N = Expr<S>::num_args;
112  const approx_type* opas[N];
113  for (int i=0; i<N; i++)
114  opas[i] = &(x.getArg(i));
115  ExprQuadFuncWrapper< N, Expr<S> > func(x);
116  quad_expansion_->nary_op(func, *th_, opas);
117  }
118 }
119 
120 template <typename T, typename Storage>
121 OrthogPolyImpl<T,Storage>::
122 OrthogPolyImpl() :
123  expansion_(const_expansion_),
124  quad_expansion_(),
125  th_(new Stokhos::OrthogPolyApprox<int,value_type,Storage>)
126 {
127 }
128 
129 template <typename T, typename Storage>
130 OrthogPolyImpl<T,Storage>::
131 OrthogPolyImpl(const typename OrthogPolyImpl<T,Storage>::value_type& x) :
132  expansion_(const_expansion_),
133  quad_expansion_(),
134  th_(new Stokhos::OrthogPolyApprox<int,value_type,Storage>(Teuchos::null, 1, &x))
135 {
136 }
137 
138 template <typename T, typename Storage>
139 OrthogPolyImpl<T,Storage>::
140 OrthogPolyImpl(const Teuchos::RCP<expansion_type>& expansion) :
141  expansion_(expansion),
142  quad_expansion_(Teuchos::rcp_dynamic_cast<quad_expansion_type>(expansion_)),
143  th_(new Stokhos::OrthogPolyApprox<int,value_type,Storage>(expansion_->getBasis()))
144 {
145 }
146 
147 template <typename T, typename Storage>
148 OrthogPolyImpl<T,Storage>::
149 OrthogPolyImpl(const Teuchos::RCP<expansion_type>& expansion,
150  ordinal_type sz) :
151  expansion_(expansion),
152  quad_expansion_(Teuchos::rcp_dynamic_cast<quad_expansion_type>(expansion_)),
153  th_(new Stokhos::OrthogPolyApprox<int,value_type,Storage>(expansion_->getBasis(), sz))
154 {
155 }
156 
157 template <typename T, typename Storage>
158 OrthogPolyImpl<T,Storage>::
159 OrthogPolyImpl(const OrthogPolyImpl<T,Storage>& x) :
160  expansion_(x.expansion_),
161  quad_expansion_(x.quad_expansion_),
162  th_(x.th_)
163 {
164 }
165 
166 template <typename T, typename Storage>
167 template <typename S>
168 OrthogPolyImpl<T,Storage>::
169 OrthogPolyImpl(const Expr<S>& x) :
170  expansion_(x.expansion()),
171  quad_expansion_(Teuchos::rcp_dynamic_cast<quad_expansion_type>(expansion_)),
172  th_(new Stokhos::OrthogPolyApprox<int,value_type,Storage>(expansion_->getBasis(), x.size()))
173 {
174  expressionCopy(x);
175 }
176 
177 template <typename T, typename Storage>
178 void
179 OrthogPolyImpl<T,Storage>::
180 reset(const Teuchos::RCP<expansion_type>& expansion)
181 {
182  expansion_ = expansion;
183  quad_expansion_ = Teuchos::rcp_dynamic_cast<quad_expansion_type>(expansion_);
184  th_->reset(expansion_->getBasis());
185 }
186 
187 template <typename T, typename Storage>
188 void
189 OrthogPolyImpl<T,Storage>::
190 reset(const Teuchos::RCP<expansion_type>& expansion, ordinal_type sz)
191 {
192  expansion_ = expansion;
193  quad_expansion_ = Teuchos::rcp_dynamic_cast<quad_expansion_type>(expansion_);
194  th_->reset(expansion_->getBasis(), sz);
195 }
196 
197 template <typename T, typename Storage>
199 OrthogPolyImpl<T,Storage>::
200 evaluate(const Teuchos::Array<typename OrthogPolyImpl<T,Storage>::value_type>& point) const
201 {
202  return th_->evaluate(point);
203 }
204 
205 template <typename T, typename Storage>
207 OrthogPolyImpl<T,Storage>::
208 evaluate(
210  const Teuchos::Array<typename OrthogPolyImpl<T,Storage>::value_type>& bvals) const
211 {
212  return th_->evaluate(point, bvals);
213 }
214 
215 template <typename T, typename Storage>
216 template <typename S>
217 bool
218 OrthogPolyImpl<T,Storage>::
219 isEqualTo(const Expr<S>& x) const {
220  typedef IsEqual<value_type> IE;
221  if (x.size() != this->size()) return false;
222  // Allow expansions to be different if their size is 1 and one
223  // of them is a constant expansion
224  if (expansion_ != x.expansion_) {
225  if (x.size() != 1)
226  return false;
227  if ((expansion_ != const_expansion_) &&
228  (x.expansion_ != const_expansion_))
229  return false;
230  }
231  bool eq = true;
232  for (int i=0; i<this->size(); i++)
233  eq = eq && IE::eval(x.coeff(i), this->coeff(i));
234  return eq;
235 }
236 
237 template <typename T, typename Storage>
238 OrthogPolyImpl<T,Storage>&
239 OrthogPolyImpl<T,Storage>::
240 operator=(const typename OrthogPolyImpl<T,Storage>::value_type& v)
241 {
242  th_.makeOwnCopy();
243  *th_ = v;
244  return *this;
245 }
246 
247 template <typename T, typename Storage>
248 OrthogPolyImpl<T,Storage>&
249 OrthogPolyImpl<T,Storage>::
250 operator=(const OrthogPolyImpl<T,Storage>& x)
251 {
252  expansion_ = x.expansion_;
253  quad_expansion_ = x.quad_expansion_;
254  th_ = x.th_;
255  return *this;
256 }
257 
258 template <typename T, typename Storage>
259 template <typename S>
260 OrthogPolyImpl<T,Storage>&
261 OrthogPolyImpl<T,Storage>::
262 operator=(const Expr<S>& x)
263 {
264  th_.makeOwnCopy();
265  expansion_ = x.expansion();
266  quad_expansion_ = Teuchos::rcp_dynamic_cast<quad_expansion_type>(expansion_);
267  th_->reset(expansion_->getBasis(), x.size());
268  expressionCopy(x);
269  return *this;
270 }
271 
272 template <typename T, typename Storage>
273 OrthogPolyImpl<T,Storage>&
274 OrthogPolyImpl<T,Storage>::
275 operator+=(const typename OrthogPolyImpl<T,Storage>::value_type& v)
276 {
277  th_.makeOwnCopy();
278  expansion_->plusEqual(*th_, v);
279  return *this;
280 }
281 
282 template <typename T, typename Storage>
283 OrthogPolyImpl<T,Storage>&
284 OrthogPolyImpl<T,Storage>::
285 operator-=(const typename OrthogPolyImpl<T,Storage>::value_type& v)
286 {
287  th_.makeOwnCopy();
288  expansion_->minusEqual(*th_, v);
289  return *this;
290 }
291 
292 template <typename T, typename Storage>
293 OrthogPolyImpl<T,Storage>&
294 OrthogPolyImpl<T,Storage>::
295 operator*=(const typename OrthogPolyImpl<T,Storage>::value_type& v)
296 {
297  th_.makeOwnCopy();
298  expansion_->timesEqual(*th_, v);
299  return *this;
300 }
301 
302 template <typename T, typename Storage>
303 OrthogPolyImpl<T,Storage>&
304 OrthogPolyImpl<T,Storage>::
305 operator/=(const typename OrthogPolyImpl<T,Storage>::value_type& v)
306 {
307  th_.makeOwnCopy();
308  expansion_->divideEqual(*th_, v);
309  return *this;
310 }
311 
312 template <typename T, typename Storage>
313 std::ostream&
314 operator << (std::ostream& os, const OrthogPoly<T,Storage>& a)
315 {
317 
318  os << "[ ";
319 
320  for (ordinal_type i=0; i<a.size(); i++) {
321  os << a.coeff(i) << " ";
322  }
323 
324  os << "]\n";
325  return os;
326 }
327 
328 template <typename T, typename Storage>
329 std::istream&
330 operator >> (std::istream& is, OrthogPoly<T,Storage>& a)
331 {
333 
334  // Read in the opening "["
335  char bracket;
336  is >> bracket;
337 
338  for (ordinal_type i=0; i<a.size(); i++) {
339  is >> a.fastAccessCoeff(i);
340  }
341 
342  // Read in the closing "]"
343 
344  is >> bracket;
345  return is;
346 }
347 
348 } // namespace PCE
349 } // namespace Sacado
#define TEUCHOS_FUNC_TIME_MONITOR(FUNCNAME)
KERNEL_PREFIX value_type operator()(tuple_type x) const
std::istream & operator>>(std::istream &is, OrthogPoly< T, Storage > &a)