# 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?

edit retag close merge delete

For 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(...)

Thanks 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.

Sort by » oldest newest most voted

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.

more

No, neither srange(1,10) nor [1..9] works. Same behavior as described. Even when I use v = fermat(Integer(n)) it's the same.

Yes, the train wifi cut off my correction. I will try again.

I cannot get it to accept my correct answer and have to leave the train. Edit: finally got it.

Is it rightly expected that a polynomial will be returned in all cases, yes? Then this is a programming error?

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