I am trying to extract numerator and denominator of expression using sympy.fraction method. The code is as follows-
from sympy import *
x=symbols('x')
a=1/(1+1/x)
print(fraction(a))
The output that I want is
(x,x+1)
but it is outputting
(1, 1+1/x)
Ask for simplification of the expression first:
>>> from sympy import *
>>> var('x')
x
>>> a = 1/(1+1/x)
>>> a.simplify()
x/(x + 1)
>>> fraction(a.simplify())
(x, x + 1)
The docstring of fraction
says:
This function will not make any attempt to simplify nested fractions or to do any term rewriting at all.
The rewrite you want is done by together
.
print(fraction(together(a))) # (x, x + 1)
simplify
has the same effect, but it's a heavier tool that tries a lot of various simplification techniques. The output of specific functions like together
, cancel
, factor
, etc is more predictable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With