OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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
32template <class T>
34Taylor(int order):
35 itsCoeffs(order + 1)
36{}
37
38
39template <class T>
41Taylor():
42 itsCoeffs()
43{}
44
45
46template <class T>
48Taylor(const Taylor &rhs):
49 itsCoeffs(rhs.itsCoeffs)
50{}
51
52
53template <class T>
55~Taylor()
56{}
57
58
59template <class T>
61operator=(const Taylor &rhs) {
62 itsCoeffs = rhs.itsCoeffs;
63 return *this;
64}
65
66
67template<class T> inline
69begin() {
70 return itsCoeffs.begin();
71}
72
73
74template<class T> inline
76begin() const {
77 return itsCoeffs.begin();
78}
79
80
81template<class T> inline
83end() {
84 return itsCoeffs.end();
85}
86
87
88template<class T> inline
90end() const {
91 return itsCoeffs.end();
92}
93
94
95template<class T> inline
97 return itsCoeffs[i];
98}
99
100
101template<class T> inline
102const T &Taylor<T>::operator[](int i) const {
103 return itsCoeffs[i];
104}
105
106
107template<class T>
109 Taylor<T> result(getOrder());
110 std::transform(begin(), end(), result.begin(), std::negate<T>());
111 return result;
112}
113
114
115template<class T>
117 std::transform(begin(), end(), begin(),
118 std::bind(std::multiplies<T>(), std::placeholders::_2, val));
119 return *this;
120}
121
122
123template<class T>
125 std::transform(begin(), end(), begin(),
126 std::bind(std::divides<T>(), std::placeholders::_2, val));
127 return *this;
128}
129
130
131template<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
145template<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
159template <class T>
161clear() {
162 erase(begin(), end());
163}
164
165
166template <class T>
168integrate() 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
184template <class T>
186sum() const {
187 return std::accumulate(begin(), end(), T(0));
188}
189
190
191template <class T> inline
193getOrder() const {
194 return itsCoeffs.size() - 1;
195}
196
197
198// Global functions.
199// ------------------------------------------------------------------------
200
201template <class T>
203operator+(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
217template <class T>
219operator-(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
233template <class T>
235operator*(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
246template <class T>
248operator*(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
259template <class T>
261operator/(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
272template <class T>
274PoissonBracket(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
289template <class T>
290std::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
Taylor< T > operator/(const Taylor< T > &lhs, double rhs)
Divide by scalar.
Definition: Taylor.hpp:261
Taylor< T > operator*(const Taylor< T > &lhs, double rhs)
Multiply by scalar.
Definition: Taylor.hpp:235
Taylor< T > operator-(const Taylor< T > &lhs, const Taylor< T > &rhs)
Subtract.
Definition: Taylor.hpp:219
std::ostream & operator<<(std::ostream &os, const Taylor< T > &series)
Output.
Definition: Taylor.hpp:290
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)
Add.
Definition: Taylor.hpp:203
PartBunchBase< T, Dim >::ConstIterator end(PartBunchBase< T, Dim > const &bunch)
PartBunchBase< T, Dim >::ConstIterator begin(PartBunchBase< T, Dim > const &bunch)
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