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