Request: Have the len function output a Sage Integer instead of a Python int In the following code (See https://sagecell.sagemath.org/?q=cctcfj ):

a=[4,5,6]

b=[0,1,2]

for i in zip([0,1,2],a):

print i,"divided by",len(a),"is equal to",i/len(a)

a=[4,5,6]

b=range(len(a))

print(b)

for i in zip(b,a):

print i,"divided by",len(a),"is equal to",i/len(a)

Gives the following output

0 divided by 3 is equal to 0

1 divided by 3 is equal to 1/3

2 divided by 3 is equal to 2/3

[0, 1, 2]
0 divided by 3 is equal to 0

1 divided by 3 is equal to 0

2 divided by 3 is equal to 0

Greetings

-A

edit retag close merge delete

Sort by » oldest newest most voted In the second part you are using Python ints instead of SageMath Integers.

Use srange (with universe=ZZ if you're not passing an Integer) instead of range, or convert to an Integer before the division, e.g. Integer(i)/len(a).

more

I think python int's should be converted to sage's; but it is a good workaround. I still think it is a bug.

It is not a bug. One should be aware of the difference between int and Integer. The second part of your code uses int division, there is no magic Sage can do to change that. Just be careful to write srange when you want Integers.

1

Thank you very much for the clarifications, the quick response, and for the suggestions, which solved the problems in the code I had.

However, the same behaviour occurs if I change range by srange. The problem seems to be that zip reverts sage Integer to Python int.

Is that intended?

Ah, there is a subtlety with srange (explained in the documentation) that by default the output consists of numbers of the type you put in. You can override that by passing universe=ZZ to srange; I've added this to the answer.

As you can check, srange already had produced sage Integers, and not ints... so the problem is rather in the zip command, I think.

I will study how zip behaves a bit further.