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 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef SACADO_TAY_CACHETAYLOR_HPP
11 #define SACADO_TAY_CACHETAYLOR_HPP
12 
13 #include <valarray>
14 
16 
17 // forward decalarations
18 namespace Sacado {
19  namespace Tay {
20  template <class ExprT> class UnaryPlusOp;
21  template <class ExprT> class UnaryMinusOp;
22  }
23 }
24 
25 namespace Sacado {
26 
27  namespace Tay {
28 
29  // Forward declaration
30  template <typename T> class CacheTaylor;
31 
33 
38  template <typename T>
40 
41  public:
42 
44  typedef T value_type;
45 
47  typedef typename ScalarType<T>::type scalar_type;
48 
51 
53 
56  CacheTaylorImplementation(const T& x) : coeff_(x,1) {}
57 
59 
62  CacheTaylorImplementation(int d, const T & x) :
63  coeff_(T(0.),d+1) {
64  coeff_[0] = x;
65  }
66 
69  coeff_(x.coeff_) {}
70 
73 
75 
79  void resize(int d, bool keep_coeffs) {
80  if (keep_coeffs)
81  resizeCoeffs(d);
82  else
83  coeff_.resize(d+1, T(0.));
84  }
85 
90 
92  const T& val() const { return coeff_[0];}
93 
95  T& val() { return coeff_[0];}
96 
98 
103 
105  int degree() const { return coeff_size()-1;}
106 
108  bool hasFastAccess(int d) const { return coeff_size()>=d+1;}
109 
111  const std::valarray<T>& coeff() const { return coeff_;}
112 
114  const T coeff(int i) const {
115  T tmp= i<coeff_size() ? coeff_[i]:T(0.); return tmp;}
116 
118  T coeff(int i) {
119  T tmp= i<coeff_size() ? coeff_[i]:T(0.); return tmp;}
120 
122  T& fastAccessCoeff(int i) { return coeff_[i];}
123 
125  const T& fastAccessCoeff(int i) const { return coeff_[i];}
126 
128  void allocateCache(int d) const {}
129 
131  template <typename S>
132  bool isEqualTo(const Expr<S>& x) const {
133  typedef IsEqual<value_type> IE;
134  if (x.degree() != this->degree()) return false;
135  bool eq = true;
136  for (int i=0; i<=this->degree(); i++)
137  eq = eq && IE::eval(x.coeff(i), this->coeff(i));
138  return eq;
139  }
140 
142 
143  protected:
144 
146  void resizeCoeffs(int dnew) {
147  std::valarray<T> tmp = coeff_;
148  int sz = coeff_size();
149  coeff_.resize(dnew+1,T(0.));
150  if (sz > dnew+1) {
151  std::slice s(0,dnew+1,1);
152  coeff_ = tmp[s];
153  }
154  else {
155  std::slice s(0,sz,1);
156  coeff_[s] = tmp;
157  }
158  }
159 
160  int coeff_size() const { return coeff_.size(); }
161 
162  protected:
163 
165  std::valarray<T> coeff_;
166 
167  }; // class CacheTaylorImplementation
168 
170 
173  template <typename T>
175  public CacheTaylorImplementation<T> {
176 
177  public:
178 
181 
184 
186 
189  Expr(const T & x) : CacheTaylorImplementation<T>(x) {}
190 
192 
195  Expr(int d, const T & x) : CacheTaylorImplementation<T>(d,x) {}
196 
199 
200  }; // class Expr< CacheTaylorImplementation<T> >
201 
203 
207  template <typename T>
208  class CacheTaylor : public Expr< CacheTaylorImplementation<T> > {
209 
210  public:
211 
213  typedef T value_type;
214 
217 
219  template <typename U>
220  struct apply {
222  };
223 
228 
231 
233 
237 
239 
245 
247 
250  CacheTaylor(int d, const T & x) :
251  Expr< CacheTaylorImplementation<T> >(d,x) {}
252 
255 
257  template <typename S> CacheTaylor(const Expr<S>& x);
258 
260 
263 
268 
270  CacheTaylor<T>& operator=(const T& v);
271 
273 
279  return operator=(value_type(val));
280  }
281 
284 
286  template <typename S> CacheTaylor<T>& operator=(const Expr<S>& x);
287 
289 
294 
297  operator + () const {
298  typedef UnaryExpr< CacheTaylor<T>, UnaryPlusOp > expr_t;
299  return Expr<expr_t>(expr_t(*this));
300  }
301 
304  operator - () const {
305  typedef UnaryExpr< CacheTaylor<T>, UnaryMinusOp > expr_t;
306  return Expr<expr_t>(expr_t(*this));
307  }
308 
310  CacheTaylor<T>& operator += (const T& x);
311 
313  CacheTaylor<T>& operator -= (const T& x);
314 
316  CacheTaylor<T>& operator *= (const T& x);
317 
319  CacheTaylor<T>& operator /= (const T& x);
320 
322  template <typename S> CacheTaylor<T>& operator += (const S& x);
323 
325  template <typename S> CacheTaylor<T>& operator -= (const S& x);
326 
328  template <typename S> CacheTaylor<T>& operator *= (const S& x);
329 
331  template <typename S> CacheTaylor<T>& operator /= (const S& x);
332 
334 
335  }; // class CacheTaylor<T>
336 
337  } // namespace Tay
338 
339 } // namespace Sacado
340 
344 
345 #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:553
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)