[PATCH 3/3] power::to_polinomial: improve handling of negative powers
Now collect_common_factors((x*y*a+x*y*b)^(-3) + (x*z + x*y)^(-2)) will evaluate to x^(-2)*(x^(-1)*(a+b)^(-3)*y^(-3)+(z+y)^(-2)) which is actually what I expect. --- ginac/normal.cpp | 11 ++++++++++- 1 files changed, 10 insertions(+), 1 deletions(-) diff --git a/ginac/normal.cpp b/ginac/normal.cpp index fc89710..b2a4e8c 100644 --- a/ginac/normal.cpp +++ b/ginac/normal.cpp @@ -2455,7 +2455,16 @@ ex power::to_polynomial(exmap & repl) co if (exponent.info(info_flags::posint)) return power(basis.to_rational(repl), exponent); else if (exponent.info(info_flags::negint)) - return power(replace_with_symbol(power(basis, _ex_1), repl), -exponent); + { + ex basis_pref = collect_common_factors(basis); + if (is_exactly_a<mul>(basis_pref) || is_exactly_a<power>(basis_pref)) { + // (A*B)^n will be automagically transformed to A^n*B^n + ex t = power(basis_pref, exponent); + return t.to_polynomial(repl); + } + else + return power(replace_with_symbol(power(basis, _ex_1), repl), -exponent); + } else return replace_with_symbol(*this, repl); } -- All science is either physics or stamp collecting.
participants (1)
-
Alexei Sheplyakov