Ask Your Question
0

How to test an identity of rational numbers?

asked 2017-07-15 13:17:59 +0100

roland gravatar image

Hi,

Why is there a difference in output between:

 print 5-1 == 0
 print 3^(1/3) - 3^(1/5) == 0

 False
 3^(1/3) - 3^(1/5) == 0

What can I do if $x,y,s,t \in \mathbb Q$, and I want to test $x^y == s^t$?

Thanks for your support!

Roland

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-07-15 13:28:05 +0100

vdelecroix gravatar image

updated 2017-07-15 13:28:28 +0100

The difference you noticed comes from the fact that 5 and 3^(1/3) have different natures

sage: parent(5)
Integer Ring
sage: parent(3^(1/3))
Symbolic Ring

Comparisons between integers (and more generally rationals) is very different from comparisons between symbolic elements. In order to test x^y == s^t you would better assume that bot y and t are integers (that you can always do by taken appropriate powers). Assuming that you only care about positive numbers you can use

def test_equality(x, y, s, t):
    m = lcm(y.denominator(), t.denominator())
    return x^(y*m) == s^(t*m)
edit flag offensive delete link more
0

answered 2017-07-15 17:42:01 +0100

dan_fulea gravatar image

There is also a difference between the level of evaluation of the two expressions. The sage (man) sees 5-1, and thinks mayby it is better to write a 4 immediately, then seeing 4 == 0... and in the other case thinks, let it be as it comes...

If a True or a False is needed, then we have to require in the second case an explicit evaluation.

sage:  print 3^(1/3) - 3^(1/5) == 0
3^(1/3) - 3^(1/5) == 0
sage:  print bool( 3^(1/3) - 3^(1/5) == 0 )
False

Note also how the sage interpreter reshapes expressions in the following cases:

sage:  print 4 == 0
False
sage:  print 3^(1/5) - 3^(1/5) == 0
0 == 0
sage:  print bool( 3^(1/5) - 3^(1/5) ) == 0
True

sage:  print type( 3^(1/5) - 3^(1/5) )
<type 'sage.symbolic.expression.Expression'>
sage: print type(0)
<type 'sage.rings.integer.Integer'>

sage: print 98^(1/2) - 8^(1/6) == 5184^(1/4)
7*sqrt(2) - 8^(1/6) == 6*4^(1/4)
sage: print bool( 98^(1/2) - 8^(1/6) == 5184^(1/4) )
True

Note: This answer differs only psychologically from the above one. (Please let it above.)

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-07-15 13:17:59 +0100

Seen: 345 times

Last updated: Jul 15 '17