3 * Implementation of class for extended truncated power-series and
4 * methods for series expansion. */
7 * GiNaC Copyright (C) 1999 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
29 #include "relational.h"
37 * Default constructor, destructor, copy constructor, assignment operator and helpers
40 series::series() : basic(TINFO_series)
42 debugmsg("series default constructor", LOGLEVEL_CONSTRUCT);
47 debugmsg("series destructor", LOGLEVEL_DESTRUCT);
51 series::series(series const &other)
53 debugmsg("series copy constructor", LOGLEVEL_CONSTRUCT);
57 series const &series::operator=(series const & other)
59 debugmsg("series operator=", LOGLEVEL_ASSIGNMENT);
67 void series::copy(series const &other)
69 inherited::copy(other);
75 void series::destroy(bool call_parent)
78 inherited::destroy(call_parent);
86 /** Construct series from a vector of coefficients and powers.
87 * expair.rest holds the coefficient, expair.coeff holds the power.
88 * The powers must be integers (positive or negative) and in ascending order;
89 * the last coefficient can be Order(exONE()) to represent a truncated,
90 * non-terminating series.
92 * @param var_ series variable (must hold a symbol)
93 * @param point_ expansion point
94 * @param ops_ vector of {coefficient, power} pairs (coefficient must not be zero)
95 * @return newly constructed series */
96 series::series(ex const &var_, ex const &point_, epvector const &ops_)
97 : basic(TINFO_series), seq(ops_), var(var_), point(point_)
99 debugmsg("series constructor from ex,ex,epvector", LOGLEVEL_CONSTRUCT);
100 GINAC_ASSERT(is_ex_exactly_of_type(var_, symbol));
105 * Functions overriding virtual functions from base classes
108 basic *series::duplicate() const
110 debugmsg("series duplicate", LOGLEVEL_DUPLICATE);
111 return new series(*this);
114 // Highest degree of variable
115 int series::degree(symbol const &s) const
117 if (var.is_equal(s)) {
118 // Return last exponent
120 return ex_to_numeric((*(seq.end() - 1)).coeff).to_int();
124 epvector::const_iterator it = seq.begin(), itend = seq.end();
127 int max_pow = INT_MIN;
128 while (it != itend) {
129 int pow = it->rest.degree(s);
138 // Lowest degree of variable
139 int series::ldegree(symbol const &s) const
141 if (var.is_equal(s)) {
142 // Return first exponent
144 return ex_to_numeric((*(seq.begin())).coeff).to_int();
148 epvector::const_iterator it = seq.begin(), itend = seq.end();
151 int min_pow = INT_MAX;
152 while (it != itend) {
153 int pow = it->rest.ldegree(s);
162 // Coefficient of variable
163 ex series::coeff(symbol const &s, int n) const
165 if (var.is_equal(s)) {
166 epvector::const_iterator it = seq.begin(), itend = seq.end();
167 while (it != itend) {
168 int pow = ex_to_numeric(it->coeff).to_int();
177 return convert_to_poly().coeff(s, n);
180 ex series::eval(int level) const
185 // Construct a new series with evaluated coefficients
187 new_seq.reserve(seq.size());
188 epvector::const_iterator it = seq.begin(), itend = seq.end();
189 while (it != itend) {
190 new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
193 return (new series(var, point, new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
196 ex series::evalf(int level) const
198 return convert_to_poly().evalf(level);
203 * Construct expression (polynomial) out of series
206 /** Convert a series object to an ordinary polynomial.
208 * @param no_order flag: discard higher order terms */
209 ex series::convert_to_poly(bool no_order) const
212 epvector::const_iterator it = seq.begin(), itend = seq.end();
214 while (it != itend) {
215 if (is_order_function(it->rest)) {
217 e += Order(power(var - point, it->coeff));
219 e += it->rest * power(var - point, it->coeff);
227 * Implementation of series expansion
230 /** Default implementation of ex::series(). This performs Taylor expansion.
232 ex basic::series(symbol const & s, ex const & point, int order) const
237 ex coeff = deriv.subs(s == point);
238 if (!coeff.is_zero())
239 seq.push_back(expair(coeff, numeric(0)));
242 for (n=1; n<order; n++) {
243 fac = fac.mul(numeric(n));
244 deriv = deriv.diff(s).expand();
245 if (deriv.is_zero()) {
247 return series::series(s, point, seq);
249 coeff = power(fac, -1) * deriv.subs(s == point);
250 if (!coeff.is_zero())
251 seq.push_back(expair(coeff, numeric(n)));
254 // Higher-order terms, if present
255 deriv = deriv.diff(s);
256 if (!deriv.is_zero())
257 seq.push_back(expair(Order(exONE()), numeric(n)));
258 return series::series(s, point, seq);
262 /** Add one series object to another, producing a series object that represents
265 * @param other series object to add with
266 * @return the sum as a series */
267 ex series::add_series(const series &other) const
269 // Adding two series with different variables or expansion points
270 // results in an empty (constant) series
271 if (!is_compatible_to(other)) {
273 nul.push_back(expair(Order(exONE()), exZERO()));
274 return series(var, point, nul);
279 epvector::const_iterator a = seq.begin();
280 epvector::const_iterator b = other.seq.begin();
281 epvector::const_iterator a_end = seq.end();
282 epvector::const_iterator b_end = other.seq.end();
283 int pow_a = INT_MAX, pow_b = INT_MAX;
285 // If a is empty, fill up with elements from b and stop
288 new_seq.push_back(*b);
293 pow_a = ex_to_numeric((*a).coeff).to_int();
295 // If b is empty, fill up with elements from a and stop
298 new_seq.push_back(*a);
303 pow_b = ex_to_numeric((*b).coeff).to_int();
305 // a and b are non-empty, compare powers
307 // a has lesser power, get coefficient from a
308 new_seq.push_back(*a);
309 if (is_order_function((*a).rest))
312 } else if (pow_b < pow_a) {
313 // b has lesser power, get coefficient from b
314 new_seq.push_back(*b);
315 if (is_order_function((*b).rest))
319 // Add coefficient of a and b
320 if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
321 new_seq.push_back(expair(Order(exONE()), (*a).coeff));
322 break; // Order term ends the sequence
324 ex sum = (*a).rest + (*b).rest;
325 if (!(sum.is_zero()))
326 new_seq.push_back(expair(sum, numeric(pow_a)));
332 return series(var, point, new_seq);
336 /** Implementation of ex::series() for sums. This performs series addition when
337 * adding series objects.
340 ex add::series(symbol const & s, ex const & point, int order) const
342 ex acc; // Series accumulator
345 epvector::const_iterator it = seq.begin();
346 epvector::const_iterator itend = seq.end();
348 if (is_ex_exactly_of_type(it->rest, series))
351 acc = it->rest.series(s, point, order);
352 if (!it->coeff.is_equal(exONE()))
353 acc = ex_to_series(acc).mul_const(ex_to_numeric(it->coeff));
357 // Add remaining terms
358 for (; it!=itend; it++) {
360 if (is_ex_exactly_of_type(it->rest, series))
363 op = it->rest.series(s, point, order);
364 if (!it->coeff.is_equal(exONE()))
365 op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
368 acc = ex_to_series(acc).add_series(ex_to_series(op));
373 ex add::series(symbol const & s, ex const & point, int order) const
375 ex acc; // Series accumulator
377 // Get first term from overall_coeff
378 acc = overall_coeff.series(s,point,order);
380 // Add remaining terms
381 epvector::const_iterator it = seq.begin();
382 epvector::const_iterator itend = seq.end();
383 for (; it!=itend; it++) {
385 if (is_ex_exactly_of_type(it->rest, series))
388 op = it->rest.series(s, point, order);
389 if (!it->coeff.is_equal(exONE()))
390 op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
393 acc = ex_to_series(acc).add_series(ex_to_series(op));
399 /** Multiply a series object with a numeric constant, producing a series object
400 * that represents the product.
402 * @param other constant to multiply with
403 * @return the product as a series */
404 ex series::mul_const(const numeric &other) const
407 new_seq.reserve(seq.size());
409 epvector::const_iterator it = seq.begin(), itend = seq.end();
410 while (it != itend) {
411 if (!is_order_function(it->rest))
412 new_seq.push_back(expair(it->rest * other, it->coeff));
414 new_seq.push_back(*it);
417 return series(var, point, new_seq);
421 /** Multiply one series object to another, producing a series object that
422 * represents the product.
424 * @param other series object to multiply with
425 * @return the product as a series */
426 ex series::mul_series(const series &other) const
428 // Multiplying two series with different variables or expansion points
429 // results in an empty (constant) series
430 if (!is_compatible_to(other)) {
432 nul.push_back(expair(Order(exONE()), exZERO()));
433 return series(var, point, nul);
436 // Series multiplication
439 const symbol *s = static_cast<symbol *>(var.bp);
440 int a_max = degree(*s);
441 int b_max = other.degree(*s);
442 int a_min = ldegree(*s);
443 int b_min = other.ldegree(*s);
444 int cdeg_min = a_min + b_min;
445 int cdeg_max = a_max + b_max;
447 int higher_order_a = INT_MAX;
448 int higher_order_b = INT_MAX;
449 if (is_order_function(coeff(*s, a_max)))
450 higher_order_a = a_max + b_min;
451 if (is_order_function(other.coeff(*s, b_max)))
452 higher_order_b = b_max + a_min;
453 int higher_order_c = min(higher_order_a, higher_order_b);
454 if (cdeg_max >= higher_order_c)
455 cdeg_max = higher_order_c - 1;
457 for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
459 // c(i)=a(0)b(i)+...+a(i)b(0)
460 for (int i=a_min; cdeg-i>=b_min; i++) {
461 ex a_coeff = coeff(*s, i);
462 ex b_coeff = other.coeff(*s, cdeg-i);
463 if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
464 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
467 new_seq.push_back(expair(co, numeric(cdeg)));
469 if (higher_order_c < INT_MAX)
470 new_seq.push_back(expair(Order(exONE()), numeric(higher_order_c)));
471 return series::series(var, point, new_seq);
475 /** Implementation of ex::series() for product. This performs series multiplication when multiplying series.
478 ex mul::series(symbol const & s, ex const & point, int order) const
480 ex acc; // Series accumulator
483 epvector::const_iterator it = seq.begin();
484 epvector::const_iterator itend = seq.end();
486 if (is_ex_exactly_of_type(it->rest, series))
489 acc = it->rest.series(s, point, order);
490 if (!it->coeff.is_equal(exONE()))
491 acc = ex_to_series(acc).power_const(ex_to_numeric(it->coeff), order);
495 // Multiply with remaining terms
496 for (; it!=itend; it++) {
498 if (op.info(info_flags::numeric)) {
499 // series * const (special case, faster)
500 ex f = power(op, it->coeff);
501 acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
503 } else if (!is_ex_exactly_of_type(op, series))
504 op = op.series(s, point, order);
505 if (!it->coeff.is_equal(exONE()))
506 op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
508 // Series multiplication
509 acc = ex_to_series(acc).mul_series(ex_to_series(op));
515 ex mul::series(symbol const & s, ex const & point, int order) const
517 ex acc; // Series accumulator
519 // Get first term from overall_coeff
520 acc = overall_coeff.series(s, point, order);
522 // Multiply with remaining terms
523 epvector::const_iterator it = seq.begin();
524 epvector::const_iterator itend = seq.end();
525 for (; it!=itend; it++) {
527 if (op.info(info_flags::numeric)) {
528 // series * const (special case, faster)
529 ex f = power(op, it->coeff);
530 acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
532 } else if (!is_ex_exactly_of_type(op, series))
533 op = op.series(s, point, order);
534 if (!it->coeff.is_equal(exONE()))
535 op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
537 // Series multiplication
538 acc = ex_to_series(acc).mul_series(ex_to_series(op));
544 /** Compute the p-th power of a series.
546 * @param p power to compute
547 * @param deg truncation order of series calculation */
548 ex series::power_const(const numeric &p, int deg) const
551 const symbol *s = static_cast<symbol *>(var.bp);
552 int ldeg = ldegree(*s);
554 // Calculate coefficients of powered series
558 co.push_back(co0 = power(coeff(*s, ldeg), p));
559 bool all_sums_zero = true;
560 for (i=1; i<deg; i++) {
562 for (int j=1; j<=i; j++) {
563 ex c = coeff(*s, j + ldeg);
564 if (is_order_function(c)) {
565 co.push_back(Order(exONE()));
568 sum += (p * j - (i - j)) * co[i - j] * c;
571 all_sums_zero = false;
572 co.push_back(co0 * sum / numeric(i));
575 // Construct new series (of non-zero coefficients)
577 bool higher_order = false;
578 for (i=0; i<deg; i++) {
579 if (!co[i].is_zero())
580 new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
581 if (is_order_function(co[i])) {
586 if (!higher_order && !all_sums_zero)
587 new_seq.push_back(expair(Order(exONE()), numeric(deg) + p * ldeg));
588 return series::series(var, point, new_seq);
592 /** Implementation of ex::series() for powers. This performs Laurent expansion
593 * of reciprocals of series at singularities.
595 ex power::series(symbol const & s, ex const & point, int order) const
598 if (!is_ex_exactly_of_type(basis, series)) {
599 // Basis is not a series, may there be a singulary?
600 if (!exponent.info(info_flags::negint))
601 return basic::series(s, point, order);
603 // Expression is of type something^(-int), check for singularity
604 if (!basis.subs(s == point).is_zero())
605 return basic::series(s, point, order);
607 // Singularity encountered, expand basis into series
608 e = basis.series(s, point, order);
615 return ex_to_series(e).power_const(ex_to_numeric(exponent), order);
619 /** Compute the truncated series expansion of an expression.
620 * This function returns an expression containing an object of class series to
621 * represent the series. If the series does not terminate within the given
622 * truncation order, the last term of the series will be an order term.
624 * @param s expansion variable
625 * @param point expansion point
626 * @param order truncation order of series calculations
627 * @return an expression holding a series object */
628 ex ex::series(symbol const &s, ex const &point, int order) const
631 return bp->series(s, point, order);
636 const series some_series;
637 type_info const & typeid_series = typeid(some_series);