ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 04 Sep 2018 06:13:49 -0500defining boolean variables in sagehttp://ask.sagemath.org/question/26662/defining-boolean-variables-in-sage/ 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!Tue, 28 Apr 2015 01:56:42 -0500http://ask.sagemath.org/question/26662/defining-boolean-variables-in-sage/Answer by slelievre for <p>Hi Guys,</p>
<p>By writing this:</p>
<pre><code>B.<a,b> = BooleanPolynomialRing()
</code></pre>
<p>Not only a Boolean Polynomial Ring in 'a' and 'b' is defined, but 'a' and 'b' are also treated as boolean variables.</p>
<p>However, if we write in this manner:</p>
<pre><code>B = BooleanPolynomialRing(names = ['a','b'])
</code></pre>
<p>We'll obtain a Boolean Polynomial Ring in 'a' and 'b', but we don't even get 'a' and 'b' as variables.</p>
<p>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!</p>
http://ask.sagemath.org/question/26662/defining-boolean-variables-in-sage/?answer=26663#post-id-26663There 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 + b1Tue, 28 Apr 2015 02:17:40 -0500http://ask.sagemath.org/question/26662/defining-boolean-variables-in-sage/?answer=26663#post-id-26663Comment by santu for <p>There are many ways to call the variables of the boolean polynomial ring <code>B</code>
after it has been created, and to assign them to Python variables.</p>
<p>We will list four below. Sometimes you may want to use more than one.</p>
<p>Suppose we have defined, as in the question,</p>
<pre><code>sage: B = BooleanPolynomialRing(names=['a', 'b'])
</code></pre>
<p>Ways to access the variables of <code>B</code> include:</p>
<ol>
<li><p>Call them as <code>B.0</code>, <code>B.1</code> (and so on if there are more than two).</p>
<p>This works only with actuan numbers after the dot; for instance, even
if you have defined <code>k = 0</code> you can't do <code>B.k</code> to get <code>B.0</code>.</p></li>
<li><p>Call them as <code>B.gen(0)</code>, <code>B.gen(1)</code> (and so on if there are more than two).</p>
<p>If you need to call these variables in a loop, an iterator, or a sum,
this syntax <code>B.gen(k)</code> is usually helpful. </p></li>
<li><p>The command <code>B.gens()</code> will provide a tuple of all the variables in <code>B</code>.</p>
<p>You could use that to give a name to all the generators, say <code>gen_B = B.gens()</code>
and then call <code>gen_B[0]</code>, <code>gen_B[1]</code> (and so on if there are more than two).</p></li>
<li><p>The command <code>B.inject_variables()</code> will make some Python variables with
the correct names to call the variables as named in the declaration of <code>B</code>.</p>
<p>For example,</p>
<pre><code>sage: B = BooleanPolynomialRing(names=['a', 'b'])
sage: B.inject_variables()
sage: (a + b)*(a - b)
a + b
</code></pre></li>
</ol>
<p>The syntax <code>BooleanPolynomialRing(10, names='a')</code> allows to name the variables
<code>a0</code>, <code>a1</code>, etc. For hundreds of variables, this naming scheme is more adapted
than using the alphabet.</p>
<p>That syntax combines well with the naming of the tuple of generators, see below.</p>
<p>Create the boolean polynomial ring:</p>
<pre><code>sage: B = BooleanPolynomialRing(10, names='a')
sage: B
Boolean PolynomialRing in a0, a1, a2, a3, a4, a5, a6, a7, a8, a9
</code></pre>
<p>Access its variables with method 1:</p>
<pre><code>sage: B.8
a8
</code></pre>
<p>or with method 2:</p>
<pre><code>sage: B.gen(8)
a8
</code></pre>
<p>Use method 2 with literal indices:</p>
<pre><code>sage: sum(B.gen(k) for k in (1, 3, 5, 7))
a1 + a3 + a5 + a7
</code></pre>
<p>Give a name to the tuple of generators, consistent with the naming scheme
for the variables, eg <code>a = B.gens()</code>, so that <code>a[0]</code> is <code>a0</code> and so on.</p>
<pre><code>sage: a = B.gens()
sage: a
(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
sage: a[3]
a3
</code></pre>
<p>Use that for looping, summing, etc.</p>
<pre><code>sage: sum(a[k]*a[k+1] for k in (1, 3, 5, 7))
a1*a2 + a3*a4 + a5*a6 + a7*a8
</code></pre>
<p>If you have variables named with several letters and numbers, as in</p>
<pre><code>sage: B = BooleanPolynomialRing(7, ['a0', 'a1', 'a2', 'a3', 'b0', 'b1', 'b2'])
</code></pre>
<p>then you could inject the variables and use them:</p>
<pre><code>sage: B.inject_variables()
sage: (a0 + b0)*(a1 - b1)
a0*a1 + a0*b1 + a1*b0 + b0*b1
</code></pre>
<p>and/or you could do something like</p>
<pre><code>sage: ab = B.gens()
sage: a = ab[:4]
sage: b = ab[4:]
</code></pre>
<p>so that</p>
<pre><code>sage: a
(a0, a1, a2, a3)
sage: b
(b0, b1, b2)
</code></pre>
<p>and</p>
<pre><code>sage: a[0] + a[3] + b[1]
a0 + a3 + b1
</code></pre>
http://ask.sagemath.org/question/26662/defining-boolean-variables-in-sage/?comment=43580#post-id-43580Actually 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].Tue, 04 Sep 2018 06:13:49 -0500http://ask.sagemath.org/question/26662/defining-boolean-variables-in-sage/?comment=43580#post-id-43580Comment by slelievre for <p>There are many ways to call the variables of the boolean polynomial ring <code>B</code>
after it has been created, and to assign them to Python variables.</p>
<p>We will list four below. Sometimes you may want to use more than one.</p>
<p>Suppose we have defined, as in the question,</p>
<pre><code>sage: B = BooleanPolynomialRing(names=['a', 'b'])
</code></pre>
<p>Ways to access the variables of <code>B</code> include:</p>
<ol>
<li><p>Call them as <code>B.0</code>, <code>B.1</code> (and so on if there are more than two).</p>
<p>This works only with actuan numbers after the dot; for instance, even
if you have defined <code>k = 0</code> you can't do <code>B.k</code> to get <code>B.0</code>.</p></li>
<li><p>Call them as <code>B.gen(0)</code>, <code>B.gen(1)</code> (and so on if there are more than two).</p>
<p>If you need to call these variables in a loop, an iterator, or a sum,
this syntax <code>B.gen(k)</code> is usually helpful. </p></li>
<li><p>The command <code>B.gens()</code> will provide a tuple of all the variables in <code>B</code>.</p>
<p>You could use that to give a name to all the generators, say <code>gen_B = B.gens()</code>
and then call <code>gen_B[0]</code>, <code>gen_B[1]</code> (and so on if there are more than two).</p></li>
<li><p>The command <code>B.inject_variables()</code> will make some Python variables with
the correct names to call the variables as named in the declaration of <code>B</code>.</p>
<p>For example,</p>
<pre><code>sage: B = BooleanPolynomialRing(names=['a', 'b'])
sage: B.inject_variables()
sage: (a + b)*(a - b)
a + b
</code></pre></li>
</ol>
<p>The syntax <code>BooleanPolynomialRing(10, names='a')</code> allows to name the variables
<code>a0</code>, <code>a1</code>, etc. For hundreds of variables, this naming scheme is more adapted
than using the alphabet.</p>
<p>That syntax combines well with the naming of the tuple of generators, see below.</p>
<p>Create the boolean polynomial ring:</p>
<pre><code>sage: B = BooleanPolynomialRing(10, names='a')
sage: B
Boolean PolynomialRing in a0, a1, a2, a3, a4, a5, a6, a7, a8, a9
</code></pre>
<p>Access its variables with method 1:</p>
<pre><code>sage: B.8
a8
</code></pre>
<p>or with method 2:</p>
<pre><code>sage: B.gen(8)
a8
</code></pre>
<p>Use method 2 with literal indices:</p>
<pre><code>sage: sum(B.gen(k) for k in (1, 3, 5, 7))
a1 + a3 + a5 + a7
</code></pre>
<p>Give a name to the tuple of generators, consistent with the naming scheme
for the variables, eg <code>a = B.gens()</code>, so that <code>a[0]</code> is <code>a0</code> and so on.</p>
<pre><code>sage: a = B.gens()
sage: a
(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
sage: a[3]
a3
</code></pre>
<p>Use that for looping, summing, etc.</p>
<pre><code>sage: sum(a[k]*a[k+1] for k in (1, 3, 5, 7))
a1*a2 + a3*a4 + a5*a6 + a7*a8
</code></pre>
<p>If you have variables named with several letters and numbers, as in</p>
<pre><code>sage: B = BooleanPolynomialRing(7, ['a0', 'a1', 'a2', 'a3', 'b0', 'b1', 'b2'])
</code></pre>
<p>then you could inject the variables and use them:</p>
<pre><code>sage: B.inject_variables()
sage: (a0 + b0)*(a1 - b1)
a0*a1 + a0*b1 + a1*b0 + b0*b1
</code></pre>
<p>and/or you could do something like</p>
<pre><code>sage: ab = B.gens()
sage: a = ab[:4]
sage: b = ab[4:]
</code></pre>
<p>so that</p>
<pre><code>sage: a
(a0, a1, a2, a3)
sage: b
(b0, b1, b2)
</code></pre>
<p>and</p>
<pre><code>sage: a[0] + a[3] + b[1]
a0 + a3 + b1
</code></pre>
http://ask.sagemath.org/question/26662/defining-boolean-variables-in-sage/?comment=43557#post-id-43557I edited my answer to address the case of variables named a0, a1, a2, a3, b0, b1, b2.Sun, 02 Sep 2018 13:10:49 -0500http://ask.sagemath.org/question/26662/defining-boolean-variables-in-sage/?comment=43557#post-id-43557Comment by santu for <p>There are many ways to call the variables of the boolean polynomial ring <code>B</code>
after it has been created, and to assign them to Python variables.</p>
<p>We will list four below. Sometimes you may want to use more than one.</p>
<p>Suppose we have defined, as in the question,</p>
<pre><code>sage: B = BooleanPolynomialRing(names=['a', 'b'])
</code></pre>
<p>Ways to access the variables of <code>B</code> include:</p>
<ol>
<li><p>Call them as <code>B.0</code>, <code>B.1</code> (and so on if there are more than two).</p>
<p>This works only with actuan numbers after the dot; for instance, even
if you have defined <code>k = 0</code> you can't do <code>B.k</code> to get <code>B.0</code>.</p></li>
<li><p>Call them as <code>B.gen(0)</code>, <code>B.gen(1)</code> (and so on if there are more than two).</p>
<p>If you need to call these variables in a loop, an iterator, or a sum,
this syntax <code>B.gen(k)</code> is usually helpful. </p></li>
<li><p>The command <code>B.gens()</code> will provide a tuple of all the variables in <code>B</code>.</p>
<p>You could use that to give a name to all the generators, say <code>gen_B = B.gens()</code>
and then call <code>gen_B[0]</code>, <code>gen_B[1]</code> (and so on if there are more than two).</p></li>
<li><p>The command <code>B.inject_variables()</code> will make some Python variables with
the correct names to call the variables as named in the declaration of <code>B</code>.</p>
<p>For example,</p>
<pre><code>sage: B = BooleanPolynomialRing(names=['a', 'b'])
sage: B.inject_variables()
sage: (a + b)*(a - b)
a + b
</code></pre></li>
</ol>
<p>The syntax <code>BooleanPolynomialRing(10, names='a')</code> allows to name the variables
<code>a0</code>, <code>a1</code>, etc. For hundreds of variables, this naming scheme is more adapted
than using the alphabet.</p>
<p>That syntax combines well with the naming of the tuple of generators, see below.</p>
<p>Create the boolean polynomial ring:</p>
<pre><code>sage: B = BooleanPolynomialRing(10, names='a')
sage: B
Boolean PolynomialRing in a0, a1, a2, a3, a4, a5, a6, a7, a8, a9
</code></pre>
<p>Access its variables with method 1:</p>
<pre><code>sage: B.8
a8
</code></pre>
<p>or with method 2:</p>
<pre><code>sage: B.gen(8)
a8
</code></pre>
<p>Use method 2 with literal indices:</p>
<pre><code>sage: sum(B.gen(k) for k in (1, 3, 5, 7))
a1 + a3 + a5 + a7
</code></pre>
<p>Give a name to the tuple of generators, consistent with the naming scheme
for the variables, eg <code>a = B.gens()</code>, so that <code>a[0]</code> is <code>a0</code> and so on.</p>
<pre><code>sage: a = B.gens()
sage: a
(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9)
sage: a[3]
a3
</code></pre>
<p>Use that for looping, summing, etc.</p>
<pre><code>sage: sum(a[k]*a[k+1] for k in (1, 3, 5, 7))
a1*a2 + a3*a4 + a5*a6 + a7*a8
</code></pre>
<p>If you have variables named with several letters and numbers, as in</p>
<pre><code>sage: B = BooleanPolynomialRing(7, ['a0', 'a1', 'a2', 'a3', 'b0', 'b1', 'b2'])
</code></pre>
<p>then you could inject the variables and use them:</p>
<pre><code>sage: B.inject_variables()
sage: (a0 + b0)*(a1 - b1)
a0*a1 + a0*b1 + a1*b0 + b0*b1
</code></pre>
<p>and/or you could do something like</p>
<pre><code>sage: ab = B.gens()
sage: a = ab[:4]
sage: b = ab[4:]
</code></pre>
<p>so that</p>
<pre><code>sage: a
(a0, a1, a2, a3)
sage: b
(b0, b1, b2)
</code></pre>
<p>and</p>
<pre><code>sage: a[0] + a[3] + b[1]
a0 + a3 + b1
</code></pre>
http://ask.sagemath.org/question/26662/defining-boolean-variables-in-sage/?comment=43548#post-id-43548hi guys, can you give the solution for double variables as "Boolean PolynomialRing in a0, a1, a2, a3, b0, b1, b2"Sat, 01 Sep 2018 02:33:10 -0500http://ask.sagemath.org/question/26662/defining-boolean-variables-in-sage/?comment=43548#post-id-43548Answer by santu for <p>Hi Guys,</p>
<p>By writing this:</p>
<pre><code>B.<a,b> = BooleanPolynomialRing()
</code></pre>
<p>Not only a Boolean Polynomial Ring in 'a' and 'b' is defined, but 'a' and 'b' are also treated as boolean variables.</p>
<p>However, if we write in this manner:</p>
<pre><code>B = BooleanPolynomialRing(names = ['a','b'])
</code></pre>
<p>We'll obtain a Boolean Polynomial Ring in 'a' and 'b', but we don't even get 'a' and 'b' as variables.</p>
<p>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!</p>
http://ask.sagemath.org/question/26662/defining-boolean-variables-in-sage/?answer=43547#post-id-43547hi guys, can you give the solution for double variables as "Boolean PolynomialRing in a0, a1, a2, a3, b0, b1, b2"Sat, 01 Sep 2018 02:30:51 -0500http://ask.sagemath.org/question/26662/defining-boolean-variables-in-sage/?answer=43547#post-id-43547