Ask Your Question
1

How to substitute a random sample of variables in Boolean Function

asked 2016-07-02 13:05:28 +0100

this post is marked as community wiki

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

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-07-02 21:11:04 +0100

tmonteil gravatar image

updated 2016-07-02 21:11:43 +0100

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

Comments

Thank you.This is so helpful!

kristi gravatar imagekristi ( 2016-07-03 00:13:44 +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

1 follower

Stats

Asked: 2016-07-02 13:05:28 +0100

Seen: 626 times

Last updated: Jul 02 '16