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
30 #include "relational.h"
36 #ifndef NO_GINAC_NAMESPACE
38 #endif // ndef NO_GINAC_NAMESPACE
40 GINAC_IMPLEMENT_REGISTERED_CLASS(pseries, basic)
43 * Default constructor, destructor, copy constructor, assignment operator and helpers
46 pseries::pseries() : basic(TINFO_pseries)
48 debugmsg("pseries default constructor", LOGLEVEL_CONSTRUCT);
53 debugmsg("pseries destructor", LOGLEVEL_DESTRUCT);
57 pseries::pseries(pseries const &other)
59 debugmsg("pseries copy constructor", LOGLEVEL_CONSTRUCT);
63 pseries const &pseries::operator=(pseries const & other)
65 debugmsg("pseries operator=", LOGLEVEL_ASSIGNMENT);
73 void pseries::copy(pseries const &other)
75 inherited::copy(other);
81 void pseries::destroy(bool call_parent)
84 inherited::destroy(call_parent);
92 /** Construct pseries from a vector of coefficients and powers.
93 * expair.rest holds the coefficient, expair.coeff holds the power.
94 * The powers must be integers (positive or negative) and in ascending order;
95 * the last coefficient can be Order(_ex1()) to represent a truncated,
96 * non-terminating series.
98 * @param var_ series variable (must hold a symbol)
99 * @param point_ expansion point
100 * @param ops_ vector of {coefficient, power} pairs (coefficient must not be zero)
101 * @return newly constructed pseries */
102 pseries::pseries(ex const &var_, ex const &point_, epvector const &ops_)
103 : basic(TINFO_pseries), seq(ops_), var(var_), point(point_)
105 debugmsg("pseries constructor from ex,ex,epvector", LOGLEVEL_CONSTRUCT);
106 GINAC_ASSERT(is_ex_exactly_of_type(var_, symbol));
114 /** Construct object from archive_node. */
115 pseries::pseries(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
117 debugmsg("pseries constructor from archive_node", LOGLEVEL_CONSTRUCT);
118 for (unsigned int i=0; true; i++) {
121 if (n.find_ex("coeff", rest, sym_lst, i) && n.find_ex("power", coeff, sym_lst, i))
122 seq.push_back(expair(rest, coeff));
126 n.find_ex("var", var, sym_lst);
127 n.find_ex("point", point, sym_lst);
130 /** Unarchive the object. */
131 ex pseries::unarchive(const archive_node &n, const lst &sym_lst)
133 return (new pseries(n, sym_lst))->setflag(status_flags::dynallocated);
136 /** Archive the object. */
137 void pseries::archive(archive_node &n) const
139 inherited::archive(n);
140 epvector::const_iterator i = seq.begin(), iend = seq.end();
142 n.add_ex("coeff", i->rest);
143 n.add_ex("power", i->coeff);
146 n.add_ex("var", var);
147 n.add_ex("point", point);
152 * Functions overriding virtual functions from base classes
155 basic *pseries::duplicate() const
157 debugmsg("pseries duplicate", LOGLEVEL_DUPLICATE);
158 return new pseries(*this);
161 void pseries::print(ostream &os, unsigned upper_precedence) const
163 debugmsg("symbol print", LOGLEVEL_PRINT);
164 convert_to_poly().print(os, upper_precedence);
167 void pseries::printraw(ostream &os) const
169 debugmsg("symbol printraw", LOGLEVEL_PRINT);
170 os << "pseries(" << var << ";" << point << ";";
171 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); i++) {
172 os << "(" << (*i).rest << "," << (*i).coeff << "),";
177 int pseries::degree(symbol const &s) const
179 if (var.is_equal(s)) {
180 // Return last exponent
182 return ex_to_numeric((*(seq.end() - 1)).coeff).to_int();
186 epvector::const_iterator it = seq.begin(), itend = seq.end();
189 int max_pow = INT_MIN;
190 while (it != itend) {
191 int pow = it->rest.degree(s);
200 int pseries::ldegree(symbol const &s) const
202 if (var.is_equal(s)) {
203 // Return first exponent
205 return ex_to_numeric((*(seq.begin())).coeff).to_int();
209 epvector::const_iterator it = seq.begin(), itend = seq.end();
212 int min_pow = INT_MAX;
213 while (it != itend) {
214 int pow = it->rest.ldegree(s);
223 ex pseries::coeff(symbol const &s, int const n) const
225 if (var.is_equal(s)) {
226 epvector::const_iterator it = seq.begin(), itend = seq.end();
227 while (it != itend) {
228 int pow = ex_to_numeric(it->coeff).to_int();
237 return convert_to_poly().coeff(s, n);
240 ex pseries::eval(int level) const
245 // Construct a new series with evaluated coefficients
247 new_seq.reserve(seq.size());
248 epvector::const_iterator it = seq.begin(), itend = seq.end();
249 while (it != itend) {
250 new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
253 return (new pseries(var, point, new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
256 /** Evaluate numerically. The order term is dropped. */
257 ex pseries::evalf(int level) const
259 return convert_to_poly().evalf(level);
262 ex pseries::subs(lst const & ls, lst const & lr) const
264 // If expansion variable is being substituted, convert the series to a
265 // polynomial and do the substitution there because the result might
266 // no longer be a power series
268 return convert_to_poly(true).subs(ls, lr);
270 // Otherwise construct a new series with substituted coefficients and
273 new_seq.reserve(seq.size());
274 epvector::const_iterator it = seq.begin(), itend = seq.end();
275 while (it != itend) {
276 new_seq.push_back(expair(it->rest.subs(ls, lr), it->coeff));
279 return (new pseries(var, point.subs(ls, lr), new_seq))->setflag(status_flags::dynallocated);
284 * Construct ordinary polynomial out of series
287 /** Convert a pseries object to an ordinary polynomial.
289 * @param no_order flag: discard higher order terms */
290 ex pseries::convert_to_poly(bool no_order) const
293 epvector::const_iterator it = seq.begin(), itend = seq.end();
295 while (it != itend) {
296 if (is_order_function(it->rest)) {
298 e += Order(power(var - point, it->coeff));
300 e += it->rest * power(var - point, it->coeff);
308 * Implementation of series expansion
311 /** Default implementation of ex::series(). This performs Taylor expansion.
313 ex basic::series(symbol const & s, ex const & point, int order) const
318 ex coeff = deriv.subs(s == point);
319 if (!coeff.is_zero())
320 seq.push_back(expair(coeff, numeric(0)));
323 for (n=1; n<order; n++) {
324 fac = fac.mul(numeric(n));
325 deriv = deriv.diff(s).expand();
326 if (deriv.is_zero()) {
328 return pseries(s, point, seq);
330 coeff = fac.inverse() * deriv.subs(s == point);
331 if (!coeff.is_zero())
332 seq.push_back(expair(coeff, numeric(n)));
335 // Higher-order terms, if present
336 deriv = deriv.diff(s);
337 if (!deriv.is_zero())
338 seq.push_back(expair(Order(_ex1()), numeric(n)));
339 return pseries(s, point, seq);
343 /** Implementation of ex::series() for symbols.
345 ex symbol::series(symbol const & s, ex const & point, int order) const
349 if (order > 0 && !point.is_zero())
350 seq.push_back(expair(point, _ex0()));
352 seq.push_back(expair(_ex1(), _ex1()));
354 seq.push_back(expair(Order(_ex1()), numeric(order)));
356 seq.push_back(expair(*this, _ex0()));
357 return pseries(s, point, seq);
361 /** Add one series object to another, producing a pseries object that represents
364 * @param other pseries object to add with
365 * @return the sum as a pseries */
366 ex pseries::add_series(const pseries &other) const
368 // Adding two series with different variables or expansion points
369 // results in an empty (constant) series
370 if (!is_compatible_to(other)) {
372 nul.push_back(expair(Order(_ex1()), _ex0()));
373 return pseries(var, point, nul);
378 epvector::const_iterator a = seq.begin();
379 epvector::const_iterator b = other.seq.begin();
380 epvector::const_iterator a_end = seq.end();
381 epvector::const_iterator b_end = other.seq.end();
382 int pow_a = INT_MAX, pow_b = INT_MAX;
384 // If a is empty, fill up with elements from b and stop
387 new_seq.push_back(*b);
392 pow_a = ex_to_numeric((*a).coeff).to_int();
394 // If b is empty, fill up with elements from a and stop
397 new_seq.push_back(*a);
402 pow_b = ex_to_numeric((*b).coeff).to_int();
404 // a and b are non-empty, compare powers
406 // a has lesser power, get coefficient from a
407 new_seq.push_back(*a);
408 if (is_order_function((*a).rest))
411 } else if (pow_b < pow_a) {
412 // b has lesser power, get coefficient from b
413 new_seq.push_back(*b);
414 if (is_order_function((*b).rest))
418 // Add coefficient of a and b
419 if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
420 new_seq.push_back(expair(Order(_ex1()), (*a).coeff));
421 break; // Order term ends the sequence
423 ex sum = (*a).rest + (*b).rest;
424 if (!(sum.is_zero()))
425 new_seq.push_back(expair(sum, numeric(pow_a)));
431 return pseries(var, point, new_seq);
435 /** Implementation of ex::series() for sums. This performs series addition when
436 * adding pseries objects.
438 ex add::series(symbol const & s, ex const & point, int order) const
440 ex acc; // Series accumulator
442 // Get first term from overall_coeff
443 acc = overall_coeff.series(s, point, order);
445 // Add remaining terms
446 epvector::const_iterator it = seq.begin();
447 epvector::const_iterator itend = seq.end();
448 for (; it!=itend; it++) {
450 if (is_ex_exactly_of_type(it->rest, pseries))
453 op = it->rest.series(s, point, order);
454 if (!it->coeff.is_equal(_ex1()))
455 op = ex_to_pseries(op).mul_const(ex_to_numeric(it->coeff));
458 acc = ex_to_pseries(acc).add_series(ex_to_pseries(op));
464 /** Multiply a pseries object with a numeric constant, producing a pseries object
465 * that represents the product.
467 * @param other constant to multiply with
468 * @return the product as a pseries */
469 ex pseries::mul_const(const numeric &other) const
472 new_seq.reserve(seq.size());
474 epvector::const_iterator it = seq.begin(), itend = seq.end();
475 while (it != itend) {
476 if (!is_order_function(it->rest))
477 new_seq.push_back(expair(it->rest * other, it->coeff));
479 new_seq.push_back(*it);
482 return pseries(var, point, new_seq);
486 /** Multiply one pseries object to another, producing a pseries object that
487 * represents the product.
489 * @param other pseries object to multiply with
490 * @return the product as a pseries */
491 ex pseries::mul_series(const pseries &other) const
493 // Multiplying two series with different variables or expansion points
494 // results in an empty (constant) series
495 if (!is_compatible_to(other)) {
497 nul.push_back(expair(Order(_ex1()), _ex0()));
498 return pseries(var, point, nul);
501 // Series multiplication
504 const symbol *s = static_cast<symbol *>(var.bp);
505 int a_max = degree(*s);
506 int b_max = other.degree(*s);
507 int a_min = ldegree(*s);
508 int b_min = other.ldegree(*s);
509 int cdeg_min = a_min + b_min;
510 int cdeg_max = a_max + b_max;
512 int higher_order_a = INT_MAX;
513 int higher_order_b = INT_MAX;
514 if (is_order_function(coeff(*s, a_max)))
515 higher_order_a = a_max + b_min;
516 if (is_order_function(other.coeff(*s, b_max)))
517 higher_order_b = b_max + a_min;
518 int higher_order_c = min(higher_order_a, higher_order_b);
519 if (cdeg_max >= higher_order_c)
520 cdeg_max = higher_order_c - 1;
522 for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
524 // c(i)=a(0)b(i)+...+a(i)b(0)
525 for (int i=a_min; cdeg-i>=b_min; i++) {
526 ex a_coeff = coeff(*s, i);
527 ex b_coeff = other.coeff(*s, cdeg-i);
528 if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
529 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
532 new_seq.push_back(expair(co, numeric(cdeg)));
534 if (higher_order_c < INT_MAX)
535 new_seq.push_back(expair(Order(_ex1()), numeric(higher_order_c)));
536 return pseries(var, point, new_seq);
540 /** Implementation of ex::series() for product. This performs series
541 * multiplication when multiplying series.
543 ex mul::series(symbol const & s, ex const & point, int order) const
545 ex acc; // Series accumulator
547 // Get first term from overall_coeff
548 acc = overall_coeff.series(s, point, order);
550 // Multiply with remaining terms
551 epvector::const_iterator it = seq.begin();
552 epvector::const_iterator itend = seq.end();
553 for (; it!=itend; it++) {
555 if (op.info(info_flags::numeric)) {
556 // series * const (special case, faster)
557 ex f = power(op, it->coeff);
558 acc = ex_to_pseries(acc).mul_const(ex_to_numeric(f));
560 } else if (!is_ex_exactly_of_type(op, pseries))
561 op = op.series(s, point, order);
562 if (!it->coeff.is_equal(_ex1()))
563 op = ex_to_pseries(op).power_const(ex_to_numeric(it->coeff), order);
565 // Series multiplication
566 acc = ex_to_pseries(acc).mul_series(ex_to_pseries(op));
572 /** Compute the p-th power of a series.
574 * @param p power to compute
575 * @param deg truncation order of series calculation */
576 ex pseries::power_const(const numeric &p, int deg) const
579 const symbol *s = static_cast<symbol *>(var.bp);
580 int ldeg = ldegree(*s);
582 // Calculate coefficients of powered series
586 co.push_back(co0 = power(coeff(*s, ldeg), p));
587 bool all_sums_zero = true;
588 for (i=1; i<deg; i++) {
590 for (int j=1; j<=i; j++) {
591 ex c = coeff(*s, j + ldeg);
592 if (is_order_function(c)) {
593 co.push_back(Order(_ex1()));
596 sum += (p * j - (i - j)) * co[i - j] * c;
599 all_sums_zero = false;
600 co.push_back(co0 * sum / numeric(i));
603 // Construct new series (of non-zero coefficients)
605 bool higher_order = false;
606 for (i=0; i<deg; i++) {
607 if (!co[i].is_zero())
608 new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
609 if (is_order_function(co[i])) {
614 if (!higher_order && !all_sums_zero)
615 new_seq.push_back(expair(Order(_ex1()), numeric(deg) + p * ldeg));
616 return pseries(var, point, new_seq);
620 /** Implementation of ex::series() for powers. This performs Laurent expansion
621 * of reciprocals of series at singularities.
623 ex power::series(symbol const & s, ex const & point, int order) const
626 if (!is_ex_exactly_of_type(basis, pseries)) {
627 // Basis is not a series, may there be a singulary?
628 if (!exponent.info(info_flags::negint))
629 return basic::series(s, point, order);
631 // Expression is of type something^(-int), check for singularity
632 if (!basis.subs(s == point).is_zero())
633 return basic::series(s, point, order);
635 // Singularity encountered, expand basis into series
636 e = basis.series(s, point, order);
643 return ex_to_pseries(e).power_const(ex_to_numeric(exponent), order);
647 /** Compute the truncated series expansion of an expression.
648 * This function returns an expression containing an object of class pseries to
649 * represent the series. If the series does not terminate within the given
650 * truncation order, the last term of the series will be an order term.
652 * @param s expansion variable
653 * @param point expansion point
654 * @param order truncation order of series calculations
655 * @return an expression holding a pseries object */
656 ex ex::series(symbol const &s, ex const &point, int order) const
659 return bp->series(s, point, order);
664 const pseries some_pseries;
665 type_info const & typeid_pseries = typeid(some_pseries);
667 #ifndef NO_GINAC_NAMESPACE
669 #endif // ndef NO_GINAC_NAMESPACE