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