3 * Implementation of class for extended truncated power-series and
4 * methods for series expansion. */
10 * Default constructor, destructor, copy constructor, assignment operator and helpers
13 series::series() : basic(TINFO_SERIES)
15 debugmsg("series default constructor", LOGLEVEL_CONSTRUCT);
20 debugmsg("series destructor", LOGLEVEL_DESTRUCT);
24 series::series(series const &other)
26 debugmsg("series copy constructor", LOGLEVEL_CONSTRUCT);
30 series const &series::operator=(series const & other)
32 debugmsg("series operator=", LOGLEVEL_ASSIGNMENT);
40 void series::copy(series const &other)
42 inherited::copy(other);
48 void series::destroy(bool call_parent)
51 inherited::destroy(call_parent);
59 /** Construct series from a vector of coefficients and powers.
60 * expair.rest holds the coefficient, expair.coeff holds the power.
61 * The powers must be integers (positive or negative) and in ascending order;
62 * the last coefficient can be Order(exONE()) to represent a truncated,
63 * non-terminating series.
65 * @param var_ series variable (must hold a symbol)
66 * @param point_ expansion point
67 * @param ops_ vector of {coefficient, power} pairs (coefficient must not be zero)
68 * @return newly constructed series */
69 series::series(ex const &var_, ex const &point_, epvector const &ops_)
70 : basic(TINFO_SERIES), seq(ops_), var(var_), point(point_)
72 debugmsg("series constructor from ex,ex,epvector", LOGLEVEL_CONSTRUCT);
73 ASSERT(is_ex_exactly_of_type(var_, symbol));
78 * Functions overriding virtual functions from base classes
81 basic *series::duplicate() const
83 debugmsg("series duplicate", LOGLEVEL_DUPLICATE);
84 return new series(*this);
87 // Highest degree of variable
88 int series::degree(symbol const &s) const
91 // Return last exponent
93 return ex_to_numeric((*(seq.end() - 1)).coeff).to_int();
97 epvector::const_iterator it = seq.begin(), itend = seq.end();
100 int max_pow = INT_MIN;
101 while (it != itend) {
102 int pow = it->rest.degree(s);
111 // Lowest degree of variable
112 int series::ldegree(symbol const &s) const
115 // Return first exponent
117 return ex_to_numeric((*(seq.begin())).coeff).to_int();
121 epvector::const_iterator it = seq.begin(), itend = seq.end();
124 int min_pow = INT_MAX;
125 while (it != itend) {
126 int pow = it->rest.ldegree(s);
135 // Coefficient of variable
136 ex series::coeff(symbol const &s, int n) const
139 epvector::const_iterator it = seq.begin(), itend = seq.end();
140 while (it != itend) {
141 int pow = ex_to_numeric(it->coeff).to_int();
150 return convert_to_poly().coeff(s, n);
153 ex series::eval(int level) const
158 // Construct a new series with evaluated coefficients
160 new_seq.reserve(seq.size());
161 epvector::const_iterator it = seq.begin(), itend = seq.end();
162 while (it != itend) {
163 new_seq.push_back(expair(it->rest.eval(level-1), it->coeff));
166 return (new series(var, point, new_seq))->setflag(status_flags::dynallocated | status_flags::evaluated);
169 ex series::evalf(int level) const
171 return convert_to_poly().evalf(level);
176 * Construct expression (polynomial) out of series
179 /** Convert a series object to an ordinary polynomial.
181 * @param no_order flag: discard higher order terms */
182 ex series::convert_to_poly(bool no_order) const
185 epvector::const_iterator it = seq.begin(), itend = seq.end();
187 while (it != itend) {
188 if (is_order_function(it->rest)) {
190 e += Order(power(var - point, it->coeff));
192 e += it->rest * power(var - point, it->coeff);
200 * Implementation of series expansion
203 /** Default implementation of ex::series(). This performs Taylor expansion.
205 ex basic::series(symbol const & s, ex const & point, int order) const
210 ex coeff = deriv.subs(s == point);
211 if (!coeff.is_zero())
212 seq.push_back(expair(coeff, numeric(0)));
215 for (n=1; n<order; n++) {
216 fac = fac.mul(numeric(n));
217 deriv = deriv.diff(s).expand();
218 if (deriv.is_zero()) {
220 return series::series(s, point, seq);
222 coeff = power(fac, -1) * deriv.subs(s == point);
223 if (!coeff.is_zero())
224 seq.push_back(expair(coeff, numeric(n)));
227 // Higher-order terms, if present
228 deriv = deriv.diff(s);
229 if (!deriv.is_zero())
230 seq.push_back(expair(Order(exONE()), numeric(n)));
231 return series::series(s, point, seq);
235 /** Add one series object to another, producing a series object that represents
238 * @param other series object to add with
239 * @return the sum as a series */
240 ex series::add_series(const series &other) const
242 // Adding two series with different variables or expansion points
243 // results in an empty (constant) series
244 if (!is_compatible_to(other)) {
246 nul.push_back(expair(Order(exONE()), exZERO()));
247 return series(var, point, nul);
252 epvector::const_iterator a = seq.begin();
253 epvector::const_iterator b = other.seq.begin();
254 epvector::const_iterator a_end = seq.end();
255 epvector::const_iterator b_end = other.seq.end();
256 int pow_a = INT_MAX, pow_b = INT_MAX;
258 // If a is empty, fill up with elements from b and stop
261 new_seq.push_back(*b);
266 pow_a = ex_to_numeric((*a).coeff).to_int();
268 // If b is empty, fill up with elements from a and stop
271 new_seq.push_back(*a);
276 pow_b = ex_to_numeric((*b).coeff).to_int();
278 // a and b are non-empty, compare powers
280 // a has lesser power, get coefficient from a
281 new_seq.push_back(*a);
282 if (is_order_function((*a).rest))
285 } else if (pow_b < pow_a) {
286 // b has lesser power, get coefficient from b
287 new_seq.push_back(*b);
288 if (is_order_function((*b).rest))
292 // Add coefficient of a and b
293 if (is_order_function((*a).rest) || is_order_function((*b).rest)) {
294 new_seq.push_back(expair(Order(exONE()), (*a).coeff));
295 break; // Order term ends the sequence
297 ex sum = (*a).rest + (*b).rest;
298 if (!(sum == exZERO()))
299 new_seq.push_back(expair(sum, numeric(pow_a)));
305 return series(var, point, new_seq);
309 /** Implementation of ex::series() for sums. This performs series addition when
310 * adding series objects.
313 ex add::series(symbol const & s, ex const & point, int order) const
315 ex acc; // Series accumulator
318 epvector::const_iterator it = seq.begin();
319 epvector::const_iterator itend = seq.end();
321 if (is_ex_exactly_of_type(it->rest, series))
324 acc = it->rest.series(s, point, order);
325 if (!it->coeff.is_equal(exONE()))
326 acc = ex_to_series(acc).mul_const(ex_to_numeric(it->coeff));
330 // Add remaining terms
331 for (; it!=itend; it++) {
333 if (is_ex_exactly_of_type(it->rest, series))
336 op = it->rest.series(s, point, order);
337 if (!it->coeff.is_equal(exONE()))
338 op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
341 acc = ex_to_series(acc).add_series(ex_to_series(op));
346 ex add::series(symbol const & s, ex const & point, int order) const
348 ex acc; // Series accumulator
350 // Get first term from overall_coeff
351 acc = overall_coeff.series(s,point,order);
353 // Add remaining terms
354 epvector::const_iterator it = seq.begin();
355 epvector::const_iterator itend = seq.end();
356 for (; it!=itend; it++) {
358 if (is_ex_exactly_of_type(it->rest, series))
361 op = it->rest.series(s, point, order);
362 if (!it->coeff.is_equal(exONE()))
363 op = ex_to_series(op).mul_const(ex_to_numeric(it->coeff));
366 acc = ex_to_series(acc).add_series(ex_to_series(op));
372 /** Multiply a series object with a numeric constant, producing a series object
373 * that represents the product.
375 * @param other constant to multiply with
376 * @return the product as a series */
377 ex series::mul_const(const numeric &other) const
380 new_seq.reserve(seq.size());
382 epvector::const_iterator it = seq.begin(), itend = seq.end();
383 while (it != itend) {
384 if (!is_order_function(it->rest))
385 new_seq.push_back(expair(it->rest * other, it->coeff));
387 new_seq.push_back(*it);
390 return series(var, point, new_seq);
394 /** Multiply one series object to another, producing a series object that
395 * represents the product.
397 * @param other series object to multiply with
398 * @return the product as a series */
399 ex series::mul_series(const series &other) const
401 // Multiplying two series with different variables or expansion points
402 // results in an empty (constant) series
403 if (!is_compatible_to(other)) {
405 nul.push_back(expair(Order(exONE()), exZERO()));
406 return series(var, point, nul);
409 // Series multiplication
412 const symbol *s = static_cast<symbol *>(var.bp);
413 int a_max = degree(*s);
414 int b_max = other.degree(*s);
415 int a_min = ldegree(*s);
416 int b_min = other.ldegree(*s);
417 int cdeg_min = a_min + b_min;
418 int cdeg_max = a_max + b_max;
420 int higher_order_a = INT_MAX;
421 int higher_order_b = INT_MAX;
422 if (is_order_function(coeff(*s, a_max)))
423 higher_order_a = a_max + b_min;
424 if (is_order_function(other.coeff(*s, b_max)))
425 higher_order_b = b_max + a_min;
426 int higher_order_c = min(higher_order_a, higher_order_b);
427 if (cdeg_max >= higher_order_c)
428 cdeg_max = higher_order_c - 1;
430 for (int cdeg=cdeg_min; cdeg<=cdeg_max; cdeg++) {
432 // c(i)=a(0)b(i)+...+a(i)b(0)
433 for (int i=a_min; cdeg-i>=b_min; i++) {
434 ex a_coeff = coeff(*s, i);
435 ex b_coeff = other.coeff(*s, cdeg-i);
436 if (!is_order_function(a_coeff) && !is_order_function(b_coeff))
437 co += coeff(*s, i) * other.coeff(*s, cdeg-i);
440 new_seq.push_back(expair(co, numeric(cdeg)));
442 if (higher_order_c < INT_MAX)
443 new_seq.push_back(expair(Order(exONE()), numeric(higher_order_c)));
444 return series::series(var, point, new_seq);
448 /** Implementation of ex::series() for product. This performs series multiplication when multiplying series.
451 ex mul::series(symbol const & s, ex const & point, int order) const
453 ex acc; // Series accumulator
456 epvector::const_iterator it = seq.begin();
457 epvector::const_iterator itend = seq.end();
459 if (is_ex_exactly_of_type(it->rest, series))
462 acc = it->rest.series(s, point, order);
463 if (!it->coeff.is_equal(exONE()))
464 acc = ex_to_series(acc).power_const(ex_to_numeric(it->coeff), order);
468 // Multiply with remaining terms
469 for (; it!=itend; it++) {
471 if (op.info(info_flags::numeric)) {
472 // series * const (special case, faster)
473 ex f = power(op, it->coeff);
474 acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
476 } else if (!is_ex_exactly_of_type(op, series))
477 op = op.series(s, point, order);
478 if (!it->coeff.is_equal(exONE()))
479 op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
481 // Series multiplication
482 acc = ex_to_series(acc).mul_series(ex_to_series(op));
488 ex mul::series(symbol const & s, ex const & point, int order) const
490 ex acc; // Series accumulator
492 // Get first term from overall_coeff
493 acc = overall_coeff.series(s, point, order);
495 // Multiply with remaining terms
496 epvector::const_iterator it = seq.begin();
497 epvector::const_iterator itend = seq.end();
498 for (; it!=itend; it++) {
500 if (op.info(info_flags::numeric)) {
501 // series * const (special case, faster)
502 ex f = power(op, it->coeff);
503 acc = ex_to_series(acc).mul_const(ex_to_numeric(f));
505 } else if (!is_ex_exactly_of_type(op, series))
506 op = op.series(s, point, order);
507 if (!it->coeff.is_equal(exONE()))
508 op = ex_to_series(op).power_const(ex_to_numeric(it->coeff), order);
510 // Series multiplication
511 acc = ex_to_series(acc).mul_series(ex_to_series(op));
517 /** Compute the p-th power of a series.
519 * @param p power to compute
520 * @param deg truncation order of series calculation */
521 ex series::power_const(const numeric &p, int deg) const
524 const symbol *s = static_cast<symbol *>(var.bp);
525 int ldeg = ldegree(*s);
527 // Calculate coefficients of powered series
531 co.push_back(co0 = power(coeff(*s, ldeg), p));
532 bool all_sums_zero = true;
533 for (i=1; i<deg; i++) {
535 for (int j=1; j<=i; j++) {
536 ex c = coeff(*s, j + ldeg);
537 if (is_order_function(c)) {
538 co.push_back(Order(exONE()));
541 sum += (p * j - (i - j)) * co[i - j] * c;
544 all_sums_zero = false;
545 co.push_back(co0 * sum / numeric(i));
548 // Construct new series (of non-zero coefficients)
550 bool higher_order = false;
551 for (i=0; i<deg; i++) {
552 if (!co[i].is_zero())
553 new_seq.push_back(expair(co[i], numeric(i) + p * ldeg));
554 if (is_order_function(co[i])) {
559 if (!higher_order && !all_sums_zero)
560 new_seq.push_back(expair(Order(exONE()), numeric(deg) + p * ldeg));
561 return series::series(var, point, new_seq);
565 /** Implementation of ex::series() for powers. This performs Laurent expansion
566 * of reciprocals of series at singularities.
568 ex power::series(symbol const & s, ex const & point, int order) const
571 if (!is_ex_exactly_of_type(basis, series)) {
572 // Basis is not a series, may there be a singulary?
573 if (!exponent.info(info_flags::negint))
574 return basic::series(s, point, order);
576 // Expression is of type something^(-int), check for singularity
577 if (!basis.subs(s == point).is_zero())
578 return basic::series(s, point, order);
580 // Singularity encountered, expand basis into series
581 e = basis.series(s, point, order);
588 return ex_to_series(e).power_const(ex_to_numeric(exponent), order);
592 /** Compute the truncated series expansion of an expression.
593 * This function returns an expression containing an object of class series to
594 * represent the series. If the series does not terminate within the given
595 * truncation order, the last term of the series will be an order term.
597 * @param s expansion variable
598 * @param point expansion point
599 * @param order truncation order of series calculations
600 * @return an expression holding a series object */
601 ex ex::series(symbol const &s, ex const &point, int order) const
604 return bp->series(s, point, order);
609 const series some_series;
610 type_info const & typeid_series = typeid(some_series);