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)) {
245 epvector::const_iterator it = seq.begin(), itend = seq.end();
246 while (it != itend) {
247 int pow = ex_to_numeric(it->coeff).to_int();
256 return convert_to_poly().coeff(s, n);
259 ex pseries::collect(const symbol &s) const
262 return convert_to_poly();
264 return inherited::collect(s);
267 ex pseries::eval(int level) const
272 // Construct a new series with evaluated coefficients
274 new_seq.reserve(seq.size());
275 epvector::const_iterator it = seq.begin(), itend = seq.end();
276 while (it != itend) {
277 new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
280 return (new pseries(var, point, new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
283 /** Evaluate numerically. The order term is dropped. */
284 ex pseries::evalf(int level) const
286 return convert_to_poly().evalf(level);
289 ex pseries::subs(const lst & ls, const lst & lr) const
291 // If expansion variable is being substituted, convert the series to a
292 // polynomial and do the substitution there because the result might
293 // no longer be a power series
295 return convert_to_poly(true).subs(ls, lr);
297 // Otherwise construct a new series with substituted coefficients and
300 new_seq.reserve(seq.size());
301 epvector::const_iterator it = seq.begin(), itend = seq.end();
302 while (it != itend) {
303 new_seq.push_back(expair(it->rest.subs(ls, lr), it->coeff));
306 return (new pseries(var, point.subs(ls, lr), new_seq))->setflag(status_flags::dynallocated);
309 /** Implementation of ex::diff() for a power series. It treats the series as a
312 ex pseries::derivative(const symbol & s) const
316 epvector::const_iterator it = seq.begin(), itend = seq.end();
318 // FIXME: coeff might depend on var
319 while (it != itend) {
320 if (is_order_function(it->rest)) {
321 new_seq.push_back(expair(it->rest, it->coeff - 1));
323 ex c = it->rest * it->coeff;
325 new_seq.push_back(expair(c, it->coeff - 1));
329 return pseries(var, point, new_seq);
337 * Construct ordinary polynomial out of series
340 /** Convert a pseries object to an ordinary polynomial.
342 * @param no_order flag: discard higher order terms */
343 ex pseries::convert_to_poly(bool no_order) const
346 epvector::const_iterator it = seq.begin(), itend = seq.end();
348 while (it != itend) {
349 if (is_order_function(it->rest)) {
351 e += Order(power(var - point, it->coeff));
353 e += it->rest * power(var - point, it->coeff);
361 * Implementation of series expansion
364 /** Default implementation of ex::series(). This performs Taylor expansion.
366 ex basic::series(const symbol & s, const ex & point, int order) const
371 ex coeff = deriv.subs(s == point);
372 if (!coeff.is_zero())
373 seq.push_back(expair(coeff, numeric(0)));
376 for (n=1; n<order; n++) {
377 fac = fac.mul(numeric(n));
378 deriv = deriv.diff(s).expand();
379 if (deriv.is_zero()) {
381 return pseries(s, point, seq);
383 coeff = fac.inverse() * deriv.subs(s == point);
384 if (!coeff.is_zero())
385 seq.push_back(expair(coeff, numeric(n)));
388 // Higher-order terms, if present
389 deriv = deriv.diff(s);
390 if (!deriv.is_zero())
391 seq.push_back(expair(Order(_ex1()), numeric(n)));
392 return pseries(s, point, seq);
396 /** Implementation of ex::series() for symbols.
398 ex symbol::series(const symbol & s, const ex & point, int order) const
402 if (order > 0 && !point.is_zero())
403 seq.push_back(expair(point, _ex0()));
405 seq.push_back(expair(_ex1(), _ex1()));
407 seq.push_back(expair(Order(_ex1()), numeric(order)));
409 seq.push_back(expair(*this, _ex0()));
410 return pseries(s, point, seq);
414 /** Add one series object to another, producing a pseries object that
415 * represents the sum.
417 * @param other pseries object to add with
418 * @return the sum as a pseries */
419 ex pseries::add_series(const pseries &other) const
421 // Adding two series with different variables or expansion points
422 // results in an empty (constant) series
423 if (!is_compatible_to(other)) {
425 nul.push_back(expair(Order(_ex1()), _ex0()));
426 return pseries(var, point, nul);
431 epvector::const_iterator a = seq.begin();
432 epvector::const_iterator b = other.seq.begin();
433 epvector::const_iterator a_end = seq.end();
434 epvector::const_iterator b_end = other.seq.end();
435 int pow_a = INT_MAX, pow_b = INT_MAX;
437 // If a is empty, fill up with elements from b and stop
440 new_seq.push_back(*b);
445 pow_a = ex_to_numeric((*a).coeff).to_int();
447 // If b is empty, fill up with elements from a and stop
450 new_seq.push_back(*a);
455 pow_b = ex_to_numeric((*b).coeff).to_int();
457 // a and b are non-empty, compare powers
459 // a has lesser power, get coefficient from a
460 new_seq.push_back(*a);
461 if (is_order_function((*a).rest))
464 } else if (pow_b < pow_a) {
465 // b has lesser power, get coefficient from b
466 new_seq.push_back(*b);
467 if (is_order_function((*b).rest))
471 // Add coefficient of a and b
472 if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
473 new_seq.push_back(expair(Order(_ex1()), (*a).coeff));
474 break; // Order term ends the sequence
476 ex sum = (*a).rest + (*b).rest;
477 if (!(sum.is_zero()))
478 new_seq.push_back(expair(sum, numeric(pow_a)));
484 return pseries(var, point, new_seq);
488 /** Implementation of ex::series() for sums. This performs series addition when
489 * adding pseries objects.
491 ex add::series(const symbol & s, const ex & point, int order) const
493 ex acc; // Series accumulator
495 // Get first term from overall_coeff
496 acc = overall_coeff.series(s, point, order);
498 // Add remaining terms
499 epvector::const_iterator it = seq.begin();
500 epvector::const_iterator itend = seq.end();
501 for (; it!=itend; it++) {
503 if (is_ex_exactly_of_type(it->rest, pseries))
506 op = it->rest.series(s, point, order);
507 if (!it->coeff.is_equal(_ex1()))
508 op = ex_to_pseries(op).mul_const(ex_to_numeric(it->coeff));
511 acc = ex_to_pseries(acc).add_series(ex_to_pseries(op));
517 /** Multiply a pseries object with a numeric constant, producing a pseries
518 * object that represents the product.
520 * @param other constant to multiply with
521 * @return the product as a pseries */
522 ex pseries::mul_const(const numeric &other) const
525 new_seq.reserve(seq.size());
527 epvector::const_iterator it = seq.begin(), itend = seq.end();
528 while (it != itend) {
529 if (!is_order_function(it->rest))
530 new_seq.push_back(expair(it->rest * other, it->coeff));
532 new_seq.push_back(*it);
535 return pseries(var, point, new_seq);
539 /** Multiply one pseries object to another, producing a pseries object that
540 * represents the product.
542 * @param other pseries object to multiply with
543 * @return the product as a pseries */
544 ex pseries::mul_series(const pseries &other) const
546 // Multiplying two series with different variables or expansion points
547 // results in an empty (constant) series
548 if (!is_compatible_to(other)) {
550 nul.push_back(expair(Order(_ex1()), _ex0()));
551 return pseries(var, point, nul);
554 // Series multiplication
557 const symbol *s = static_cast<symbol *>(var.bp);
558 int a_max = degree(*s);
559 int b_max = other.degree(*s);
560 int a_min = ldegree(*s);
561 int b_min = other.ldegree(*s);
562 int cdeg_min = a_min + b_min;
563 int cdeg_max = a_max + b_max;
565 int higher_order_a = INT_MAX;
566 int higher_order_b = INT_MAX;
567 if (is_order_function(coeff(*s, a_max)))
568 higher_order_a = a_max + b_min;
569 if (is_order_function(other.coeff(*s, b_max)))
570 higher_order_b = b_max + a_min;
571 int higher_order_c = min(higher_order_a, higher_order_b);
572 if (cdeg_max >= higher_order_c)
573 cdeg_max = higher_order_c - 1;
575 for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
577 // c(i)=a(0)b(i)+...+a(i)b(0)
578 for (int i=a_min; cdeg-i>=b_min; i++) {
579 ex a_coeff = coeff(*s, i);
580 ex b_coeff = other.coeff(*s, cdeg-i);
581 if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
582 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
585 new_seq.push_back(expair(co, numeric(cdeg)));
587 if (higher_order_c < INT_MAX)
588 new_seq.push_back(expair(Order(_ex1()), numeric(higher_order_c)));
589 return pseries(var, point, new_seq);
593 /** Implementation of ex::series() for product. This performs series
594 * multiplication when multiplying series.
596 ex mul::series(const symbol & s, const ex & point, int order) const
598 ex acc; // Series accumulator
600 // Get first term from overall_coeff
601 acc = overall_coeff.series(s, point, order);
603 // Multiply with remaining terms
604 epvector::const_iterator it = seq.begin();
605 epvector::const_iterator itend = seq.end();
606 for (; it!=itend; it++) {
608 if (op.info(info_flags::numeric)) {
609 // series * const (special case, faster)
610 ex f = power(op, it->coeff);
611 acc = ex_to_pseries(acc).mul_const(ex_to_numeric(f));
613 } else if (!is_ex_exactly_of_type(op, pseries))
614 op = op.series(s, point, order);
615 if (!it->coeff.is_equal(_ex1()))
616 op = ex_to_pseries(op).power_const(ex_to_numeric(it->coeff), order);
618 // Series multiplication
619 acc = ex_to_pseries(acc).mul_series(ex_to_pseries(op));
625 /** Compute the p-th power of a series.
627 * @param p power to compute
628 * @param deg truncation order of series calculation */
629 ex pseries::power_const(const numeric &p, int deg) const
632 const symbol *s = static_cast<symbol *>(var.bp);
633 int ldeg = ldegree(*s);
635 // Calculate coefficients of powered series
639 co.push_back(co0 = power(coeff(*s, ldeg), p));
640 bool all_sums_zero = true;
641 for (i=1; i<deg; i++) {
643 for (int j=1; j<=i; j++) {
644 ex c = coeff(*s, j + ldeg);
645 if (is_order_function(c)) {
646 co.push_back(Order(_ex1()));
649 sum += (p * j - (i - j)) * co[i - j] * c;
652 all_sums_zero = false;
653 co.push_back(co0 * sum / numeric(i));
656 // Construct new series (of non-zero coefficients)
658 bool higher_order = false;
659 for (i=0; i<deg; i++) {
660 if (!co[i].is_zero())
661 new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
662 if (is_order_function(co[i])) {
667 if (!higher_order && !all_sums_zero)
668 new_seq.push_back(expair(Order(_ex1()), numeric(deg) + p * ldeg));
669 return pseries(var, point, new_seq);
673 /** Implementation of ex::series() for powers. This performs Laurent expansion
674 * of reciprocals of series at singularities.
676 ex power::series(const symbol & s, const ex & point, int order) const
679 if (!is_ex_exactly_of_type(basis, pseries)) {
680 // Basis is not a series, may there be a singulary?
681 if (!exponent.info(info_flags::negint))
682 return basic::series(s, point, order);
684 // Expression is of type something^(-int), check for singularity
685 if (!basis.subs(s == point).is_zero())
686 return basic::series(s, point, order);
688 // Singularity encountered, expand basis into series
689 e = basis.series(s, point, order);
696 return ex_to_pseries(e).power_const(ex_to_numeric(exponent), order);
700 /** Re-expansion of a pseries object. */
701 ex pseries::series(const symbol & s, const ex & p, int order) const
703 if (var.is_equal(s) && point.is_equal(p)) {
704 if (order > degree(s))
708 epvector::const_iterator it = seq.begin(), itend = seq.end();
709 while (it != itend) {
710 int o = ex_to_numeric(it->coeff).to_int();
712 new_seq.push_back(expair(Order(_ex1()), o));
715 new_seq.push_back(*it);
718 return pseries(var, point, new_seq);
721 return convert_to_poly().series(s, p, order);
725 /** Compute the truncated series expansion of an expression.
726 * This function returns an expression containing an object of class pseries to
727 * represent the series. If the series does not terminate within the given
728 * truncation order, the last term of the series will be an order term.
730 * @param s expansion variable
731 * @param point expansion point
732 * @param order truncation order of series calculations
733 * @return an expression holding a pseries object */
734 ex ex::series(const symbol &s, const ex &point, int order) const
737 return bp->series(s, point, order);
742 const pseries some_pseries;
743 const type_info & typeid_pseries = typeid(some_pseries);
745 #ifndef NO_NAMESPACE_GINAC
747 #endif // ndef NO_NAMESPACE_GINAC