Ask Your Question
0

Why is SageMath generating errors during compilation?

asked 2020-04-07 09:23:25 -0500

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
b=[0,0]
for r in srange(0,2):
    b[r]=random.randint(0,3^15-1)

for i in srange(1,4):
    q=NP(b[0])+NP(b[1])
    print q

Here are the errors:

 File "file.sage.py", line 16, in <module>
    q=NP(b[_sage_const_0 ])+NP(b[_sage_const_1 ])
  File "file.sage.py", 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
3

answered 2020-04-07 10:35:18 -0500

tmonteil gravatar image

updated 2020-04-07 10:39:22 -0500

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

or

  • 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
3

answered 2020-04-07 10:33:26 -0500

updated 2020-04-07 11:31:24 -0500

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

b[r]=Integer(random.randint(0,3^15-1))

I would also recommend changing the last line to

print(q)

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

Stats

Asked: 2020-04-07 09:23:25 -0500

Seen: 52 times

Last updated: Apr 07