Ask Your Question
0

Maple versus Sage, porting issues

asked 2012-08-05 07:30:48 +0100

petropolis gravatar image

updated 2012-08-05 08:01:09 +0100

vdelecroix gravatar image

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

1 Answer

Sort by ยป oldest newest most voted
6

answered 2012-08-05 07:59:13 +0100

vdelecroix gravatar image

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
edit flag offensive delete link more

Comments

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.

petropolis gravatar imagepetropolis ( 2012-08-05 09:18:17 +0100 )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!

vdelecroix gravatar imagevdelecroix ( 2012-08-05 10:06:33 +0100 )edit

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

petropolis gravatar imagepetropolis ( 2012-08-05 10:21:59 +0100 )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.

vdelecroix gravatar imagevdelecroix ( 2012-08-05 11:40:46 +0100 )edit

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

petropolis gravatar imagepetropolis ( 2012-08-06 08:07:21 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2012-08-05 07:30:48 +0100

Seen: 791 times

Last updated: Aug 05 '12