Ask Your Question
1

mod(k, n) versus k.mod(n)

asked 2026-02-24 20:30:56 +0100

Peter Luschny gravatar image

Where can I read about the difference between mod(k, n) and k.mod(n)? I naively assumed that these were just two different ways of writing the same thing. Is that correct?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
2

answered 2026-02-25 00:09:05 +0100

Max Alekseyev gravatar image

updated 2026-02-25 00:10:06 +0100

The two entities may appear identical when printed, but they are different objects:

  • k.mod(n) computes the (integer) remainder of division and is equivalent to k % n;
  • mod(k,n) creates an object representing the residue class of k modulo n, that is, $k + n\mathbb Z$.

Take a look:

sage: ZZ(10).mod(3)
1
sage: type( ZZ(10).mod(3) )
<class 'sage.rings.integer.Integer'>

sage: r = mod(10,3); print(r)
1
sage: type( r )
<class 'sage.rings.finite_rings.integer_mod.IntegerMod_int'>
sage: r.modulus()
3
sage: r.lift()
1
edit flag offensive delete link more

Comments

One important difference: k.mod(0) = mod(k,0) = k for all k, but k % 0 leads to ZeroDivisionError.

Peter Luschny gravatar imagePeter Luschny ( 2026-02-25 09:00:46 +0100 )edit
1

answered 2026-02-25 21:36:28 +0100

To answer your question about where to read about the difference, the live documentation might help. Compare the output from these:

sage: a = 10
sage: a.mod?
Signature:      a.mod(self, I)
Docstring:     
Return a representative for "self" modulo the ideal I (or the ideal
generated by the elements of I if I is not an ideal.)
... (and then some examples are provided)

vs.

sage: mod?
Signature:      mod(n, m, parent=None)
Call signature: mod(*args, **kwds)
Type:           cython_function_or_method
String form:    <cyfunction Mod at 0x10a31ca00>
File:           ~/Sage/git/sage/src/sage/rings/finite_rings/integer_mod.pyx
Docstring:     
Return the equivalence class of n modulo m as an element of \ZZ/m\ZZ.
...
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: 2026-02-24 20:30:56 +0100

Seen: 137 times

Last updated: Feb 25