Ask Your Question

Truth value of an expression (Boolean Polynomials)

asked 2013-11-26 03:20:18 +0200

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-26 03:39:16 +0200

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 07:33:16 +0200 )edit

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

pp gravatar imagepp ( 2013-11-26 12:17:18 +0200 )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 12:32:40 +0200 )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 14:56:23 +0200 )edit

Another problem, how can I use something like: (expr([s'%d'%(i)=True for i in range(2)])) ?

pp gravatar imagepp ( 2018-02-05 16:54:07 +0200 )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-26 03:20:18 +0200

Seen: 460 times

Last updated: Nov 26 '13