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_CacheTaylorImp.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 template <typename T>
11 template <typename S>
13  Expr< CacheTaylorImplementation<T> >(x.degree(), T(0.))
14 {
15  int d = this->degree();
16 
17  x.allocateCache(d);
18 
19  // We copy the coefficients from the highest degree to the lowest just
20  // to be consistent with operator=(), even though it is not strictly
21  // necessary since "this" cannot be on the RHS in the copy constructor.
22  if (x.hasFastAccess(d))
23  for(int i=d; i>=0; --i)
24  this->coeff_[i] = x.fastAccessCoeff(i);
25  else
26  for(int i=d; i>=0; --i)
27  this->coeff_[i] = x.coeff(i);
28 
29 }
30 
31 template <typename T>
34 {
35  this->coeff_[0] = v;
36 
37  for (int i=1; i<this->coeff_size(); i++)
38  this->coeff_[i] = T(0.);
39 
40  return *this;
41 }
42 
43 template <typename T>
46 {
47  if (x.coeff_size() != this->coeff_size())
48  this->coeff_.resize(x.coeff_size());
49  this->coeff_ = x.coeff_;
50 
51  return *this;
52 }
53 
54 template <typename T>
55 template <typename S>
58 {
59  int d = this->degree();
60  int xd = x.degree();
61 
62  // Resize polynomial for "this" if x has greater degree
63  if (xd > d) {
64  this->coeff_.resize(xd+1);
65  d = xd;
66  }
67 
68  x.allocateCache(d);
69 
70  // Copy coefficients. Note: we copy from the last term down to the first
71  // to take into account "this" being involved in an expression on the RHS.
72  // By overwriting the degree k term, we are guarranteed not to affect any
73  // of the lower degree terms. However, this in general will force each
74  // term in the expression to compute all of its coefficients at once instead
75  // traversing the expression once for each degree.
76  if (x.hasFastAccess(d))
77  for(int i=d; i>=0; --i)
78  this->coeff_[i] = x.fastAccessCoeff(i);
79  else
80  for(int i=d; i>=0; --i)
81  this->coeff_[i] = x.coeff(i);
82 
83  return *this;
84 }
85 
86 template <typename T>
89 {
90  this->coeff_[0] += v;
91 
92  return *this;
93 }
94 
95 template <typename T>
98 {
99  this->coeff_[0] -= v;
100 
101  return *this;
102 }
103 
104 template <typename T>
107 {
108  this->coeff_ *= v;
109 
110  return *this;
111 }
112 
113 template <typename T>
116 {
117  this->coeff_ /= v;
118 
119  return *this;
120 }
121 
122 template <typename T>
123 template <typename S>
126 {
127  int xd = x.degree();
128  int d = this->degree();
129 
130  // Resize polynomial for "this" if x has greater degree
131  if (xd > d) {
132  this->resizeCoeffs(xd);
133  d = xd;
134  }
135 
136  x.allocateCache(d);
137 
138  if (x.hasFastAccess(d))
139  for (int i=d; i>=0; --i)
140  this->coeff_[i] += x.fastAccessCoeff(i);
141  else
142  for (int i=xd; i>=0; --i)
143  this->coeff_[i] += x.coeff(i);
144 
145  return *this;
146 }
147 
148 template <typename T>
149 template <typename S>
152 {
153  int xd = x.degree();
154  int d = this->degree();
155 
156  // Resize polynomial for "this" if x has greater degree
157  if (xd > d) {
158  this->resizeCoeffs(xd);
159  d = xd;
160  }
161 
162  x.allocateCache(d);
163 
164  if (x.hasFastAccess(d))
165  for (int i=d; i>=0; --i)
166  this->coeff_[i] -= x.fastAccessCoeff(i);
167  else
168  for (int i=xd; i>=0; --i)
169  this->coeff_[i] -= x.coeff(i);
170 
171  return *this;
172 }
173 
174 template <typename T>
175 template <typename S>
178 {
179  int xd = x.degree();
180  int d = this->degree();
181  int dfinal = d;
182 
183  // Resize polynomial for "this" if x has greater degree
184  if (xd > d) {
185  this->resizeCoeffs(xd);
186  dfinal = xd;
187  }
188 
189  x.allocateCache(dfinal);
190 
191  if (xd) {
192  if (d) {
193  T tmp;
194  if (x.hasFastAccess(dfinal))
195  for(int i=dfinal; i>=0; --i) {
196  tmp = T(0.);
197  for (int k=0; k<=i; ++k)
198  tmp += this->coeff_[k]*x.fastAccessCoeff(i-k);
199  this->coeff_[i] = tmp;
200  }
201  else
202  for(int i=dfinal; i>=0; --i) {
203  tmp = T(0.);
204  for (int k=0; k<=i; ++k)
205  tmp += this->coeff_[k]*x.coeff(i-k);
206  this->coeff_[i] = tmp;
207  }
208  }
209  else {
210  if (x.hasFastAccess(dfinal))
211  for(int i=dfinal; i>=0; --i)
212  this->coeff_[i] = this->coeff_[0] * x.fastAccessCoeff(i);
213  else
214  for(int i=dfinal; i>=0; --i)
215  this->coeff_[i] = this->coeff_[0] * x.coeff(i);
216  }
217  }
218  else
219  this->coeff_ *= x.coeff(0);
220 
221  return *this;
222 }
223 
224 template <typename T>
225 template <typename S>
228 {
229  int xd = x.degree();
230  int d = this->degree();
231  int dfinal = d;
232 
233  // Resize polynomial for "this" if x has greater degree
234  if (xd > d) {
235  this->resizeCoeffs(xd);
236  dfinal = xd;
237  }
238 
239  x.allocateCache(dfinal);
240 
241  if (xd) {
242  std::valarray<T> tmp(this->coeff_);
243  if (x.hasFastAccess(dfinal))
244  for(int i=0; i<=dfinal; i++) {
245  for (int k=1; k<=i; k++)
246  tmp[i] -= x.fastAccessCoeff(k)*tmp[i-k];
247  tmp[i] /= x.fastAccessCoeff(0);
248  }
249  else
250  for(int i=0; i<=dfinal; i++) {
251  for (int k=1; k<=i; k++)
252  tmp[i] -= x.coeff(k)*tmp[i-k];
253  tmp[i] /= x.coeff(0);
254  }
255  this->coeff_ = tmp;
256  }
257  else
258  this->coeff_ /= x.coeff(0);
259 
260  return *this;
261 }
262 
CacheTaylor< T > & operator=(const T &v)
Assignment operator with constant right-hand-side.
Forward-mode AD class using dynamic memory allocation.
unsigned int degree() const
Return degree of polynomial.
int degree() const
Returns degree of polynomial.
CacheTaylor()
Default constructor.
value_type fastAccessCoeff(unsigned int i) const
Return degree i term of expression.
#define T
Definition: Sacado_rad.hpp:553
bool hasFastAccess(unsigned int d) const
Return if expression has fast access.
std::valarray< T > coeff_
Taylor polynomial coefficients.
CacheTaylor< T > & operator/=(const T &x)
Division-assignment operator with constant right-hand-side.
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.
value_type coeff(unsigned int i) const
Return degree i term of expression.
CacheTaylor< T > & operator*=(const T &x)
Multiplication-assignment operator with constant right-hand-side.
void allocateCache(unsigned int d) const
Allocate coefficient cache.
Wrapper for a generic expression template.
Taylor polynomial class using caching expression templates.