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:
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
.
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.
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).
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