3 * Implementation of class for extended truncated power-series and
4 * methods for series expansion.
6 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "relational.h"
33 * Default constructor, destructor, copy constructor, assignment operator and helpers
36 series::series() : basic(TINFO_series)
38 debugmsg("series default constructor", LOGLEVEL_CONSTRUCT);
43 debugmsg("series destructor", LOGLEVEL_DESTRUCT);
47 series::series(series const &other)
49 debugmsg("series copy constructor", LOGLEVEL_CONSTRUCT);
53 series const &series::operator=(series const & other)
55 debugmsg("series operator=", LOGLEVEL_ASSIGNMENT);
63 void series::copy(series const &other)
65 inherited::copy(other);
71 void series::destroy(bool call_parent)
74 inherited::destroy(call_parent);
82 /** Construct series from a vector of coefficients and powers.
83 * expair.rest holds the coefficient, expair.coeff holds the power.
84 * The powers must be integers (positive or negative) and in ascending order;
85 * the last coefficient can be Order(exONE()) to represent a truncated,
86 * non-terminating series.
88 * @param var_ series variable (must hold a symbol)
89 * @param point_ expansion point
90 * @param ops_ vector of {coefficient, power} pairs (coefficient must not be zero)
91 * @return newly constructed series */
92 series::series(ex const &var_, ex const &point_, epvector const &ops_)
93 : basic(TINFO_series), seq(ops_), var(var_), point(point_)
95 debugmsg("series constructor from ex,ex,epvector", LOGLEVEL_CONSTRUCT);
96 ASSERT(is_ex_exactly_of_type(var_, symbol));
101 * Functions overriding virtual functions from base classes
104 basic *series::duplicate() const
106 debugmsg("series duplicate", LOGLEVEL_DUPLICATE);
107 return new series(*this);
110 // Highest degree of variable
111 int series::degree(symbol const &s) const
113 if (var.is_equal(s)) {
114 // Return last exponent
116 return ex_to_numeric((*(seq.end() - 1)).coeff).to_int();
120 epvector::const_iterator it = seq.begin(), itend = seq.end();
123 int max_pow = INT_MIN;
124 while (it != itend) {
125 int pow = it->rest.degree(s);
134 // Lowest degree of variable
135 int series::ldegree(symbol const &s) const
137 if (var.is_equal(s)) {
138 // Return first exponent
140 return ex_to_numeric((*(seq.begin())).coeff).to_int();
144 epvector::const_iterator it = seq.begin(), itend = seq.end();
147 int min_pow = INT_MAX;
148 while (it != itend) {
149 int pow = it->rest.ldegree(s);
158 // Coefficient of variable
159 ex series::coeff(symbol const &s, int n) const
161 if (var.is_equal(s)) {
162 epvector::const_iterator it = seq.begin(), itend = seq.end();
163 while (it != itend) {
164 int pow = ex_to_numeric(it->coeff).to_int();
173 return convert_to_poly().coeff(s, n);
176 ex series::eval(int level) const
181 // Construct a new series with evaluated coefficients
183 new_seq.reserve(seq.size());
184 epvector::const_iterator it = seq.begin(), itend = seq.end();
185 while (it != itend) {
186 new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
189 return (new series(var, point, new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
192 ex series::evalf(int level) const
194 return convert_to_poly().evalf(level);
199 * Construct expression (polynomial) out of series
202 /** Convert a series object to an ordinary polynomial.
204 * @param no_order flag: discard higher order terms */
205 ex series::convert_to_poly(bool no_order) const
208 epvector::const_iterator it = seq.begin(), itend = seq.end();
210 while (it != itend) {
211 if (is_order_function(it->rest)) {
213 e += Order(power(var - point, it->coeff));
215 e += it->rest * power(var - point, it->coeff);
223 * Implementation of series expansion
226 /** Default implementation of ex::series(). This performs Taylor expansion.
228 ex basic::series(symbol const & s, ex const & point, int order) const
233 ex coeff = deriv.subs(s == point);
234 if (!coeff.is_zero())
235 seq.push_back(expair(coeff, numeric(0)));
238 for (n=1; n<order; n++) {
239 fac = fac.mul(numeric(n));
240 deriv = deriv.diff(s).expand();
241 if (deriv.is_zero()) {
243 return series::series(s, point, seq);
245 coeff = power(fac, -1) * deriv.subs(s == point);
246 if (!coeff.is_zero())
247 seq.push_back(expair(coeff, numeric(n)));
250 // Higher-order terms, if present
251 deriv = deriv.diff(s);
252 if (!deriv.is_zero())
253 seq.push_back(expair(Order(exONE()), numeric(n)));
254 return series::series(s, point, seq);
258 /** Add one series object to another, producing a series object that represents
261 * @param other series object to add with
262 * @return the sum as a series */
263 ex series::add_series(const series &other) const
265 // Adding two series with different variables or expansion points
266 // results in an empty (constant) series
267 if (!is_compatible_to(other)) {
269 nul.push_back(expair(Order(exONE()), exZERO()));
270 return series(var, point, nul);
275 epvector::const_iterator a = seq.begin();
276 epvector::const_iterator b = other.seq.begin();
277 epvector::const_iterator a_end = seq.end();
278 epvector::const_iterator b_end = other.seq.end();
279 int pow_a = INT_MAX, pow_b = INT_MAX;
281 // If a is empty, fill up with elements from b and stop
284 new_seq.push_back(*b);
289 pow_a = ex_to_numeric((*a).coeff).to_int();
291 // If b is empty, fill up with elements from a and stop
294 new_seq.push_back(*a);
299 pow_b = ex_to_numeric((*b).coeff).to_int();
301 // a and b are non-empty, compare powers
303 // a has lesser power, get coefficient from a
304 new_seq.push_back(*a);
305 if (is_order_function((*a).rest))
308 } else if (pow_b < pow_a) {
309 // b has lesser power, get coefficient from b
310 new_seq.push_back(*b);
311 if (is_order_function((*b).rest))
315 // Add coefficient of a and b
316 if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
317 new_seq.push_back(expair(Order(exONE()), (*a).coeff));
318 break; // Order term ends the sequence
320 ex sum = (*a).rest + (*b).rest;
321 if (!(sum.is_zero()))
322 new_seq.push_back(expair(sum, numeric(pow_a)));
328 return series(var, point, new_seq);
332 /** Implementation of ex::series() for sums. This performs series addition when
333 * adding series objects.
336 ex add::series(symbol const & s, ex const & point, int order) const
338 ex acc; // Series accumulator
341 epvector::const_iterator it = seq.begin();
342 epvector::const_iterator itend = seq.end();
344 if (is_ex_exactly_of_type(it->rest, series))
347 acc = it->rest.series(s, point, order);
348 if (!it->coeff.is_equal(exONE()))
349 acc = ex_to_series(acc).mul_const(ex_to_numeric(it->coeff));
353 // Add remaining terms
354 for (; it!=itend; it++) {
356 if (is_ex_exactly_of_type(it->rest, series))
359 op = it->rest.series(s, point, order);
360 if (!it->coeff.is_equal(exONE()))
361 op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
364 acc = ex_to_series(acc).add_series(ex_to_series(op));
369 ex add::series(symbol const & s, ex const & point, int order) const
371 ex acc; // Series accumulator
373 // Get first term from overall_coeff
374 acc = overall_coeff.series(s,point,order);
376 // Add remaining terms
377 epvector::const_iterator it = seq.begin();
378 epvector::const_iterator itend = seq.end();
379 for (; it!=itend; it++) {
381 if (is_ex_exactly_of_type(it->rest, series))
384 op = it->rest.series(s, point, order);
385 if (!it->coeff.is_equal(exONE()))
386 op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
389 acc = ex_to_series(acc).add_series(ex_to_series(op));
395 /** Multiply a series object with a numeric constant, producing a series object
396 * that represents the product.
398 * @param other constant to multiply with
399 * @return the product as a series */
400 ex series::mul_const(const numeric &other) const
403 new_seq.reserve(seq.size());
405 epvector::const_iterator it = seq.begin(), itend = seq.end();
406 while (it != itend) {
407 if (!is_order_function(it->rest))
408 new_seq.push_back(expair(it->rest * other, it->coeff));
410 new_seq.push_back(*it);
413 return series(var, point, new_seq);
417 /** Multiply one series object to another, producing a series object that
418 * represents the product.
420 * @param other series object to multiply with
421 * @return the product as a series */
422 ex series::mul_series(const series &other) const
424 // Multiplying two series with different variables or expansion points
425 // results in an empty (constant) series
426 if (!is_compatible_to(other)) {
428 nul.push_back(expair(Order(exONE()), exZERO()));
429 return series(var, point, nul);
432 // Series multiplication
435 const symbol *s = static_cast<symbol *>(var.bp);
436 int a_max = degree(*s);
437 int b_max = other.degree(*s);
438 int a_min = ldegree(*s);
439 int b_min = other.ldegree(*s);
440 int cdeg_min = a_min + b_min;
441 int cdeg_max = a_max + b_max;
443 int higher_order_a = INT_MAX;
444 int higher_order_b = INT_MAX;
445 if (is_order_function(coeff(*s, a_max)))
446 higher_order_a = a_max + b_min;
447 if (is_order_function(other.coeff(*s, b_max)))
448 higher_order_b = b_max + a_min;
449 int higher_order_c = min(higher_order_a, higher_order_b);
450 if (cdeg_max >= higher_order_c)
451 cdeg_max = higher_order_c - 1;
453 for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
455 // c(i)=a(0)b(i)+...+a(i)b(0)
456 for (int i=a_min; cdeg-i>=b_min; i++) {
457 ex a_coeff = coeff(*s, i);
458 ex b_coeff = other.coeff(*s, cdeg-i);
459 if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
460 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
463 new_seq.push_back(expair(co, numeric(cdeg)));
465 if (higher_order_c < INT_MAX)
466 new_seq.push_back(expair(Order(exONE()), numeric(higher_order_c)));
467 return series::series(var, point, new_seq);
471 /** Implementation of ex::series() for product. This performs series multiplication when multiplying series.
474 ex mul::series(symbol const & s, ex const & point, int order) const
476 ex acc; // Series accumulator
479 epvector::const_iterator it = seq.begin();
480 epvector::const_iterator itend = seq.end();
482 if (is_ex_exactly_of_type(it->rest, series))
485 acc = it->rest.series(s, point, order);
486 if (!it->coeff.is_equal(exONE()))
487 acc = ex_to_series(acc).power_const(ex_to_numeric(it->coeff), order);
491 // Multiply with remaining terms
492 for (; it!=itend; it++) {
494 if (op.info(info_flags::numeric)) {
495 // series * const (special case, faster)
496 ex f = power(op, it->coeff);
497 acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
499 } else if (!is_ex_exactly_of_type(op, series))
500 op = op.series(s, point, order);
501 if (!it->coeff.is_equal(exONE()))
502 op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
504 // Series multiplication
505 acc = ex_to_series(acc).mul_series(ex_to_series(op));
511 ex mul::series(symbol const & s, ex const & point, int order) const
513 ex acc; // Series accumulator
515 // Get first term from overall_coeff
516 acc = overall_coeff.series(s, point, order);
518 // Multiply with remaining terms
519 epvector::const_iterator it = seq.begin();
520 epvector::const_iterator itend = seq.end();
521 for (; it!=itend; it++) {
523 if (op.info(info_flags::numeric)) {
524 // series * const (special case, faster)
525 ex f = power(op, it->coeff);
526 acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
528 } else if (!is_ex_exactly_of_type(op, series))
529 op = op.series(s, point, order);
530 if (!it->coeff.is_equal(exONE()))
531 op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
533 // Series multiplication
534 acc = ex_to_series(acc).mul_series(ex_to_series(op));
540 /** Compute the p-th power of a series.
542 * @param p power to compute
543 * @param deg truncation order of series calculation */
544 ex series::power_const(const numeric &p, int deg) const
547 const symbol *s = static_cast<symbol *>(var.bp);
548 int ldeg = ldegree(*s);
550 // Calculate coefficients of powered series
554 co.push_back(co0 = power(coeff(*s, ldeg), p));
555 bool all_sums_zero = true;
556 for (i=1; i<deg; i++) {
558 for (int j=1; j<=i; j++) {
559 ex c = coeff(*s, j + ldeg);
560 if (is_order_function(c)) {
561 co.push_back(Order(exONE()));
564 sum += (p * j - (i - j)) * co[i - j] * c;
567 all_sums_zero = false;
568 co.push_back(co0 * sum / numeric(i));
571 // Construct new series (of non-zero coefficients)
573 bool higher_order = false;
574 for (i=0; i<deg; i++) {
575 if (!co[i].is_zero())
576 new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
577 if (is_order_function(co[i])) {
582 if (!higher_order && !all_sums_zero)
583 new_seq.push_back(expair(Order(exONE()), numeric(deg) + p * ldeg));
584 return series::series(var, point, new_seq);
588 /** Implementation of ex::series() for powers. This performs Laurent expansion
589 * of reciprocals of series at singularities.
591 ex power::series(symbol const & s, ex const & point, int order) const
594 if (!is_ex_exactly_of_type(basis, series)) {
595 // Basis is not a series, may there be a singulary?
596 if (!exponent.info(info_flags::negint))
597 return basic::series(s, point, order);
599 // Expression is of type something^(-int), check for singularity
600 if (!basis.subs(s == point).is_zero())
601 return basic::series(s, point, order);
603 // Singularity encountered, expand basis into series
604 e = basis.series(s, point, order);
611 return ex_to_series(e).power_const(ex_to_numeric(exponent), order);
615 /** Compute the truncated series expansion of an expression.
616 * This function returns an expression containing an object of class series to
617 * represent the series. If the series does not terminate within the given
618 * truncation order, the last term of the series will be an order term.
620 * @param s expansion variable
621 * @param point expansion point
622 * @param order truncation order of series calculations
623 * @return an expression holding a series object */
624 ex ex::series(symbol const &s, ex const &point, int order) const
627 return bp->series(s, point, order);
632 const series some_series;
633 type_info const & typeid_series = typeid(some_series);