- Overwritten: pseries::printtree().
[ginac.git] / ginac / pseries.cpp
1 /** @file pseries.cpp
2  *
3  *  Implementation of class for extended truncated power series and
4  *  methods for series expansion. */
5
6 /*
7  *  GiNaC Copyright (C) 1999-2000 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 <stdexcept>
25
26 #include "pseries.h"
27 #include "add.h"
28 #include "inifcns.h"
29 #include "lst.h"
30 #include "mul.h"
31 #include "power.h"
32 #include "relational.h"
33 #include "symbol.h"
34 #include "archive.h"
35 #include "utils.h"
36 #include "debugmsg.h"
37
38 #ifndef NO_NAMESPACE_GINAC
39 namespace GiNaC {
40 #endif // ndef NO_NAMESPACE_GINAC
41
42 GINAC_IMPLEMENT_REGISTERED_CLASS(pseries, basic)
43
44 /*
45  *  Default constructor, destructor, copy constructor, assignment operator and helpers
46  */
47
48 pseries::pseries() : basic(TINFO_pseries)
49 {
50     debugmsg("pseries default constructor", LOGLEVEL_CONSTRUCT);
51 }
52
53 pseries::~pseries()
54 {
55     debugmsg("pseries destructor", LOGLEVEL_DESTRUCT);
56     destroy(false);
57 }
58
59 pseries::pseries(const pseries &other)
60 {
61     debugmsg("pseries copy constructor", LOGLEVEL_CONSTRUCT);
62     copy(other);
63 }
64
65 const pseries &pseries::operator=(const pseries & other)
66 {
67     debugmsg("pseries operator=", LOGLEVEL_ASSIGNMENT);
68     if (this != &other) {
69         destroy(true);
70         copy(other);
71     }
72     return *this;
73 }
74
75 void pseries::copy(const pseries &other)
76 {
77     inherited::copy(other);
78     seq = other.seq;
79     var = other.var;
80     point = other.point;
81 }
82
83 void pseries::destroy(bool call_parent)
84 {
85     if (call_parent)
86         inherited::destroy(call_parent);
87 }
88
89
90 /*
91  *  Other constructors
92  */
93
94 /** Construct pseries from a vector of coefficients and powers.
95  *  expair.rest holds the coefficient, expair.coeff holds the power.
96  *  The powers must be integers (positive or negative) and in ascending order;
97  *  the last coefficient can be Order(_ex1()) to represent a truncated,
98  *  non-terminating series.
99  *
100  *  @param rel__  expansion variable and point (must hold a relational)
101  *  @param ops_  vector of {coefficient, power} pairs (coefficient must not be zero)
102  *  @return newly constructed pseries */
103 pseries::pseries(const ex &rel_, const epvector &ops_)
104     : basic(TINFO_pseries), seq(ops_)
105 {
106     debugmsg("pseries constructor from rel,epvector", LOGLEVEL_CONSTRUCT);
107     GINAC_ASSERT(is_ex_exactly_of_type(rel_, relational));
108     GINAC_ASSERT(is_ex_exactly_of_type(rel_.lhs(),symbol));
109     point = rel_.rhs();
110     var = *static_cast<symbol *>(rel_.lhs().bp);
111 }
112
113
114 /*
115  *  Archiving
116  */
117
118 /** Construct object from archive_node. */
119 pseries::pseries(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
120 {
121     debugmsg("pseries constructor from archive_node", LOGLEVEL_CONSTRUCT);
122     for (unsigned int i=0; true; i++) {
123         ex rest;
124         ex coeff;
125         if (n.find_ex("coeff", rest, sym_lst, i) && n.find_ex("power", coeff, sym_lst, i))
126             seq.push_back(expair(rest, coeff));
127         else
128             break;
129     }
130     n.find_ex("var", var, sym_lst);
131     n.find_ex("point", point, sym_lst);
132 }
133
134 /** Unarchive the object. */
135 ex pseries::unarchive(const archive_node &n, const lst &sym_lst)
136 {
137     return (new pseries(n, sym_lst))->setflag(status_flags::dynallocated);
138 }
139
140 /** Archive the object. */
141 void pseries::archive(archive_node &n) const
142 {
143     inherited::archive(n);
144     epvector::const_iterator i = seq.begin(), iend = seq.end();
145     while (i != iend) {
146         n.add_ex("coeff", i->rest);
147         n.add_ex("power", i->coeff);
148         i++;
149     }
150     n.add_ex("var", var);
151     n.add_ex("point", point);
152 }
153
154
155 /*
156  *  Functions overriding virtual functions from base classes
157  */
158
159 basic *pseries::duplicate() const
160 {
161     debugmsg("pseries duplicate", LOGLEVEL_DUPLICATE);
162     return new pseries(*this);
163 }
164
165 void pseries::print(ostream &os, unsigned upper_precedence) const
166 {
167     debugmsg("pseries print", LOGLEVEL_PRINT);
168     convert_to_poly().print(os, upper_precedence);
169 }
170
171 void pseries::printraw(ostream &os) const
172 {
173         debugmsg("pseries printraw", LOGLEVEL_PRINT);
174         os << "pseries(" << var << ";" << point << ";";
175         for (epvector::const_iterator i=seq.begin(); i!=seq.end(); i++) {
176                 os << "(" << (*i).rest << "," << (*i).coeff << "),";
177         }
178         os << ")";
179 }
180
181 void pseries::printtree(ostream & os, unsigned indent) const
182 {
183     debugmsg("pseries printtree",LOGLEVEL_PRINT);
184     os << string(indent,' ') << "pseries " 
185        << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
186        << ", flags=" << flags << endl;
187     for (unsigned i=0; i<seq.size(); ++i) {
188         seq[i].rest.printtree(os,indent+delta_indent);
189         seq[i].coeff.printtree(os,indent+delta_indent);
190         if (i!=seq.size()-1) {
191             os << string(indent+delta_indent,' ') << "-----" << endl;
192         }
193     }
194     var.printtree(os, indent+delta_indent);
195     point.printtree(os, indent+delta_indent);
196 }
197
198 unsigned pseries::nops(void) const
199 {
200     return seq.size();
201 }
202
203 ex pseries::op(int i) const
204 {
205     if (i < 0 || unsigned(i) >= seq.size())
206         throw (std::out_of_range("op() out of range"));
207     return seq[i].rest * power(var - point, seq[i].coeff);
208 }
209
210 ex &pseries::let_op(int i)
211 {
212     throw (std::logic_error("let_op not defined for pseries"));
213 }
214
215 int pseries::degree(const symbol &s) const
216 {
217     if (var.is_equal(s)) {
218         // Return last exponent
219         if (seq.size())
220             return ex_to_numeric((*(seq.end() - 1)).coeff).to_int();
221         else
222             return 0;
223     } else {
224         epvector::const_iterator it = seq.begin(), itend = seq.end();
225         if (it == itend)
226             return 0;
227         int max_pow = INT_MIN;
228         while (it != itend) {
229             int pow = it->rest.degree(s);
230             if (pow > max_pow)
231                 max_pow = pow;
232             it++;
233         }
234         return max_pow;
235     }
236 }
237
238 int pseries::ldegree(const symbol &s) const
239 {
240     if (var.is_equal(s)) {
241         // Return first exponent
242         if (seq.size())
243             return ex_to_numeric((*(seq.begin())).coeff).to_int();
244         else
245             return 0;
246     } else {
247         epvector::const_iterator it = seq.begin(), itend = seq.end();
248         if (it == itend)
249             return 0;
250         int min_pow = INT_MAX;
251         while (it != itend) {
252             int pow = it->rest.ldegree(s);
253             if (pow < min_pow)
254                 min_pow = pow;
255             it++;
256         }
257         return min_pow;
258     }
259 }
260
261 ex pseries::coeff(const symbol &s, int n) const
262 {
263     if (var.is_equal(s)) {
264                 if (seq.size() == 0)
265                         return _ex0();
266
267                 // Binary search in sequence for given power
268                 numeric looking_for = numeric(n);
269                 int lo = 0, hi = seq.size() - 1;
270                 while (lo <= hi) {
271                         int mid = (lo + hi) / 2;
272                         GINAC_ASSERT(is_ex_exactly_of_type(seq[mid].coeff, numeric));
273                         int cmp = ex_to_numeric(seq[mid].coeff).compare(looking_for);
274                         switch (cmp) {
275                                 case -1:
276                                         lo = mid + 1;
277                                         break;
278                                 case 0:
279                                         return seq[mid].rest;
280                                 case 1:
281                                         hi = mid - 1;
282                                         break;
283                                 default:
284                                         throw(std::logic_error("pseries::coeff: compare() didn't return -1, 0 or 1"));
285                         }
286                 }
287                 return _ex0();
288     } else
289         return convert_to_poly().coeff(s, n);
290 }
291
292 ex pseries::collect(const symbol &s) const
293 {
294         return *this;
295 }
296
297 /** Evaluate coefficients. */
298 ex pseries::eval(int level) const
299 {
300     if (level == 1)
301         return this->hold();
302
303         if (level == -max_recursion_level)
304                 throw (std::runtime_error("pseries::eval(): recursion limit exceeded"));
305     
306     // Construct a new series with evaluated coefficients
307     epvector new_seq;
308     new_seq.reserve(seq.size());
309     epvector::const_iterator it = seq.begin(), itend = seq.end();
310     while (it != itend) {
311         new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
312         it++;
313     }
314     return (new pseries(relational(var,point), new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
315 }
316
317 /** Evaluate coefficients numerically. */
318 ex pseries::evalf(int level) const
319 {
320         if (level == 1)
321                 return *this;
322
323         if (level == -max_recursion_level)
324                 throw (std::runtime_error("pseries::evalf(): recursion limit exceeded"));
325
326     // Construct a new series with evaluated coefficients
327     epvector new_seq;
328     new_seq.reserve(seq.size());
329     epvector::const_iterator it = seq.begin(), itend = seq.end();
330     while (it != itend) {
331         new_seq.push_back(expair(it->rest.evalf(level-1), it->coeff));
332         it++;
333     }
334     return (new pseries(relational(var,point), new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
335 }
336
337 ex pseries::subs(const lst & ls, const lst & lr) const
338 {
339         // If expansion variable is being substituted, convert the series to a
340         // polynomial and do the substitution there because the result might
341         // no longer be a power series
342         if (ls.has(var))
343                 return convert_to_poly(true).subs(ls, lr);
344
345         // Otherwise construct a new series with substituted coefficients and
346         // expansion point
347         epvector new_seq;
348         new_seq.reserve(seq.size());
349         epvector::const_iterator it = seq.begin(), itend = seq.end();
350         while (it != itend) {
351                 new_seq.push_back(expair(it->rest.subs(ls, lr), it->coeff));
352                 it++;
353         }
354     return (new pseries(relational(var,point.subs(ls, lr)), new_seq))->setflag(status_flags::dynallocated);
355 }
356
357 /** Implementation of ex::diff() for a power series.  It treats the series as a
358  *  polynomial.
359  *  @see ex::diff */
360 ex pseries::derivative(const symbol & s) const
361 {
362     if (s == var) {
363         epvector new_seq;
364         epvector::const_iterator it = seq.begin(), itend = seq.end();
365         
366         // FIXME: coeff might depend on var
367         while (it != itend) {
368             if (is_order_function(it->rest)) {
369                 new_seq.push_back(expair(it->rest, it->coeff - 1));
370             } else {
371                 ex c = it->rest * it->coeff;
372                 if (!c.is_zero())
373                     new_seq.push_back(expair(c, it->coeff - 1));
374             }
375             it++;
376         }
377         return pseries(relational(var,point), new_seq);
378     } else {
379         return *this;
380     }
381 }
382
383
384 /*
385  *  Construct ordinary polynomial out of series
386  */
387
388 /** Convert a pseries object to an ordinary polynomial.
389  *
390  *  @param no_order flag: discard higher order terms */
391 ex pseries::convert_to_poly(bool no_order) const
392 {
393     ex e;
394     epvector::const_iterator it = seq.begin(), itend = seq.end();
395     
396     while (it != itend) {
397         if (is_order_function(it->rest)) {
398             if (!no_order)
399                 e += Order(power(var - point, it->coeff));
400         } else
401             e += it->rest * power(var - point, it->coeff);
402         it++;
403     }
404     return e;
405 }
406
407
408 /*
409  *  Implementation of series expansion
410  */
411
412 /** Default implementation of ex::series(). This performs Taylor expansion.
413  *  @see ex::series */
414 ex basic::series(const relational & r, int order) const
415 {
416     epvector seq;
417     numeric fac(1);
418     ex deriv = *this;
419     ex coeff = deriv.subs(r);
420     const symbol *s = static_cast<symbol *>(r.lhs().bp);
421     
422     if (!coeff.is_zero())
423         seq.push_back(expair(coeff, numeric(0)));
424     
425     int n;
426     for (n=1; n<order; n++) {
427         fac = fac.mul(numeric(n));
428         deriv = deriv.diff(*s).expand();
429         if (deriv.is_zero()) {
430             // Series terminates
431             return pseries(r, seq);
432         }
433         coeff = fac.inverse() * deriv.subs(r);
434         if (!coeff.is_zero())
435             seq.push_back(expair(coeff, numeric(n)));
436     }
437     
438     // Higher-order terms, if present
439     deriv = deriv.diff(*s);
440     if (!deriv.is_zero())
441         seq.push_back(expair(Order(_ex1()), numeric(n)));
442     return pseries(r, seq);
443 }
444
445
446 /** Implementation of ex::series() for symbols.
447  *  @see ex::series */
448 ex symbol::series(const relational & r, int order) const
449 {
450         epvector seq;
451     const ex point = r.rhs();
452     GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
453     const symbol *s = static_cast<symbol *>(r.lhs().bp);
454     
455         if (this->is_equal(*s)) {
456                 if (order > 0 && !point.is_zero())
457                         seq.push_back(expair(point, _ex0()));
458                 if (order > 1)
459                         seq.push_back(expair(_ex1(), _ex1()));
460                 else
461                         seq.push_back(expair(Order(_ex1()), numeric(order)));
462         } else
463                 seq.push_back(expair(*this, _ex0()));
464         return pseries(r, seq);
465 }
466
467
468 /** Add one series object to another, producing a pseries object that
469  *  represents the sum.
470  *
471  *  @param other  pseries object to add with
472  *  @return the sum as a pseries */
473 ex pseries::add_series(const pseries &other) const
474 {
475     // Adding two series with different variables or expansion points
476     // results in an empty (constant) series 
477     if (!is_compatible_to(other)) {
478         epvector nul;
479         nul.push_back(expair(Order(_ex1()), _ex0()));
480         return pseries(relational(var,point), nul);
481     }
482     
483     // Series addition
484     epvector new_seq;
485     epvector::const_iterator a = seq.begin();
486     epvector::const_iterator b = other.seq.begin();
487     epvector::const_iterator a_end = seq.end();
488     epvector::const_iterator b_end = other.seq.end();
489     int pow_a = INT_MAX, pow_b = INT_MAX;
490     for (;;) {
491         // If a is empty, fill up with elements from b and stop
492         if (a == a_end) {
493             while (b != b_end) {
494                 new_seq.push_back(*b);
495                 b++;
496             }
497             break;
498         } else
499             pow_a = ex_to_numeric((*a).coeff).to_int();
500         
501         // If b is empty, fill up with elements from a and stop
502         if (b == b_end) {
503             while (a != a_end) {
504                 new_seq.push_back(*a);
505                 a++;
506             }
507             break;
508         } else
509             pow_b = ex_to_numeric((*b).coeff).to_int();
510         
511         // a and b are non-empty, compare powers
512         if (pow_a < pow_b) {
513             // a has lesser power, get coefficient from a
514             new_seq.push_back(*a);
515             if (is_order_function((*a).rest))
516                 break;
517             a++;
518         } else if (pow_b < pow_a) {
519             // b has lesser power, get coefficient from b
520             new_seq.push_back(*b);
521             if (is_order_function((*b).rest))
522                 break;
523             b++;
524         } else {
525             // Add coefficient of a and b
526             if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
527                 new_seq.push_back(expair(Order(_ex1()), (*a).coeff));
528                 break;  // Order term ends the sequence
529             } else {
530                 ex sum = (*a).rest + (*b).rest;
531                 if (!(sum.is_zero()))
532                     new_seq.push_back(expair(sum, numeric(pow_a)));
533                 a++;
534                 b++;
535             }
536         }
537     }
538     return pseries(relational(var,point), new_seq);
539 }
540
541
542 /** Implementation of ex::series() for sums. This performs series addition when
543  *  adding pseries objects.
544  *  @see ex::series */
545 ex add::series(const relational & r, int order) const
546 {
547     ex acc; // Series accumulator
548     
549     // Get first term from overall_coeff
550     acc = overall_coeff.series(r, order);
551
552     // Add remaining terms
553     epvector::const_iterator it = seq.begin();
554     epvector::const_iterator itend = seq.end();
555     for (; it!=itend; it++) {
556         ex op;
557         if (is_ex_exactly_of_type(it->rest, pseries))
558             op = it->rest;
559         else
560             op = it->rest.series(r, order);
561         if (!it->coeff.is_equal(_ex1()))
562             op = ex_to_pseries(op).mul_const(ex_to_numeric(it->coeff));
563         
564         // Series addition
565         acc = ex_to_pseries(acc).add_series(ex_to_pseries(op));
566     }
567     return acc;
568 }
569
570
571 /** Multiply a pseries object with a numeric constant, producing a pseries
572  *  object that represents the product.
573  *
574  *  @param other  constant to multiply with
575  *  @return the product as a pseries */
576 ex pseries::mul_const(const numeric &other) const
577 {
578     epvector new_seq;
579     new_seq.reserve(seq.size());
580     
581     epvector::const_iterator it = seq.begin(), itend = seq.end();
582     while (it != itend) {
583         if (!is_order_function(it->rest))
584             new_seq.push_back(expair(it->rest * other, it->coeff));
585         else
586             new_seq.push_back(*it);
587         it++;
588     }
589     return pseries(relational(var,point), new_seq);
590 }
591
592
593 /** Multiply one pseries object to another, producing a pseries object that
594  *  represents the product.
595  *
596  *  @param other  pseries object to multiply with
597  *  @return the product as a pseries */
598 ex pseries::mul_series(const pseries &other) const
599 {
600     // Multiplying two series with different variables or expansion points
601     // results in an empty (constant) series 
602     if (!is_compatible_to(other)) {
603         epvector nul;
604         nul.push_back(expair(Order(_ex1()), _ex0()));
605         return pseries(relational(var,point), nul);
606     }
607
608     // Series multiplication
609     epvector new_seq;
610     
611     const symbol *s = static_cast<symbol *>(var.bp);
612     int a_max = degree(*s);
613     int b_max = other.degree(*s);
614     int a_min = ldegree(*s);
615     int b_min = other.ldegree(*s);
616     int cdeg_min = a_min + b_min;
617     int cdeg_max = a_max + b_max;
618     
619     int higher_order_a = INT_MAX;
620     int higher_order_b = INT_MAX;
621     if (is_order_function(coeff(*s, a_max)))
622         higher_order_a = a_max + b_min;
623     if (is_order_function(other.coeff(*s, b_max)))
624         higher_order_b = b_max + a_min;
625     int higher_order_c = min(higher_order_a, higher_order_b);
626     if (cdeg_max >= higher_order_c)
627         cdeg_max = higher_order_c - 1;
628     
629     for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
630         ex co = _ex0();
631         // c(i)=a(0)b(i)+...+a(i)b(0)
632         for (int i=a_min; cdeg-i>=b_min; i++) {
633             ex a_coeff = coeff(*s, i);
634             ex b_coeff = other.coeff(*s, cdeg-i);
635             if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
636                 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
637         }
638         if (!co.is_zero())
639             new_seq.push_back(expair(co, numeric(cdeg)));
640     }
641     if (higher_order_c < INT_MAX)
642         new_seq.push_back(expair(Order(_ex1()), numeric(higher_order_c)));
643     return pseries(relational(var,point), new_seq);
644 }
645
646
647 /** Implementation of ex::series() for product. This performs series
648  *  multiplication when multiplying series.
649  *  @see ex::series */
650 ex mul::series(const relational & r, int order) const
651 {
652     ex acc; // Series accumulator
653     
654     // Get first term from overall_coeff
655     acc = overall_coeff.series(r, order);
656     
657     // Multiply with remaining terms
658     epvector::const_iterator it = seq.begin();
659     epvector::const_iterator itend = seq.end();
660     for (; it!=itend; it++) {
661         ex op = it->rest;
662         if (op.info(info_flags::numeric)) {
663             // series * const (special case, faster)
664             ex f = power(op, it->coeff);
665             acc = ex_to_pseries(acc).mul_const(ex_to_numeric(f));
666             continue;
667         } else if (!is_ex_exactly_of_type(op, pseries))
668             op = op.series(r, order);
669         if (!it->coeff.is_equal(_ex1()))
670             op = ex_to_pseries(op).power_const(ex_to_numeric(it->coeff), order);
671
672         // Series multiplication
673         acc = ex_to_pseries(acc).mul_series(ex_to_pseries(op));
674     }
675     return acc;
676 }
677
678
679 /** Compute the p-th power of a series.
680  *
681  *  @param p  power to compute
682  *  @param deg  truncation order of series calculation */
683 ex pseries::power_const(const numeric &p, int deg) const
684 {
685     int i;
686     const symbol *s = static_cast<symbol *>(var.bp);
687     int ldeg = ldegree(*s);
688     
689     // Calculate coefficients of powered series
690     exvector co;
691     co.reserve(deg);
692     ex co0;
693     co.push_back(co0 = power(coeff(*s, ldeg), p));
694     bool all_sums_zero = true;
695     for (i=1; i<deg; i++) {
696         ex sum = _ex0();
697         for (int j=1; j<=i; j++) {
698             ex c = coeff(*s, j + ldeg);
699             if (is_order_function(c)) {
700                 co.push_back(Order(_ex1()));
701                 break;
702             } else
703                 sum += (p * j - (i - j)) * co[i - j] * c;
704         }
705         if (!sum.is_zero())
706             all_sums_zero = false;
707         co.push_back(co0 * sum / numeric(i));
708     }
709     
710     // Construct new series (of non-zero coefficients)
711     epvector new_seq;
712     bool higher_order = false;
713     for (i=0; i<deg; i++) {
714         if (!co[i].is_zero())
715             new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
716         if (is_order_function(co[i])) {
717             higher_order = true;
718             break;
719         }
720     }
721     if (!higher_order && !all_sums_zero)
722         new_seq.push_back(expair(Order(_ex1()), numeric(deg) + p * ldeg));
723     return pseries(relational(var,point), new_seq);
724 }
725
726
727 /** Implementation of ex::series() for powers. This performs Laurent expansion
728  *  of reciprocals of series at singularities.
729  *  @see ex::series */
730 ex power::series(const relational & r, int order) const
731 {
732     ex e;
733     if (!is_ex_exactly_of_type(basis, pseries)) {
734         // Basis is not a series, may there be a singulary?
735         if (!exponent.info(info_flags::negint))
736             return basic::series(r, order);
737         
738         // Expression is of type something^(-int), check for singularity
739         if (!basis.subs(r).is_zero())
740             return basic::series(r, order);
741         
742         // Singularity encountered, expand basis into series
743         e = basis.series(r, order);
744     } else {
745         // Basis is a series
746         e = basis;
747     }
748     
749     // Power e
750     return ex_to_pseries(e).power_const(ex_to_numeric(exponent), order);
751 }
752
753
754 /** Re-expansion of a pseries object. */
755 ex pseries::series(const relational & r, int order) const
756 {
757     const ex p = r.rhs();
758     GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
759     const symbol *s = static_cast<symbol *>(r.lhs().bp);
760     
761         if (var.is_equal(*s) && point.is_equal(p)) {
762                 if (order > degree(*s))
763                         return *this;
764                 else {
765                 epvector new_seq;
766                 epvector::const_iterator it = seq.begin(), itend = seq.end();
767                         while (it != itend) {
768                                 int o = ex_to_numeric(it->coeff).to_int();
769                                 if (o >= order) {
770                                         new_seq.push_back(expair(Order(_ex1()), o));
771                                         break;
772                                 }
773                                 new_seq.push_back(*it);
774                                 it++;
775                         }
776                         return pseries(r, new_seq);
777                 }
778         } else
779                 return convert_to_poly().series(r, order);
780 }
781
782
783 /** Compute the truncated series expansion of an expression.
784  *  This function returns an expression containing an object of class pseries 
785  *  to represent the series. If the series does not terminate within the given
786  *  truncation order, the last term of the series will be an order term.
787  *
788  *  @param r  expansion relation, lhs holds variable and rhs holds point
789  *  @param order  truncation order of series calculations
790  *  @return an expression holding a pseries object */
791 ex ex::series(const ex & r, int order) const
792 {
793     GINAC_ASSERT(bp!=0);
794     ex e;
795     relational rel_;
796     
797     if (is_ex_exactly_of_type(r,relational))
798         rel_ = ex_to_relational(r);
799     else if (is_ex_exactly_of_type(r,symbol))
800         rel_ = relational(r,_ex0());
801     else
802         throw (std::logic_error("ex::series(): expansion point has unknown type"));
803     
804     try {
805         e = bp->series(rel_, order);
806     } catch (exception &x) {
807         throw (std::logic_error(string("unable to compute series (") + x.what() + ")"));
808     }
809     return e;
810 }
811
812
813 // Global constants
814 const pseries some_pseries;
815 const type_info & typeid_pseries = typeid(some_pseries);
816
817 #ifndef NO_NAMESPACE_GINAC
818 } // namespace GiNaC
819 #endif // ndef NO_NAMESPACE_GINAC