Ask Your Question
1

How to dynamically add boolean variables?

asked 2017-01-13 09:02:13 +0100

asante gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2017-01-16 12:32:12 +0100

ndomes gravatar image

updated 2017-01-16 20:09:31 +0100

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[0].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[0].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[0].parent()

updated answer:

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
edit flag offensive delete link more

Comments

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

asante gravatar imageasante ( 2017-01-16 15:20:05 +0100 )edit

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

ndomes gravatar imagendomes ( 2017-01-16 20:07:44 +0100 )edit

:) +1 for your update, this nicely comprehends your first part.

asante gravatar imageasante ( 2017-01-17 10:02:26 +0100 )edit
0

answered 2017-01-15 13:45:13 +0100

tmonteil gravatar image

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]
edit flag offensive delete link more

Comments

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?

asante gravatar imageasante ( 2017-01-16 15:22:27 +0100 )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

Stats

Asked: 2017-01-13 09:02:13 +0100

Seen: 480 times

Last updated: Jan 16 '17