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