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 // Highest degree of variable
116 int series::degree(symbol const &s) const
118 if (var.is_equal(s)) {
119 // Return last exponent
121 return ex_to_numeric((*(seq.end() - 1)).coeff).to_int();
125 epvector::const_iterator it = seq.begin(), itend = seq.end();
128 int max_pow = INT_MIN;
129 while (it != itend) {
130 int pow = it->rest.degree(s);
139 // Lowest degree of variable
140 int series::ldegree(symbol const &s) const
142 if (var.is_equal(s)) {
143 // Return first exponent
145 return ex_to_numeric((*(seq.begin())).coeff).to_int();
149 epvector::const_iterator it = seq.begin(), itend = seq.end();
152 int min_pow = INT_MAX;
153 while (it != itend) {
154 int pow = it->rest.ldegree(s);
163 // Coefficient of variable
164 ex series::coeff(symbol const &s, int n) const
166 if (var.is_equal(s)) {
167 epvector::const_iterator it = seq.begin(), itend = seq.end();
168 while (it != itend) {
169 int pow = ex_to_numeric(it->coeff).to_int();
178 return convert_to_poly().coeff(s, n);
181 ex series::eval(int level) const
186 // Construct a new series with evaluated coefficients
188 new_seq.reserve(seq.size());
189 epvector::const_iterator it = seq.begin(), itend = seq.end();
190 while (it != itend) {
191 new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
194 return (new series(var, point, new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
197 /** Evaluate numerically. The order term is dropped. */
198 ex series::evalf(int level) const
200 return convert_to_poly().evalf(level);
204 * Construct expression (polynomial) out of series
207 /** Convert a series object to an ordinary polynomial.
209 * @param no_order flag: discard higher order terms */
210 ex series::convert_to_poly(bool no_order) const
213 epvector::const_iterator it = seq.begin(), itend = seq.end();
215 while (it != itend) {
216 if (is_order_function(it->rest)) {
218 e += Order(power(var - point, it->coeff));
220 e += it->rest * power(var - point, it->coeff);
228 * Implementation of series expansion
231 /** Default implementation of ex::series(). This performs Taylor expansion.
233 ex basic::series(symbol const & s, ex const & point, int order) const
238 ex coeff = deriv.subs(s == point);
239 if (!coeff.is_zero())
240 seq.push_back(expair(coeff, numeric(0)));
243 for (n=1; n<order; n++) {
244 fac = fac.mul(numeric(n));
245 deriv = deriv.diff(s).expand();
246 if (deriv.is_zero()) {
248 return series::series(s, point, seq);
250 coeff = power(fac, -1) * deriv.subs(s == point);
251 if (!coeff.is_zero())
252 seq.push_back(expair(coeff, numeric(n)));
255 // Higher-order terms, if present
256 deriv = deriv.diff(s);
257 if (!deriv.is_zero())
258 seq.push_back(expair(Order(exONE()), numeric(n)));
259 return series::series(s, point, seq);
263 /** Add one series object to another, producing a series object that represents
266 * @param other series object to add with
267 * @return the sum as a series */
268 ex series::add_series(const series &other) const
270 // Adding two series with different variables or expansion points
271 // results in an empty (constant) series
272 if (!is_compatible_to(other)) {
274 nul.push_back(expair(Order(exONE()), exZERO()));
275 return series(var, point, nul);
280 epvector::const_iterator a = seq.begin();
281 epvector::const_iterator b = other.seq.begin();
282 epvector::const_iterator a_end = seq.end();
283 epvector::const_iterator b_end = other.seq.end();
284 int pow_a = INT_MAX, pow_b = INT_MAX;
286 // If a is empty, fill up with elements from b and stop
289 new_seq.push_back(*b);
294 pow_a = ex_to_numeric((*a).coeff).to_int();
296 // If b is empty, fill up with elements from a and stop
299 new_seq.push_back(*a);
304 pow_b = ex_to_numeric((*b).coeff).to_int();
306 // a and b are non-empty, compare powers
308 // a has lesser power, get coefficient from a
309 new_seq.push_back(*a);
310 if (is_order_function((*a).rest))
313 } else if (pow_b < pow_a) {
314 // b has lesser power, get coefficient from b
315 new_seq.push_back(*b);
316 if (is_order_function((*b).rest))
320 // Add coefficient of a and b
321 if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
322 new_seq.push_back(expair(Order(exONE()), (*a).coeff));
323 break; // Order term ends the sequence
325 ex sum = (*a).rest + (*b).rest;
326 if (!(sum.is_zero()))
327 new_seq.push_back(expair(sum, numeric(pow_a)));
333 return series(var, point, new_seq);
337 /** Implementation of ex::series() for sums. This performs series addition when
338 * adding series objects.
341 ex add::series(symbol const & s, ex const & point, int order) const
343 ex acc; // Series accumulator
346 epvector::const_iterator it = seq.begin();
347 epvector::const_iterator itend = seq.end();
349 if (is_ex_exactly_of_type(it->rest, series))
352 acc = it->rest.series(s, point, order);
353 if (!it->coeff.is_equal(exONE()))
354 acc = ex_to_series(acc).mul_const(ex_to_numeric(it->coeff));
358 // Add remaining terms
359 for (; it!=itend; it++) {
361 if (is_ex_exactly_of_type(it->rest, series))
364 op = it->rest.series(s, point, order);
365 if (!it->coeff.is_equal(exONE()))
366 op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
369 acc = ex_to_series(acc).add_series(ex_to_series(op));
374 ex add::series(symbol const & s, ex const & point, int order) const
376 ex acc; // Series accumulator
378 // Get first term from overall_coeff
379 acc = overall_coeff.series(s,point,order);
381 // Add remaining terms
382 epvector::const_iterator it = seq.begin();
383 epvector::const_iterator itend = seq.end();
384 for (; it!=itend; it++) {
386 if (is_ex_exactly_of_type(it->rest, series))
389 op = it->rest.series(s, point, order);
390 if (!it->coeff.is_equal(exONE()))
391 op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
394 acc = ex_to_series(acc).add_series(ex_to_series(op));
400 /** Multiply a series object with a numeric constant, producing a series object
401 * that represents the product.
403 * @param other constant to multiply with
404 * @return the product as a series */
405 ex series::mul_const(const numeric &other) const
408 new_seq.reserve(seq.size());
410 epvector::const_iterator it = seq.begin(), itend = seq.end();
411 while (it != itend) {
412 if (!is_order_function(it->rest))
413 new_seq.push_back(expair(it->rest * other, it->coeff));
415 new_seq.push_back(*it);
418 return series(var, point, new_seq);
422 /** Multiply one series object to another, producing a series object that
423 * represents the product.
425 * @param other series object to multiply with
426 * @return the product as a series */
427 ex series::mul_series(const series &other) const
429 // Multiplying two series with different variables or expansion points
430 // results in an empty (constant) series
431 if (!is_compatible_to(other)) {
433 nul.push_back(expair(Order(exONE()), exZERO()));
434 return series(var, point, nul);
437 // Series multiplication
440 const symbol *s = static_cast<symbol *>(var.bp);
441 int a_max = degree(*s);
442 int b_max = other.degree(*s);
443 int a_min = ldegree(*s);
444 int b_min = other.ldegree(*s);
445 int cdeg_min = a_min + b_min;
446 int cdeg_max = a_max + b_max;
448 int higher_order_a = INT_MAX;
449 int higher_order_b = INT_MAX;
450 if (is_order_function(coeff(*s, a_max)))
451 higher_order_a = a_max + b_min;
452 if (is_order_function(other.coeff(*s, b_max)))
453 higher_order_b = b_max + a_min;
454 int higher_order_c = min(higher_order_a, higher_order_b);
455 if (cdeg_max >= higher_order_c)
456 cdeg_max = higher_order_c - 1;
458 for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
460 // c(i)=a(0)b(i)+...+a(i)b(0)
461 for (int i=a_min; cdeg-i>=b_min; i++) {
462 ex a_coeff = coeff(*s, i);
463 ex b_coeff = other.coeff(*s, cdeg-i);
464 if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
465 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
468 new_seq.push_back(expair(co, numeric(cdeg)));
470 if (higher_order_c < INT_MAX)
471 new_seq.push_back(expair(Order(exONE()), numeric(higher_order_c)));
472 return series::series(var, point, new_seq);
477 ex mul::series(symbol const & s, ex const & point, int order) const
479 ex acc; // Series accumulator
482 epvector::const_iterator it = seq.begin();
483 epvector::const_iterator itend = seq.end();
485 if (is_ex_exactly_of_type(it->rest, series))
488 acc = it->rest.series(s, point, order);
489 if (!it->coeff.is_equal(exONE()))
490 acc = ex_to_series(acc).power_const(ex_to_numeric(it->coeff), order);
494 // Multiply with remaining terms
495 for (; it!=itend; it++) {
497 if (op.info(info_flags::numeric)) {
498 // series * const (special case, faster)
499 ex f = power(op, it->coeff);
500 acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
502 } else if (!is_ex_exactly_of_type(op, series))
503 op = op.series(s, point, order);
504 if (!it->coeff.is_equal(exONE()))
505 op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
507 // Series multiplication
508 acc = ex_to_series(acc).mul_series(ex_to_series(op));
514 /** Implementation of ex::series() for product. This performs series
515 * multiplication when multiplying series.
517 ex mul::series(symbol const & s, ex const & point, int order) const
519 ex acc; // Series accumulator
521 // Get first term from overall_coeff
522 acc = overall_coeff.series(s, point, order);
524 // Multiply with remaining terms
525 epvector::const_iterator it = seq.begin();
526 epvector::const_iterator itend = seq.end();
527 for (; it!=itend; it++) {
529 if (op.info(info_flags::numeric)) {
530 // series * const (special case, faster)
531 ex f = power(op, it->coeff);
532 acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
534 } else if (!is_ex_exactly_of_type(op, series))
535 op = op.series(s, point, order);
536 if (!it->coeff.is_equal(exONE()))
537 op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
539 // Series multiplication
540 acc = ex_to_series(acc).mul_series(ex_to_series(op));
546 /** Compute the p-th power of a series.
548 * @param p power to compute
549 * @param deg truncation order of series calculation */
550 ex series::power_const(const numeric &p, int deg) const
553 const symbol *s = static_cast<symbol *>(var.bp);
554 int ldeg = ldegree(*s);
556 // Calculate coefficients of powered series
560 co.push_back(co0 = power(coeff(*s, ldeg), p));
561 bool all_sums_zero = true;
562 for (i=1; i<deg; i++) {
564 for (int j=1; j<=i; j++) {
565 ex c = coeff(*s, j + ldeg);
566 if (is_order_function(c)) {
567 co.push_back(Order(exONE()));
570 sum += (p * j - (i - j)) * co[i - j] * c;
573 all_sums_zero = false;
574 co.push_back(co0 * sum / numeric(i));
577 // Construct new series (of non-zero coefficients)
579 bool higher_order = false;
580 for (i=0; i<deg; i++) {
581 if (!co[i].is_zero())
582 new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
583 if (is_order_function(co[i])) {
588 if (!higher_order && !all_sums_zero)
589 new_seq.push_back(expair(Order(exONE()), numeric(deg) + p * ldeg));
590 return series::series(var, point, new_seq);
594 /** Implementation of ex::series() for powers. This performs Laurent expansion
595 * of reciprocals of series at singularities.
597 ex power::series(symbol const & s, ex const & point, int order) const
600 if (!is_ex_exactly_of_type(basis, series)) {
601 // Basis is not a series, may there be a singulary?
602 if (!exponent.info(info_flags::negint))
603 return basic::series(s, point, order);
605 // Expression is of type something^(-int), check for singularity
606 if (!basis.subs(s == point).is_zero())
607 return basic::series(s, point, order);
609 // Singularity encountered, expand basis into series
610 e = basis.series(s, point, order);
617 return ex_to_series(e).power_const(ex_to_numeric(exponent), order);
621 /** Compute the truncated series expansion of an expression.
622 * This function returns an expression containing an object of class series to
623 * represent the series. If the series does not terminate within the given
624 * truncation order, the last term of the series will be an order term.
626 * @param s expansion variable
627 * @param point expansion point
628 * @param order truncation order of series calculations
629 * @return an expression holding a series object */
630 ex ex::series(symbol const &s, ex const &point, int order) const
633 return bp->series(s, point, order);
638 const series some_series;
639 type_info const & typeid_series = typeid(some_series);
641 #ifndef NO_GINAC_NAMESPACE
643 #endif // ndef NO_GINAC_NAMESPACE