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_Taylor.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_TAYLOR_HPP
11 #define SACADO_TAY_TAYLOR_HPP
12 
13 #include "Sacado_ConfigDefs.h"
14 #include "Sacado_Base.hpp"
15 #include "Sacado_Handle.hpp"
16 #include <cmath>
17 #include <algorithm> // for std::min and std::max
18 #include <ostream> // for std::ostream
19 #include "Sacado_dummy_arg.hpp"
20 
21 namespace Sacado {
22 
24  namespace Tay {
25 
27 
31  template <typename T>
32  class Taylor : public Base< Taylor<T> > {
33  public:
34 
36  template <typename U>
37  struct apply {
38  typedef Taylor<U> type;
39  };
40 
42  typedef T value_type;
43 
46 
48  Taylor();
49 
51 
54  Taylor(const T& x);
55 
57 
62 
64 
67  Taylor(int d, const T & x);
68 
70 
73  explicit Taylor(int d);
74 
76  Taylor(const Taylor& x);
77 
79  ~Taylor();
80 
82 
86  void resize(int d, bool keep_coeffs);
87 
89 
92  void reserve(int d);
93 
95 
104  void copyForWrite() { th.makeOwnCopy(); }
105 
107  bool isEqualTo(const Taylor& x) const {
108  typedef IsEqual<value_type> IE;
109  if (x.degree() != this->degree()) return false;
110  bool eq = true;
111  for (int i=0; i<=this->degree(); i++)
112  eq = eq && IE::eval(x.coeff(i), this->coeff(i));
113  return eq;
114  }
115 
120 
122  Taylor<T>& operator=(const T& val);
123 
125 
130 
132  Taylor<T>& operator=(const Taylor<T>& x);
133 
135 
140 
142  const T& val() const { return th->coeff_[0];}
143 
145  T& val() { return th->coeff_[0];}
146 
148 
153 
155  int degree() const { return th->deg_;}
156 
158  bool hasFastAccess(int d) const { return th->deg_>=d;}
159 
161  const T* coeff() const { return th->coeff_;}
162 
164  T* coeff() { return th->coeff_;}
165 
167  T coeff(int i) const {
168  T tmp= i<=th->deg_ ? th->coeff_[i]:T(0.); return tmp;}
169 
171  T& fastAccessCoeff(int i) { return th->coeff_[i];}
172 
174  const T& fastAccessCoeff(int i) const { return th->coeff_[i];}
175 
177 
182 
184  Taylor<T> operator + () const;
185 
187  Taylor<T> operator - () const;
188 
190  Taylor<T>& operator += (const T& x);
191 
193  Taylor<T>& operator -= (const T& x);
194 
196  Taylor<T>& operator *= (const T& x);
197 
199  Taylor<T>& operator /= (const T& x);
200 
203 
206 
209 
212 
214 
215  protected:
216 
218  int length() const { return th->len_; }
219 
221  void resizeCoeffs(int len);
222 
223  protected:
224 
225  struct TaylorData {
226 
229 
231  int deg_;
232 
234  int len_;
235 
237  TaylorData();
238 
240  TaylorData(const T& x);
241 
243  TaylorData(int d, const T & x);
244 
246  TaylorData(int d);
247 
249  TaylorData(int d, int l);
250 
252  TaylorData(const TaylorData& x);
253 
255  ~TaylorData();
256 
258  TaylorData& operator=(const TaylorData& x);
259 
260  };
261 
263 
264  }; // class Taylor
265 
267  template <typename T>
268  Taylor<T> diff(const Taylor<T>& x, int n = 1) {
269  const int d = x.degree();
270  if (n <= 0)
271  return x;
272  else if (n > d) {
273  Taylor<T> y(0);
274  return y;
275  }
276  Taylor<T> y(d-n);
277  int c = 1;
278  for (int i=1; i<=n; ++i)
279  c *= i;
280  for (int i=n; i<=d; ++i) {
281  y.fastAccessCoeff(i-n) = x.fastAccessCoeff(i) * T(c);
282  c = (c / (i-n+1)) * (i+1);
283  }
284  return y;
285  }
286 
287  // Operations
288  template <typename T> Taylor<T> operator+(const Base< Taylor<T> >& a,
289  const Base< Taylor<T> >& b);
290  template <typename T> Taylor<T> operator+(const typename Taylor<T>::value_type& a,
291  const Base< Taylor<T> >& b);
292  template <typename T> Taylor<T> operator+(const Base< Taylor<T> >& a,
293  const typename Taylor<T>::value_type& b);
294  template <typename T> Taylor<T> operator-(const Base< Taylor<T> >& a,
295  const Base< Taylor<T> >& b);
296  template <typename T> Taylor<T> operator-(const typename Taylor<T>::value_type& a,
297  const Base< Taylor<T> >& b);
298  template <typename T> Taylor<T> operator-(const Base< Taylor<T> >& a,
299  const typename Taylor<T>::value_type& b);
300  template <typename T> Taylor<T> operator*(const Base< Taylor<T> >& a,
301  const Base< Taylor<T> >& b);
302  template <typename T> Taylor<T> operator*(const typename Taylor<T>::value_type& a,
303  const Base< Taylor<T> >& b);
304  template <typename T> Taylor<T> operator*(const Base< Taylor<T> >& a,
305  const typename Taylor<T>::value_type& b);
306  template <typename T> Taylor<T> operator/(const Base< Taylor<T> >& a,
307  const Base< Taylor<T> >& b);
308  template <typename T> Taylor<T> operator/(const typename Taylor<T>::value_type& a,
309  const Base< Taylor<T> >& b);
310  template <typename T> Taylor<T> operator/(const Base< Taylor<T> >& a,
311  const typename Taylor<T>::value_type& b);
312  template <typename T> Taylor<T> exp(const Base< Taylor<T> >& a);
313  template <typename T> Taylor<T> log(const Base< Taylor<T> >& a);
314  template <typename T> Taylor<T> log10(const Base< Taylor<T> >& a);
315  template <typename T> Taylor<T> sqrt(const Base< Taylor<T> >& a);
316  template <typename T> Taylor<T> cbrt(const Base< Taylor<T> >& a);
317  template <typename T> Taylor<T> pow(const Base< Taylor<T> >& a,
318  const Base< Taylor<T> >& b);
319  template <typename T> Taylor<T> pow(const typename Taylor<T>::value_type& a,
320  const Base< Taylor<T> >& b);
321  template <typename T> Taylor<T> pow(const Base< Taylor<T> >& a,
322  const typename Taylor<T>::value_type& b);
323  template <typename T> void sincos(const Base< Taylor<T> >& a,
324  Taylor<T>& s, Taylor<T>& c);
325  template <typename T> Taylor<T> cos(const Base< Taylor<T> >& a);
326  template <typename T> Taylor<T> sin(const Base< Taylor<T> >& a);
327  template <typename T> Taylor<T> tan(const Base< Taylor<T> >& a);
328  template <typename T> void sinhcosh(const Base< Taylor<T> >& a,
329  Taylor<T>& s, Taylor<T>& c);
330  template <typename T> Taylor<T> cosh(const Base< Taylor<T> >& a);
331  template <typename T> Taylor<T> sinh(const Base< Taylor<T> >& a);
332  template <typename T> Taylor<T> tanh(const Base< Taylor<T> >& a);
333  template <typename T> Taylor<T> quad(const typename Taylor<T>::value_type& c0,
334  const Base< Taylor<T> >& a,
335  const Base< Taylor<T> >& b);
336  template <typename T> Taylor<T> acos(const Base< Taylor<T> >& a);
337  template <typename T> Taylor<T> asin(const Base< Taylor<T> >& a);
338  template <typename T> Taylor<T> atan(const Base< Taylor<T> >& a);
339  template <typename T> Taylor<T> atan2(const Base< Taylor<T> >& a,
340  const Base< Taylor<T> >& b);
341  template <typename T> Taylor<T> atan2(const typename Taylor<T>::value_type& a,
342  const Base< Taylor<T> >& b);
343  template <typename T> Taylor<T> atan2(const Base< Taylor<T> >& a,
344  const typename Taylor<T>::value_type& b);
345  template <typename T> Taylor<T> acosh(const Base< Taylor<T> >& a);
346  template <typename T> Taylor<T> asinh(const Base< Taylor<T> >& a);
347  template <typename T> Taylor<T> atanh(const Base< Taylor<T> >& a);
348  template <typename T> Taylor<T> abs(const Base< Taylor<T> >& a);
349  template <typename T> Taylor<T> fabs(const Base< Taylor<T> >& a);
350  template <typename T> Taylor<T> max(const Base< Taylor<T> >& a,
351  const Base< Taylor<T> >& b);
352  template <typename T> Taylor<T> max(const typename Taylor<T>::value_type& a,
353  const Base< Taylor<T> >& b);
354  template <typename T> Taylor<T> max(const Base< Taylor<T> >& a,
355  const typename Taylor<T>::value_type& b);
356  template <typename T> Taylor<T> min(const Base< Taylor<T> >& a,
357  const Base< Taylor<T> >& b);
358  template <typename T> Taylor<T> min(const typename Taylor<T>::value_type& a,
359  const Base< Taylor<T> >& b);
360  template <typename T> Taylor<T> min(const Base< Taylor<T> >& a,
361  const typename Taylor<T>::value_type& b);
362  template <typename T> bool operator==(const Base< Taylor<T> >& a,
363  const Base< Taylor<T> >& b);
364  template <typename T> bool operator==(const typename Taylor<T>::value_type& a,
365  const Base< Taylor<T> >& b);
366  template <typename T> bool operator==(const Base< Taylor<T> >& a,
367  const typename Taylor<T>::value_type& b);
368  template <typename T> bool operator!=(const Base< Taylor<T> >& a,
369  const Base< Taylor<T> >& b);
370  template <typename T> bool operator!=(const typename Taylor<T>::value_type& a,
371  const Base< Taylor<T> >& b);
372  template <typename T> bool operator!=(const Base< Taylor<T> >& a,
373  const typename Taylor<T>::value_type& b);
374  template <typename T> bool operator<=(const Base< Taylor<T> >& a,
375  const Base< Taylor<T> >& b);
376  template <typename T> bool operator<=(const typename Taylor<T>::value_type& a,
377  const Base< Taylor<T> >& b);
378  template <typename T> bool operator<=(const Base< Taylor<T> >& a,
379  const typename Taylor<T>::value_type& b);
380  template <typename T> bool operator>=(const Base< Taylor<T> >& a,
381  const Base< Taylor<T> >& b);
382  template <typename T> bool operator>=(const typename Taylor<T>::value_type& a,
383  const Base< Taylor<T> >& b);
384  template <typename T> bool operator>=(const Base< Taylor<T> >& a,
385  const typename Taylor<T>::value_type& b);
386  template <typename T> bool operator<(const Base< Taylor<T> >& a,
387  const Base< Taylor<T> >& b);
388  template <typename T> bool operator<(const typename Taylor<T>::value_type& a,
389  const Base< Taylor<T> >& b);
390  template <typename T> bool operator<(const Base< Taylor<T> >& a,
391  const typename Taylor<T>::value_type& b);
392  template <typename T> bool operator>(const Base< Taylor<T> >& a,
393  const Base< Taylor<T> >& b);
394  template <typename T> bool operator>(const typename Taylor<T>::value_type& a,
395  const Base< Taylor<T> >& b);
396  template <typename T> bool operator>(const Base< Taylor<T> >& a,
397  const typename Taylor<T>::value_type& b);
398  template <typename T> std::ostream& operator << (std::ostream& os,
399  const Base< Taylor<T> >& a);
400 
401  } // namespace Tay
402 
403 } // namespace Sacado
404 
406 #include "Sacado_Tay_TaylorImp.hpp"
407 
408 #endif // SACADO_TAY_TAYLOR_HPP
Taylor< T > operator+(const Base< Taylor< T > > &a, const Base< Taylor< T > > &b)
A generic handle class.
ASinExprType< T >::expr_type asin(const Expr< T > &expr)
void resizeCoeffs(int len)
Resize coefficient array to new size.
void resize(int d, bool keep_coeffs)
Resize polynomial to degree d.
void sinhcosh(const Base< Taylor< T > > &a, Taylor< T > &s, Taylor< T > &c)
T value_type
Typename of values.
int deg_
Degree of polynomial.
Taylor< T > log(const Base< Taylor< T > > &a)
bool hasFastAccess(int d) const
Returns true if polynomial has degree &gt;= d.
Taylor< T > & operator-=(const T &x)
Subtraction-assignment operator with constant right-hand-side.
Taylor< T > asinh(const Base< Taylor< T > > &a)
TanhExprType< T >::expr_type tanh(const Expr< T > &expr)
T & val()
Returns value.
PowExprType< Expr< T1 >, Expr< T2 > >::expr_type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
TanExprType< T >::expr_type tan(const Expr< T > &expr)
T coeff(int i) const
Returns degree i term with bounds checking.
bool operator>(const Base< Taylor< T > > &a, const Base< Taylor< T > > &b)
Taylor< T > operator+() const
Unary-plus operator.
int len_
Length of allocated polynomial array.
TaylorData & operator=(const TaylorData &x)
Assignment operator.
Taylor< T > & operator*=(const T &x)
Multiplication-assignment operator with constant right-hand-side.
Taylor< T > operator/(const Base< Taylor< T > > &a, const Base< Taylor< T > > &b)
Taylor< T > & operator=(const T &val)
Assignment operator with constant right-hand-side.
ACosExprType< T >::expr_type acos(const Expr< T > &expr)
bool operator>=(const Base< Taylor< T > > &a, const Base< Taylor< T > > &b)
Turn Taylor into a meta-function class usable with mpl::apply.
Base class for Sacado types to control overload resolution.
Definition: Sacado_Base.hpp:26
Taylor< T > sin(const Base< Taylor< T > > &a)
#define T
Definition: Sacado_rad.hpp:553
void copyForWrite()
Prepare polynomial for writing.
const T & fastAccessCoeff(int i) const
Returns degree i term without bounds checking.
CacheTaylor< T > diff(const CacheTaylor< T > &x, int n=1)
Compute Taylor series of n-th derivative of x.
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
Taylor< T > cos(const Base< Taylor< T > > &a)
T & fastAccessCoeff(int i)
Returns degree i term without bounds checking.
Taylor< T > operator-() const
Unary-minus operator.
Taylor< T > quad(const typename Taylor< T >::value_type &c0, const Base< Taylor< T > > &a, const Base< Taylor< T > > &b)
Base template specification for testing equivalence.
bool isEqualTo(const Taylor &x) const
Returns whether two Taylor objects have the same values.
Taylor< T > operator*(const Base< Taylor< T > > &a, const Base< Taylor< T > > &b)
Taylor< T > sinh(const Base< Taylor< T > > &a)
Taylor< T > sqrt(const Base< Taylor< T > > &a)
int length() const
Return length of array.
Log10ExprType< T >::expr_type log10(const Expr< T > &expr)
Taylor< T > max(const Base< Taylor< T > > &a, const Base< Taylor< T > > &b)
Taylor< T > cbrt(const Base< Taylor< T > > &a)
Taylor< T > fabs(const Base< Taylor< T > > &a)
const T & val() const
Returns value.
const T * coeff() const
Returns Taylor coefficient array.
Sacado::Handle< TaylorData > th
ATanExprType< T >::expr_type atan(const Expr< T > &expr)
Taylor< T > abs(const Base< Taylor< T > > &a)
void reserve(int d)
Reserve space for a degree d polynomial.
std::ostream & operator<<(std::ostream &os, const Expr< ExprT > &x)
Taylor< T > atan2(const Base< Taylor< T > > &a, const Base< Taylor< T > > &b)
Taylor< T > exp(const Base< Taylor< T > > &a)
T * coeff_
Taylor polynomial coefficients.
ScalarType< value_type >::type scalar_type
Typename of scalar&#39;s (which may be different from value_type)
void sincos(const Base< Taylor< T > > &a, Taylor< T > &s, Taylor< T > &c)
Taylor< T > atanh(const Base< Taylor< T > > &a)
Taylor polynomial class.
Taylor< T > cosh(const Base< Taylor< T > > &a)
bool operator==(const Base< Taylor< T > > &a, const Base< Taylor< T > > &b)
Taylor< T > min(const Base< Taylor< T > > &a, const Base< Taylor< T > > &b)
Taylor< T > operator-(const Base< Taylor< T > > &a, const Base< Taylor< T > > &b)
Taylor< T > & operator/=(const T &x)
Division-assignment operator with constant right-hand-side.
Taylor< T > acosh(const Base< Taylor< T > > &a)
Taylor< T > & operator+=(const T &x)
Addition-assignment operator with constant right-hand-side.
int degree() const
Returns degree of polynomial.
int n
bool operator!=(const Base< Taylor< T > > &a, const Base< Taylor< T > > &b)
const double y
Taylor()
Default constructor.
T * coeff()
Returns Taylor coefficient array.