Separate a fraction

1,936 views
Skip to first unread message

Gaurav Sathe

unread,
Feb 29, 2012, 1:48:03 AM2/29/12
to sy...@googlegroups.com
If 'f' is a fraction, can someone please tell me how to separate the numerator and denominator of f????  I tried f.numerator and f.denominator but it doesnt seem to work...

Sean Vig

unread,
Feb 29, 2012, 1:53:42 AM2/29/12
to sy...@googlegroups.com
This is documented in the online docs [1] and in the docstring. If you're using ipython, you can use their introspection to check the docstring, for example, as '?Rational'. From the docstring:

**Low-level**

Access numerator and denominator as .p and .q:

>>> r = Rational(3,4)
>>> r
3/4
>>> r.p
3
>>> r.q
4


Sean

On Wed, Feb 29, 2012 at 00:48, Gaurav Sathe <gaurav....@gmail.com> wrote:
If 'f' is a fraction, can someone please tell me how to separate the numerator and denominator of f????  I tried f.numerator and f.denominator but it doesnt seem to work...

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sympy/-/kvXX5VvGsTAJ.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.

Aaron Meurer

unread,
Feb 29, 2012, 2:02:21 AM2/29/12
to sy...@googlegroups.com
A better way is to use .as_numer_denom(), which also works on symbolic
fractions:

In [380]: Rational(3, 4).as_numer_denom()
(3, 4)

In [381]: (x/y).as_numer_denom()
(x, y)

There's also the fraction() function, which does almost the same thing:

In [382]: fraction(Rational(3, 4))
(3, 4)

In [383]: fraction(x/y)
(x, y)

Aaron Meurer

Chris Smith

unread,
Feb 29, 2012, 2:05:21 AM2/29/12
to sy...@googlegroups.com
also...

>>> numer(a/y)
a
>>> denom(a/y)
y

fraction, numer and denom don't re-write the expression as a fraction:

>>> numer(a/y+x)
a/y + x

Gaurav Sathe

unread,
Feb 29, 2012, 2:18:33 AM2/29/12
to sy...@googlegroups.com
Yep that works... Thnx a lot Sean, Aaron and Chris

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.



--
Gaurav Sathe
-Student at BITS Pilani - Goa Campus


Reply all
Reply to author
Forward
0 new messages