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.Mon, 05 Feb 2018 17:02:52 +0100Truth value of an expression (Boolean Polynomials)https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/I need to find the truth value of an expression. Consider this:
sage: R = BooleanPolynomialRing( 2, \
['s%d'%(i) for i in range (2)])
sage: expr = R('s0') + R('s0')*R('s1')
Now I need `expr` is to be evaluated; given, say, both `s0` and `s1` are `True`. How can we do that?
Tue, 26 Nov 2013 03:20:18 +0100https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/Answer by tmonteil for <p>I need to find the truth value of an expression. Consider this:</p>
<pre><code>sage: R = BooleanPolynomialRing( 2, \
['s%d'%(i) for i in range (2)])
sage: expr = R('s0') + R('s0')*R('s1')
</code></pre>
<p>Now I need <code>expr</code> is to be evaluated; given, say, both <code>s0</code> and <code>s1</code> are <code>True</code>. How can we do that?</p>
https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?answer=12206#post-id-12206You can try:
sage: bool(expr(s0=True, s1=True))
True
You can also fix only some variables:
sage: expr(s0=True)
s1 + 1
Tue, 26 Nov 2013 03:39:16 +0100https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?answer=12206#post-id-12206Comment by pp for <p>You can try:</p>
<pre><code>sage: bool(expr(s0=True, s1=True))
True
</code></pre>
<p>You can also fix only some variables:</p>
<pre><code>sage: expr(s0=True)
s1 + 1
</code></pre>
https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=40981#post-id-40981Another problem, how can I use something like:
`(expr([s'%d'%(i)=True for i in range(2)]))`
?Mon, 05 Feb 2018 16:54:07 +0100https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=40981#post-id-40981Comment by pp for <p>You can try:</p>
<pre><code>sage: bool(expr(s0=True, s1=True))
True
</code></pre>
<p>You can also fix only some variables:</p>
<pre><code>sage: expr(s0=True)
s1 + 1
</code></pre>
https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=40982#post-id-40982Should I create a ticket?Mon, 05 Feb 2018 16:56:23 +0100https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=40982#post-id-40982Comment by pp for <p>You can try:</p>
<pre><code>sage: bool(expr(s0=True, s1=True))
True
</code></pre>
<p>You can also fix only some variables:</p>
<pre><code>sage: expr(s0=True)
s1 + 1
</code></pre>
https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=40983#post-id-40983Besides, `bool(expr(s0=True, s1=True))` is not a solution; rather `expr(s0=True, s1=True) & 1` is close to a solution.Mon, 05 Feb 2018 17:02:52 +0100https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=40983#post-id-40983Comment by tmonteil for <p>You can try:</p>
<pre><code>sage: bool(expr(s0=True, s1=True))
True
</code></pre>
<p>You can also fix only some variables:</p>
<pre><code>sage: expr(s0=True)
s1 + 1
</code></pre>
https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=16610#post-id-16610Well, this how a boolean expression should be called, see `expr.__call__?` for more details. Now, if you want to evaluate according to a dictionary, you can type `expr.substitute(d)` or `bool(expr.substitute(d))`. I didn't check, but if you need more than 256 variables and it does not work, you should open a ticket on the trac server http://trac.sagemath.org . Concerning the values, this is the way Python handles boolean: 0 stands for `False` and any positive int stands for `True`, you can notice that it is consistent with the addition as the or `and` the multiplication as the `and`. By the way, to see all available methods that can be applied to `expr`, you can type `expr.[TABULATION]` and see the list.Tue, 26 Nov 2013 14:56:23 +0100https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=16610#post-id-16610Comment by pp for <p>You can try:</p>
<pre><code>sage: bool(expr(s0=True, s1=True))
True
</code></pre>
<p>You can also fix only some variables:</p>
<pre><code>sage: expr(s0=True)
s1 + 1
</code></pre>
https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=16612#post-id-16612@tmonteil But, more than 256 variables is not allowed in this method!Tue, 26 Nov 2013 12:17:18 +0100https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=16612#post-id-16612Comment by pp for <p>You can try:</p>
<pre><code>sage: bool(expr(s0=True, s1=True))
True
</code></pre>
<p>You can also fix only some variables:</p>
<pre><code>sage: expr(s0=True)
s1 + 1
</code></pre>
https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=16611#post-id-16611@tmonteil Moreover, I found that instead of doing calculations over finite of degree 2, it simply calculates the value!!Tue, 26 Nov 2013 12:32:40 +0100https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=16611#post-id-16611Comment by pp for <p>You can try:</p>
<pre><code>sage: bool(expr(s0=True, s1=True))
True
</code></pre>
<p>You can also fix only some variables:</p>
<pre><code>sage: expr(s0=True)
s1 + 1
</code></pre>
https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=16615#post-id-16615@tmonteil Thanks. In my current situation, I get the truth values of the variables from solving a SAT problem. Thus, I have a dictionary like `{s0: True, s1: False}`, I need that these values are put to the expression. But I am stuck with that! Can you help me?Tue, 26 Nov 2013 07:33:16 +0100https://ask.sagemath.org/question/10775/truth-value-of-an-expression-boolean-polynomials/?comment=16615#post-id-16615