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