Possible bug when using Rational(1, 2) for sqrt instead of S.Half

24 views
Skip to first unread message

Nicolas Patry

unread,
Sep 4, 2017, 3:31:51 PM9/4/17
to sympy
Hello everyone,

I think I identified a bug. It originated in two sets not being equal.
Here i will write sqrt instead of Pow(..., Rational(1, 2)) for readability.
{exp(sqrt(3/8)), exp(-sqrt(3/8)} and {exp(sqrt(2)*sqrt(3)/4), exp(-sqrt(2)*sqrt(3/4)}

After going deep into this bug, it appears the problem lies with the ordering function of FiniteSet.

FiniteSet seems to order improperly the first set, and this cannot do the correct comparison.
the function 'ordered' seems to have trouble with Pow(...., Rational(1, 2)) because it works correctly when I use
Pow(...., S.Half).

Is there is a reason for this behaviour ? It seems the two mechanisms should give the same results.
Also to be noted, the raw comparison ( < ) gives the correct result no matter the arguments.


Here is the minimal failing test I could come up with exhibiting the behaviour
    from sympy import Pow, Rational                                            
    l = [Pow(3, Rational(1, 2)), -Pow(3, Rational(1, 2))]                      
    assert list(ordered(tuple(l), warn=True)) == [-Pow(3, Rational(1, 2)), Pow(3, Rational(1, 2))]

I am still trying to figure out why there is a difference, but I want to make sure there is not some intended purpose for this.


Cheers,
Nicolas Patry

Aaron Meurer

unread,
Sep 4, 2017, 4:16:22 PM9/4/17
to sy...@googlegroups.com
Are you sure this is the source of the problem? Rational(1, 2) and S.Half should give the exact same object:

>>> Rational(1, 2) is S.Half
True

and furthermore sqrt(x) is just a shortcut function for Pow(x, S.Half). 

By the way, ordered() isn't designed to give numerical order, just a consistent order. 

It looks like the problem has to do with exp(-sqrt(2)*sqrt(3/4)), which is exp(-sqrt(6)/2), vs. exp(-sqrt(3/8)), which is exp(-sqrt(6)/4) (the other one is correct because you have the 4 outside the sqrt()).

Aaron Meurer


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/2d73e756-7450-4d69-be95-8e2cdb493bb5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages