ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 07 Feb 2012 15:55:30 +0100Simplification of boolean valueshttps://ask.sagemath.org/question/8701/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?
Tue, 07 Feb 2012 15:47:50 +0100https://ask.sagemath.org/question/8701/simplification-of-boolean-values/Answer by DSM for <pre><code>def F(n) :
return exp(log(n))
def test1(n) :
return n == F(n)
[test1(i) for i in (4..8)]
</code></pre>
<p>gives <code>[4 == 4, 5 == 5, 6 == 6, 7 == 7, 8 == 8]</code></p>
<pre><code>def G(n) :
return n
def test2(n) :
return n == G(n)
[test2(i) for i in (4..8)]
</code></pre>
<p>gives <code>[True, True, True, True, True]</code></p>
<p>What do I have to do so that test1 gives the same result as test2?</p>
https://ask.sagemath.org/question/8701/simplification-of-boolean-values/?answer=13258#post-id-13258What'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."
Tue, 07 Feb 2012 15:55:30 +0100https://ask.sagemath.org/question/8701/simplification-of-boolean-values/?answer=13258#post-id-13258