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