OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
Tps.h
Go to the documentation of this file.
1 #ifndef CLASSIC_Tps_HH
2 #define CLASSIC_Tps_HH
3 
4 // ------------------------------------------------------------------------
5 // $RCSfile: Tps.h,v $
6 // ------------------------------------------------------------------------
7 // $Revision: 1.2.2.1 $
8 // ------------------------------------------------------------------------
9 // Copyright: see Copyright.readme
10 // ------------------------------------------------------------------------
11 //
12 // Template Class: Tps<class T>
13 // Truncated power series in n variables of type T.
14 //
15 // ------------------------------------------------------------------------
16 // Class category: Algebra
17 // ------------------------------------------------------------------------
18 //
19 // $Date: 2002/03/25 20:44:14 $
20 // $Author: dbruhwil $
21 //
22 // ------------------------------------------------------------------------
23 
24 #include <iosfwd>
25 #include <climits>
26 
27 template <class T> class Tps;
28 template <class T> class TpsRep;
29 template <class T> class Matrix;
30 template <class T> class Vector;
31 template <class T> class VpsMap;
32 
33 class TpsMonomial;
34 
35 
36 // Class Tps
37 // ------------------------------------------------------------------------
39 // Truncated power series in [b]n[/b] variables of type [b]T[/b].
40 // All divide operations throw DivideError,
41 // if the constant part of the divisor is zero.
42 // All operations throw SizeError, if the operands are not consistent.
43 // All operations throw RangeError, if an index is out of range.
44 
45 template <class T>
46 class Tps {
47 
48 public:
49 
51  // Define maximum order and number of variables, value = 0.
52  Tps(int maxOrder, int nVar);
53 
55  // From constant [b]y[/b].
56  Tps(const T &y);
57 
59  // From int [b]y[/b].
60  Tps(int y);
61 
62  Tps();
63  Tps(const Tps<T> &x);
64  ~Tps();
65  Tps<T> &operator=(const Tps<T> &y);
66 
68  Tps<T> &operator=(const T &y);
69 
71  // Return the orders [b]lowOrder[/b] through [b]highOrder[/b],
72  // both included.
73  Tps<T> filter(int lowOrder, int highOrder) const;
74 
76  // Change the maximum order to [b]trunc[/b]; may also reserve more space.
77  Tps<T> truncate(int trunc);
78 
80  // Return the coefficient denoted by the Giorgilli index.
81  // Throw RangeError, if index is out of range.
82  const T getCoefficient(int index) const;
83 
85  // Assign the coefficient denoted by the Giorgilli index.
86  // Expand size if index is out of range.
87  void setCoefficient(int index, const T &value);
88 
90  // Return the coefficient denoted by monomial exponents.
91  // Throw RangeError, if index is out of range.
92  const T getCoefficient(const TpsMonomial &monomial) const;
93 
95  // Assign the coefficient denoted by monomial exponents.
96  // Throw RangeError, if index is out of range.
97  void setCoefficient(const TpsMonomial &monomial, const T &value);
98 
100  // Return the coefficient denoted by the Giorgilli index.
101  // Result is undefined, if index is out of range.
102  const T operator[](int index) const;
103 
105  // REturn a reference to the coefficient denoted by the Giorgilli index.
106  // Result is undefined, if index is out of range.
107  T &operator[](int index);
108 
110  // Return the coefficient denoted by monomial exponents.
111  // Result is undefined, if index is out of range.
112  const T operator[](const TpsMonomial &monomial) const;
113 
115  // Return reference to the coefficient denoted by monomial exponents.
116  // Result is undefined, if index is out of range.
117  T &operator[](const TpsMonomial &monomial);
118 
120  // Construct the variable identified by the index [b]var[/b],
121  // with total of [b]nVar[/b] variables.
122  static Tps<T> makeVariable(int nVar, int var);
123 
125  // Construct power of variable [b]var[/b], with total of [b]nVar[/b]
126  // variables.
127  static Tps<T> makeVarPower(int nVar, int var, int power);
128 
130  // Construct the monomial with the exponents in [b]m[/b] and coefficient
131  // [b]t[/b].
132  static Tps<T> makeMonomial(const TpsMonomial &m, const T &t);
133 
135  Tps<T> operator+() const;
136 
138  Tps<T> operator-() const;
139 
141  Tps<T> &operator+=(const Tps<T> &y);
142 
144  Tps<T> &operator-=(const Tps<T> &y);
145 
147  Tps<T> &operator*=(const Tps<T> &y);
148 
150  Tps<T> &operator/=(const Tps<T> &y);
151 
153  Tps<T> &operator+=(const T &y);
154 
156  Tps<T> &operator-=(const T &y);
157 
159  Tps<T> &operator*=(const T &y);
160 
162  Tps<T> &operator/=(const T &y);
163 
165  bool operator==(const Tps<T> &y) const;
166 
168  bool operator==(const T &y) const;
169 
171  bool operator!=(const Tps<T> &y) const;
172 
174  bool operator!=(const T &y) const;
175 
177  // Substitute matrix [b]M[/b] in Tps, giving new Tps.
178  Tps<T> substitute(const Matrix<T> &M) const;
179 
181  // Substitute map [b]m[/b] in Tps, giving new Tps.
182  Tps<T> substitute(const VpsMap<T> &m) const;
183 
185  // Substitute vector [b]v[/b] in Tps, giving a constant.
186  T evaluate(const Vector<T> &v) const;
187 
189  void clear();
190 
192  Tps<T> inverse(int order = truncOrder) const;
193 
195  Tps<T> multiply(const Tps<T> &y, int trunc) const;
196 
198  // Return partial derivative with respect to variable [b]var[/b].
199  // Return zero for a constant.
200  Tps<T> derivative(int var) const;
201 
203  // Return partial integral with respect to variable [b]var[/b].
204  // Throw LogicalError for a constant.
205  Tps<T> integral(int var) const;
206 
208  // Throw LogicalError for a constant.
209  Tps<T> multiplyVariable(int var) const;
210 
212  // Construct new Tps<T> by multipying corresponding monomials pair-wise.
213  Tps<T> scaleMonomials(const Tps<T> &y) const;
214 
216  // Expand truncated Taylor series with coefficiens [b]series[/b]
217  // and order [b]n[/b].
218  Tps<T> Taylor(const T series[], int n) const;
219 
221  int getMaxOrder() const;
222 
224  int getTruncOrder() const;
225 
227  int getVariables() const;
228 
230  int getSize() const;
231 
233  std::istream &get(std::istream &is);
234 
236  std::ostream &put(std::ostream &os) const;
237 
239  // Return the exponent array for the given Giorgilli index.
240  const TpsMonomial &getExponents(int index) const;
241 
243  // Return the order of the monomial with the given Giorgilli index.
244  int getOrder(int index) const;
245 
247  // Return the number of monomials required for a Tps of order [b]order[/b].
248  int getSize(int order) const;
249 
251  bool isConstant() const;
252 
254  static int getGlobalTruncOrder();
255 
257  static void setGlobalTruncOrder(int order);
258 
260  static const int EXACT = INT_MAX;
261 
262 private:
263 
264  // Internal constructor.
265  Tps(TpsRep<T> *);
266 
267  // Set maximum order.
268  void setMaxOrder(int);
269 
270  // Make representation unique.
271  void unique();
272 
273  // Pointer to representation.
275 
276  // Current maximum truncation order.
277  static int truncOrder;
278 };
279 
280 
281 // Global functions acting on Tps<T> objects.
282 // ------------------------------------------------------------------------
283 
285 template <class T> Tps<T> operator+(const Tps<T> &x, const Tps<T> &y);
286 
288 template <class T> Tps<T> operator-(const Tps<T> &x, const Tps<T> &y);
289 
291 template <class T> Tps<T> operator+(const Tps<T> &x, const T &y);
292 
294 template <class T> Tps<T> operator-(const Tps<T> &x, const T &y);
295 
297 template <class T> Tps<T> operator+(const T &x, const Tps<T> &y);
298 
300 template <class T> Tps<T> operator-(const T &x, const Tps<T> &y);
301 
303 template <class T> Tps<T> operator*(const Tps<T> &x, const Tps<T> &y);
304 
306 template <class T> Tps<T> operator/(const Tps<T> &x, const Tps<T> &y);
307 
309 template <class T> Tps<T> operator*(const Tps<T> &x, const T &y);
310 
312 template <class T> Tps<T> operator/(const Tps<T> &x, const T &y);
313 
315 template <class T> Tps<T> operator*(const T &x, const Tps<T> &y);
316 
318 template <class T> Tps<T> operator/(const T &x, const Tps<T> &y);
319 
321 template <class T> bool operator==(const T &x, const Tps<T> &y);
322 
324 template <class T> std::istream &operator>>(std::istream &, Tps<T> &x);
325 
327 template <class T> std::ostream &operator<<(std::ostream &, const Tps<T> &x);
328 
329 
330 // Inline global functions.
331 // ------------------------------------------------------------------------
332 
333 template <class T> Tps<T> operator+(const Tps<T> &x, const Tps<T> &y)
334 { Tps<T> z(x); return z += y; }
335 
336 template <class T> Tps<T> operator-(const Tps<T> &x, const Tps<T> &y)
337 { Tps<T> z(x); return z -= y; }
338 
339 template <class T> inline
340 Tps<T> operator+(const Tps<T> &x, const T &y)
341 { Tps<T> z(x); return z += y; }
342 
343 template <class T> inline
344 Tps<T> operator-(const Tps<T> &x, const T &y)
345 { Tps<T> z(x); return z -= y; }
346 
347 template <class T> inline
348 Tps<T> operator+(const T &x, const Tps<T> &y)
349 { Tps<T> z(y); return z += x; }
350 
351 template <class T>
352 Tps<T> operator-(const T &x, const Tps<T> &y)
353 { Tps<T> z(- y); return z += x; }
354 
355 template <class T> Tps<T> operator*(const Tps<T> &x, const Tps<T> &y)
356 { Tps<T> z(x); return z *= y; }
357 
358 template <class T> Tps<T> operator/(const Tps<T> &x, const Tps<T> &y)
359 { Tps<T> z(x); return z /= y; }
360 
361 template <class T> Tps<T> operator*(const Tps<T> &x, const T &y)
362 { Tps<T> z(x); return z *= y; }
363 
364 template <class T> Tps<T> operator/(const Tps<T> &x, const T &y)
365 { Tps<T> z(x); return z /= y; }
366 
367 template <class T> Tps<T> operator*(const T &x, const Tps<T> &y)
368 { Tps<T> z(y); return z *= x; }
369 
370 template <class T> Tps<T> operator/(const T &x, const Tps<T> &y)
371 { Tps<T> z(y.inverse(Tps<T>::getGlobalTruncOrder())); return z *= x; }
372 
373 template <class T> bool operator==(const T &x, const Tps<T> &y)
374 { return y == x; }
375 
376 template <class T> std::istream &operator>>(std::istream &is, Tps<T> &x)
377 { return x.get(is); }
378 
379 template <class T> std::ostream &operator<<(std::ostream &os, const Tps<T> &x)
380 { return x.put(os); }
381 
382 #endif // CLASSIC_Tps_HH
Matrix< T > operator+(const Matrix< T > &, const Matrix< T > &)
Matrix addition.
Definition: Matrix.h:275
static const int EXACT
Representation of infinite precision.
Definition: Tps.h:260
int getOrder(int index) const
Get order.
Definition: Tps.hpp:1084
Tps< T > substitute(const Matrix< T > &M) const
Substitute.
Definition: Tps.hpp:614
Matrix< T > operator/(const Matrix< T > &, const T &)
Matrix divided by scalar.
Definition: Matrix.h:329
static Tps< T > makeVarPower(int nVar, int var, int power)
Make power.
Definition: Tps.hpp:391
Definition: rbendmap.h:8
Tps< T > filter(int lowOrder, int highOrder) const
Extract orders.
Definition: Tps.hpp:279
int getSize() const
Get number of coefficients.
Definition: Tps.hpp:1049
Truncate power series map.
Definition: Tps.h:31
Tps< T > & operator=(const Tps< T > &y)
Definition: Tps.hpp:259
const TpsMonomial & getExponents(int index) const
Get exponents.
Definition: Tps.hpp:1073
TpsRep< T > * rep
Definition: Tps.h:274
Tps()
Definition: Tps.hpp:221
Matrix< T > operator*(const Matrix< T > &, const Matrix< T > &)
Matrix multiply.
Definition: Matrix.h:297
Tps< T > & operator/=(const Tps< T > &y)
Divide and assign.
Definition: Tps.hpp:505
int getTruncOrder() const
Get truncation order.
Definition: Tps.hpp:1037
Tps< T > Taylor(const T series[], int n) const
Taylor series.
Definition: Tps.hpp:1014
std::istream & get(std::istream &is)
Get Tps from the stream is.
Definition: Tps.hpp:721
const T operator[](int index) const
Get coefficient.
Definition: Tps.hpp:301
bool isConstant() const
Test for constant.
Definition: Tps.hpp:1055
Tps< T > & operator*=(const Tps< T > &y)
Multiply and assign.
Definition: Tps.hpp:499
T evaluate(const Vector< T > &v) const
Substitute.
Definition: Tps.hpp:686
Tps< T > scaleMonomials(const Tps< T > &y) const
Multiply monomial-wise.
Definition: Tps.hpp:992
Tps< T > & operator+=(const Tps< T > &y)
Add and assign.
Definition: Tps.hpp:427
Tps< T > operator+() const
Unary plus.
Definition: Tps.hpp:411
void unique()
Definition: Tps.hpp:55
std::ostream & put(std::ostream &os) const
Put Tps to the stream is.
Definition: Tps.hpp:784
void setMaxOrder(int)
Definition: Tps.hpp:49
Truncated power series.
Definition: Tps.h:27
Tps< T > multiply(const Tps< T > &y, int trunc) const
Truncated multiplication.
Definition: Tps.hpp:823
Definition: Tps.h:28
static Tps< T > makeMonomial(const TpsMonomial &m, const T &t)
Make monomial.
Definition: Tps.hpp:401
Tps< T > inverse(int order=truncOrder) const
Reciprocal value.
Definition: Tps.hpp:897
Tps< T > multiplyVariable(int var) const
Multiply by variable [b]var[/b].
Definition: Tps.hpp:969
bool operator==(const Tps< T > &y) const
Equality operator.
Definition: Tps.hpp:545
bool operator!=(const Tps< T > &y) const
Inequality operator.
Definition: Tps.hpp:602
static int getGlobalTruncOrder()
Get global truncation order.
Definition: Tps.hpp:1061
Tps< T > truncate(int trunc)
Truncate.
Definition: Tps.hpp:295
~Tps()
Definition: Tps.hpp:253
int getMaxOrder() const
Get maximal order.
Definition: Tps.hpp:1031
void setCoefficient(int index, const T &value)
Set coefficient.
Definition: Tps.hpp:337
int getVariables() const
Get number of variables.
Definition: Tps.hpp:1043
Exponent array for Tps&lt;T&gt;.
Definition: TpsMonomial.h:31
static Tps< T > makeVariable(int nVar, int var)
Make variable.
Definition: Tps.hpp:383
Tps< T > integral(int var) const
Partial integral.
Definition: Tps.hpp:946
Matrix.
Definition: Matrix.h:38
Tps< T > operator-() const
Unary minus.
Definition: Tps.hpp:417
Matrix< T > operator-(const Matrix< T > &, const Matrix< T > &)
Matrix subtraction.
Definition: Matrix.h:282
void clear()
Set to zero.
Definition: Tps.hpp:714
const T getCoefficient(int index) const
Get coefficient.
Definition: Tps.hpp:327
Tps< T > & operator-=(const Tps< T > &y)
Subtract and assign.
Definition: Tps.hpp:462
Vector.
static void setGlobalTruncOrder(int order)
Set global truncation order.
Definition: Tps.hpp:1067
Tps< T > derivative(int var) const
Partial derivative.
Definition: Tps.hpp:920
std::istream & operator>>(std::istream &, LieMap< T > &x)
Extract LieMap&lt;T&gt; from stream.
Definition: LieMap.h:231
bool operator==(const TwoPolynomial &left, const TwoPolynomial &right)
static int truncOrder
Definition: Tps.h:277