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