Ask Your Question

Simplification of boolean values

asked 2012-02-07 08:47:50 -0500

petropolis gravatar image

updated 2012-02-07 08:49:45 -0500

DSM gravatar image
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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-02-07 08:55:30 -0500

DSM gravatar image

updated 2012-02-07 08:58:00 -0500

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
sage: 2 == 2.0
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)))

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."

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


Asked: 2012-02-07 08:47:50 -0500

Seen: 148 times

Last updated: Feb 07 '12