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.Thu, 26 Jan 2017 04:39:32 -0600Help to understand an AttributeError in a polynomial ringhttp://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/ F. Chapoton recently wrote a program
the behaviour of which I do not understand.
def fermat(n):
q = polygen(ZZ, 'q')
return sum(n ** j * binomial(n, j) * (-1) ** (i + n + j) *
binomial(n - 2 - j + 1, i + 1) * q ** i
for j in range(n - 1)
for i in range(n - 1 - j))
Now consider:
v = fermat(5)
print v.parent()
print v.list()
This outputs
Univariate Polynomial Ring in q over Integer Ring
[821, 181, 21, 1]
which is fine. However the loop
for n in (1..9):
v = fermat(n)
print v.parent()
print v.list()
gives the errors:
AttributeError: 'int' object has no attribute 'parent'
AttributeError: 'int' object has no attribute 'list'
What happens here?
Wed, 25 Jan 2017 16:33:10 -0600http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/Comment by Peter Luschny for <p>F. Chapoton recently wrote a program
the behaviour of which I do not understand.</p>
<pre><code>def fermat(n):
q = polygen(ZZ, 'q')
return sum(n ** j * binomial(n, j) * (-1) ** (i + n + j) *
binomial(n - 2 - j + 1, i + 1) * q ** i
for j in range(n - 1)
for i in range(n - 1 - j))
</code></pre>
<p>Now consider:</p>
<pre><code>v = fermat(5)
print v.parent()
print v.list()
</code></pre>
<p>This outputs</p>
<pre><code>Univariate Polynomial Ring in q over Integer Ring
[821, 181, 21, 1]
</code></pre>
<p>which is fine. However the loop</p>
<pre><code>for n in (1..9):
v = fermat(n)
print v.parent()
print v.list()
</code></pre>
<p>gives the errors:</p>
<pre><code>AttributeError: 'int' object has no attribute 'parent'
AttributeError: 'int' object has no attribute 'list'
</code></pre>
<p>What happens here? </p>
http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36397#post-id-36397Thanks Frédéric. If you write your comment as an answer I will accept it. So all this has nothing to do with the preparser or range formats as suggested in kcrisman's answer.Thu, 26 Jan 2017 04:39:32 -0600http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36397#post-id-36397Comment by FrédéricC for <p>F. Chapoton recently wrote a program
the behaviour of which I do not understand.</p>
<pre><code>def fermat(n):
q = polygen(ZZ, 'q')
return sum(n ** j * binomial(n, j) * (-1) ** (i + n + j) *
binomial(n - 2 - j + 1, i + 1) * q ** i
for j in range(n - 1)
for i in range(n - 1 - j))
</code></pre>
<p>Now consider:</p>
<pre><code>v = fermat(5)
print v.parent()
print v.list()
</code></pre>
<p>This outputs</p>
<pre><code>Univariate Polynomial Ring in q over Integer Ring
[821, 181, 21, 1]
</code></pre>
<p>which is fine. However the loop</p>
<pre><code>for n in (1..9):
v = fermat(n)
print v.parent()
print v.list()
</code></pre>
<p>gives the errors:</p>
<pre><code>AttributeError: 'int' object has no attribute 'parent'
AttributeError: 'int' object has no attribute 'list'
</code></pre>
<p>What happens here? </p>
http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36396#post-id-36396For n=1, the sum is empty and by default this gives a python int. That is because I simplified my program for oeis. If you care, you need to add R=q.parent() and then use R.sum(...)Thu, 26 Jan 2017 01:46:00 -0600http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36396#post-id-36396Answer by kcrisman for <p>F. Chapoton recently wrote a program
the behaviour of which I do not understand.</p>
<pre><code>def fermat(n):
q = polygen(ZZ, 'q')
return sum(n ** j * binomial(n, j) * (-1) ** (i + n + j) *
binomial(n - 2 - j + 1, i + 1) * q ** i
for j in range(n - 1)
for i in range(n - 1 - j))
</code></pre>
<p>Now consider:</p>
<pre><code>v = fermat(5)
print v.parent()
print v.list()
</code></pre>
<p>This outputs</p>
<pre><code>Univariate Polynomial Ring in q over Integer Ring
[821, 181, 21, 1]
</code></pre>
<p>which is fine. However the loop</p>
<pre><code>for n in (1..9):
v = fermat(n)
print v.parent()
print v.list()
</code></pre>
<p>gives the errors:</p>
<pre><code>AttributeError: 'int' object has no attribute 'parent'
AttributeError: 'int' object has no attribute 'list'
</code></pre>
<p>What happens here? </p>
http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?answer=36388#post-id-36388Old answer:
> Preparsing is what happened here ...
That was wrong. The problem is that fermat(1) ends up doing sum([]) which defaults to the Python summation, not Sage summation, and so you just get the Python result of a sum, which is the int zero (as an empty sum). I'm not 100% sure how to fix that but you could at the very least special-case it. I don't think srange would help here.Wed, 25 Jan 2017 16:44:50 -0600http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?answer=36388#post-id-36388Comment by kcrisman for <p>Old answer:</p>
<blockquote>
<p>Preparsing is what happened here ...</p>
</blockquote>
<p>That was wrong. The problem is that fermat(1) ends up doing sum([]) which defaults to the Python summation, not Sage summation, and so you just get the Python result of a sum, which is the int zero (as an empty sum). I'm not 100% sure how to fix that but you could at the very least special-case it. I don't think srange would help here.</p>
http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36394#post-id-36394I don't know that this is a programming error unless this is in Sage itself. If it's just a program someone wrote for their own purposes they may not have really cared about `fermat(1)`.Wed, 25 Jan 2017 20:32:18 -0600http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36394#post-id-36394Comment by kcrisman for <p>Old answer:</p>
<blockquote>
<p>Preparsing is what happened here ...</p>
</blockquote>
<p>That was wrong. The problem is that fermat(1) ends up doing sum([]) which defaults to the Python summation, not Sage summation, and so you just get the Python result of a sum, which is the int zero (as an empty sum). I'm not 100% sure how to fix that but you could at the very least special-case it. I don't think srange would help here.</p>
http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36391#post-id-36391I cannot get it to accept my correct answer and have to leave the train. Edit: finally got it.Wed, 25 Jan 2017 17:02:19 -0600http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36391#post-id-36391Comment by Peter Luschny for <p>Old answer:</p>
<blockquote>
<p>Preparsing is what happened here ...</p>
</blockquote>
<p>That was wrong. The problem is that fermat(1) ends up doing sum([]) which defaults to the Python summation, not Sage summation, and so you just get the Python result of a sum, which is the int zero (as an empty sum). I'm not 100% sure how to fix that but you could at the very least special-case it. I don't think srange would help here.</p>
http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36392#post-id-36392Is it rightly expected that a polynomial will be returned in all cases, yes? Then this is a programming error?Wed, 25 Jan 2017 17:17:52 -0600http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36392#post-id-36392Comment by kcrisman for <p>Old answer:</p>
<blockquote>
<p>Preparsing is what happened here ...</p>
</blockquote>
<p>That was wrong. The problem is that fermat(1) ends up doing sum([]) which defaults to the Python summation, not Sage summation, and so you just get the Python result of a sum, which is the int zero (as an empty sum). I'm not 100% sure how to fix that but you could at the very least special-case it. I don't think srange would help here.</p>
http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36390#post-id-36390Yes, the train wifi cut off my correction. I will try again.Wed, 25 Jan 2017 17:00:18 -0600http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36390#post-id-36390Comment by Peter Luschny for <p>Old answer:</p>
<blockquote>
<p>Preparsing is what happened here ...</p>
</blockquote>
<p>That was wrong. The problem is that fermat(1) ends up doing sum([]) which defaults to the Python summation, not Sage summation, and so you just get the Python result of a sum, which is the int zero (as an empty sum). I'm not 100% sure how to fix that but you could at the very least special-case it. I don't think srange would help here.</p>
http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36389#post-id-36389No, neither srange(1,10) nor [1..9] works. Same behavior as described. Even when I use v = fermat(Integer(n)) it's the same.Wed, 25 Jan 2017 16:57:59 -0600http://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36389#post-id-36389