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