Ask Your Question

Truth value of an expression (Boolean Polynomials)

asked 2013-11-25 20:20:18 -0500

pp gravatar image

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?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-11-25 20:39:16 -0500

tmonteil gravatar image

You can try:

sage: bool(expr(s0=True, s1=True))

You can also fix only some variables:

sage: expr(s0=True)
s1 + 1
edit flag offensive delete link more


@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?

pp gravatar imagepp ( 2013-11-26 00:33:16 -0500 )edit

@tmonteil But, more than 256 variables is not allowed in this method!

pp gravatar imagepp ( 2013-11-26 05:17:18 -0500 )edit

@tmonteil Moreover, I found that instead of doing calculations over finite of degree 2, it simply calculates the value!!

pp gravatar imagepp ( 2013-11-26 05:32:40 -0500 )edit

Well, 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 . 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.

tmonteil gravatar imagetmonteil ( 2013-11-26 07:56:23 -0500 )edit

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: 2013-11-25 20:20:18 -0500

Seen: 110 times

Last updated: Nov 25 '13