3 * Implementation of class for extended truncated power series and
4 * methods for series expansion. */
7 * GiNaC Copyright (C) 1999-2000 Johannes Gutenberg University Mainz, Germany
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.
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.
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
32 #include "relational.h"
38 #ifndef NO_NAMESPACE_GINAC
40 #endif // ndef NO_NAMESPACE_GINAC
42 GINAC_IMPLEMENT_REGISTERED_CLASS(pseries, basic)
45 * Default constructor, destructor, copy constructor, assignment operator and helpers
48 pseries::pseries() : basic(TINFO_pseries)
50 debugmsg("pseries default constructor", LOGLEVEL_CONSTRUCT);
55 debugmsg("pseries destructor", LOGLEVEL_DESTRUCT);
59 pseries::pseries(const pseries &other)
61 debugmsg("pseries copy constructor", LOGLEVEL_CONSTRUCT);
65 const pseries &pseries::operator=(const pseries & other)
67 debugmsg("pseries operator=", LOGLEVEL_ASSIGNMENT);
75 void pseries::copy(const pseries &other)
77 inherited::copy(other);
83 void pseries::destroy(bool call_parent)
86 inherited::destroy(call_parent);
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.
100 * @param var_ series variable (must hold a symbol)
101 * @param point_ expansion point
102 * @param ops_ vector of {coefficient, power} pairs (coefficient must not be zero)
103 * @return newly constructed pseries */
104 pseries::pseries(const ex &var_, const ex &point_, const epvector &ops_)
105 : basic(TINFO_pseries), seq(ops_), var(var_), point(point_)
107 debugmsg("pseries constructor from ex,ex,epvector", LOGLEVEL_CONSTRUCT);
108 GINAC_ASSERT(is_ex_exactly_of_type(var_, symbol));
116 /** Construct object from archive_node. */
117 pseries::pseries(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
119 debugmsg("pseries constructor from archive_node", LOGLEVEL_CONSTRUCT);
120 for (unsigned int i=0; true; i++) {
123 if (n.find_ex("coeff", rest, sym_lst, i) && n.find_ex("power", coeff, sym_lst, i))
124 seq.push_back(expair(rest, coeff));
128 n.find_ex("var", var, sym_lst);
129 n.find_ex("point", point, sym_lst);
132 /** Unarchive the object. */
133 ex pseries::unarchive(const archive_node &n, const lst &sym_lst)
135 return (new pseries(n, sym_lst))->setflag(status_flags::dynallocated);
138 /** Archive the object. */
139 void pseries::archive(archive_node &n) const
141 inherited::archive(n);
142 epvector::const_iterator i = seq.begin(), iend = seq.end();
144 n.add_ex("coeff", i->rest);
145 n.add_ex("power", i->coeff);
148 n.add_ex("var", var);
149 n.add_ex("point", point);
154 * Functions overriding virtual functions from base classes
157 basic *pseries::duplicate() const
159 debugmsg("pseries duplicate", LOGLEVEL_DUPLICATE);
160 return new pseries(*this);
163 void pseries::print(ostream &os, unsigned upper_precedence) const
165 debugmsg("pseries print", LOGLEVEL_PRINT);
166 convert_to_poly().print(os, upper_precedence);
169 void pseries::printraw(ostream &os) const
171 debugmsg("pseries printraw", LOGLEVEL_PRINT);
172 os << "pseries(" << var << ";" << point << ";";
173 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); i++) {
174 os << "(" << (*i).rest << "," << (*i).coeff << "),";
179 unsigned pseries::nops(void) const
184 ex pseries::op(int i) const
186 if (i < 0 || unsigned(i) >= seq.size())
187 throw (std::out_of_range("op() out of range"));
188 return seq[i].rest * power(var - point, seq[i].coeff);
191 ex &pseries::let_op(int i)
193 throw (std::logic_error("let_op not defined for pseries"));
196 int pseries::degree(const symbol &s) const
198 if (var.is_equal(s)) {
199 // Return last exponent
201 return ex_to_numeric((*(seq.end() - 1)).coeff).to_int();
205 epvector::const_iterator it = seq.begin(), itend = seq.end();
208 int max_pow = INT_MIN;
209 while (it != itend) {
210 int pow = it->rest.degree(s);
219 int pseries::ldegree(const symbol &s) const
221 if (var.is_equal(s)) {
222 // Return first exponent
224 return ex_to_numeric((*(seq.begin())).coeff).to_int();
228 epvector::const_iterator it = seq.begin(), itend = seq.end();
231 int min_pow = INT_MAX;
232 while (it != itend) {
233 int pow = it->rest.ldegree(s);
242 ex pseries::coeff(const symbol &s, int n) const
244 if (var.is_equal(s)) {
248 // Binary search in sequence for given power
249 numeric looking_for = numeric(n);
250 int lo = 0, hi = seq.size() - 1;
252 int mid = (lo + hi) / 2;
253 GINAC_ASSERT(is_ex_exactly_of_type(seq[mid].coeff, numeric));
254 int cmp = ex_to_numeric(seq[mid].coeff).compare(looking_for);
260 return seq[mid].rest;
265 throw(std::logic_error("pseries::coeff: compare() didn't return -1, 0 or 1"));
270 return convert_to_poly().coeff(s, n);
273 ex pseries::collect(const symbol &s) const
278 /** Evaluate coefficients. */
279 ex pseries::eval(int level) const
284 if (level == -max_recursion_level)
285 throw (std::runtime_error("pseries::eval(): recursion limit exceeded"));
287 // Construct a new series with evaluated coefficients
289 new_seq.reserve(seq.size());
290 epvector::const_iterator it = seq.begin(), itend = seq.end();
291 while (it != itend) {
292 new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
295 return (new pseries(var, point, new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
298 /** Evaluate coefficients numerically. */
299 ex pseries::evalf(int level) const
304 if (level == -max_recursion_level)
305 throw (std::runtime_error("pseries::evalf(): recursion limit exceeded"));
307 // Construct a new series with evaluated coefficients
309 new_seq.reserve(seq.size());
310 epvector::const_iterator it = seq.begin(), itend = seq.end();
311 while (it != itend) {
312 new_seq.push_back(expair(it->rest.evalf(level-1), it->coeff));
315 return (new pseries(var, point, new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
318 ex pseries::subs(const lst & ls, const lst & lr) const
320 // If expansion variable is being substituted, convert the series to a
321 // polynomial and do the substitution there because the result might
322 // no longer be a power series
324 return convert_to_poly(true).subs(ls, lr);
326 // Otherwise construct a new series with substituted coefficients and
329 new_seq.reserve(seq.size());
330 epvector::const_iterator it = seq.begin(), itend = seq.end();
331 while (it != itend) {
332 new_seq.push_back(expair(it->rest.subs(ls, lr), it->coeff));
335 return (new pseries(var, point.subs(ls, lr), new_seq))->setflag(status_flags::dynallocated);
338 /** Implementation of ex::diff() for a power series. It treats the series as a
341 ex pseries::derivative(const symbol & s) const
345 epvector::const_iterator it = seq.begin(), itend = seq.end();
347 // FIXME: coeff might depend on var
348 while (it != itend) {
349 if (is_order_function(it->rest)) {
350 new_seq.push_back(expair(it->rest, it->coeff - 1));
352 ex c = it->rest * it->coeff;
354 new_seq.push_back(expair(c, it->coeff - 1));
358 return pseries(var, point, new_seq);
366 * Construct ordinary polynomial out of series
369 /** Convert a pseries object to an ordinary polynomial.
371 * @param no_order flag: discard higher order terms */
372 ex pseries::convert_to_poly(bool no_order) const
375 epvector::const_iterator it = seq.begin(), itend = seq.end();
377 while (it != itend) {
378 if (is_order_function(it->rest)) {
380 e += Order(power(var - point, it->coeff));
382 e += it->rest * power(var - point, it->coeff);
390 * Implementation of series expansion
393 /** Default implementation of ex::series(). This performs Taylor expansion.
395 ex basic::series(const symbol & s, const ex & point, int order) const
400 ex coeff = deriv.subs(s == point);
401 if (!coeff.is_zero())
402 seq.push_back(expair(coeff, numeric(0)));
405 for (n=1; n<order; n++) {
406 fac = fac.mul(numeric(n));
407 deriv = deriv.diff(s).expand();
408 if (deriv.is_zero()) {
410 return pseries(s, point, seq);
412 coeff = fac.inverse() * deriv.subs(s == point);
413 if (!coeff.is_zero())
414 seq.push_back(expair(coeff, numeric(n)));
417 // Higher-order terms, if present
418 deriv = deriv.diff(s);
419 if (!deriv.is_zero())
420 seq.push_back(expair(Order(_ex1()), numeric(n)));
421 return pseries(s, point, seq);
425 /** Implementation of ex::series() for symbols.
427 ex symbol::series(const symbol & s, const ex & point, int order) const
431 if (order > 0 && !point.is_zero())
432 seq.push_back(expair(point, _ex0()));
434 seq.push_back(expair(_ex1(), _ex1()));
436 seq.push_back(expair(Order(_ex1()), numeric(order)));
438 seq.push_back(expair(*this, _ex0()));
439 return pseries(s, point, seq);
443 /** Add one series object to another, producing a pseries object that
444 * represents the sum.
446 * @param other pseries object to add with
447 * @return the sum as a pseries */
448 ex pseries::add_series(const pseries &other) const
450 // Adding two series with different variables or expansion points
451 // results in an empty (constant) series
452 if (!is_compatible_to(other)) {
454 nul.push_back(expair(Order(_ex1()), _ex0()));
455 return pseries(var, point, nul);
460 epvector::const_iterator a = seq.begin();
461 epvector::const_iterator b = other.seq.begin();
462 epvector::const_iterator a_end = seq.end();
463 epvector::const_iterator b_end = other.seq.end();
464 int pow_a = INT_MAX, pow_b = INT_MAX;
466 // If a is empty, fill up with elements from b and stop
469 new_seq.push_back(*b);
474 pow_a = ex_to_numeric((*a).coeff).to_int();
476 // If b is empty, fill up with elements from a and stop
479 new_seq.push_back(*a);
484 pow_b = ex_to_numeric((*b).coeff).to_int();
486 // a and b are non-empty, compare powers
488 // a has lesser power, get coefficient from a
489 new_seq.push_back(*a);
490 if (is_order_function((*a).rest))
493 } else if (pow_b < pow_a) {
494 // b has lesser power, get coefficient from b
495 new_seq.push_back(*b);
496 if (is_order_function((*b).rest))
500 // Add coefficient of a and b
501 if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
502 new_seq.push_back(expair(Order(_ex1()), (*a).coeff));
503 break; // Order term ends the sequence
505 ex sum = (*a).rest + (*b).rest;
506 if (!(sum.is_zero()))
507 new_seq.push_back(expair(sum, numeric(pow_a)));
513 return pseries(var, point, new_seq);
517 /** Implementation of ex::series() for sums. This performs series addition when
518 * adding pseries objects.
520 ex add::series(const symbol & s, const ex & point, int order) const
522 ex acc; // Series accumulator
524 // Get first term from overall_coeff
525 acc = overall_coeff.series(s, point, order);
527 // Add remaining terms
528 epvector::const_iterator it = seq.begin();
529 epvector::const_iterator itend = seq.end();
530 for (; it!=itend; it++) {
532 if (is_ex_exactly_of_type(it->rest, pseries))
535 op = it->rest.series(s, point, order);
536 if (!it->coeff.is_equal(_ex1()))
537 op = ex_to_pseries(op).mul_const(ex_to_numeric(it->coeff));
540 acc = ex_to_pseries(acc).add_series(ex_to_pseries(op));
546 /** Multiply a pseries object with a numeric constant, producing a pseries
547 * object that represents the product.
549 * @param other constant to multiply with
550 * @return the product as a pseries */
551 ex pseries::mul_const(const numeric &other) const
554 new_seq.reserve(seq.size());
556 epvector::const_iterator it = seq.begin(), itend = seq.end();
557 while (it != itend) {
558 if (!is_order_function(it->rest))
559 new_seq.push_back(expair(it->rest * other, it->coeff));
561 new_seq.push_back(*it);
564 return pseries(var, point, new_seq);
568 /** Multiply one pseries object to another, producing a pseries object that
569 * represents the product.
571 * @param other pseries object to multiply with
572 * @return the product as a pseries */
573 ex pseries::mul_series(const pseries &other) const
575 // Multiplying two series with different variables or expansion points
576 // results in an empty (constant) series
577 if (!is_compatible_to(other)) {
579 nul.push_back(expair(Order(_ex1()), _ex0()));
580 return pseries(var, point, nul);
583 // Series multiplication
586 const symbol *s = static_cast<symbol *>(var.bp);
587 int a_max = degree(*s);
588 int b_max = other.degree(*s);
589 int a_min = ldegree(*s);
590 int b_min = other.ldegree(*s);
591 int cdeg_min = a_min + b_min;
592 int cdeg_max = a_max + b_max;
594 int higher_order_a = INT_MAX;
595 int higher_order_b = INT_MAX;
596 if (is_order_function(coeff(*s, a_max)))
597 higher_order_a = a_max + b_min;
598 if (is_order_function(other.coeff(*s, b_max)))
599 higher_order_b = b_max + a_min;
600 int higher_order_c = min(higher_order_a, higher_order_b);
601 if (cdeg_max >= higher_order_c)
602 cdeg_max = higher_order_c - 1;
604 for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
606 // c(i)=a(0)b(i)+...+a(i)b(0)
607 for (int i=a_min; cdeg-i>=b_min; i++) {
608 ex a_coeff = coeff(*s, i);
609 ex b_coeff = other.coeff(*s, cdeg-i);
610 if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
611 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
614 new_seq.push_back(expair(co, numeric(cdeg)));
616 if (higher_order_c < INT_MAX)
617 new_seq.push_back(expair(Order(_ex1()), numeric(higher_order_c)));
618 return pseries(var, point, new_seq);
622 /** Implementation of ex::series() for product. This performs series
623 * multiplication when multiplying series.
625 ex mul::series(const symbol & s, const ex & point, int order) const
627 ex acc; // Series accumulator
629 // Get first term from overall_coeff
630 acc = overall_coeff.series(s, point, order);
632 // Multiply with remaining terms
633 epvector::const_iterator it = seq.begin();
634 epvector::const_iterator itend = seq.end();
635 for (; it!=itend; it++) {
637 if (op.info(info_flags::numeric)) {
638 // series * const (special case, faster)
639 ex f = power(op, it->coeff);
640 acc = ex_to_pseries(acc).mul_const(ex_to_numeric(f));
642 } else if (!is_ex_exactly_of_type(op, pseries))
643 op = op.series(s, point, order);
644 if (!it->coeff.is_equal(_ex1()))
645 op = ex_to_pseries(op).power_const(ex_to_numeric(it->coeff), order);
647 // Series multiplication
648 acc = ex_to_pseries(acc).mul_series(ex_to_pseries(op));
654 /** Compute the p-th power of a series.
656 * @param p power to compute
657 * @param deg truncation order of series calculation */
658 ex pseries::power_const(const numeric &p, int deg) const
661 const symbol *s = static_cast<symbol *>(var.bp);
662 int ldeg = ldegree(*s);
664 // Calculate coefficients of powered series
668 co.push_back(co0 = power(coeff(*s, ldeg), p));
669 bool all_sums_zero = true;
670 for (i=1; i<deg; i++) {
672 for (int j=1; j<=i; j++) {
673 ex c = coeff(*s, j + ldeg);
674 if (is_order_function(c)) {
675 co.push_back(Order(_ex1()));
678 sum += (p * j - (i - j)) * co[i - j] * c;
681 all_sums_zero = false;
682 co.push_back(co0 * sum / numeric(i));
685 // Construct new series (of non-zero coefficients)
687 bool higher_order = false;
688 for (i=0; i<deg; i++) {
689 if (!co[i].is_zero())
690 new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
691 if (is_order_function(co[i])) {
696 if (!higher_order && !all_sums_zero)
697 new_seq.push_back(expair(Order(_ex1()), numeric(deg) + p * ldeg));
698 return pseries(var, point, new_seq);
702 /** Implementation of ex::series() for powers. This performs Laurent expansion
703 * of reciprocals of series at singularities.
705 ex power::series(const symbol & s, const ex & point, int order) const
708 if (!is_ex_exactly_of_type(basis, pseries)) {
709 // Basis is not a series, may there be a singulary?
710 if (!exponent.info(info_flags::negint))
711 return basic::series(s, point, order);
713 // Expression is of type something^(-int), check for singularity
714 if (!basis.subs(s == point).is_zero())
715 return basic::series(s, point, order);
717 // Singularity encountered, expand basis into series
718 e = basis.series(s, point, order);
725 return ex_to_pseries(e).power_const(ex_to_numeric(exponent), order);
729 /** Re-expansion of a pseries object. */
730 ex pseries::series(const symbol & s, const ex & p, int order) const
732 if (var.is_equal(s) && point.is_equal(p)) {
733 if (order > degree(s))
737 epvector::const_iterator it = seq.begin(), itend = seq.end();
738 while (it != itend) {
739 int o = ex_to_numeric(it->coeff).to_int();
741 new_seq.push_back(expair(Order(_ex1()), o));
744 new_seq.push_back(*it);
747 return pseries(var, point, new_seq);
750 return convert_to_poly().series(s, p, order);
754 /** Compute the truncated series expansion of an expression.
755 * This function returns an expression containing an object of class pseries to
756 * represent the series. If the series does not terminate within the given
757 * truncation order, the last term of the series will be an order term.
759 * @param s expansion variable
760 * @param point expansion point
761 * @param order truncation order of series calculations
762 * @return an expression holding a pseries object */
763 ex ex::series(const symbol &s, const ex &point, int order) const
766 return bp->series(s, point, order);
771 const pseries some_pseries;
772 const type_info & typeid_pseries = typeid(some_pseries);
774 #ifndef NO_NAMESPACE_GINAC
776 #endif // ndef NO_NAMESPACE_GINAC