1

A long operation with sympy yielded the expression (1/x)^a / x^2, which one would normally write as x^(-2-a). What is the right sequence of sympy operations that I can apply to arrive to the simplified form?

I have tried the following, and none of them seems to simplify the expression at all.

import sympy as sym

expr = sym.sympify("(1/x)^a / x^2")

print(sym.simplify(expr))
print(sym.expand_power_exp(expr))
print(sym.expand_power_base(expr, force=True))
print(sym.powsimp(expr, force=True))
print(sym.collect(expr, "x"))
print(sym.ratsimp(expr))

# prints (1/x)**a/x**2 every time
Carlos Pinzón
  • 1,286
  • 2
  • 15
  • 24

1 Answers1

4

That simplification can only occurs when x is positive.

from sympy import *
x = symbols("x", positive=True)
expr = sympify("(1/x)^a / x^2", locals={"x": x})
powsimp(expr)
Davide_sd
  • 10,578
  • 3
  • 18
  • 30
  • Accepted. Do you know how to do it if `x` was a whole expression, say `y+z`? I mean, simplifying `(1/(y+z))^a / (y+z)^2` – Carlos Pinzón Jul 17 '23 at 13:24
  • 2
    Sadly, sympy's assumption system is not that advanced. In this case I would replace `y+z` with a positive symbol, perform the simplification, then substitute the symbol with `y+z`. – Davide_sd Jul 17 '23 at 14:03
  • 1
    If y and z and a are all declared positive then it will work. Otherwise `powsimp((1/(y+z))**x / (y+z)**x, force=True)` can be used. – Oscar Benjamin Jul 17 '23 at 17:10
  • 1
    This method also works: `sym.powdenest(expr, force=True)` – Carlos Pinzón Aug 02 '23 at 13:52