Ask Your Question
0

Help to understand an AttributeError in a polynomial ring

asked 2017-01-25 23:33:10 +0100

Peter Luschny gravatar image

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 flag offensive close merge delete

Comments

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

FrédéricC gravatar imageFrédéricC ( 2017-01-26 08:46:00 +0100 )edit

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.

Peter Luschny gravatar imagePeter Luschny ( 2017-01-26 11:39:32 +0100 )edit

1 Answer

Sort by » oldest newest most voted
2

answered 2017-01-25 23:44:50 +0100

kcrisman gravatar image

updated 2017-01-26 03:29:12 +0100

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

edit flag offensive delete link more

Comments

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.

Peter Luschny gravatar imagePeter Luschny ( 2017-01-25 23:57:59 +0100 )edit

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

kcrisman gravatar imagekcrisman ( 2017-01-26 00:00:18 +0100 )edit

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

kcrisman gravatar imagekcrisman ( 2017-01-26 00:02:19 +0100 )edit

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

Peter Luschny gravatar imagePeter Luschny ( 2017-01-26 00:17:52 +0100 )edit

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

kcrisman gravatar imagekcrisman ( 2017-01-26 03:32:18 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-01-25 23:33:10 +0100

Seen: 398 times

Last updated: Jan 26 '17