Ask Your Question

defining boolean variables in sage

asked 2015-04-28 01:56:42 -0500

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

answered 2015-04-28 02:17:40 -0500

slelievre gravatar image

updated 2015-04-28 02:53:13 -0500

Once B is created, its variables can be accessed (a) as B.0, B.1, and so on, and (b) as B.gen(0), B.gen(1), and so on. Use the second syntax if you need a literal index, eg B.gen(k), in a loop, a sum, etc. The command B.gens() will provide a tuple of all the variables in 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 (a):

sage: B.8

or with method (b):

sage: B.gen(8)

Use method (b) 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]

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

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


Asked: 2015-04-28 01:56:42 -0500

Seen: 83 times

Last updated: Apr 28 '15