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"
33 #ifndef NO_GINAC_NAMESPACE
35 #endif // ndef NO_GINAC_NAMESPACE
38 * Default constructor, destructor, copy constructor, assignment operator and helpers
41 series::series() : basic(TINFO_series)
43 debugmsg("series default constructor", LOGLEVEL_CONSTRUCT);
48 debugmsg("series destructor", LOGLEVEL_DESTRUCT);
52 series::series(series const &other)
54 debugmsg("series copy constructor", LOGLEVEL_CONSTRUCT);
58 series const &series::operator=(series const & other)
60 debugmsg("series operator=", LOGLEVEL_ASSIGNMENT);
68 void series::copy(series const &other)
70 inherited::copy(other);
76 void series::destroy(bool call_parent)
79 inherited::destroy(call_parent);
87 /** Construct series from a vector of coefficients and powers.
88 * expair.rest holds the coefficient, expair.coeff holds the power.
89 * The powers must be integers (positive or negative) and in ascending order;
90 * the last coefficient can be Order(exONE()) to represent a truncated,
91 * non-terminating series.
93 * @param var_ series variable (must hold a symbol)
94 * @param point_ expansion point
95 * @param ops_ vector of {coefficient, power} pairs (coefficient must not be zero)
96 * @return newly constructed series */
97 series::series(ex const &var_, ex const &point_, epvector const &ops_)
98 : basic(TINFO_series), seq(ops_), var(var_), point(point_)
100 debugmsg("series constructor from ex,ex,epvector", LOGLEVEL_CONSTRUCT);
101 GINAC_ASSERT(is_ex_exactly_of_type(var_, symbol));
106 * Functions overriding virtual functions from base classes
109 basic *series::duplicate() const
111 debugmsg("series duplicate", LOGLEVEL_DUPLICATE);
112 return new series(*this);
115 void series::print(ostream &os, unsigned upper_precedence) const
117 debugmsg("symbol print", LOGLEVEL_PRINT);
118 convert_to_poly().print(os, upper_precedence);
121 void series::printraw(ostream &os) const
123 debugmsg("symbol printraw", LOGLEVEL_PRINT);
124 os << "series(" << var << ";" << point << ";";
125 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); i++) {
126 os << "(" << (*i).rest << "," << (*i).coeff << "),";
131 // Highest degree of variable
132 int series::degree(symbol const &s) const
134 if (var.is_equal(s)) {
135 // Return last exponent
137 return ex_to_numeric((*(seq.end() - 1)).coeff).to_int();
141 epvector::const_iterator it = seq.begin(), itend = seq.end();
144 int max_pow = INT_MIN;
145 while (it != itend) {
146 int pow = it->rest.degree(s);
155 // Lowest degree of variable
156 int series::ldegree(symbol const &s) const
158 if (var.is_equal(s)) {
159 // Return first exponent
161 return ex_to_numeric((*(seq.begin())).coeff).to_int();
165 epvector::const_iterator it = seq.begin(), itend = seq.end();
168 int min_pow = INT_MAX;
169 while (it != itend) {
170 int pow = it->rest.ldegree(s);
179 // Coefficient of variable
180 ex series::coeff(symbol const &s, int n) const
182 if (var.is_equal(s)) {
183 epvector::const_iterator it = seq.begin(), itend = seq.end();
184 while (it != itend) {
185 int pow = ex_to_numeric(it->coeff).to_int();
194 return convert_to_poly().coeff(s, n);
197 ex series::eval(int level) const
202 // Construct a new series with evaluated coefficients
204 new_seq.reserve(seq.size());
205 epvector::const_iterator it = seq.begin(), itend = seq.end();
206 while (it != itend) {
207 new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
210 return (new series(var, point, new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
213 /** Evaluate numerically. The order term is dropped. */
214 ex series::evalf(int level) const
216 return convert_to_poly().evalf(level);
220 * Construct expression (polynomial) out of series
223 /** Convert a series object to an ordinary polynomial.
225 * @param no_order flag: discard higher order terms */
226 ex series::convert_to_poly(bool no_order) const
229 epvector::const_iterator it = seq.begin(), itend = seq.end();
231 while (it != itend) {
232 if (is_order_function(it->rest)) {
234 e += Order(power(var - point, it->coeff));
236 e += it->rest * power(var - point, it->coeff);
244 * Implementation of series expansion
247 /** Default implementation of ex::series(). This performs Taylor expansion.
249 ex basic::series(symbol const & s, ex const & point, int order) const
254 ex coeff = deriv.subs(s == point);
255 if (!coeff.is_zero())
256 seq.push_back(expair(coeff, numeric(0)));
259 for (n=1; n<order; n++) {
260 fac = fac.mul(numeric(n));
261 deriv = deriv.diff(s).expand();
262 if (deriv.is_zero()) {
264 return series::series(s, point, seq);
266 coeff = power(fac, -1) * deriv.subs(s == point);
267 if (!coeff.is_zero())
268 seq.push_back(expair(coeff, numeric(n)));
271 // Higher-order terms, if present
272 deriv = deriv.diff(s);
273 if (!deriv.is_zero())
274 seq.push_back(expair(Order(exONE()), numeric(n)));
275 return series::series(s, point, seq);
279 /** Add one series object to another, producing a series object that represents
282 * @param other series object to add with
283 * @return the sum as a series */
284 ex series::add_series(const series &other) const
286 // Adding two series with different variables or expansion points
287 // results in an empty (constant) series
288 if (!is_compatible_to(other)) {
290 nul.push_back(expair(Order(exONE()), exZERO()));
291 return series(var, point, nul);
296 epvector::const_iterator a = seq.begin();
297 epvector::const_iterator b = other.seq.begin();
298 epvector::const_iterator a_end = seq.end();
299 epvector::const_iterator b_end = other.seq.end();
300 int pow_a = INT_MAX, pow_b = INT_MAX;
302 // If a is empty, fill up with elements from b and stop
305 new_seq.push_back(*b);
310 pow_a = ex_to_numeric((*a).coeff).to_int();
312 // If b is empty, fill up with elements from a and stop
315 new_seq.push_back(*a);
320 pow_b = ex_to_numeric((*b).coeff).to_int();
322 // a and b are non-empty, compare powers
324 // a has lesser power, get coefficient from a
325 new_seq.push_back(*a);
326 if (is_order_function((*a).rest))
329 } else if (pow_b < pow_a) {
330 // b has lesser power, get coefficient from b
331 new_seq.push_back(*b);
332 if (is_order_function((*b).rest))
336 // Add coefficient of a and b
337 if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
338 new_seq.push_back(expair(Order(exONE()), (*a).coeff));
339 break; // Order term ends the sequence
341 ex sum = (*a).rest + (*b).rest;
342 if (!(sum.is_zero()))
343 new_seq.push_back(expair(sum, numeric(pow_a)));
349 return series(var, point, new_seq);
353 /** Implementation of ex::series() for sums. This performs series addition when
354 * adding series objects.
357 ex add::series(symbol const & s, ex const & point, int order) const
359 ex acc; // Series accumulator
362 epvector::const_iterator it = seq.begin();
363 epvector::const_iterator itend = seq.end();
365 if (is_ex_exactly_of_type(it->rest, series))
368 acc = it->rest.series(s, point, order);
369 if (!it->coeff.is_equal(exONE()))
370 acc = ex_to_series(acc).mul_const(ex_to_numeric(it->coeff));
374 // Add remaining terms
375 for (; it!=itend; it++) {
377 if (is_ex_exactly_of_type(it->rest, series))
380 op = it->rest.series(s, point, order);
381 if (!it->coeff.is_equal(exONE()))
382 op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
385 acc = ex_to_series(acc).add_series(ex_to_series(op));
390 ex add::series(symbol const & s, ex const & point, int order) const
392 ex acc; // Series accumulator
394 // Get first term from overall_coeff
395 acc = overall_coeff.series(s,point,order);
397 // Add remaining terms
398 epvector::const_iterator it = seq.begin();
399 epvector::const_iterator itend = seq.end();
400 for (; it!=itend; it++) {
402 if (is_ex_exactly_of_type(it->rest, series))
405 op = it->rest.series(s, point, order);
406 if (!it->coeff.is_equal(exONE()))
407 op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
410 acc = ex_to_series(acc).add_series(ex_to_series(op));
416 /** Multiply a series object with a numeric constant, producing a series object
417 * that represents the product.
419 * @param other constant to multiply with
420 * @return the product as a series */
421 ex series::mul_const(const numeric &other) const
424 new_seq.reserve(seq.size());
426 epvector::const_iterator it = seq.begin(), itend = seq.end();
427 while (it != itend) {
428 if (!is_order_function(it->rest))
429 new_seq.push_back(expair(it->rest * other, it->coeff));
431 new_seq.push_back(*it);
434 return series(var, point, new_seq);
438 /** Multiply one series object to another, producing a series object that
439 * represents the product.
441 * @param other series object to multiply with
442 * @return the product as a series */
443 ex series::mul_series(const series &other) const
445 // Multiplying two series with different variables or expansion points
446 // results in an empty (constant) series
447 if (!is_compatible_to(other)) {
449 nul.push_back(expair(Order(exONE()), exZERO()));
450 return series(var, point, nul);
453 // Series multiplication
456 const symbol *s = static_cast<symbol *>(var.bp);
457 int a_max = degree(*s);
458 int b_max = other.degree(*s);
459 int a_min = ldegree(*s);
460 int b_min = other.ldegree(*s);
461 int cdeg_min = a_min + b_min;
462 int cdeg_max = a_max + b_max;
464 int higher_order_a = INT_MAX;
465 int higher_order_b = INT_MAX;
466 if (is_order_function(coeff(*s, a_max)))
467 higher_order_a = a_max + b_min;
468 if (is_order_function(other.coeff(*s, b_max)))
469 higher_order_b = b_max + a_min;
470 int higher_order_c = min(higher_order_a, higher_order_b);
471 if (cdeg_max >= higher_order_c)
472 cdeg_max = higher_order_c - 1;
474 for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
476 // c(i)=a(0)b(i)+...+a(i)b(0)
477 for (int i=a_min; cdeg-i>=b_min; i++) {
478 ex a_coeff = coeff(*s, i);
479 ex b_coeff = other.coeff(*s, cdeg-i);
480 if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
481 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
484 new_seq.push_back(expair(co, numeric(cdeg)));
486 if (higher_order_c < INT_MAX)
487 new_seq.push_back(expair(Order(exONE()), numeric(higher_order_c)));
488 return series::series(var, point, new_seq);
493 ex mul::series(symbol const & s, ex const & point, int order) const
495 ex acc; // Series accumulator
498 epvector::const_iterator it = seq.begin();
499 epvector::const_iterator itend = seq.end();
501 if (is_ex_exactly_of_type(it->rest, series))
504 acc = it->rest.series(s, point, order);
505 if (!it->coeff.is_equal(exONE()))
506 acc = ex_to_series(acc).power_const(ex_to_numeric(it->coeff), order);
510 // Multiply with remaining terms
511 for (; it!=itend; it++) {
513 if (op.info(info_flags::numeric)) {
514 // series * const (special case, faster)
515 ex f = power(op, it->coeff);
516 acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
518 } else if (!is_ex_exactly_of_type(op, series))
519 op = op.series(s, point, order);
520 if (!it->coeff.is_equal(exONE()))
521 op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
523 // Series multiplication
524 acc = ex_to_series(acc).mul_series(ex_to_series(op));
530 /** Implementation of ex::series() for product. This performs series
531 * multiplication when multiplying series.
533 ex mul::series(symbol const & s, ex const & point, int order) const
535 ex acc; // Series accumulator
537 // Get first term from overall_coeff
538 acc = overall_coeff.series(s, point, order);
540 // Multiply with remaining terms
541 epvector::const_iterator it = seq.begin();
542 epvector::const_iterator itend = seq.end();
543 for (; it!=itend; it++) {
545 if (op.info(info_flags::numeric)) {
546 // series * const (special case, faster)
547 ex f = power(op, it->coeff);
548 acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
550 } else if (!is_ex_exactly_of_type(op, series))
551 op = op.series(s, point, order);
552 if (!it->coeff.is_equal(exONE()))
553 op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
555 // Series multiplication
556 acc = ex_to_series(acc).mul_series(ex_to_series(op));
562 /** Compute the p-th power of a series.
564 * @param p power to compute
565 * @param deg truncation order of series calculation */
566 ex series::power_const(const numeric &p, int deg) const
569 const symbol *s = static_cast<symbol *>(var.bp);
570 int ldeg = ldegree(*s);
572 // Calculate coefficients of powered series
576 co.push_back(co0 = power(coeff(*s, ldeg), p));
577 bool all_sums_zero = true;
578 for (i=1; i<deg; i++) {
580 for (int j=1; j<=i; j++) {
581 ex c = coeff(*s, j + ldeg);
582 if (is_order_function(c)) {
583 co.push_back(Order(exONE()));
586 sum += (p * j - (i - j)) * co[i - j] * c;
589 all_sums_zero = false;
590 co.push_back(co0 * sum / numeric(i));
593 // Construct new series (of non-zero coefficients)
595 bool higher_order = false;
596 for (i=0; i<deg; i++) {
597 if (!co[i].is_zero())
598 new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
599 if (is_order_function(co[i])) {
604 if (!higher_order && !all_sums_zero)
605 new_seq.push_back(expair(Order(exONE()), numeric(deg) + p * ldeg));
606 return series::series(var, point, new_seq);
610 /** Implementation of ex::series() for powers. This performs Laurent expansion
611 * of reciprocals of series at singularities.
613 ex power::series(symbol const & s, ex const & point, int order) const
616 if (!is_ex_exactly_of_type(basis, series)) {
617 // Basis is not a series, may there be a singulary?
618 if (!exponent.info(info_flags::negint))
619 return basic::series(s, point, order);
621 // Expression is of type something^(-int), check for singularity
622 if (!basis.subs(s == point).is_zero())
623 return basic::series(s, point, order);
625 // Singularity encountered, expand basis into series
626 e = basis.series(s, point, order);
633 return ex_to_series(e).power_const(ex_to_numeric(exponent), order);
637 /** Compute the truncated series expansion of an expression.
638 * This function returns an expression containing an object of class series to
639 * represent the series. If the series does not terminate within the given
640 * truncation order, the last term of the series will be an order term.
642 * @param s expansion variable
643 * @param point expansion point
644 * @param order truncation order of series calculations
645 * @return an expression holding a series object */
646 ex ex::series(symbol const &s, ex const &point, int order) const
649 return bp->series(s, point, order);
654 const series some_series;
655 type_info const & typeid_series = typeid(some_series);
657 #ifndef NO_GINAC_NAMESPACE
659 #endif // ndef NO_GINAC_NAMESPACE