# How to dynamically add boolean variables? I want to experiment with Groebner bases for a system of equations in boolean unknowns. In order to simplify equations I would like to dynamically insert new variables for some terms. Is this possible somehow?

An example could be:

F = BooleanPolynomialRing(4, ["x0", "x1", "x2", "x3"])
F.inject_variables()
eqns = [x0 + x1*x2 + x3 + 1, x0 + x1*x2*x3, x0 + 1]
# now add 'y == x1*x2'
eqns = [x0 + y + x3 + 1, x0 + y*x3, x0 + 1] # NameError: name 'y' is not defined

edit retag close merge delete

Sort by » oldest newest most voted I can't see any method for adding new variables to a boolean polynomial ring. I think you have to define a new ring with additional variables. There seems to be nothing like substitute_expressions. So I used replacing sub-strings as a workaround.

F0 = BooleanPolynomialRing(4,'x')
F0.inject_variables()
eqns = [x0 + x1*x2 + x3 + 1, x0 + x1*x2*x3, x0 + 1]
print eqns, eqns.parent()
# defining new ring with additional variable y
F1 = BooleanPolynomialRing(5, 'x0,x1,x2,x3,y')
F1.inject_variables()
# changing ring for all equations
eqns1 = [F1(eq) for eq in eqns]
print eqns1, eqns1.parent()
# replacing expressions
eqns2 = [eval(str(eq).replace('x1*x2','y')) for eq in eqns1]
# defining new ring by removing variables
F2 = F1.remove_var(x1,x2)
# changing ring for all equations
eqns2 = [F2(eq) for eq in eqns2]
print eqns2, eqns2.parent()


def add_variables(R,new_vars):
"""
R           - BooleanPolynomialRing
new_vars    - string of comma- or space separated variable names
"""
if ',' in new_vars:
new = new_vars.split(',')
else:
new = new_vars.split(' ')
R_new = BooleanPolynomialRing(R.n_variables()+len(new),list(R.variable_names())+new)
R_new.inject_variables()
return R_new

more

Yes, that's what I initially thought of - but my hope was, that there is something more "clever".

IMHO my proposal is quite "clever" ;-) Using python functions for more convenience: see updated answer.

I am not sure about the meaning of your question, but is there something wrong with using Python names ?

sage: F = BooleanPolynomialRing(4, ["x0", "x1", "x2", "x3"])
sage: F.inject_variables()
Defining x0, x1, x2, x3
sage: y = x1*x2
sage: eqns = [x0 + y + x3 + 1, x0 + y*x3, x0 + 1]
sage: eqns
[x0 + x1*x2 + x3 + 1, x0 + x1*x2*x3, x0 + 1]

more

Oh, that's actually a good idea, I hadn't thought of this :D But I still have another concern: I might want to actually add the variable y to the system of equations, so that the Groebner basis algorithms work with y - maybe this has some advantages for Groebner bases? Using an ordinary python variable does not help in this way, right?