ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 06 Aug 2012 08:07:21 +0200Maple versus Sage, porting issueshttps://ask.sagemath.org/question/9210/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?
Sun, 05 Aug 2012 07:30:48 +0200https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/Answer by vdelecroix for <p>Consider the expression</p>
<pre><code>if (m == 3) or (m == -3) : print n, m
</code></pre>
<p>Could a simple minded programmer ever expect to see an output
different from n, 3 or n, -3 for some n? </p>
<p>No, because it is /verboten/ by logic that m can have a
different value from 3 or -3!</p>
<p>Let's check it nevertheless with Maple:</p>
<pre><code>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
</code></pre>
<p>And now the same with Sage. </p>
<pre><code>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
</code></pre>
<p>How many simple minded Maple programmers have broken their necks with such alike?</p>
<p>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? </p>
https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/?answer=13899#post-id-13899Hi,
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 3Sun, 05 Aug 2012 07:59:13 +0200https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/?answer=13899#post-id-13899Comment by vdelecroix for <p>Hi,</p>
<p>It is just because of the modulo operation which returns you not an integer but an element of the quotient Z/nZ.</p>
<pre><code>sage: type(5)
<type 'sage.rings.integer.Integer'>
sage: type(mod(5,2))
<type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'>
</code></pre>
<p>This is documented in the function mod.</p>
<p>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 <strong>remainder</strong> and not the number <strong>modulo</strong> an other number you may use the symbol % as in</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/?comment=19278#post-id-19278I 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!Sun, 05 Aug 2012 10:06:33 +0200https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/?comment=19278#post-id-19278Comment by petropolis for <p>Hi,</p>
<p>It is just because of the modulo operation which returns you not an integer but an element of the quotient Z/nZ.</p>
<pre><code>sage: type(5)
<type 'sage.rings.integer.Integer'>
sage: type(mod(5,2))
<type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'>
</code></pre>
<p>This is documented in the function mod.</p>
<p>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 <strong>remainder</strong> and not the number <strong>modulo</strong> an other number you may use the symbol % as in</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/?comment=19277#post-id-19277"I do not agree with your assumption about the function mod." What do you mean by that exactly? What assumption?Sun, 05 Aug 2012 10:21:59 +0200https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/?comment=19277#post-id-19277Comment by petropolis for <p>Hi,</p>
<p>It is just because of the modulo operation which returns you not an integer but an element of the quotient Z/nZ.</p>
<pre><code>sage: type(5)
<type 'sage.rings.integer.Integer'>
sage: type(mod(5,2))
<type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'>
</code></pre>
<p>This is documented in the function mod.</p>
<p>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 <strong>remainder</strong> and not the number <strong>modulo</strong> an other number you may use the symbol % as in</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/?comment=19270#post-id-19270Yes, I meant "Within Maple, ...". Mon, 06 Aug 2012 08:07:21 +0200https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/?comment=19270#post-id-19270Comment by petropolis for <p>Hi,</p>
<p>It is just because of the modulo operation which returns you not an integer but an element of the quotient Z/nZ.</p>
<pre><code>sage: type(5)
<type 'sage.rings.integer.Integer'>
sage: type(mod(5,2))
<type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'>
</code></pre>
<p>This is documented in the function mod.</p>
<p>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 <strong>remainder</strong> and not the number <strong>modulo</strong> an other number you may use the symbol % as in</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/?comment=19279#post-id-19279Thanks, 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.Sun, 05 Aug 2012 09:18:17 +0200https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/?comment=19279#post-id-19279Comment by vdelecroix for <p>Hi,</p>
<p>It is just because of the modulo operation which returns you not an integer but an element of the quotient Z/nZ.</p>
<pre><code>sage: type(5)
<type 'sage.rings.integer.Integer'>
sage: type(mod(5,2))
<type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'>
</code></pre>
<p>This is documented in the function mod.</p>
<p>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 <strong>remainder</strong> and not the number <strong>modulo</strong> an other number you may use the symbol % as in</p>
<pre><code>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
</code></pre>
https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/?comment=19276#post-id-19276Precisely : "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.Sun, 05 Aug 2012 11:40:46 +0200https://ask.sagemath.org/question/9210/maple-versus-sage-porting-issues/?comment=19276#post-id-19276