Ask Your Question
1

AttributeError: 'int' object has no attribute 'mod'

asked 2011-05-27 05:47:58 +0100

KKondo gravatar image

updated 2011-05-27 05:58:55 +0100

Hello,

How can I solve the problem with an error message: "AttributeError: 'int' object has no attribute 'mod'" when executing the following program?

maxdeg = 5
maxord = maxdeg
for m in range(maxord + 1):
    for l in range(m, maxdeg + 1):
        print l, m, gen_legendre_P(l, m, z) # this uses "l.mod(2).is_zero()."

Sincerely, KKondo

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2011-05-27 07:00:10 +0100

DSM gravatar image

Hi!

7-8 times out of 10 in Sage you don't want to use range, which is a Python function, and returns Python ints. All the fun stuff lives in the Sage Integer type, and only some functions coerce. You can get a Sage Integer by using srange/sxrange instead of range/xrange:

sage: map(type, range(2))
[<type 'int'>, <type 'int'>]
sage: map(type, srange(2))
[<type 'sage.rings.integer.Integer'>, <type 'sage.rings.integer.Integer'>]
sage: int(2).mod(4)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
[...]
AttributeError: 'int' object has no attribute 'mod'
sage: Integer(2).mod(4)
2

so that

z = var("z")
maxdeg = 5
maxord = maxdeg
for m in srange(maxord + 1):
    for l in srange(m, maxdeg + 1):
        print l, m, gen_legendre_P(l, m, z)

produces

0 0 1
1 0 z
2 0 3/2*(z - 1)^2 + 3*z - 2
3 0 5/2*(z - 1)^3 + 15/2*(z - 1)^2 + 6*z - 5
4 0 35/8*(z - 1)^4 + 35/2*(z - 1)^3 + 45/2*(z - 1)^2 + 10*z - 9
5 0 63/8*(z - 1)^5 + 315/8*(z - 1)^4 + 70*(z - 1)^3 + 105/2*(z - 1)^2 + 15*z - 14
[.. etc]
edit flag offensive delete link more

Comments

Thank you for your answer.

KKondo gravatar imageKKondo ( 2011-05-27 07:14:33 +0100 )edit

Thank you for your answer.

KKondo gravatar imageKKondo ( 2011-05-27 07:14:33 +0100 )edit

Thank you for your answer.

KKondo gravatar imageKKondo ( 2011-05-27 07:14:34 +0100 )edit

Thank you for your answer.

KKondo gravatar imageKKondo ( 2011-05-27 07:14:34 +0100 )edit
0

answered 2011-05-27 07:22:37 +0100

KKondo gravatar image

Thank you for your answer. I understand the sensitive issue concerning the Sage Integer type. I also have tested the following program without any error:

for l in range(m, maxdeg + 1):
    print l, m, bool(p[l] == gen_legendre_P(l, Integer(m), z))
          or
    print l, m, bool(p[l] == gen_legendre_P(l, m+1, z))

Sincerely, KKondo

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2011-05-27 05:47:58 +0100

Seen: 2,837 times

Last updated: May 27 '11