# Maple versus Sage, porting issues

Consider the expression

if (m == 3) or (m == -3) : print n, m


Could a simple minded programmer ever expect to see an output different from n, 3 or n, -3 for some n?

No, because it is /verboten/ by logic that m can have a different value from 3 or -3!

Let's check it nevertheless with Maple:

for n from 0 to 50 do
m := n mod 8:
if (m = 3) or (m = -3) then print(n, m) fi od:

3, 3
11, 3
19, 3
27, 3
35, 3
43, 3


And now the same with Sage.

for n in (0..50):
m = mod(n, 8)
if (m == 3) or (m == -3) : print n, m

3 3
5 5
11 3
13 5
19 3
21 5
27 3
29 5
35 3
37 5
43 3
45 5


How many simple minded Maple programmers have broken their necks with such alike?

Could someone please point to a page in the Sage documentation where this is explained and due warnings are given, if such a page exists?

edit retag close merge delete

Sort by » oldest newest most voted

Hi,

It is just because of the modulo operation which returns you not an integer but an element of the quotient Z/nZ.

sage: type(5)
<type 'sage.rings.integer.Integer'>
sage: type(mod(5,2))
<type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'>


This is documented in the function mod.

Now comes the question what should be the comparison of an element of Z with an element of Z/nZ. The Sage's answer is that as there exists a natural coercion from Z to Z/nZ we first convert the element of Z to Z/nZ and then make the test. Notice that if you want the remainder and not the number modulo an other number you may use the symbol % as in

sage: for n in (0..50):
....:     m = n % 8
....:     if m == 3 or m == -3: print n,m
....:
3 3
11 3
19 3
27 3
35 3
43 3

more

Thanks, but it has nothing to do with remainder versus modulo. A Maple user would use the operation irem if wants a remainder. The mod operator is supposed to evaluate an expression over the integers modulo m.

( 2012-08-05 02:18:17 -0600 )edit
1

I do not agree with your assumption about the function mod. Anyway, Sage documentation is very clear : "Return the equivalence class of n modulo m as an element of ZZ/mZZ." Good luck for learning oddity of Sage from the point of view of a Maple user!

( 2012-08-05 03:06:33 -0600 )edit

"I do not agree with your assumption about the function mod." What do you mean by that exactly? What assumption?

( 2012-08-05 03:21:59 -0600 )edit
1

Precisely : "The mod operator is supposed to evaluate an expression over the integers modulo m". But perhaps I misunderstood and you meant "Within Maple, ...". Note that the "Maple" operation "n mod q" is equivalent in Sage to "ZZ(mod(n,q))" which I found more explicit about what is done.

( 2012-08-05 04:40:46 -0600 )edit

Yes, I meant "Within Maple, ...".

( 2012-08-06 01:07:21 -0600 )edit