Ask Your Question
2

defining boolean variables in sage

asked 2015-04-28 08:56:42 +0100

freako89 gravatar image

Hi Guys,

By writing this:

B.<a,b> = BooleanPolynomialRing()

Not only a Boolean Polynomial Ring in 'a' and 'b' is defined, but 'a' and 'b' are also treated as boolean variables.

However, if we write in this manner:

B = BooleanPolynomialRing(names = ['a','b'])

We'll obtain a Boolean Polynomial Ring in 'a' and 'b', but we don't even get 'a' and 'b' as variables.

Is there any way to resolve the issue in the second method, especially, if we have a boolean polynomial ring of >1000 variables? Thanks in advance!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
7

answered 2015-04-28 09:17:40 +0100

slelievre gravatar image

updated 2018-09-02 20:10:11 +0100

There are many ways to call the variables of the boolean polynomial ring B after it has been created, and to assign them to Python variables.

We will list four below. Sometimes you may want to use more than one.

Suppose we have defined, as in the question,

sage: B = BooleanPolynomialRing(names=['a', 'b'])

Ways to access the variables of B include:

  1. Call them as B.0, B.1 (and so on if there are more than two).

    This works only with actuan numbers after the dot; for instance, even if you have defined k = 0 you can't do B.k to get B.0.

  2. Call them as B.gen(0), B.gen(1) (and so on if there are more than two).

    If you need to call these variables in a loop, an iterator, or a sum, this syntax B.gen(k) is usually helpful.

  3. The command B.gens() will provide a tuple of all the variables in B.

    You could use that to give a name to all the generators, say gen_B = B.gens() and then call gen_B[0], gen_B[1] (and so on if there are more than two).

  4. The command B.inject_variables() will make some Python variables with the correct names to call the variables as named in the declaration of B.

    For example,

    sage: B = BooleanPolynomialRing(names=['a', 'b'])
    sage: B.inject_variables()
    sage: (a + b)*(a - b)
    a + b
    

The syntax BooleanPolynomialRing(10, names='a') allows to name the variables a0, a1, etc. For hundreds of variables, this naming scheme is more adapted than using the alphabet.

That syntax combines well with the naming of the tuple of generators, see below.

Create the boolean polynomial ring:

sage: B = BooleanPolynomialRing(10, names='a')
sage: B
Boolean PolynomialRing in a0, a1, a2, a3, a4, a5, a6, a7, a8, a9

Access its variables with method 1:

sage: B.8
a8

or with method 2:

sage: B.gen(8)
a8

Use method 2 with literal indices:

sage: sum(B.gen(k) for k in (1, 3, 5, 7))
a1 + a3 + a5 + a7

Give a name to the tuple of generators, consistent with the naming scheme for the variables, eg a = B.gens(), so that a[0] is a0 and so on.

sage: a = B.gens()
sage: a
(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
sage: a[3]
a3

Use that for looping, summing, etc.

sage: sum(a[k]*a[k+1] for k in (1, 3, 5, 7))
a1*a2 + a3*a4 + a5*a6 + a7*a8

If you have variables named with several letters and numbers, as in

sage: B = BooleanPolynomialRing(7, ['a0', 'a1', 'a2', 'a3', 'b0', 'b1', 'b2'])

then you could inject the variables and use them:

sage: B.inject_variables()
sage: (a0 + b0)*(a1 - b1)
a0*a1 + a0*b1 + a1*b0 + b0*b1

and/or you could do something like

sage: ab = B.gens()
sage: a = ab[:4]
sage: b = ab[4:]

so that

sage: a
(a0, a1, a2, a3)
sage: b
(b0, b1, b2)

and

sage: a[0] + a[3] + b[1]
a0 + a3 + b1
edit flag offensive delete link more

Comments

hi guys, can you give the solution for double variables as "Boolean PolynomialRing in a0, a1, a2, a3, b0, b1, b2"

santu gravatar imagesantu ( 2018-09-01 09:33:10 +0100 )edit
1

I edited my answer to address the case of variables named a0, a1, a2, a3, b0, b1, b2.

slelievre gravatar imageslelievre ( 2018-09-02 20:10:49 +0100 )edit

Actually it can be done also by declaring R.<a1,...,a3.b1,...,b2>=BooleanPolynomialRing(7), but my question was how can i define the ring with 1000 variables e.g. R.<a1,...,a1000,b1,...,b1000> with a single line command e.g. B = BooleanPolynomialRing(10, names='a,b'). and also the problem with that i can't assign any value to a[0],...,a[3] e.g suppose a[0]=a[3].

santu gravatar imagesantu ( 2018-09-04 13:13:49 +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: 2015-04-28 08:56:42 +0100

Seen: 2,108 times

Last updated: Sep 02 '18