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 unsigned pseries::nops(void) const
186 ex pseries::op(int i) const
188 if (i < 0 || unsigned(i) >= seq.size())
189 throw (std::out_of_range("op() out of range"));
190 return seq[i].rest * power(var - point, seq[i].coeff);
193 ex &pseries::let_op(int i)
195 throw (std::logic_error("let_op not defined for pseries"));
198 int pseries::degree(const symbol &s) const
200 if (var.is_equal(s)) {
201 // Return last exponent
203 return ex_to_numeric((*(seq.end() - 1)).coeff).to_int();
207 epvector::const_iterator it = seq.begin(), itend = seq.end();
210 int max_pow = INT_MIN;
211 while (it != itend) {
212 int pow = it->rest.degree(s);
221 int pseries::ldegree(const symbol &s) const
223 if (var.is_equal(s)) {
224 // Return first exponent
226 return ex_to_numeric((*(seq.begin())).coeff).to_int();
230 epvector::const_iterator it = seq.begin(), itend = seq.end();
233 int min_pow = INT_MAX;
234 while (it != itend) {
235 int pow = it->rest.ldegree(s);
244 ex pseries::coeff(const symbol &s, int n) const
246 if (var.is_equal(s)) {
250 // Binary search in sequence for given power
251 numeric looking_for = numeric(n);
252 int lo = 0, hi = seq.size() - 1;
254 int mid = (lo + hi) / 2;
255 GINAC_ASSERT(is_ex_exactly_of_type(seq[mid].coeff, numeric));
256 int cmp = ex_to_numeric(seq[mid].coeff).compare(looking_for);
262 return seq[mid].rest;
267 throw(std::logic_error("pseries::coeff: compare() didn't return -1, 0 or 1"));
272 return convert_to_poly().coeff(s, n);
275 ex pseries::collect(const symbol &s) const
280 /** Evaluate coefficients. */
281 ex pseries::eval(int level) const
286 if (level == -max_recursion_level)
287 throw (std::runtime_error("pseries::eval(): recursion limit exceeded"));
289 // Construct a new series with evaluated coefficients
291 new_seq.reserve(seq.size());
292 epvector::const_iterator it = seq.begin(), itend = seq.end();
293 while (it != itend) {
294 new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
297 return (new pseries(relational(var,point), new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
300 /** Evaluate coefficients numerically. */
301 ex pseries::evalf(int level) const
306 if (level == -max_recursion_level)
307 throw (std::runtime_error("pseries::evalf(): recursion limit exceeded"));
309 // Construct a new series with evaluated coefficients
311 new_seq.reserve(seq.size());
312 epvector::const_iterator it = seq.begin(), itend = seq.end();
313 while (it != itend) {
314 new_seq.push_back(expair(it->rest.evalf(level-1), it->coeff));
317 return (new pseries(relational(var,point), new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
320 ex pseries::subs(const lst & ls, const lst & lr) const
322 // If expansion variable is being substituted, convert the series to a
323 // polynomial and do the substitution there because the result might
324 // no longer be a power series
326 return convert_to_poly(true).subs(ls, lr);
328 // Otherwise construct a new series with substituted coefficients and
331 new_seq.reserve(seq.size());
332 epvector::const_iterator it = seq.begin(), itend = seq.end();
333 while (it != itend) {
334 new_seq.push_back(expair(it->rest.subs(ls, lr), it->coeff));
337 return (new pseries(relational(var,point.subs(ls, lr)), new_seq))->setflag(status_flags::dynallocated);
340 /** Implementation of ex::diff() for a power series. It treats the series as a
343 ex pseries::derivative(const symbol & s) const
347 epvector::const_iterator it = seq.begin(), itend = seq.end();
349 // FIXME: coeff might depend on var
350 while (it != itend) {
351 if (is_order_function(it->rest)) {
352 new_seq.push_back(expair(it->rest, it->coeff - 1));
354 ex c = it->rest * it->coeff;
356 new_seq.push_back(expair(c, it->coeff - 1));
360 return pseries(relational(var,point), new_seq);
368 * Construct ordinary polynomial out of series
371 /** Convert a pseries object to an ordinary polynomial.
373 * @param no_order flag: discard higher order terms */
374 ex pseries::convert_to_poly(bool no_order) const
377 epvector::const_iterator it = seq.begin(), itend = seq.end();
379 while (it != itend) {
380 if (is_order_function(it->rest)) {
382 e += Order(power(var - point, it->coeff));
384 e += it->rest * power(var - point, it->coeff);
392 * Implementation of series expansion
395 /** Default implementation of ex::series(). This performs Taylor expansion.
397 ex basic::series(const relational & r, int order) const
402 ex coeff = deriv.subs(r);
403 const symbol *s = static_cast<symbol *>(r.lhs().bp);
405 if (!coeff.is_zero())
406 seq.push_back(expair(coeff, numeric(0)));
409 for (n=1; n<order; n++) {
410 fac = fac.mul(numeric(n));
411 deriv = deriv.diff(*s).expand();
412 if (deriv.is_zero()) {
414 return pseries(r, seq);
416 coeff = fac.inverse() * deriv.subs(r);
417 if (!coeff.is_zero())
418 seq.push_back(expair(coeff, numeric(n)));
421 // Higher-order terms, if present
422 deriv = deriv.diff(*s);
423 if (!deriv.is_zero())
424 seq.push_back(expair(Order(_ex1()), numeric(n)));
425 return pseries(r, seq);
429 /** Implementation of ex::series() for symbols.
431 ex symbol::series(const relational & r, int order) const
434 const ex point = r.rhs();
435 GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
436 const symbol *s = static_cast<symbol *>(r.lhs().bp);
438 if (this->is_equal(*s)) {
439 if (order > 0 && !point.is_zero())
440 seq.push_back(expair(point, _ex0()));
442 seq.push_back(expair(_ex1(), _ex1()));
444 seq.push_back(expair(Order(_ex1()), numeric(order)));
446 seq.push_back(expair(*this, _ex0()));
447 return pseries(r, seq);
451 /** Add one series object to another, producing a pseries object that
452 * represents the sum.
454 * @param other pseries object to add with
455 * @return the sum as a pseries */
456 ex pseries::add_series(const pseries &other) const
458 // Adding two series with different variables or expansion points
459 // results in an empty (constant) series
460 if (!is_compatible_to(other)) {
462 nul.push_back(expair(Order(_ex1()), _ex0()));
463 return pseries(relational(var,point), nul);
468 epvector::const_iterator a = seq.begin();
469 epvector::const_iterator b = other.seq.begin();
470 epvector::const_iterator a_end = seq.end();
471 epvector::const_iterator b_end = other.seq.end();
472 int pow_a = INT_MAX, pow_b = INT_MAX;
474 // If a is empty, fill up with elements from b and stop
477 new_seq.push_back(*b);
482 pow_a = ex_to_numeric((*a).coeff).to_int();
484 // If b is empty, fill up with elements from a and stop
487 new_seq.push_back(*a);
492 pow_b = ex_to_numeric((*b).coeff).to_int();
494 // a and b are non-empty, compare powers
496 // a has lesser power, get coefficient from a
497 new_seq.push_back(*a);
498 if (is_order_function((*a).rest))
501 } else if (pow_b < pow_a) {
502 // b has lesser power, get coefficient from b
503 new_seq.push_back(*b);
504 if (is_order_function((*b).rest))
508 // Add coefficient of a and b
509 if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
510 new_seq.push_back(expair(Order(_ex1()), (*a).coeff));
511 break; // Order term ends the sequence
513 ex sum = (*a).rest + (*b).rest;
514 if (!(sum.is_zero()))
515 new_seq.push_back(expair(sum, numeric(pow_a)));
521 return pseries(relational(var,point), new_seq);
525 /** Implementation of ex::series() for sums. This performs series addition when
526 * adding pseries objects.
528 ex add::series(const relational & r, int order) const
530 ex acc; // Series accumulator
532 // Get first term from overall_coeff
533 acc = overall_coeff.series(r, order);
535 // Add remaining terms
536 epvector::const_iterator it = seq.begin();
537 epvector::const_iterator itend = seq.end();
538 for (; it!=itend; it++) {
540 if (is_ex_exactly_of_type(it->rest, pseries))
543 op = it->rest.series(r, order);
544 if (!it->coeff.is_equal(_ex1()))
545 op = ex_to_pseries(op).mul_const(ex_to_numeric(it->coeff));
548 acc = ex_to_pseries(acc).add_series(ex_to_pseries(op));
554 /** Multiply a pseries object with a numeric constant, producing a pseries
555 * object that represents the product.
557 * @param other constant to multiply with
558 * @return the product as a pseries */
559 ex pseries::mul_const(const numeric &other) const
562 new_seq.reserve(seq.size());
564 epvector::const_iterator it = seq.begin(), itend = seq.end();
565 while (it != itend) {
566 if (!is_order_function(it->rest))
567 new_seq.push_back(expair(it->rest * other, it->coeff));
569 new_seq.push_back(*it);
572 return pseries(relational(var,point), new_seq);
576 /** Multiply one pseries object to another, producing a pseries object that
577 * represents the product.
579 * @param other pseries object to multiply with
580 * @return the product as a pseries */
581 ex pseries::mul_series(const pseries &other) const
583 // Multiplying two series with different variables or expansion points
584 // results in an empty (constant) series
585 if (!is_compatible_to(other)) {
587 nul.push_back(expair(Order(_ex1()), _ex0()));
588 return pseries(relational(var,point), nul);
591 // Series multiplication
594 const symbol *s = static_cast<symbol *>(var.bp);
595 int a_max = degree(*s);
596 int b_max = other.degree(*s);
597 int a_min = ldegree(*s);
598 int b_min = other.ldegree(*s);
599 int cdeg_min = a_min + b_min;
600 int cdeg_max = a_max + b_max;
602 int higher_order_a = INT_MAX;
603 int higher_order_b = INT_MAX;
604 if (is_order_function(coeff(*s, a_max)))
605 higher_order_a = a_max + b_min;
606 if (is_order_function(other.coeff(*s, b_max)))
607 higher_order_b = b_max + a_min;
608 int higher_order_c = min(higher_order_a, higher_order_b);
609 if (cdeg_max >= higher_order_c)
610 cdeg_max = higher_order_c - 1;
612 for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
614 // c(i)=a(0)b(i)+...+a(i)b(0)
615 for (int i=a_min; cdeg-i>=b_min; i++) {
616 ex a_coeff = coeff(*s, i);
617 ex b_coeff = other.coeff(*s, cdeg-i);
618 if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
619 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
622 new_seq.push_back(expair(co, numeric(cdeg)));
624 if (higher_order_c < INT_MAX)
625 new_seq.push_back(expair(Order(_ex1()), numeric(higher_order_c)));
626 return pseries(relational(var,point), new_seq);
630 /** Implementation of ex::series() for product. This performs series
631 * multiplication when multiplying series.
633 ex mul::series(const relational & r, int order) const
635 ex acc; // Series accumulator
637 // Get first term from overall_coeff
638 acc = overall_coeff.series(r, order);
640 // Multiply with remaining terms
641 epvector::const_iterator it = seq.begin();
642 epvector::const_iterator itend = seq.end();
643 for (; it!=itend; it++) {
645 if (op.info(info_flags::numeric)) {
646 // series * const (special case, faster)
647 ex f = power(op, it->coeff);
648 acc = ex_to_pseries(acc).mul_const(ex_to_numeric(f));
650 } else if (!is_ex_exactly_of_type(op, pseries))
651 op = op.series(r, order);
652 if (!it->coeff.is_equal(_ex1()))
653 op = ex_to_pseries(op).power_const(ex_to_numeric(it->coeff), order);
655 // Series multiplication
656 acc = ex_to_pseries(acc).mul_series(ex_to_pseries(op));
662 /** Compute the p-th power of a series.
664 * @param p power to compute
665 * @param deg truncation order of series calculation */
666 ex pseries::power_const(const numeric &p, int deg) const
669 const symbol *s = static_cast<symbol *>(var.bp);
670 int ldeg = ldegree(*s);
672 // Calculate coefficients of powered series
676 co.push_back(co0 = power(coeff(*s, ldeg), p));
677 bool all_sums_zero = true;
678 for (i=1; i<deg; i++) {
680 for (int j=1; j<=i; j++) {
681 ex c = coeff(*s, j + ldeg);
682 if (is_order_function(c)) {
683 co.push_back(Order(_ex1()));
686 sum += (p * j - (i - j)) * co[i - j] * c;
689 all_sums_zero = false;
690 co.push_back(co0 * sum / numeric(i));
693 // Construct new series (of non-zero coefficients)
695 bool higher_order = false;
696 for (i=0; i<deg; i++) {
697 if (!co[i].is_zero())
698 new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
699 if (is_order_function(co[i])) {
704 if (!higher_order && !all_sums_zero)
705 new_seq.push_back(expair(Order(_ex1()), numeric(deg) + p * ldeg));
706 return pseries(relational(var,point), new_seq);
710 /** Implementation of ex::series() for powers. This performs Laurent expansion
711 * of reciprocals of series at singularities.
713 ex power::series(const relational & r, int order) const
716 if (!is_ex_exactly_of_type(basis, pseries)) {
717 // Basis is not a series, may there be a singulary?
718 if (!exponent.info(info_flags::negint))
719 return basic::series(r, order);
721 // Expression is of type something^(-int), check for singularity
722 if (!basis.subs(r).is_zero())
723 return basic::series(r, order);
725 // Singularity encountered, expand basis into series
726 e = basis.series(r, order);
733 return ex_to_pseries(e).power_const(ex_to_numeric(exponent), order);
737 /** Re-expansion of a pseries object. */
738 ex pseries::series(const relational & r, int order) const
740 const ex p = r.rhs();
741 GINAC_ASSERT(is_ex_exactly_of_type(r.lhs(),symbol));
742 const symbol *s = static_cast<symbol *>(r.lhs().bp);
744 if (var.is_equal(*s) && point.is_equal(p)) {
745 if (order > degree(*s))
749 epvector::const_iterator it = seq.begin(), itend = seq.end();
750 while (it != itend) {
751 int o = ex_to_numeric(it->coeff).to_int();
753 new_seq.push_back(expair(Order(_ex1()), o));
756 new_seq.push_back(*it);
759 return pseries(r, new_seq);
762 return convert_to_poly().series(r, order);
766 /** Compute the truncated series expansion of an expression.
767 * This function returns an expression containing an object of class pseries
768 * to represent the series. If the series does not terminate within the given
769 * truncation order, the last term of the series will be an order term.
771 * @param r expansion relation, lhs holds variable and rhs holds point
772 * @param order truncation order of series calculations
773 * @return an expression holding a pseries object */
774 ex ex::series(const ex & r, int order) const
780 if (is_ex_exactly_of_type(r,relational))
781 rel_ = ex_to_relational(r);
782 else if (is_ex_exactly_of_type(r,symbol))
783 rel_ = relational(r,_ex0());
785 throw (std::logic_error("ex::series(): expansion point has unknown type"));
788 e = bp->series(rel_, order);
789 } catch (exception &x) {
790 throw (std::logic_error(string("unable to compute series (") + x.what() + ")"));
797 const pseries some_pseries;
798 const type_info & typeid_pseries = typeid(some_pseries);
800 #ifndef NO_NAMESPACE_GINAC
802 #endif // ndef NO_NAMESPACE_GINAC