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
29 #include "relational.h"
34 #ifndef NO_GINAC_NAMESPACE
36 #endif // ndef NO_GINAC_NAMESPACE
39 * Default constructor, destructor, copy constructor, assignment operator and helpers
42 series::series() : basic(TINFO_series)
44 debugmsg("series default constructor", LOGLEVEL_CONSTRUCT);
49 debugmsg("series destructor", LOGLEVEL_DESTRUCT);
53 series::series(series const &other)
55 debugmsg("series copy constructor", LOGLEVEL_CONSTRUCT);
59 series const &series::operator=(series const & other)
61 debugmsg("series operator=", LOGLEVEL_ASSIGNMENT);
69 void series::copy(series const &other)
71 inherited::copy(other);
77 void series::destroy(bool call_parent)
80 inherited::destroy(call_parent);
88 /** Construct series from a vector of coefficients and powers.
89 * expair.rest holds the coefficient, expair.coeff holds the power.
90 * The powers must be integers (positive or negative) and in ascending order;
91 * the last coefficient can be Order(_ex1()) to represent a truncated,
92 * non-terminating series.
94 * @param var_ series variable (must hold a symbol)
95 * @param point_ expansion point
96 * @param ops_ vector of {coefficient, power} pairs (coefficient must not be zero)
97 * @return newly constructed series */
98 series::series(ex const &var_, ex const &point_, epvector const &ops_)
99 : basic(TINFO_series), seq(ops_), var(var_), point(point_)
101 debugmsg("series constructor from ex,ex,epvector", LOGLEVEL_CONSTRUCT);
102 GINAC_ASSERT(is_ex_exactly_of_type(var_, symbol));
107 * Functions overriding virtual functions from base classes
110 basic *series::duplicate() const
112 debugmsg("series duplicate", LOGLEVEL_DUPLICATE);
113 return new series(*this);
116 void series::print(ostream &os, unsigned upper_precedence) const
118 debugmsg("symbol print", LOGLEVEL_PRINT);
119 convert_to_poly().print(os, upper_precedence);
122 void series::printraw(ostream &os) const
124 debugmsg("symbol printraw", LOGLEVEL_PRINT);
125 os << "series(" << var << ";" << point << ";";
126 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); i++) {
127 os << "(" << (*i).rest << "," << (*i).coeff << "),";
132 // Highest degree of variable
133 int series::degree(symbol const &s) const
135 if (var.is_equal(s)) {
136 // Return last exponent
138 return ex_to_numeric((*(seq.end() - 1)).coeff).to_int();
142 epvector::const_iterator it = seq.begin(), itend = seq.end();
145 int max_pow = INT_MIN;
146 while (it != itend) {
147 int pow = it->rest.degree(s);
156 // Lowest degree of variable
157 int series::ldegree(symbol const &s) const
159 if (var.is_equal(s)) {
160 // Return first exponent
162 return ex_to_numeric((*(seq.begin())).coeff).to_int();
166 epvector::const_iterator it = seq.begin(), itend = seq.end();
169 int min_pow = INT_MAX;
170 while (it != itend) {
171 int pow = it->rest.ldegree(s);
180 // Coefficient of variable
181 ex series::coeff(symbol const &s, int const n) const
183 if (var.is_equal(s)) {
184 epvector::const_iterator it = seq.begin(), itend = seq.end();
185 while (it != itend) {
186 int pow = ex_to_numeric(it->coeff).to_int();
195 return convert_to_poly().coeff(s, n);
198 ex series::eval(int level) const
203 // Construct a new series with evaluated coefficients
205 new_seq.reserve(seq.size());
206 epvector::const_iterator it = seq.begin(), itend = seq.end();
207 while (it != itend) {
208 new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
211 return (new series(var, point, new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
214 /** Evaluate numerically. The order term is dropped. */
215 ex series::evalf(int level) const
217 return convert_to_poly().evalf(level);
221 * Construct expression (polynomial) out of series
224 /** Convert a series object to an ordinary polynomial.
226 * @param no_order flag: discard higher order terms */
227 ex series::convert_to_poly(bool no_order) const
230 epvector::const_iterator it = seq.begin(), itend = seq.end();
232 while (it != itend) {
233 if (is_order_function(it->rest)) {
235 e += Order(power(var - point, it->coeff));
237 e += it->rest * power(var - point, it->coeff);
245 * Implementation of series expansion
248 /** Default implementation of ex::series(). This performs Taylor expansion.
250 ex basic::series(symbol const & s, ex const & point, int order) const
255 ex coeff = deriv.subs(s == point);
256 if (!coeff.is_zero())
257 seq.push_back(expair(coeff, numeric(0)));
260 for (n=1; n<order; n++) {
261 fac = fac.mul(numeric(n));
262 deriv = deriv.diff(s).expand();
263 if (deriv.is_zero()) {
265 return series::series(s, point, seq);
267 coeff = fac.inverse() * deriv.subs(s == point);
268 if (!coeff.is_zero())
269 seq.push_back(expair(coeff, numeric(n)));
272 // Higher-order terms, if present
273 deriv = deriv.diff(s);
274 if (!deriv.is_zero())
275 seq.push_back(expair(Order(_ex1()), numeric(n)));
276 return series::series(s, point, seq);
280 /** Add one series object to another, producing a series object that represents
283 * @param other series object to add with
284 * @return the sum as a series */
285 ex series::add_series(const series &other) const
287 // Adding two series with different variables or expansion points
288 // results in an empty (constant) series
289 if (!is_compatible_to(other)) {
291 nul.push_back(expair(Order(_ex1()), _ex0()));
292 return series(var, point, nul);
297 epvector::const_iterator a = seq.begin();
298 epvector::const_iterator b = other.seq.begin();
299 epvector::const_iterator a_end = seq.end();
300 epvector::const_iterator b_end = other.seq.end();
301 int pow_a = INT_MAX, pow_b = INT_MAX;
303 // If a is empty, fill up with elements from b and stop
306 new_seq.push_back(*b);
311 pow_a = ex_to_numeric((*a).coeff).to_int();
313 // If b is empty, fill up with elements from a and stop
316 new_seq.push_back(*a);
321 pow_b = ex_to_numeric((*b).coeff).to_int();
323 // a and b are non-empty, compare powers
325 // a has lesser power, get coefficient from a
326 new_seq.push_back(*a);
327 if (is_order_function((*a).rest))
330 } else if (pow_b < pow_a) {
331 // b has lesser power, get coefficient from b
332 new_seq.push_back(*b);
333 if (is_order_function((*b).rest))
337 // Add coefficient of a and b
338 if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
339 new_seq.push_back(expair(Order(_ex1()), (*a).coeff));
340 break; // Order term ends the sequence
342 ex sum = (*a).rest + (*b).rest;
343 if (!(sum.is_zero()))
344 new_seq.push_back(expair(sum, numeric(pow_a)));
350 return series(var, point, new_seq);
354 /** Implementation of ex::series() for sums. This performs series addition when
355 * adding series objects.
358 ex add::series(symbol const & s, ex const & point, int order) const
360 ex acc; // Series accumulator
363 epvector::const_iterator it = seq.begin();
364 epvector::const_iterator itend = seq.end();
366 if (is_ex_exactly_of_type(it->rest, series))
369 acc = it->rest.series(s, point, order);
370 if (!it->coeff.is_equal(_ex1()))
371 acc = ex_to_series(acc).mul_const(ex_to_numeric(it->coeff));
375 // Add remaining terms
376 for (; it!=itend; it++) {
378 if (is_ex_exactly_of_type(it->rest, series))
381 op = it->rest.series(s, point, order);
382 if (!it->coeff.is_equal(_ex1()))
383 op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
386 acc = ex_to_series(acc).add_series(ex_to_series(op));
391 ex add::series(symbol const & s, ex const & point, int order) const
393 ex acc; // Series accumulator
395 // Get first term from overall_coeff
396 acc = overall_coeff.series(s,point,order);
398 // Add remaining terms
399 epvector::const_iterator it = seq.begin();
400 epvector::const_iterator itend = seq.end();
401 for (; it!=itend; it++) {
403 if (is_ex_exactly_of_type(it->rest, series))
406 op = it->rest.series(s, point, order);
407 if (!it->coeff.is_equal(_ex1()))
408 op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
411 acc = ex_to_series(acc).add_series(ex_to_series(op));
417 /** Multiply a series object with a numeric constant, producing a series object
418 * that represents the product.
420 * @param other constant to multiply with
421 * @return the product as a series */
422 ex series::mul_const(const numeric &other) const
425 new_seq.reserve(seq.size());
427 epvector::const_iterator it = seq.begin(), itend = seq.end();
428 while (it != itend) {
429 if (!is_order_function(it->rest))
430 new_seq.push_back(expair(it->rest * other, it->coeff));
432 new_seq.push_back(*it);
435 return series(var, point, new_seq);
439 /** Multiply one series object to another, producing a series object that
440 * represents the product.
442 * @param other series object to multiply with
443 * @return the product as a series */
444 ex series::mul_series(const series &other) const
446 // Multiplying two series with different variables or expansion points
447 // results in an empty (constant) series
448 if (!is_compatible_to(other)) {
450 nul.push_back(expair(Order(_ex1()), _ex0()));
451 return series(var, point, nul);
454 // Series multiplication
457 const symbol *s = static_cast<symbol *>(var.bp);
458 int a_max = degree(*s);
459 int b_max = other.degree(*s);
460 int a_min = ldegree(*s);
461 int b_min = other.ldegree(*s);
462 int cdeg_min = a_min + b_min;
463 int cdeg_max = a_max + b_max;
465 int higher_order_a = INT_MAX;
466 int higher_order_b = INT_MAX;
467 if (is_order_function(coeff(*s, a_max)))
468 higher_order_a = a_max + b_min;
469 if (is_order_function(other.coeff(*s, b_max)))
470 higher_order_b = b_max + a_min;
471 int higher_order_c = min(higher_order_a, higher_order_b);
472 if (cdeg_max >= higher_order_c)
473 cdeg_max = higher_order_c - 1;
475 for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
477 // c(i)=a(0)b(i)+...+a(i)b(0)
478 for (int i=a_min; cdeg-i>=b_min; i++) {
479 ex a_coeff = coeff(*s, i);
480 ex b_coeff = other.coeff(*s, cdeg-i);
481 if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
482 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
485 new_seq.push_back(expair(co, numeric(cdeg)));
487 if (higher_order_c < INT_MAX)
488 new_seq.push_back(expair(Order(_ex1()), numeric(higher_order_c)));
489 return series::series(var, point, new_seq);
494 ex mul::series(symbol const & s, ex const & point, int order) const
496 ex acc; // Series accumulator
499 epvector::const_iterator it = seq.begin();
500 epvector::const_iterator itend = seq.end();
502 if (is_ex_exactly_of_type(it->rest, series))
505 acc = it->rest.series(s, point, order);
506 if (!it->coeff.is_equal(_ex1()))
507 acc = ex_to_series(acc).power_const(ex_to_numeric(it->coeff), order);
511 // Multiply with remaining terms
512 for (; it!=itend; it++) {
514 if (op.info(info_flags::numeric)) {
515 // series * const (special case, faster)
516 ex f = power(op, it->coeff);
517 acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
519 } else if (!is_ex_exactly_of_type(op, series))
520 op = op.series(s, point, order);
521 if (!it->coeff.is_equal(_ex1()))
522 op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
524 // Series multiplication
525 acc = ex_to_series(acc).mul_series(ex_to_series(op));
531 /** Implementation of ex::series() for product. This performs series
532 * multiplication when multiplying series.
534 ex mul::series(symbol const & s, ex const & point, int order) const
536 ex acc; // Series accumulator
538 // Get first term from overall_coeff
539 acc = overall_coeff.series(s, point, order);
541 // Multiply with remaining terms
542 epvector::const_iterator it = seq.begin();
543 epvector::const_iterator itend = seq.end();
544 for (; it!=itend; it++) {
546 if (op.info(info_flags::numeric)) {
547 // series * const (special case, faster)
548 ex f = power(op, it->coeff);
549 acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
551 } else if (!is_ex_exactly_of_type(op, series))
552 op = op.series(s, point, order);
553 if (!it->coeff.is_equal(_ex1()))
554 op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
556 // Series multiplication
557 acc = ex_to_series(acc).mul_series(ex_to_series(op));
563 /** Compute the p-th power of a series.
565 * @param p power to compute
566 * @param deg truncation order of series calculation */
567 ex series::power_const(const numeric &p, int deg) const
570 const symbol *s = static_cast<symbol *>(var.bp);
571 int ldeg = ldegree(*s);
573 // Calculate coefficients of powered series
577 co.push_back(co0 = power(coeff(*s, ldeg), p));
578 bool all_sums_zero = true;
579 for (i=1; i<deg; i++) {
581 for (int j=1; j<=i; j++) {
582 ex c = coeff(*s, j + ldeg);
583 if (is_order_function(c)) {
584 co.push_back(Order(_ex1()));
587 sum += (p * j - (i - j)) * co[i - j] * c;
590 all_sums_zero = false;
591 co.push_back(co0 * sum / numeric(i));
594 // Construct new series (of non-zero coefficients)
596 bool higher_order = false;
597 for (i=0; i<deg; i++) {
598 if (!co[i].is_zero())
599 new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
600 if (is_order_function(co[i])) {
605 if (!higher_order && !all_sums_zero)
606 new_seq.push_back(expair(Order(_ex1()), numeric(deg) + p * ldeg));
607 return series::series(var, point, new_seq);
611 /** Implementation of ex::series() for powers. This performs Laurent expansion
612 * of reciprocals of series at singularities.
614 ex power::series(symbol const & s, ex const & point, int order) const
617 if (!is_ex_exactly_of_type(basis, series)) {
618 // Basis is not a series, may there be a singulary?
619 if (!exponent.info(info_flags::negint))
620 return basic::series(s, point, order);
622 // Expression is of type something^(-int), check for singularity
623 if (!basis.subs(s == point).is_zero())
624 return basic::series(s, point, order);
626 // Singularity encountered, expand basis into series
627 e = basis.series(s, point, order);
634 return ex_to_series(e).power_const(ex_to_numeric(exponent), order);
638 /** Compute the truncated series expansion of an expression.
639 * This function returns an expression containing an object of class series to
640 * represent the series. If the series does not terminate within the given
641 * truncation order, the last term of the series will be an order term.
643 * @param s expansion variable
644 * @param point expansion point
645 * @param order truncation order of series calculations
646 * @return an expression holding a series object */
647 ex ex::series(symbol const &s, ex const &point, int order) const
650 return bp->series(s, point, order);
655 const series some_series;
656 type_info const & typeid_series = typeid(some_series);
658 #ifndef NO_GINAC_NAMESPACE
660 #endif // ndef NO_GINAC_NAMESPACE