Difference between fraction and as_numer_denom

86 views
Skip to first unread message

Paul Royik

unread,
Mar 26, 2015, 1:09:15 PM3/26/15
to sy...@googlegroups.com
I used as_numer_denom earlier and now found fraction.
Is there any difference between them?

Aaron Meurer

unread,
Mar 26, 2015, 7:40:39 PM3/26/15
to sy...@googlegroups.com
It looks like fraction takes into account assumptions, but on the
other hand, it doesn't do any rewriting of the expression to combine
sums of fractions (e.g., fraction(1/x + 1/y) gives (1/x + 1/y, 1),
whereas (1/x + 1/y).as_numer_denom() gives (x + y, x*y)).

Aaron Meurer

On Thu, Mar 26, 2015 at 12:09 PM, Paul Royik <distan...@gmail.com> wrote:
> I used as_numer_denom earlier and now found fraction.
> Is there any difference between them?
>
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/64b73b09-213c-4735-a80b-f5a37a4c2766%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Paul Royik

unread,
Mar 27, 2015, 5:28:28 AM3/27/15
to sy...@googlegroups.com
Both fraction and as_numer_denom return (2x+2, sin(x)+1) for expression 2(x+1)/(sin(x)+1), i.e. they rewrite numerator and denominator.
I suggest simple fix for fraction: substitute last line return Mul(*numer), Mul(*denom) with return Mul(*numer, evaluate=False), Mul(*denom, evaluate=False)
For as_numer_denom fix seems harder to implement.

Is it possible to change them?

Chris Smith

unread,
Mar 27, 2015, 10:31:58 AM3/27/15
to sy...@googlegroups.com
That seems reasonable since fraction is allowing the expr to rewrite:

>>> 2*e
2*(x + 1)/(x - 1)
>>> fraction(_)
(2*x + 2, x - 1)

Aaron Meurer

unread,
Mar 27, 2015, 11:28:57 AM3/27/15
to sy...@googlegroups.com
Isn't this just because the Mul evaluates itself. I don't think it's
actually fraction() that is rewriting things.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/12fcfa1f-5701-400f-ace6-31ee0aa00daf%40googlegroups.com.

Paul Royik

unread,
Mar 27, 2015, 2:47:29 PM3/27/15
to sy...@googlegroups.com
This dosn't seem reasonable.
Expanding goes on the line Mul(*numer). Mul(*numer, evaluate=False) fixes things.

Aaron Meurer

unread,
Mar 27, 2015, 3:51:52 PM3/27/15
to sy...@googlegroups.com
We need to just remove this automatic distribution of numbers. Until
we do that, we will continue to have confusing behavior like this. See
https://github.com/sympy/sympy/issues/4596 for more information.

Aaron Meurer
> https://groups.google.com/d/msgid/sympy/c592330a-4b9c-4812-a342-4e44573aac2b%40googlegroups.com.

Paul Royik

unread,
Mar 27, 2015, 6:04:50 PM3/27/15
to sy...@googlegroups.com
I see now.
Reply all
Reply to author
Forward
0 new messages