ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 26 Jan 2017 11:39:32 +0100Help to understand an AttributeError in a polynomial ringhttps://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 23:33:10 +0100https://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/Comment 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>
https://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 08:46:00 +0100https://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36396#post-id-36396Comment 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>
https://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 11:39:32 +0100https://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36397#post-id-36397Answer 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>
https://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 23:44:50 +0100https://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?answer=36388#post-id-36388Comment 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>
https://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 23:57:59 +0100https://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36389#post-id-36389Comment 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>
https://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.Thu, 26 Jan 2017 00:00:18 +0100https://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36390#post-id-36390Comment 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>
https://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.Thu, 26 Jan 2017 00:02:19 +0100https://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>
https://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?Thu, 26 Jan 2017 00:17:52 +0100https://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>
https://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)`.Thu, 26 Jan 2017 03:32:18 +0100https://ask.sagemath.org/question/36387/help-to-understand-an-attributeerror-in-a-polynomial-ring/?comment=36394#post-id-36394