OPAL (Object Oriented Parallel Accelerator Library) 2022.1
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
27template <class T> class Tps;
28template <class T> class TpsRep;
29template <class T> class Matrix;
30template <class T> class Vector;
31template <class T> class VpsMap;
32
33class 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
45template <class T>
46class Tps {
47
48public:
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
262private:
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
285template <class T> Tps<T> operator+(const Tps<T> &x, const Tps<T> &y);
286
288template <class T> Tps<T> operator-(const Tps<T> &x, const Tps<T> &y);
289
291template <class T> Tps<T> operator+(const Tps<T> &x, const T &y);
292
294template <class T> Tps<T> operator-(const Tps<T> &x, const T &y);
295
297template <class T> Tps<T> operator+(const T &x, const Tps<T> &y);
298
300template <class T> Tps<T> operator-(const T &x, const Tps<T> &y);
301
303template <class T> Tps<T> operator*(const Tps<T> &x, const Tps<T> &y);
304
306template <class T> Tps<T> operator/(const Tps<T> &x, const Tps<T> &y);
307
309template <class T> Tps<T> operator*(const Tps<T> &x, const T &y);
310
312template <class T> Tps<T> operator/(const Tps<T> &x, const T &y);
313
315template <class T> Tps<T> operator*(const T &x, const Tps<T> &y);
316
318template <class T> Tps<T> operator/(const T &x, const Tps<T> &y);
319
321template <class T> bool operator==(const T &x, const Tps<T> &y);
322
324template <class T> std::istream &operator>>(std::istream &, Tps<T> &x);
325
327template <class T> std::ostream &operator<<(std::ostream &, const Tps<T> &x);
328
329
330// Inline global functions.
331// ------------------------------------------------------------------------
332
333template <class T> Tps<T> operator+(const Tps<T> &x, const Tps<T> &y)
334{ Tps<T> z(x); return z += y; }
335
336template <class T> Tps<T> operator-(const Tps<T> &x, const Tps<T> &y)
337{ Tps<T> z(x); return z -= y; }
338
339template <class T> inline
340Tps<T> operator+(const Tps<T> &x, const T &y)
341{ Tps<T> z(x); return z += y; }
342
343template <class T> inline
344Tps<T> operator-(const Tps<T> &x, const T &y)
345{ Tps<T> z(x); return z -= y; }
346
347template <class T> inline
348Tps<T> operator+(const T &x, const Tps<T> &y)
349{ Tps<T> z(y); return z += x; }
350
351template <class T>
352Tps<T> operator-(const T &x, const Tps<T> &y)
353{ Tps<T> z(- y); return z += x; }
354
355template <class T> Tps<T> operator*(const Tps<T> &x, const Tps<T> &y)
356{ Tps<T> z(x); return z *= y; }
357
358template <class T> Tps<T> operator/(const Tps<T> &x, const Tps<T> &y)
359{ Tps<T> z(x); return z /= y; }
360
361template <class T> Tps<T> operator*(const Tps<T> &x, const T &y)
362{ Tps<T> z(x); return z *= y; }
363
364template <class T> Tps<T> operator/(const Tps<T> &x, const T &y)
365{ Tps<T> z(x); return z /= y; }
366
367template <class T> Tps<T> operator*(const T &x, const Tps<T> &y)
368{ Tps<T> z(y); return z *= x; }
369
370template <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
373template <class T> bool operator==(const T &x, const Tps<T> &y)
374{ return y == x; }
375
376template <class T> std::istream &operator>>(std::istream &is, Tps<T> &x)
377{ return x.get(is); }
378
379template <class T> std::ostream &operator<<(std::ostream &os, const Tps<T> &x)
380{ return x.put(os); }
381
382#endif // CLASSIC_Tps_HH
Tps< T > operator-(const Tps< T > &x, const Tps< T > &y)
Subtract.
Definition: Tps.h:336
Tps< T > operator+(const Tps< T > &x, const Tps< T > &y)
Add.
Definition: Tps.h:333
Tps< T > operator/(const Tps< T > &x, const Tps< T > &y)
Divide.
Definition: Tps.h:358
bool operator==(const T &x, const Tps< T > &y)
Test for equality.
Definition: Tps.h:373
std::istream & operator>>(std::istream &, Tps< T > &x)
Extract from stream.
Definition: Tps.h:376
std::ostream & operator<<(std::ostream &, const Tps< T > &x)
Insert to stream.
Definition: Tps.h:379
Tps< T > operator*(const Tps< T > &x, const Tps< T > &y)
Multiply.
Definition: Tps.h:355
Matrix.
Definition: Matrix.h:39
Truncated power series.
Definition: Tps.h:46
int getVariables() const
Get number of variables.
Definition: Tps.hpp:1043
Tps< T > & operator=(const Tps< T > &y)
Definition: Tps.hpp:259
void setMaxOrder(int)
Definition: Tps.hpp:49
std::ostream & put(std::ostream &os) const
Put Tps to the stream is.
Definition: Tps.hpp:784
Tps< T > multiply(const Tps< T > &y, int trunc) const
Truncated multiplication.
Definition: Tps.hpp:823
Tps< T > substitute(const Matrix< T > &M) const
Substitute.
Definition: Tps.hpp:614
int getSize() const
Get number of coefficients.
Definition: Tps.hpp:1049
void unique()
Definition: Tps.hpp:55
static Tps< T > makeMonomial(const TpsMonomial &m, const T &t)
Make monomial.
Definition: Tps.hpp:401
Tps< T > multiplyVariable(int var) const
Multiply by variable [b]var[/b].
Definition: Tps.hpp:969
Tps< T > integral(int var) const
Partial integral.
Definition: Tps.hpp:946
~Tps()
Definition: Tps.hpp:253
bool operator==(const Tps< T > &y) const
Equality operator.
Definition: Tps.hpp:545
const TpsMonomial & getExponents(int index) const
Get exponents.
Definition: Tps.hpp:1073
static int getGlobalTruncOrder()
Get global truncation order.
Definition: Tps.hpp:1061
Tps< T > truncate(int trunc)
Truncate.
Definition: Tps.hpp:295
Tps< T > & operator+=(const Tps< T > &y)
Add and assign.
Definition: Tps.hpp:427
TpsRep< T > * rep
Definition: Tps.h:274
Tps< T > inverse(int order=truncOrder) const
Reciprocal value.
Definition: Tps.hpp:897
void setCoefficient(int index, const T &value)
Set coefficient.
Definition: Tps.hpp:337
Tps< T > filter(int lowOrder, int highOrder) const
Extract orders.
Definition: Tps.hpp:279
Tps< T > derivative(int var) const
Partial derivative.
Definition: Tps.hpp:920
static const int EXACT
Representation of infinite precision.
Definition: Tps.h:260
const T operator[](int index) const
Get coefficient.
Definition: Tps.hpp:301
static void setGlobalTruncOrder(int order)
Set global truncation order.
Definition: Tps.hpp:1067
static int truncOrder
Definition: Tps.h:277
Tps< T > operator+() const
Unary plus.
Definition: Tps.hpp:411
static Tps< T > makeVarPower(int nVar, int var, int power)
Make power.
Definition: Tps.hpp:391
Tps< T > scaleMonomials(const Tps< T > &y) const
Multiply monomial-wise.
Definition: Tps.hpp:992
Tps()
Definition: Tps.hpp:221
Tps< T > Taylor(const T series[], int n) const
Taylor series.
Definition: Tps.hpp:1014
const T getCoefficient(int index) const
Get coefficient.
Definition: Tps.hpp:327
T evaluate(const Vector< T > &v) const
Substitute.
Definition: Tps.hpp:686
bool isConstant() const
Test for constant.
Definition: Tps.hpp:1055
static Tps< T > makeVariable(int nVar, int var)
Make variable.
Definition: Tps.hpp:383
Tps< T > & operator*=(const Tps< T > &y)
Multiply and assign.
Definition: Tps.hpp:499
Tps< T > operator-() const
Unary minus.
Definition: Tps.hpp:417
std::istream & get(std::istream &is)
Get Tps from the stream is.
Definition: Tps.hpp:721
int getTruncOrder() const
Get truncation order.
Definition: Tps.hpp:1037
int getMaxOrder() const
Get maximal order.
Definition: Tps.hpp:1031
int getOrder(int index) const
Get order.
Definition: Tps.hpp:1084
void clear()
Set to zero.
Definition: Tps.hpp:714
Tps< T > & operator-=(const Tps< T > &y)
Subtract and assign.
Definition: Tps.hpp:462
bool operator!=(const Tps< T > &y) const
Inequality operator.
Definition: Tps.hpp:602
Tps< T > & operator/=(const Tps< T > &y)
Divide and assign.
Definition: Tps.hpp:505
Definition: Tps.hpp:76
Vector.
Definition: Vector.h:37
Truncate power series map.
Definition: VpsMap.h:43
Exponent array for Tps<T>.
Definition: TpsMonomial.h:31