Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Simplify (1/x)^a / x^2 in sympy

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading