- 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);
- } else {
- // Basis is a series
- e = basis;
+ // 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;
+ }
+
+ // Is the expression of type something^(-int)?
+ if (!must_expand_basis && !exponent.info(info_flags::negint) && !is_a<add>(basis))
+ 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))
+ 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, new_seq);
+ }
+
+ // No, expand basis into series
+
+ numeric numexp = ex_to<numeric>(exponent);
+ const ex& sym = r.lhs();
+ // find existing minimal degree
+ int real_ldegree = basis.expand().ldegree(sym-r.rhs());
+ if (real_ldegree == 0) {
+ int orderloop = 0;
+ do {
+ orderloop++;
+ real_ldegree = basis.series(r, orderloop, options).ldegree(sym);
+ } while (real_ldegree == orderloop);