Ways of using(assigning) variables in Sage
I need to test an nvariable Boolean Function f = f(x0,...,xn1)
. I need to fix x0, then run some tests for the g1 = f(x1,...,xn1)
, then fix x1 and so on.
The problem is that I don't really understand how to do it with Sage.
At first, I tried to create a vector of values, that controls "fixing" of the variables
R.<x0,x1,x2,x3> = BooleanPolynomialRing()
v = [None,1,None, 0]
if v[0] != None:
x0=v[0]
if v[1] != None:
x1=v[1]
if v[2] != None:
x2=v[2]
if v[3] != None:
x3=v[3]
f = BooleanFunction(x0+x3+x0*x1+x0*x1*x2)
print(f.algebraic_normal_form())
output:x0*x2
This works fine, but it doesn't fit my task because I want to be able to automate the fixing process.
I want to replace the "if
"s with a loop, but in this case, I don't know how to address variables inside the loop using indexes.
I'm new to Sage so I would appreciate any advice!
1 answer

I'm not sure what
BooleanFunction
is, but:sage: R.<x0,x1,x2,x3> = BooleanPolynomialRing()
If at this point you do something like
x1=1
, thenx1
is no longer a generator of this ring, so let's try to avoid that.sage: f = x0+x3+x0*x1+x0*x1*x2 # f is in R sage: f.substitute({x1: 1}) x0*x2 + x3
I think what you want is a good way to carry out the
substitute
part of this. A helpful observation: you can convert strings to variable names:sage: R('x0') x0
So:
sage: d = {} sage: for i in range(len(v)): ....: if v[i] is not None: ....: d[R('x' + str(i))] = v[i] ....: sage: d {x1: 1, x3: 0} sage: f.substitute(d) x0*x2