Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Sacado_Tay_CacheTaylor.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Sacado Package
5 // Copyright (2006) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // This library is free software; you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as
12 // published by the Free Software Foundation; either version 2.1 of the
13 // License, or (at your option) any later version.
14 //
15 // This library is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public
21 // License along with this library; if not, write to the Free Software
22 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 // USA
24 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
25 // (etphipp@sandia.gov).
26 //
27 // ***********************************************************************
28 // @HEADER
29 
30 #ifndef SACADO_TAY_CACHETAYLOR_HPP
31 #define SACADO_TAY_CACHETAYLOR_HPP
32 
33 #include <valarray>
34 
36 
37 // forward decalarations
38 namespace Sacado {
39  namespace Tay {
40  template <class ExprT> class UnaryPlusOp;
41  template <class ExprT> class UnaryMinusOp;
42  }
43 }
44 
45 namespace Sacado {
46 
47  namespace Tay {
48 
49  // Forward declaration
50  template <typename T> class CacheTaylor;
51 
53 
58  template <typename T>
60 
61  public:
62 
64  typedef T value_type;
65 
67  typedef typename ScalarType<T>::type scalar_type;
68 
71 
73 
76  CacheTaylorImplementation(const T& x) : coeff_(x,1) {}
77 
79 
82  CacheTaylorImplementation(int d, const T & x) :
83  coeff_(T(0.),d+1) {
84  coeff_[0] = x;
85  }
86 
89  coeff_(x.coeff_) {}
90 
93 
95 
99  void resize(int d, bool keep_coeffs) {
100  if (keep_coeffs)
101  resizeCoeffs(d);
102  else
103  coeff_.resize(d+1, T(0.));
104  }
105 
110 
112  const T& val() const { return coeff_[0];}
113 
115  T& val() { return coeff_[0];}
116 
118 
123 
125  int degree() const { return coeff_size()-1;}
126 
128  bool hasFastAccess(int d) const { return coeff_size()>=d+1;}
129 
131  const std::valarray<T>& coeff() const { return coeff_;}
132 
134  const T coeff(int i) const {
135  T tmp= i<coeff_size() ? coeff_[i]:T(0.); return tmp;}
136 
138  T coeff(int i) {
139  T tmp= i<coeff_size() ? coeff_[i]:T(0.); return tmp;}
140 
142  T& fastAccessCoeff(int i) { return coeff_[i];}
143 
145  const T& fastAccessCoeff(int i) const { return coeff_[i];}
146 
148  void allocateCache(int d) const {}
149 
151  template <typename S>
152  bool isEqualTo(const Expr<S>& x) const {
153  typedef IsEqual<value_type> IE;
154  if (x.degree() != this->degree()) return false;
155  bool eq = true;
156  for (int i=0; i<=this->degree(); i++)
157  eq = eq && IE::eval(x.coeff(i), this->coeff(i));
158  return eq;
159  }
160 
162 
163  protected:
164 
166  void resizeCoeffs(int dnew) {
167  std::valarray<T> tmp = coeff_;
168  int sz = coeff_size();
169  coeff_.resize(dnew+1,T(0.));
170  if (sz > dnew+1) {
171  std::slice s(0,dnew+1,1);
172  coeff_ = tmp[s];
173  }
174  else {
175  std::slice s(0,sz,1);
176  coeff_[s] = tmp;
177  }
178  }
179 
180  int coeff_size() const { return coeff_.size(); }
181 
182  protected:
183 
185  std::valarray<T> coeff_;
186 
187  }; // class CacheTaylorImplementation
188 
190 
193  template <typename T>
195  public CacheTaylorImplementation<T> {
196 
197  public:
198 
201 
204 
206 
209  Expr(const T & x) : CacheTaylorImplementation<T>(x) {}
210 
212 
215  Expr(int d, const T & x) : CacheTaylorImplementation<T>(d,x) {}
216 
219 
220  }; // class Expr< CacheTaylorImplementation<T> >
221 
223 
227  template <typename T>
228  class CacheTaylor : public Expr< CacheTaylorImplementation<T> > {
229 
230  public:
231 
233  typedef T value_type;
234 
237 
239  template <typename U>
240  struct apply {
242  };
243 
248 
251 
253 
257 
259 
265 
267 
270  CacheTaylor(int d, const T & x) :
271  Expr< CacheTaylorImplementation<T> >(d,x) {}
272 
275 
277  template <typename S> CacheTaylor(const Expr<S>& x);
278 
280 
283 
288 
290  CacheTaylor<T>& operator=(const T& v);
291 
293 
299  return operator=(value_type(val));
300  }
301 
304 
306  template <typename S> CacheTaylor<T>& operator=(const Expr<S>& x);
307 
309 
314 
317  operator + () const {
318  typedef UnaryExpr< CacheTaylor<T>, UnaryPlusOp > expr_t;
319  return Expr<expr_t>(expr_t(*this));
320  }
321 
324  operator - () const {
325  typedef UnaryExpr< CacheTaylor<T>, UnaryMinusOp > expr_t;
326  return Expr<expr_t>(expr_t(*this));
327  }
328 
330  CacheTaylor<T>& operator += (const T& x);
331 
333  CacheTaylor<T>& operator -= (const T& x);
334 
336  CacheTaylor<T>& operator *= (const T& x);
337 
339  CacheTaylor<T>& operator /= (const T& x);
340 
342  template <typename S> CacheTaylor<T>& operator += (const S& x);
343 
345  template <typename S> CacheTaylor<T>& operator -= (const S& x);
346 
348  template <typename S> CacheTaylor<T>& operator *= (const S& x);
349 
351  template <typename S> CacheTaylor<T>& operator /= (const S& x);
352 
354 
355  }; // class CacheTaylor<T>
356 
357  } // namespace Tay
358 
359 } // namespace Sacado
360 
364 
365 #endif // SACADO_TAYLOR_CACHETAYLOR_HPP
ScalarType< T >::type scalar_type
Typename of scalar&#39;s (which may be different from ValueT)
CacheTaylor< T > & operator=(const T &v)
Assignment operator with constant right-hand-side.
const T & val() const
Returns value.
Forward-mode AD class using dynamic memory allocation.
unsigned int degree() const
Return degree of polynomial.
bool hasFastAccess(int d) const
Returns true if polynomial has degree &gt;= d.
CacheTaylorImplementation(const CacheTaylorImplementation &x)
Copy constructor.
int degree() const
Returns degree of polynomial.
bool isEqualTo(const Expr< S > &x) const
Returns whether two Taylor objects have the same values.
CacheTaylor()
Default constructor.
T coeff(int i)
Returns degree i term with bounds checking.
Unary expression template.
CacheTaylor< T > base_expr_type
Typename of base-expressions.
CacheTaylor(int d, const T &x)
Constructor with degree d and value x.
void resizeCoeffs(int dnew)
Resize coefficient array to new size.
void allocateCache(int d) const
Allocate coefficient cache.
T & fastAccessCoeff(int i)
Returns degree i term without bounds checking.
T value_type
Typename of values.
const T coeff(int i) const
Returns degree i term with bounds checking.
#define T
Definition: Sacado_rad.hpp:573
Base template specification for testing equivalence.
Turn CacheTaylor into a meta-function class usable with mpl::apply.
std::valarray< T > coeff_
Taylor polynomial coefficients.
Expr< UnaryExpr< CacheTaylor< T >, UnaryMinusOp > > operator-() const
Unary-minus operator.
CacheTaylor< T > & operator/=(const T &x)
Division-assignment operator with constant right-hand-side.
CacheTaylor(const CacheTaylor &x)
Copy constructor.
CacheTaylor< T > & operator+=(const T &x)
Addition-assignment operator with constant right-hand-side.
CacheTaylor< T > & operator-=(const T &x)
Subtraction-assignment operator with constant right-hand-side.
const std::valarray< T > & coeff() const
Returns Taylor coefficient array.
value_type coeff(unsigned int i) const
Return degree i term of expression.
Expr(int d, const T &x)
Constructor with degree d and value x.
CacheTaylor(const typename dummy< value_type, scalar_type >::type &x)
Constructor with supplied value x.
CacheTaylor(const T &x)
Constructor with supplied value x.
CacheTaylor< T > & operator=(const typename dummy< value_type, scalar_type >::type &val)
Assignment operator with constant right-hand-side.
Expr< UnaryExpr< CacheTaylor< T >, UnaryPlusOp > > operator+() const
Unary-plus operator.
CacheTaylor< T > & operator*=(const T &x)
Multiplication-assignment operator with constant right-hand-side.
CacheTaylorImplementation(const T &x)
Constructor with supplied value x.
void resize(int d, bool keep_coeffs)
Resize polynomial to degree d.
Wrapper for a generic expression template.
CacheTaylorImplementation(int d, const T &x)
Constructor with degree d and value x.
Expr(const T &x)
Constructor with supplied value x.
const T & fastAccessCoeff(int i) const
Returns degree i term without bounds checking.
Taylor polynomial class using caching expression templates.
ScalarType< T >::type scalar_type
Typename of scalar&#39;s (which may be different from ValueT)