# Simplification of boolean values  def F(n) :
return exp(log(n))

def test1(n) :
return n == F(n)

[test1(i) for i in (4..8)]


gives [4 == 4, 5 == 5, 6 == 6, 7 == 7, 8 == 8]

def G(n) :
return n

def test2(n) :
return n == G(n)

[test2(i) for i in (4..8)]


gives [True, True, True, True, True]

What do I have to do so that test1 gives the same result as test2?

edit retag close merge delete

Sort by » oldest newest most voted What's going on is that many non-symbolic equalities and inequalities are immediately evaluated but symbolic ones aren't, so that we can have equations. [Otherwise symbolic equations would always be being evaluated, and you could never write "x==2", because it'd be false.] For example:

sage: 2 == 2
True
sage: 2 == 2.0
True
sage: 2 == SR(2)
2 == 2


And in this case, exp(log(n)) is symbolic:

sage: parent(2)
Integer Ring
sage: parent(log(2))
Symbolic Ring
sage: parent(exp(log(2)))
Symbolic Ring


The solution is to call bool explicitly when you want a boolean output:

sage: 2 == exp(log(2))
2 == 2
sage: bool(2 == exp(log(2)))
True


I should also give the standard warning, which is that Sage inherits its definitions of True and False for equations from Maxima: "False" doesn't necessarily mean false, it might only mean "Sage couldn't figure out how to prove it was true."

more