OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
Taylor.hpp
Go to the documentation of this file.
1 #ifndef CLASSIC_Taylor_CC
2 #define CLASSIC_Taylor_CC
3 // ------------------------------------------------------------------------
4 // $RCSfile: Taylor.cpp,v $
5 // ------------------------------------------------------------------------
6 // $Revision: 1.2.2.1 $
7 // ------------------------------------------------------------------------
8 // Copyright: see Copyright.readme
9 // ------------------------------------------------------------------------
10 //
11 // Template class: Taylor<T>
12 //
13 // ------------------------------------------------------------------------
14 // Class category: FixedAlgebra
15 // ------------------------------------------------------------------------
16 //
17 // $Date: 2004/11/18 22:18:06 $
18 // $Author: jsberg $
19 //
20 // ------------------------------------------------------------------------
21 
22 #include <functional>
23 #include <numeric>
24 #include <iomanip>
25 
26 #include "FixedAlgebra/Taylor.h"
27 
28 
29 // Template class Taylor<T>.
30 // ------------------------------------------------------------------------
31 
32 template <class T>
34 Taylor(int order):
35  itsCoeffs(order + 1)
36 {}
37 
38 
39 template <class T>
41 Taylor():
42  itsCoeffs()
43 {}
44 
45 
46 template <class T>
48 Taylor(const Taylor &rhs):
49  itsCoeffs(rhs.itsCoeffs)
50 {}
51 
52 
53 template <class T>
55 ~Taylor()
56 {}
57 
58 
59 template <class T>
61 operator=(const Taylor &rhs) {
62  itsCoeffs = rhs.itsCoeffs;
63  return *this;
64 }
65 
66 
67 template<class T> inline
69 begin() {
70  return itsCoeffs.begin();
71 }
72 
73 
74 template<class T> inline
75 const T *Taylor<T>::
76 begin() const {
77  return itsCoeffs.begin();
78 }
79 
80 
81 template<class T> inline
83 end() {
84  return itsCoeffs.end();
85 }
86 
87 
88 template<class T> inline
89 const T *Taylor<T>::
90 end() const {
91  return itsCoeffs.end();
92 }
93 
94 
95 template<class T> inline
97  return itsCoeffs[i];
98 }
99 
100 
101 template<class T> inline
102 const T &Taylor<T>::operator[](int i) const {
103  return itsCoeffs[i];
104 }
105 
106 
107 template<class T>
109  Taylor<T> result(getOrder());
110  std::transform(begin(), end(), result.begin(), std::negate<T>());
111  return result;
112 }
113 
114 
115 template<class T>
117  std::transform(begin(), end(), begin(),
118  std::bind(std::multiplies<T>(), std::placeholders::_2, val));
119  return *this;
120 }
121 
122 
123 template<class T>
125  std::transform(begin(), end(), begin(),
126  std::bind(std::divides<T>(), std::placeholders::_2, val));
127  return *this;
128 }
129 
130 
131 template<class T>
133  if(getOrder() < rhs.getOrder()) {
134  Taylor<T> temp(rhs.getOrder());
135  std::copy(begin(), end(), temp.begin());
136  *this = temp;
137  }
138 
139  int n = rhs.end() - rhs.begin();
140  std::transform(begin(), begin() + n, rhs.begin(), begin(), std::plus<T>());
141  return *this;
142 }
143 
144 
145 template<class T>
147  if(getOrder() < rhs.getOrder()) {
148  Taylor<T> temp(rhs.getOrder());
149  std::copy(begin(), end(), temp.begin());
150  *this = temp;
151  }
152 
153  int n = rhs.end() - rhs.begin();
154  std::transform(begin(), begin() + n, rhs.begin(), begin(), std::minus<T>());
155  return *this;
156 }
157 
158 
159 template <class T>
161 clear() {
162  erase(begin(), end());
163 }
164 
165 
166 template <class T>
168 integrate() const {
169  Taylor<T> result(getOrder() + 1);
170  const T *x1 = begin();
171  const T *x2 = end();
172  T *z = result.begin();
173  double div = 0.0;
174 
175  for(const T *x = x1; x < x2; ++x, ++z) {
176  div += 1.0;
177  *z = *x / div;
178  }
179 
180  return result;
181 }
182 
183 
184 template <class T>
186 sum() const {
187  return std::accumulate(begin(), end(), T(0));
188 }
189 
190 
191 template <class T> inline
193 getOrder() const {
194  return itsCoeffs.size() - 1;
195 }
196 
197 
198 // Global functions.
199 // ------------------------------------------------------------------------
200 
201 template <class T>
202 Taylor<T>
203 operator+(const Taylor<T> &lhs, const Taylor<T> &rhs) {
204  Taylor<T> result(std::max(lhs.getOrder(), rhs.getOrder()));
205  const T *x = lhs.begin();
206  const T *y = rhs.begin();
207  T *z = result.begin();
208  while(x != lhs.end() && y != rhs.end()) {
209  *z++ = *x++ + *y++;
210  }
211  while(x != lhs.end()) *z++ = *x++;
212  while(y != rhs.end()) *z++ = *y++;
213  return result;
214 }
215 
216 
217 template <class T>
218 Taylor<T>
219 operator-(const Taylor<T> &lhs, const Taylor<T> &rhs) {
220  Taylor<T> result(std::max(lhs.getOrder(), rhs.getOrder()));
221  const T *x = lhs.begin();
222  const T *y = rhs.begin();
223  T *z = result.begin();
224  while(x != lhs.end() && y != rhs.end()) {
225  *z++ = *x++ - *y++;
226  }
227  while(x != lhs.end()) *z++ = *x++;
228  while(y != rhs.end()) *z++ = - *y++;
229  return result;
230 }
231 
232 
233 template <class T>
234 Taylor<T>
235 operator*(const Taylor<T> &lhs, double rhs) {
236  Taylor<T> result(lhs.getOrder());
237  const T *x = lhs.begin();
238  T *z = result.begin();
239  while(x != lhs.end()) {
240  *z++ = *x++ * rhs;
241  }
242  return result;
243 }
244 
245 
246 template <class T>
247 Taylor<T>
248 operator*(double lhs, const Taylor<T> &rhs) {
249  Taylor<T> result(rhs.getOrder());
250  const T *x = rhs.begin();
251  T *z = result.begin();
252  while(x != rhs.end()) {
253  *z++ = lhs * *x++;
254  }
255  return result;
256 }
257 
258 
259 template <class T>
260 Taylor<T>
261 operator/(const Taylor<T> &lhs, double rhs) {
262  Taylor<T> result(lhs.getOrder());
263  const T *x = lhs.begin();
264  T *z = result.begin();
265  while(x != lhs.end()) {
266  *z++ = *x++ / rhs;
267  }
268  return result;
269 }
270 
271 
272 template <class T>
273 Taylor<T>
274 PoissonBracket(const Taylor<T> &lhs, const Taylor<T> &rhs) {
275  Taylor<T> result(lhs.getOrder() + rhs.getOrder());
276  for(int i = 0; i <= lhs.getOrder(); ++i) {
277  if(! lhs[i].isZero()) {
278  for(int j = 0; j <= rhs.getOrder(); ++j) {
279  if(! rhs[j].isZero()) {
280  result[i+j] += PoissonBracket(lhs[i], rhs[j]);
281  }
282  }
283  }
284  }
285  return result;
286 }
287 
288 
289 template <class T>
290 std::ostream &operator<<(std::ostream &os, const Taylor<T> &series) {
291  os << "Taylor" << std::setw(4) << series.getOrder() << std::endl;
292 
293  for(int i = 0; i <= series.getOrder(); ++i) {
294  os << "Terms of order" << std::setw(4) << i << " " << series[i];
295  }
296 
297  return os;
298 }
299 
300 #endif
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
Taylor< T > PoissonBracket(const Taylor< T > &lhs, const Taylor< T > &rhs)
Poisson bracket of two Taylor seriess.
Definition: Taylor.hpp:274
Taylor< T > operator-(const Taylor< T > &lhs, const Taylor< T > &rhs)
Subtract.
Definition: Taylor.hpp:219
Taylor< T > operator+(const Taylor< T > &lhs, const Taylor< T > &rhs)
Add.
Definition: Taylor.hpp:203
Taylor< T > operator/(const Taylor< T > &lhs, double rhs)
Divide by scalar.
Definition: Taylor.hpp:261
std::ostream & operator<<(std::ostream &os, const Taylor< T > &series)
Output.
Definition: Taylor.hpp:290
Taylor< T > operator*(const Taylor< T > &lhs, double rhs)
Multiply by scalar.
Definition: Taylor.hpp:235
T::PETE_Expr_t::PETE_Return_t max(const PETE_Expr< T > &expr, NDIndex< D > &loc)
Definition: ReductionLoc.h:84
Inform & endl(Inform &inf)
Definition: Inform.cpp:42
A representation for a Taylor series in one variable,.
Definition: Taylor.h:36
T * end()
Get pointer to end of series (one beyond highest term).
Definition: Taylor.hpp:83
void clear()
Clear all coefficients.
Definition: Taylor.hpp:161
int getOrder() const
Return order of this series.
Definition: Taylor.hpp:193
Taylor operator-() const
Change sign of series.
Definition: Taylor.hpp:108
T * begin()
Get pointer to beginning of series (zero-order term).
Definition: Taylor.hpp:69
Taylor & operator/=(const T &)
Divide by scalar and assign.
Definition: Taylor.hpp:124
T sum() const
Return sum of series.
Definition: Taylor.hpp:186
Taylor()
Definition: Taylor.hpp:41
Taylor & operator-=(const Taylor &)
Subtract series and assign.
Definition: Taylor.hpp:146
~Taylor()
Definition: Taylor.hpp:55
T & operator[](int n)
Get coefficient.
Definition: Taylor.hpp:96
Taylor & operator+=(const Taylor &)
Add series and assign.
Definition: Taylor.hpp:132
const Taylor & operator=(const Taylor &)
Definition: Taylor.hpp:61
Taylor integrate() const
Integrate with respect to the variable.
Definition: Taylor.hpp:168
Taylor & operator*=(const T &)
Multiply by scalar and assign.
Definition: Taylor.hpp:116
Array1D< T > itsCoeffs
Definition: Taylor.h:108