Ask Your Question
1

How to substitute a random sample of variables in Boolean Function

asked 2016-07-02 06:05:28 -0500

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 14:11:04 -0500

tmonteil gravatar image

updated 2016-07-02 14:11:43 -0500

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 it is so helpful!

kristina gravatar imagekristina ( 2016-07-02 17:13:44 -0500 )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 06:05:28 -0500

Seen: 32 times

Last updated: Jul 02 '16