- ex e;
- if (!is_ex_exactly_of_type(basis, pseries)) {
- // Basis is not a series, may there be a singularity?
- bool must_expand_basis = false;
- try {
- basis.subs(r);
- } catch (pole_error) {
- must_expand_basis = true;
- }
-
- // Is the expression of type something^(-int)?
- if (!must_expand_basis && !exponent.info(info_flags::negint))
- return basic::series(r, order, options);
-
- // Is the expression of type 0^something?
- if (!must_expand_basis && !basis.subs(r).is_zero())
- return basic::series(r, order, options);
-
- // Singularity encountered, expand basis into series
- e = basis.series(r, order, options);
+ // If basis is already a series, just power it
+ if (is_exactly_a<pseries>(basis))
+ return ex_to<pseries>(basis).power_const(ex_to<numeric>(exponent), order);
+
+ // Basis is not a series, may there be a singularity?
+ bool must_expand_basis = false;
+ try {
+ basis.subs(r, subs_options::no_pattern);
+ } catch (pole_error) {
+ must_expand_basis = true;
+ }
+
+ bool exponent_is_regular = true;
+ try {
+ exponent.subs(r, subs_options::no_pattern);
+ } catch (pole_error) {
+ exponent_is_regular = false;
+ }
+
+ if (!exponent_is_regular) {
+ ex l = exponent*log(basis);
+ // this == exp(l);
+ ex le = l.series(r, order, options);
+ // Note: expanding exp(l) won't help, since that will attempt
+ // Taylor expansion, and fail (because exponent is "singular")
+ // Still l itself might be expanded in Taylor series.
+ // Examples:
+ // sin(x)/x*log(cos(x))
+ // 1/x*log(1 + x)
+ return exp(le).series(r, order, options);
+ // Note: if l happens to have a Laurent expansion (with
+ // negative powers of (var - point)), expanding exp(le)
+ // will barf (which is The Right Thing).
+ }
+
+ // Is the expression of type something^(-int)?
+ if (!must_expand_basis && !exponent.info(info_flags::negint)
+ && (!is_a<add>(basis) || !is_a<numeric>(exponent)))
+ return basic::series(r, order, options);
+
+ // Is the expression of type 0^something?
+ if (!must_expand_basis && !basis.subs(r, subs_options::no_pattern).is_zero()
+ && (!is_a<add>(basis) || !is_a<numeric>(exponent)))
+ return basic::series(r, order, options);
+
+ // Singularity encountered, is the basis equal to (var - point)?
+ if (basis.is_equal(r.lhs() - r.rhs())) {
+ epvector new_seq;
+ if (ex_to<numeric>(exponent).to_int() < order)
+ new_seq.push_back(expair(_ex1, exponent));
+ else
+ new_seq.push_back(expair(Order(_ex1), exponent));
+ return pseries(r, std::move(new_seq));
+ }
+
+ // No, expand basis into series
+
+ numeric numexp;
+ if (is_a<numeric>(exponent)) {
+ numexp = ex_to<numeric>(exponent);