mod(k, n) versus k.mod(n)
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?
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
One important difference: k.mod(0) = mod(k,0) = k for all k, but k % 0 leads to ZeroDivisionError.
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.
...
Asked: 2026-02-24 20:30:56 +0100
Seen: 137 times
Last updated: Feb 25
Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.