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