# How to substitute a random sample of variables in Boolean Function

This post is a wiki. Anyone with karma >750 is welcome to improve it.

I want to make a function which generates randomly a Boolean function with n variables and then substitutes k of them with zero. I tried to get the list of terms of the function and then do the substitution which is certainly wrong.

 def Pol_sub(n,k):

if k>n:
print("substitution can't be done")

else:
B=BooleanPolynomialRing(n,'x')
list=sorted((sample(xrange(0,n),k)))
f=B.random_element()
ls=f.terms()
print(f)
zero_vec=[0 for x in xrange(0,k)]

return(ls.subs({ls[list[i]]:zero_vec[i] for i in xrange(0,k)}))

edit retag close merge delete

Sort by ยป oldest newest most voted

You are looking to substitute some x_i by 0 into f

To get the x_i, you can use (i chose k=3, n=7 in this example):

sage: B.gens()
(x0, x1, x2, x3, x4, x5, x6)


To select the first k inderetminates, you can do:

sage: B.gens()[:k]
(x0, x1, x2)


For the substitution, you have to produce a dictionary whose keys are the selected indeterminates and the values are always 0:

sage: in_dict = {g:0 for g in B.gens()[:k]}
sage: in_dict
{x2: 0, x1: 0, x0: 0}


Then you can make your substitution:

sage: f
x0 + x1*x3 + x1*x6 + x2*x4 + x3*x5 + x3
sage: f.subs(in_dict)
x3*x5 + x3

more

( 2016-07-03 00:13:44 +0200 )edit