]> www.ginac.de Git - ginac.git/blob - ginac/series.cpp
- modified GiNaC headers to Alexander's liking
[ginac.git] / ginac / series.cpp
1 /** @file series.cpp
2  *
3  *  Implementation of class for extended truncated power-series and
4  *  methods for series expansion.
5  *
6  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "series.h"
24 #include "add.h"
25 #include "inifcns.h"
26 #include "mul.h"
27 #include "power.h"
28 #include "relational.h"
29 #include "symbol.h"
30
31
32 /*
33  *  Default constructor, destructor, copy constructor, assignment operator and helpers
34  */
35
36 series::series() : basic(TINFO_series)
37 {
38     debugmsg("series default constructor", LOGLEVEL_CONSTRUCT);
39 }
40
41 series::~series()
42 {
43     debugmsg("series destructor", LOGLEVEL_DESTRUCT);
44     destroy(false);
45 }
46
47 series::series(series const &other)
48 {
49     debugmsg("series copy constructor", LOGLEVEL_CONSTRUCT);
50     copy(other);
51 }
52
53 series const &series::operator=(series const & other)
54 {
55     debugmsg("series operator=", LOGLEVEL_ASSIGNMENT);
56     if (this != &other) {
57         destroy(true);
58         copy(other);
59     }
60     return *this;
61 }
62
63 void series::copy(series const &other)
64 {
65     inherited::copy(other);
66     seq = other.seq;
67     var = other.var;
68     point = other.point;
69 }
70
71 void series::destroy(bool call_parent)
72 {
73     if (call_parent)
74         inherited::destroy(call_parent);
75 }
76
77
78 /*
79  *  Other constructors
80  */
81
82 /** Construct series from a vector of coefficients and powers.
83  *  expair.rest holds the coefficient, expair.coeff holds the power.
84  *  The powers must be integers (positive or negative) and in ascending order;
85  *  the last coefficient can be Order(exONE()) to represent a truncated,
86  *  non-terminating series.
87  *
88  *  @param var_  series variable (must hold a symbol)
89  *  @param point_  expansion point
90  *  @param ops_  vector of {coefficient, power} pairs (coefficient must not be zero)
91  *  @return newly constructed series */
92 series::series(ex const &var_, ex const &point_, epvector const &ops_)
93     : basic(TINFO_series), seq(ops_), var(var_), point(point_)
94 {
95     debugmsg("series constructor from ex,ex,epvector", LOGLEVEL_CONSTRUCT);
96     ASSERT(is_ex_exactly_of_type(var_, symbol));
97 }
98
99
100 /*
101  *  Functions overriding virtual functions from base classes
102  */
103
104 basic *series::duplicate() const
105 {
106     debugmsg("series duplicate", LOGLEVEL_DUPLICATE);
107     return new series(*this);
108 }
109
110 // Highest degree of variable
111 int series::degree(symbol const &s) const
112 {
113     if (var.is_equal(s)) {
114         // Return last exponent
115         if (seq.size())
116             return ex_to_numeric((*(seq.end() - 1)).coeff).to_int();
117         else
118             return 0;
119     } else {
120         epvector::const_iterator it = seq.begin(), itend = seq.end();
121         if (it == itend)
122             return 0;
123         int max_pow = INT_MIN;
124         while (it != itend) {
125             int pow = it->rest.degree(s);
126             if (pow > max_pow)
127                 max_pow = pow;
128             it++;
129         }
130         return max_pow;
131     }
132 }
133
134 // Lowest degree of variable
135 int series::ldegree(symbol const &s) const
136 {
137     if (var.is_equal(s)) {
138         // Return first exponent
139         if (seq.size())
140             return ex_to_numeric((*(seq.begin())).coeff).to_int();
141         else
142             return 0;
143     } else {
144         epvector::const_iterator it = seq.begin(), itend = seq.end();
145         if (it == itend)
146             return 0;
147         int min_pow = INT_MAX;
148         while (it != itend) {
149             int pow = it->rest.ldegree(s);
150             if (pow < min_pow)
151                 min_pow = pow;
152             it++;
153         }
154         return min_pow;
155     }
156 }
157
158 // Coefficient of variable
159 ex series::coeff(symbol const &s, int n) const
160 {
161     if (var.is_equal(s)) {
162         epvector::const_iterator it = seq.begin(), itend = seq.end();
163         while (it != itend) {
164             int pow = ex_to_numeric(it->coeff).to_int();
165             if (pow == n)
166                 return it->rest;
167             if (pow > n)
168                 return exZERO();
169             it++;
170         }
171         return exZERO();
172     } else
173         return convert_to_poly().coeff(s, n);
174 }
175
176 ex series::eval(int level) const
177 {
178     if (level == 1)
179         return this->hold();
180
181     // Construct a new series with evaluated coefficients
182     epvector new_seq;
183     new_seq.reserve(seq.size());
184     epvector::const_iterator it = seq.begin(), itend = seq.end();
185     while (it != itend) {
186         new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
187         it++;
188     }
189     return (new series(var, point, new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
190 }
191
192 ex series::evalf(int level) const
193 {
194     return convert_to_poly().evalf(level);
195 }
196
197
198 /*
199  *  Construct expression (polynomial) out of series
200  */
201
202 /** Convert a series object to an ordinary polynomial.
203  *
204  *  @param no_order flag: discard higher order terms */
205 ex series::convert_to_poly(bool no_order) const
206 {
207     ex e;
208     epvector::const_iterator it = seq.begin(), itend = seq.end();
209
210     while (it != itend) {
211         if (is_order_function(it->rest)) {
212             if (!no_order)
213                 e += Order(power(var - point, it->coeff));
214         } else
215             e += it->rest * power(var - point, it->coeff);
216         it++;
217     }
218     return e;
219 }
220
221
222 /*
223  *  Implementation of series expansion
224  */
225
226 /** Default implementation of ex::series(). This performs Taylor expansion.
227  *  @see ex::series */
228 ex basic::series(symbol const & s, ex const & point, int order) const
229 {
230     epvector seq;
231     numeric fac(1);
232     ex deriv = *this;
233     ex coeff = deriv.subs(s == point);
234     if (!coeff.is_zero())
235         seq.push_back(expair(coeff, numeric(0)));
236
237     int n;
238     for (n=1; n<order; n++) {
239         fac = fac.mul(numeric(n));
240         deriv = deriv.diff(s).expand();
241         if (deriv.is_zero()) {
242             // Series terminates
243             return series::series(s, point, seq);
244         }
245         coeff = power(fac, -1) * deriv.subs(s == point);
246         if (!coeff.is_zero())
247             seq.push_back(expair(coeff, numeric(n)));
248     }
249
250     // Higher-order terms, if present
251     deriv = deriv.diff(s);
252     if (!deriv.is_zero())
253         seq.push_back(expair(Order(exONE()), numeric(n)));
254     return series::series(s, point, seq);
255 }
256
257
258 /** Add one series object to another, producing a series object that represents
259  *  the sum.
260  *
261  *  @param other  series object to add with
262  *  @return the sum as a series */
263 ex series::add_series(const series &other) const
264 {
265     // Adding two series with different variables or expansion points
266     // results in an empty (constant) series 
267     if (!is_compatible_to(other)) {
268         epvector nul;
269         nul.push_back(expair(Order(exONE()), exZERO()));
270         return series(var, point, nul);
271     }
272
273     // Series addition
274     epvector new_seq;
275     epvector::const_iterator a = seq.begin();
276     epvector::const_iterator b = other.seq.begin();
277     epvector::const_iterator a_end = seq.end();
278     epvector::const_iterator b_end = other.seq.end();
279     int pow_a = INT_MAX, pow_b = INT_MAX;
280     for (;;) {
281         // If a is empty, fill up with elements from b and stop
282         if (a == a_end) {
283             while (b != b_end) {
284                 new_seq.push_back(*b);
285                 b++;
286             }
287             break;
288         } else
289             pow_a = ex_to_numeric((*a).coeff).to_int();
290
291         // If b is empty, fill up with elements from a and stop
292         if (b == b_end) {
293             while (a != a_end) {
294                 new_seq.push_back(*a);
295                 a++;
296             }
297             break;
298         } else
299             pow_b = ex_to_numeric((*b).coeff).to_int();
300
301         // a and b are non-empty, compare powers
302         if (pow_a < pow_b) {
303             // a has lesser power, get coefficient from a
304             new_seq.push_back(*a);
305             if (is_order_function((*a).rest))
306                 break;
307             a++;
308         } else if (pow_b < pow_a) {
309             // b has lesser power, get coefficient from b
310             new_seq.push_back(*b);
311             if (is_order_function((*b).rest))
312                 break;
313             b++;
314         } else {
315             // Add coefficient of a and b
316             if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
317                 new_seq.push_back(expair(Order(exONE()), (*a).coeff));
318                 break;  // Order term ends the sequence
319             } else {
320                 ex sum = (*a).rest + (*b).rest;
321                 if (!(sum.is_zero()))
322                     new_seq.push_back(expair(sum, numeric(pow_a)));
323                 a++;
324                 b++;
325             }
326         }
327     }
328     return series(var, point, new_seq);
329 }
330
331
332 /** Implementation of ex::series() for sums. This performs series addition when
333  *  adding series objects.
334  *  @see ex::series */
335 /*
336 ex add::series(symbol const & s, ex const & point, int order) const
337 {
338     ex acc; // Series accumulator
339
340     // Get first term
341     epvector::const_iterator it = seq.begin();
342     epvector::const_iterator itend = seq.end();
343     if (it != itend) {
344         if (is_ex_exactly_of_type(it->rest, series))
345             acc = it->rest;
346         else
347             acc = it->rest.series(s, point, order);
348         if (!it->coeff.is_equal(exONE()))
349             acc = ex_to_series(acc).mul_const(ex_to_numeric(it->coeff));
350         it++;
351     }
352
353     // Add remaining terms
354     for (; it!=itend; it++) {
355         ex op;
356         if (is_ex_exactly_of_type(it->rest, series))
357             op = it->rest;
358         else
359             op = it->rest.series(s, point, order);
360         if (!it->coeff.is_equal(exONE()))
361             op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
362
363         // Series addition
364         acc = ex_to_series(acc).add_series(ex_to_series(op));
365     }
366     return acc;
367 }
368 */
369 ex add::series(symbol const & s, ex const & point, int order) const
370 {
371     ex acc; // Series accumulator
372
373     // Get first term from overall_coeff
374     acc = overall_coeff.series(s,point,order);
375
376     // Add remaining terms
377     epvector::const_iterator it = seq.begin();
378     epvector::const_iterator itend = seq.end();
379     for (; it!=itend; it++) {
380         ex op;
381         if (is_ex_exactly_of_type(it->rest, series))
382             op = it->rest;
383         else
384             op = it->rest.series(s, point, order);
385         if (!it->coeff.is_equal(exONE()))
386             op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
387
388         // Series addition
389         acc = ex_to_series(acc).add_series(ex_to_series(op));
390     }
391     return acc;
392 }
393
394
395 /** Multiply a series object with a numeric constant, producing a series object
396  *  that represents the product.
397  *
398  *  @param other  constant to multiply with
399  *  @return the product as a series */
400 ex series::mul_const(const numeric &other) const
401 {
402     epvector new_seq;
403     new_seq.reserve(seq.size());
404
405     epvector::const_iterator it = seq.begin(), itend = seq.end();
406     while (it != itend) {
407         if (!is_order_function(it->rest))
408             new_seq.push_back(expair(it->rest * other, it->coeff));
409         else
410             new_seq.push_back(*it);
411         it++;
412     }
413     return series(var, point, new_seq);
414 }
415
416
417 /** Multiply one series object to another, producing a series object that
418  *  represents the product.
419  *
420  *  @param other  series object to multiply with
421  *  @return the product as a series */
422 ex series::mul_series(const series &other) const
423 {
424     // Multiplying two series with different variables or expansion points
425     // results in an empty (constant) series 
426     if (!is_compatible_to(other)) {
427         epvector nul;
428         nul.push_back(expair(Order(exONE()), exZERO()));
429         return series(var, point, nul);
430     }
431
432     // Series multiplication
433     epvector new_seq;
434
435     const symbol *s = static_cast<symbol *>(var.bp);
436     int a_max = degree(*s);
437     int b_max = other.degree(*s);
438     int a_min = ldegree(*s);
439     int b_min = other.ldegree(*s);
440     int cdeg_min = a_min + b_min;
441     int cdeg_max = a_max + b_max;
442
443     int higher_order_a = INT_MAX;
444     int higher_order_b = INT_MAX;
445     if (is_order_function(coeff(*s, a_max)))
446         higher_order_a = a_max + b_min;
447     if (is_order_function(other.coeff(*s, b_max)))
448         higher_order_b = b_max + a_min;
449     int higher_order_c = min(higher_order_a, higher_order_b);
450     if (cdeg_max >= higher_order_c)
451         cdeg_max = higher_order_c - 1;
452
453     for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
454         ex co = exZERO();
455         // c(i)=a(0)b(i)+...+a(i)b(0)
456         for (int i=a_min; cdeg-i>=b_min; i++) {
457             ex a_coeff = coeff(*s, i);
458             ex b_coeff = other.coeff(*s, cdeg-i);
459             if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
460                 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
461         }
462         if (!co.is_zero())
463             new_seq.push_back(expair(co, numeric(cdeg)));
464     }
465     if (higher_order_c < INT_MAX)
466         new_seq.push_back(expair(Order(exONE()), numeric(higher_order_c)));
467     return series::series(var, point, new_seq);
468 }
469
470
471 /** Implementation of ex::series() for product. This performs series multiplication when multiplying series.
472  *  @see ex::series */
473 /*
474 ex mul::series(symbol const & s, ex const & point, int order) const
475 {
476     ex acc; // Series accumulator
477
478     // Get first term
479     epvector::const_iterator it = seq.begin();
480     epvector::const_iterator itend = seq.end();
481     if (it != itend) {
482         if (is_ex_exactly_of_type(it->rest, series))
483             acc = it->rest;
484         else
485             acc = it->rest.series(s, point, order);
486         if (!it->coeff.is_equal(exONE()))
487             acc = ex_to_series(acc).power_const(ex_to_numeric(it->coeff), order);
488         it++;
489     }
490
491     // Multiply with remaining terms
492     for (; it!=itend; it++) {
493         ex op = it->rest;
494         if (op.info(info_flags::numeric)) {
495             // series * const (special case, faster)
496             ex f = power(op, it->coeff);
497             acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
498             continue;
499         } else if (!is_ex_exactly_of_type(op, series))
500             op = op.series(s, point, order);
501         if (!it->coeff.is_equal(exONE()))
502             op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
503
504         // Series multiplication
505         acc = ex_to_series(acc).mul_series(ex_to_series(op));
506     }
507     return acc;
508 }
509 */
510
511 ex mul::series(symbol const & s, ex const & point, int order) const
512 {
513     ex acc; // Series accumulator
514
515     // Get first term from overall_coeff
516     acc = overall_coeff.series(s, point, order);
517
518     // Multiply with remaining terms
519     epvector::const_iterator it = seq.begin();
520     epvector::const_iterator itend = seq.end();
521     for (; it!=itend; it++) {
522         ex op = it->rest;
523         if (op.info(info_flags::numeric)) {
524             // series * const (special case, faster)
525             ex f = power(op, it->coeff);
526             acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
527             continue;
528         } else if (!is_ex_exactly_of_type(op, series))
529             op = op.series(s, point, order);
530         if (!it->coeff.is_equal(exONE()))
531             op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
532
533         // Series multiplication
534         acc = ex_to_series(acc).mul_series(ex_to_series(op));
535     }
536     return acc;
537 }
538
539
540 /** Compute the p-th power of a series.
541  *
542  *  @param p  power to compute
543  *  @param deg  truncation order of series calculation */
544 ex series::power_const(const numeric &p, int deg) const
545 {
546     int i;
547     const symbol *s = static_cast<symbol *>(var.bp);
548     int ldeg = ldegree(*s);
549
550     // Calculate coefficients of powered series
551     exvector co;
552     co.reserve(deg);
553     ex co0;
554     co.push_back(co0 = power(coeff(*s, ldeg), p));
555     bool all_sums_zero = true;
556     for (i=1; i<deg; i++) {
557         ex sum = exZERO();
558         for (int j=1; j<=i; j++) {
559             ex c = coeff(*s, j + ldeg);
560             if (is_order_function(c)) {
561                 co.push_back(Order(exONE()));
562                 break;
563             } else
564                 sum += (p * j - (i - j)) * co[i - j] * c;
565         }
566         if (!sum.is_zero())
567             all_sums_zero = false;
568         co.push_back(co0 * sum / numeric(i));
569     }
570
571     // Construct new series (of non-zero coefficients)
572     epvector new_seq;
573     bool higher_order = false;
574     for (i=0; i<deg; i++) {
575         if (!co[i].is_zero())
576             new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
577         if (is_order_function(co[i])) {
578             higher_order = true;
579             break;
580         }
581     }
582     if (!higher_order && !all_sums_zero)
583         new_seq.push_back(expair(Order(exONE()), numeric(deg) + p * ldeg));
584     return series::series(var, point, new_seq);
585 }
586
587
588 /** Implementation of ex::series() for powers. This performs Laurent expansion
589  *  of reciprocals of series at singularities.
590  *  @see ex::series */
591 ex power::series(symbol const & s, ex const & point, int order) const
592 {
593     ex e;
594     if (!is_ex_exactly_of_type(basis, series)) {
595         // Basis is not a series, may there be a singulary?
596         if (!exponent.info(info_flags::negint))
597             return basic::series(s, point, order);
598
599         // Expression is of type something^(-int), check for singularity
600         if (!basis.subs(s == point).is_zero())
601             return basic::series(s, point, order);
602
603         // Singularity encountered, expand basis into series
604         e = basis.series(s, point, order);
605     } else {
606         // Basis is a series
607         e = basis;
608     }
609
610     // Power e
611     return ex_to_series(e).power_const(ex_to_numeric(exponent), order);
612 }
613
614
615 /** Compute the truncated series expansion of an expression.
616  *  This function returns an expression containing an object of class series to
617  *  represent the series. If the series does not terminate within the given
618  *  truncation order, the last term of the series will be an order term.
619  *
620  *  @param s  expansion variable
621  *  @param point  expansion point
622  *  @param order  truncation order of series calculations
623  *  @return an expression holding a series object */
624 ex ex::series(symbol const &s, ex const &point, int order) const
625 {
626     ASSERT(bp!=0);
627     return bp->series(s, point, order);
628 }
629
630
631 // Global constants
632 const series some_series;
633 type_info const & typeid_series = typeid(some_series);