Ask Your Question

Why is SageMath generating errors during compilation?

asked 2020-04-07 16:23:25 +0200

Whenever compile the following codes, it generates errors. But I don't know why is such errors occurred.

 F.<x> = GF(3^15)
def NP(a):
    return F(a.digits(3))

import random
for r in srange(0,2):

for i in srange(1,4):
    print q

Here are the errors:

 File "", line 16, in <module>
    q=NP(b[_sage_const_0 ])+NP(b[_sage_const_1 ])
  File "", line 8, in NP
    return F(a.digits(_sage_const_3 )) #integer 2 polynomial
AttributeError: 'int' object has no attribute 'digits'
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2020-04-07 17:35:18 +0200

tmonteil gravatar image

updated 2020-04-07 17:39:22 +0200

This is because random.randint(0,3^15-1) returns a Python int and not a Sage Integer, and only this latter has a digits method. To fix this, you can either

  • convert this int into a Sage Integer, by replacing random.randint(0,3^15-1) with ZZ(random.randint(0,3^15-1))


  • ask Sage to directly produce a random Sage integer, by replacing random.randint(0,3^15-1) with ZZ.random_element(0,3^15) Note that in this case, the upper bound becomes excluded by default, so you have to remove the -1.
edit flag offensive delete link more

answered 2020-04-07 17:33:26 +0200

updated 2020-04-07 18:31:24 +0200

The issue is that Python integers are not the same as Sage integers: Sage integers have more functionality. The command random.randint(...) returns a Python integer, so you need to convert it to a Sage integer, for example with


I would also recommend changing the last line to


The syntax print q will stop working with recent releases of Sage. (print with no parentheses is valid Python 2 syntax but not valid Python 3 syntax, and as of version 9.0, Sage is based on Python 3.)

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

1 follower


Asked: 2020-04-07 16:23:25 +0200

Seen: 117 times

Last updated: Apr 07 '20