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.Sat, 05 Jan 2019 04:53:24 -0600Sage not detecting integers in Boolean Polynomial Ring?http://ask.sagemath.org/question/44880/sage-not-detecting-integers-in-boolean-polynomial-ring/Hi all,
I'm trying to do a detecting system for terms in the equations used in a polynomial. I'm using Boolean Polynomials here.
Here's is my code. This prints all the terms of the equations.
P.<x,y,z> = BooleanPolynomialRing(3, order = 'lex')
equations = [x+y+z-1, y*z, x+z]
for i in range(len(equations)):
for j in equations[i]:
print j
Output:
x
y
z
1
y*z
x
z
As you can see, 1 appears in the output. However, when I implement this logic onto it, the output is empty.
for i in range(len(equations)):
for j in equations[i]:
if j == 1:
print 'true'
It's not like the logic doesn't work, if I change '1' to 'x', my logic works.
for i in range(len(equations)):
for j in equations[i]:
if j == x:
print 'true'
Output:
true
true
What am I doing wrong here?Thu, 03 Jan 2019 22:54:41 -0600http://ask.sagemath.org/question/44880/sage-not-detecting-integers-in-boolean-polynomial-ring/Comment by slelievre for <p>Hi all,</p>
<p>I'm trying to do a detecting system for terms in the equations used in a polynomial. I'm using Boolean Polynomials here.</p>
<p>Here's is my code. This prints all the terms of the equations.</p>
<pre><code>P.<x,y,z> = BooleanPolynomialRing(3, order = 'lex')
equations = [x+y+z-1, y*z, x+z]
for i in range(len(equations)):
for j in equations[i]:
print j
Output:
x
y
z
1
y*z
x
z
</code></pre>
<p>As you can see, 1 appears in the output. However, when I implement this logic onto it, the output is empty.</p>
<pre><code>for i in range(len(equations)):
for j in equations[i]:
if j == 1:
print 'true'
</code></pre>
<p>It's not like the logic doesn't work, if I change '1' to 'x', my logic works.</p>
<pre><code>for i in range(len(equations)):
for j in equations[i]:
if j == x:
print 'true'
Output:
true
true
</code></pre>
<p>What am I doing wrong here?</p>
http://ask.sagemath.org/question/44880/sage-not-detecting-integers-in-boolean-polynomial-ring/?comment=44904#post-id-44904Note: instead of
for i in range(len(equations)):
for j in equations[i]:
if j == 1:
print 'true'
you could use
for eq in equations:
for j in eq:
if j == P.one():
print('true')Sat, 05 Jan 2019 04:53:24 -0600http://ask.sagemath.org/question/44880/sage-not-detecting-integers-in-boolean-polynomial-ring/?comment=44904#post-id-44904Answer by rburing for <p>Hi all,</p>
<p>I'm trying to do a detecting system for terms in the equations used in a polynomial. I'm using Boolean Polynomials here.</p>
<p>Here's is my code. This prints all the terms of the equations.</p>
<pre><code>P.<x,y,z> = BooleanPolynomialRing(3, order = 'lex')
equations = [x+y+z-1, y*z, x+z]
for i in range(len(equations)):
for j in equations[i]:
print j
Output:
x
y
z
1
y*z
x
z
</code></pre>
<p>As you can see, 1 appears in the output. However, when I implement this logic onto it, the output is empty.</p>
<pre><code>for i in range(len(equations)):
for j in equations[i]:
if j == 1:
print 'true'
</code></pre>
<p>It's not like the logic doesn't work, if I change '1' to 'x', my logic works.</p>
<pre><code>for i in range(len(equations)):
for j in equations[i]:
if j == x:
print 'true'
Output:
true
true
</code></pre>
<p>What am I doing wrong here?</p>
http://ask.sagemath.org/question/44880/sage-not-detecting-integers-in-boolean-polynomial-ring/?answer=44884#post-id-44884The problem can be seen as follows:
sage: list(P(1))
[1]
sage: list(P(1))[0]
1
sage: list(P(1))[0] == 1
False
sage: type(list(P(1))[0])
<type 'sage.rings.polynomial.pbori.BooleanMonomial'>
When you convert a `BooleanPolynomial` to a list (which you do implicitly by iterating with `in`), the elements are of type `BooleanMonomial`, which apparently do not have comparison with the integer 1 implemented (which is probably not intentional but an oversight; I will report it).
A workaround is: when you want to compare with a constant such as 1, first convert it into the right ring (or other parent object):
sage: list(P(1))[0] == P(1)
True
So the workaround is to compare to `P(1)` instead of `1`.
Edit: I submitted this as [trac ticket #27019](https://trac.sagemath.org/ticket/27019).Fri, 04 Jan 2019 03:21:23 -0600http://ask.sagemath.org/question/44880/sage-not-detecting-integers-in-boolean-polynomial-ring/?answer=44884#post-id-44884Comment by slelievre for <p>The problem can be seen as follows:</p>
<pre><code>sage: list(P(1))
[1]
sage: list(P(1))[0]
1
sage: list(P(1))[0] == 1
False
sage: type(list(P(1))[0])
<type 'sage.rings.polynomial.pbori.BooleanMonomial'>
</code></pre>
<p>When you convert a <code>BooleanPolynomial</code> to a list (which you do implicitly by iterating with <code>in</code>), the elements are of type <code>BooleanMonomial</code>, which apparently do not have comparison with the integer 1 implemented (which is probably not intentional but an oversight; I will report it).</p>
<p>A workaround is: when you want to compare with a constant such as 1, first convert it into the right ring (or other parent object):</p>
<pre><code>sage: list(P(1))[0] == P(1)
True
</code></pre>
<p>So the workaround is to compare to <code>P(1)</code> instead of <code>1</code>.</p>
<p>Edit: I submitted this as <a href="https://trac.sagemath.org/ticket/27019">trac ticket #27019</a>.</p>
http://ask.sagemath.org/question/44880/sage-not-detecting-integers-in-boolean-polynomial-ring/?comment=44903#post-id-44903Even better: use `P.one()` instead of `P(1)`.Sat, 05 Jan 2019 04:50:44 -0600http://ask.sagemath.org/question/44880/sage-not-detecting-integers-in-boolean-polynomial-ring/?comment=44903#post-id-44903