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 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_)
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));
110 var = *static_cast<symbol *>(rel_.lhs().bp);
118 /** Construct object from archive_node. */
119 pseries::pseries(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
121 debugmsg("pseries constructor from archive_node", LOGLEVEL_CONSTRUCT);
122 for (unsigned int i=0; true; i++) {
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));
130 n.find_ex("var", var, sym_lst);
131 n.find_ex("point", point, sym_lst);
134 /** Unarchive the object. */
135 ex pseries::unarchive(const archive_node &n, const lst &sym_lst)
137 return (new pseries(n, sym_lst))->setflag(status_flags::dynallocated);
140 /** Archive the object. */
141 void pseries::archive(archive_node &n) const
143 inherited::archive(n);
144 epvector::const_iterator i = seq.begin(), iend = seq.end();
146 n.add_ex("coeff", i->rest);
147 n.add_ex("power", i->coeff);
150 n.add_ex("var", var);
151 n.add_ex("point", point);
156 * Functions overriding virtual functions from base classes
159 basic *pseries::duplicate() const
161 debugmsg("pseries duplicate", LOGLEVEL_DUPLICATE);
162 return new pseries(*this);
165 void pseries::print(ostream &os, unsigned upper_precedence) const
167 debugmsg("pseries print", LOGLEVEL_PRINT);
168 convert_to_poly().print(os, upper_precedence);
171 void pseries::printraw(ostream &os) const
173 debugmsg("pseries printraw", LOGLEVEL_PRINT);
174 os << "pseries(" << var << ";" << point << ";";
175 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); i++) {
176 os << "(" << (*i).rest << "," << (*i).coeff << "),";
181 void pseries::printtree(ostream & os, unsigned indent) const
183 debugmsg("pseries printtree",LOGLEVEL_PRINT);
184 os << string(indent,' ') << "pseries "
185 << ", hash=" << hashvalue << " (0x" << hex << hashvalue << dec << ")"
186 << ", flags=" << flags << endl;
187 for (unsigned i=0; i<seq.size(); ++i) {
188 seq[i].rest.printtree(os,indent+delta_indent);
189 seq[i].coeff.printtree(os,indent+delta_indent);
190 if (i!=seq.size()-1) {
191 os << string(indent+delta_indent,' ') << "-----" << endl;
194 var.printtree(os, indent+delta_indent);
195 point.printtree(os, indent+delta_indent);
198 unsigned pseries::nops(void) const
203 ex pseries::op(int i) const
205 if (i < 0 || unsigned(i) >= seq.size())
206 throw (std::out_of_range("op() out of range"));
207 return seq[i].rest * power(var - point, seq[i].coeff);
210 ex &pseries::let_op(int i)
212 throw (std::logic_error("let_op not defined for pseries"));
215 int pseries::degree(const symbol &s) const
217 if (var.is_equal(s)) {
218 // Return last exponent
220 return ex_to_numeric((*(seq.end() - 1)).coeff).to_int();
224 epvector::const_iterator it = seq.begin(), itend = seq.end();
227 int max_pow = INT_MIN;
228 while (it != itend) {
229 int pow = it->rest.degree(s);
238 int pseries::ldegree(const symbol &s) const
240 if (var.is_equal(s)) {
241 // Return first exponent
243 return ex_to_numeric((*(seq.begin())).coeff).to_int();
247 epvector::const_iterator it = seq.begin(), itend = seq.end();
250 int min_pow = INT_MAX;
251 while (it != itend) {
252 int pow = it->rest.ldegree(s);
261 ex pseries::coeff(const symbol &s, int n) const
263 if (var.is_equal(s)) {
267 // Binary search in sequence for given power
268 numeric looking_for = numeric(n);
269 int lo = 0, hi = seq.size() - 1;
271 int mid = (lo + hi) / 2;
272 GINAC_ASSERT(is_ex_exactly_of_type(seq[mid].coeff, numeric));
273 int cmp = ex_to_numeric(seq[mid].coeff).compare(looking_for);
279 return seq[mid].rest;
284 throw(std::logic_error("pseries::coeff: compare() didn't return -1, 0 or 1"));
289 return convert_to_poly().coeff(s, n);
292 ex pseries::collect(const symbol &s) const
297 /** Evaluate coefficients. */
298 ex pseries::eval(int level) const
303 if (level == -max_recursion_level)
304 throw (std::runtime_error("pseries::eval(): recursion limit exceeded"));
306 // Construct a new series with evaluated coefficients
308 new_seq.reserve(seq.size());
309 epvector::const_iterator it = seq.begin(), itend = seq.end();
310 while (it != itend) {
311 new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
314 return (new pseries(relational(var,point), new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
317 /** Evaluate coefficients numerically. */
318 ex pseries::evalf(int level) const
323 if (level == -max_recursion_level)
324 throw (std::runtime_error("pseries::evalf(): recursion limit exceeded"));
326 // Construct a new series with evaluated coefficients
328 new_seq.reserve(seq.size());
329 epvector::const_iterator it = seq.begin(), itend = seq.end();
330 while (it != itend) {
331 new_seq.push_back(expair(it->rest.evalf(level-1), it->coeff));
334 return (new pseries(relational(var,point), new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
337 ex pseries::subs(const lst & ls, const lst & lr) const
339 // If expansion variable is being substituted, convert the series to a
340 // polynomial and do the substitution there because the result might
341 // no longer be a power series
343 return convert_to_poly(true).subs(ls, lr);
345 // Otherwise construct a new series with substituted coefficients and
348 new_seq.reserve(seq.size());
349 epvector::const_iterator it = seq.begin(), itend = seq.end();
350 while (it != itend) {
351 new_seq.push_back(expair(it->rest.subs(ls, lr), it->coeff));
354 return (new pseries(relational(var,point.subs(ls, lr)), new_seq))->setflag(status_flags::dynallocated);
357 /** Implementation of ex::diff() for a power series. It treats the series as a
360 ex pseries::derivative(const symbol & s) const
364 epvector::const_iterator it = seq.begin(), itend = seq.end();
366 // FIXME: coeff might depend on var
367 while (it != itend) {
368 if (is_order_function(it->rest)) {
369 new_seq.push_back(expair(it->rest, it->coeff - 1));
371 ex c = it->rest * it->coeff;
373 new_seq.push_back(expair(c, it->coeff - 1));
377 return pseries(relational(var,point), new_seq);
385 * Construct ordinary polynomial out of series
388 /** Convert a pseries object to an ordinary polynomial.
390 * @param no_order flag: discard higher order terms */
391 ex pseries::convert_to_poly(bool no_order) const
394 epvector::const_iterator it = seq.begin(), itend = seq.end();
396 while (it != itend) {
397 if (is_order_function(it->rest)) {
399 e += Order(power(var - point, it->coeff));
401 e += it->rest * power(var - point, it->coeff);
409 * Implementation of series expansion
412 /** Default implementation of ex::series(). This performs Taylor expansion.
414 ex basic::series(const relational & r, int order) const
419 ex coeff = deriv.subs(r);
420 const symbol *s = static_cast<symbol *>(r.lhs().bp);
422 if (!coeff.is_zero())
423 seq.push_back(expair(coeff, numeric(0)));
426 for (n=1; n<order; n++) {
427 fac = fac.mul(numeric(n));
428 deriv = deriv.diff(*s).expand();
429 if (deriv.is_zero()) {
431 return pseries(r, seq);
433 coeff = fac.inverse() * deriv.subs(r);
434 if (!coeff.is_zero())
435 seq.push_back(expair(coeff, numeric(n)));
438 // Higher-order terms, if present
439 deriv = deriv.diff(*s);
440 if (!deriv.is_zero())
441 seq.push_back(expair(Order(_ex1()), numeric(n)));
442 return pseries(r, seq);
446 /** Implementation of ex::series() for symbols.
448 ex symbol::series(const relational & r, int order) const
451 const ex point = r.rhs();
452 GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
453 const symbol *s = static_cast<symbol *>(r.lhs().bp);
455 if (this->is_equal(*s)) {
456 if (order > 0 && !point.is_zero())
457 seq.push_back(expair(point, _ex0()));
459 seq.push_back(expair(_ex1(), _ex1()));
461 seq.push_back(expair(Order(_ex1()), numeric(order)));
463 seq.push_back(expair(*this, _ex0()));
464 return pseries(r, seq);
468 /** Add one series object to another, producing a pseries object that
469 * represents the sum.
471 * @param other pseries object to add with
472 * @return the sum as a pseries */
473 ex pseries::add_series(const pseries &other) const
475 // Adding two series with different variables or expansion points
476 // results in an empty (constant) series
477 if (!is_compatible_to(other)) {
479 nul.push_back(expair(Order(_ex1()), _ex0()));
480 return pseries(relational(var,point), nul);
485 epvector::const_iterator a = seq.begin();
486 epvector::const_iterator b = other.seq.begin();
487 epvector::const_iterator a_end = seq.end();
488 epvector::const_iterator b_end = other.seq.end();
489 int pow_a = INT_MAX, pow_b = INT_MAX;
491 // If a is empty, fill up with elements from b and stop
494 new_seq.push_back(*b);
499 pow_a = ex_to_numeric((*a).coeff).to_int();
501 // If b is empty, fill up with elements from a and stop
504 new_seq.push_back(*a);
509 pow_b = ex_to_numeric((*b).coeff).to_int();
511 // a and b are non-empty, compare powers
513 // a has lesser power, get coefficient from a
514 new_seq.push_back(*a);
515 if (is_order_function((*a).rest))
518 } else if (pow_b < pow_a) {
519 // b has lesser power, get coefficient from b
520 new_seq.push_back(*b);
521 if (is_order_function((*b).rest))
525 // Add coefficient of a and b
526 if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
527 new_seq.push_back(expair(Order(_ex1()), (*a).coeff));
528 break; // Order term ends the sequence
530 ex sum = (*a).rest + (*b).rest;
531 if (!(sum.is_zero()))
532 new_seq.push_back(expair(sum, numeric(pow_a)));
538 return pseries(relational(var,point), new_seq);
542 /** Implementation of ex::series() for sums. This performs series addition when
543 * adding pseries objects.
545 ex add::series(const relational & r, int order) const
547 ex acc; // Series accumulator
549 // Get first term from overall_coeff
550 acc = overall_coeff.series(r, order);
552 // Add remaining terms
553 epvector::const_iterator it = seq.begin();
554 epvector::const_iterator itend = seq.end();
555 for (; it!=itend; it++) {
557 if (is_ex_exactly_of_type(it->rest, pseries))
560 op = it->rest.series(r, order);
561 if (!it->coeff.is_equal(_ex1()))
562 op = ex_to_pseries(op).mul_const(ex_to_numeric(it->coeff));
565 acc = ex_to_pseries(acc).add_series(ex_to_pseries(op));
571 /** Multiply a pseries object with a numeric constant, producing a pseries
572 * object that represents the product.
574 * @param other constant to multiply with
575 * @return the product as a pseries */
576 ex pseries::mul_const(const numeric &other) const
579 new_seq.reserve(seq.size());
581 epvector::const_iterator it = seq.begin(), itend = seq.end();
582 while (it != itend) {
583 if (!is_order_function(it->rest))
584 new_seq.push_back(expair(it->rest * other, it->coeff));
586 new_seq.push_back(*it);
589 return pseries(relational(var,point), new_seq);
593 /** Multiply one pseries object to another, producing a pseries object that
594 * represents the product.
596 * @param other pseries object to multiply with
597 * @return the product as a pseries */
598 ex pseries::mul_series(const pseries &other) const
600 // Multiplying two series with different variables or expansion points
601 // results in an empty (constant) series
602 if (!is_compatible_to(other)) {
604 nul.push_back(expair(Order(_ex1()), _ex0()));
605 return pseries(relational(var,point), nul);
608 // Series multiplication
611 const symbol *s = static_cast<symbol *>(var.bp);
612 int a_max = degree(*s);
613 int b_max = other.degree(*s);
614 int a_min = ldegree(*s);
615 int b_min = other.ldegree(*s);
616 int cdeg_min = a_min + b_min;
617 int cdeg_max = a_max + b_max;
619 int higher_order_a = INT_MAX;
620 int higher_order_b = INT_MAX;
621 if (is_order_function(coeff(*s, a_max)))
622 higher_order_a = a_max + b_min;
623 if (is_order_function(other.coeff(*s, b_max)))
624 higher_order_b = b_max + a_min;
625 int higher_order_c = min(higher_order_a, higher_order_b);
626 if (cdeg_max >= higher_order_c)
627 cdeg_max = higher_order_c - 1;
629 for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
631 // c(i)=a(0)b(i)+...+a(i)b(0)
632 for (int i=a_min; cdeg-i>=b_min; i++) {
633 ex a_coeff = coeff(*s, i);
634 ex b_coeff = other.coeff(*s, cdeg-i);
635 if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
636 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
639 new_seq.push_back(expair(co, numeric(cdeg)));
641 if (higher_order_c < INT_MAX)
642 new_seq.push_back(expair(Order(_ex1()), numeric(higher_order_c)));
643 return pseries(relational(var,point), new_seq);
647 /** Implementation of ex::series() for product. This performs series
648 * multiplication when multiplying series.
650 ex mul::series(const relational & r, int order) const
652 ex acc; // Series accumulator
654 // Get first term from overall_coeff
655 acc = overall_coeff.series(r, order);
657 // Multiply with remaining terms
658 epvector::const_iterator it = seq.begin();
659 epvector::const_iterator itend = seq.end();
660 for (; it!=itend; it++) {
662 if (op.info(info_flags::numeric)) {
663 // series * const (special case, faster)
664 ex f = power(op, it->coeff);
665 acc = ex_to_pseries(acc).mul_const(ex_to_numeric(f));
667 } else if (!is_ex_exactly_of_type(op, pseries))
668 op = op.series(r, order);
669 if (!it->coeff.is_equal(_ex1()))
670 op = ex_to_pseries(op).power_const(ex_to_numeric(it->coeff), order);
672 // Series multiplication
673 acc = ex_to_pseries(acc).mul_series(ex_to_pseries(op));
679 /** Compute the p-th power of a series.
681 * @param p power to compute
682 * @param deg truncation order of series calculation */
683 ex pseries::power_const(const numeric &p, int deg) const
686 const symbol *s = static_cast<symbol *>(var.bp);
687 int ldeg = ldegree(*s);
689 // Calculate coefficients of powered series
693 co.push_back(co0 = power(coeff(*s, ldeg), p));
694 bool all_sums_zero = true;
695 for (i=1; i<deg; i++) {
697 for (int j=1; j<=i; j++) {
698 ex c = coeff(*s, j + ldeg);
699 if (is_order_function(c)) {
700 co.push_back(Order(_ex1()));
703 sum += (p * j - (i - j)) * co[i - j] * c;
706 all_sums_zero = false;
707 co.push_back(co0 * sum / numeric(i));
710 // Construct new series (of non-zero coefficients)
712 bool higher_order = false;
713 for (i=0; i<deg; i++) {
714 if (!co[i].is_zero())
715 new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
716 if (is_order_function(co[i])) {
721 if (!higher_order && !all_sums_zero)
722 new_seq.push_back(expair(Order(_ex1()), numeric(deg) + p * ldeg));
723 return pseries(relational(var,point), new_seq);
727 /** Implementation of ex::series() for powers. This performs Laurent expansion
728 * of reciprocals of series at singularities.
730 ex power::series(const relational & r, int order) const
733 if (!is_ex_exactly_of_type(basis, pseries)) {
734 // Basis is not a series, may there be a singulary?
735 if (!exponent.info(info_flags::negint))
736 return basic::series(r, order);
738 // Expression is of type something^(-int), check for singularity
739 if (!basis.subs(r).is_zero())
740 return basic::series(r, order);
742 // Singularity encountered, expand basis into series
743 e = basis.series(r, order);
750 return ex_to_pseries(e).power_const(ex_to_numeric(exponent), order);
754 /** Re-expansion of a pseries object. */
755 ex pseries::series(const relational & r, int order) const
757 const ex p = r.rhs();
758 GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
759 const symbol *s = static_cast<symbol *>(r.lhs().bp);
761 if (var.is_equal(*s) && point.is_equal(p)) {
762 if (order > degree(*s))
766 epvector::const_iterator it = seq.begin(), itend = seq.end();
767 while (it != itend) {
768 int o = ex_to_numeric(it->coeff).to_int();
770 new_seq.push_back(expair(Order(_ex1()), o));
773 new_seq.push_back(*it);
776 return pseries(r, new_seq);
779 return convert_to_poly().series(r, order);
783 /** Compute the truncated series expansion of an expression.
784 * This function returns an expression containing an object of class pseries
785 * to represent the series. If the series does not terminate within the given
786 * truncation order, the last term of the series will be an order term.
788 * @param r expansion relation, lhs holds variable and rhs holds point
789 * @param order truncation order of series calculations
790 * @return an expression holding a pseries object */
791 ex ex::series(const ex & r, int order) const
797 if (is_ex_exactly_of_type(r,relational))
798 rel_ = ex_to_relational(r);
799 else if (is_ex_exactly_of_type(r,symbol))
800 rel_ = relational(r,_ex0());
802 throw (std::logic_error("ex::series(): expansion point has unknown type"));
805 e = bp->series(rel_, order);
806 } catch (exception &x) {
807 throw (std::logic_error(string("unable to compute series (") + x.what() + ")"));
814 const pseries some_pseries;
815 const type_info & typeid_pseries = typeid(some_pseries);
817 #ifndef NO_NAMESPACE_GINAC
819 #endif // ndef NO_NAMESPACE_GINAC