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

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

Sort by » oldest newest most voted

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]

more

( 2011-05-27 07:14:33 +0200 )edit

( 2011-05-27 07:14:33 +0200 )edit

( 2011-05-27 07:14:34 +0200 )edit

( 2011-05-27 07:14:34 +0200 )edit

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

more

## Stats

Seen: 2,648 times

Last updated: May 27 '11