Somewhat similar to this question, I was trying to evaluate a Boolean expression given the right hand side variables in Sage. For simplicity, say, my Boolean expression is, $y=x_0+x_1$. For each of $(x_0,x_1) \in \{(0,0),(0,1),(1,0),(1,1)\}$, I want to evaluate $y$.
This is the basic code block to get started. Note that, when I tried separate substitution, it works. But it does not work when I tried to automatically substitute variables.
B = BooleanPolynomialRing(3, ['x0', 'x1', 'y'])
B.inject_variables()
y = x0 + x1
# This is what I want to evaluate (working fine, but cannot automate, see below)
print (eval('y').subs(x0=0, x1=0))
print (eval('y').subs(x0=0, x1=1))
print (eval('y').subs(x0=1, x1=0))
print (eval('y').subs(x0=1, x1=1))
# This is the part where I tried to automate
from itertools import product
for x in product([0, 1], repeat=2):
print (y) #### How to automatically substitute variables?
I tried multiple ways including various substitution, eval, sage_eval and exec. Got various errors including
SyntaxError: keyword can't be an expressionSyntaxError: invalid syntaxAttributeError: 'bool' object has no attribute 'items'SyntaxError: invalid syntaxKeyError: 'x'AttributeError: 'sage.rings.polynomial.pbori.pbori.BooleanMonomial' object has no attribute 'items'TypeError: subs() takes at most 1 positional argument (2 given)
Therefore, my query is how can I automatically substitute x-variables to evaluate y?
This works for me:
First, no need for
eval('z'): justzworks fine. Second, pass a dictionary tosubs, keys are the "variables" likex0, values are what you want to substitute for them.This is a little more "automatic":
and even more:
This assumes that the generators for
Blook like(x0, x1, ..., xn, y)and you are going to substitute the entries ofxfor all but the last of them:B.gens()gives the generators, andB.gens()[:-1]gives all but the last generator.