Symmetric modulo function
This functionality does not run in MATLAB.
mods(x, m) computes the integer r of least absolute value such that the integer x - r is divisible by the integer m.
If m is a non-zero integer and x is an integer, then mods returns an integer r such that x = qm + r holds for some integer q. In addition, we have for mods. See Example 2. These conditions uniquely define r.
If m is a non-zero integer and x is a rational number, say for two non-zero coprime integers u and v, then mods computes an integral solution r of the congruence . To this end, they first compute an inverse w of v modulo m, such that vw - 1 is divisible by m. This only works if v is coprime to m, i.e., if their greatest common divisor is 1. Then mods(u*w, m), as described above, is returned. Otherwise, if v and m are not coprime, then an error message is returned. See Example 2.
If the second argument m is 0 or a rational number, then an error message is returned.
_mod(x, m) is the functional equivalent of the operator notation x mod m. See Example 1.
The function mods can be used to redefine the modulo operator. E.g., after the assignment _mod:=mods, both the operator mod and the equivalent function _mod return remainders of least absolute value. See Example 3.
mods is a kernel function.
By default the operator mod and the function _mod are equivalent to modp. This can be changed by assigning a new value to _mod. See Example 3.
The example demonstrates the correspondence between the function _mod and the operator mod:
23 mod 5 = _mod(23,5)
Here are some examples where the modulus is an integer. We see that mod and modp are equivalent by default:
27 mod 3, 27 mod 4, modp(27, 4), mods(27, 4)
27 = (27 div 4)*4 + modp(27, 4)
Let us now compute modulo 5. The greatest common divisor of 3 and 5 is 1, and 2 is an inverse of 3 modulo 5. Thus modulo 5 equals 22* 2 modulo 5:
modp(22/3, 5) = modp(22*2, 5), mods(22/3, 5) = mods(22*2, 5)
The greatest common divisor of 15 and 27 is 3, so that 15 has no inverse modulo 27 and the following command fails:
Error: The modular inverse does not exist. [modp]
However, we can compute modulo 26, since 15 and 26 are coprime:
-22/15 mod 26
By default the binary operator mod and the equivalent function _mod are both equivalent to modp. This can be changed by redefining _mod:
11 mod 7, modp(11, 7), mods(11,7)
_mod := mods: 11 mod 7; _mod := modp:
If one of the arguments is not a number, then a symbolic function call is returned:
delete x, m: x mod m, x mod 2, 2 mod m
When called with non-numeric arguments, the function coinciding with _mod is printed in the operator notation:
_mod := mods: modp(x, m), mods(x, m)
_mod := modp: modp(x, m), mods(x, m)
An integer, a rational number, or an arithmetical expression
An integer or an arithmetical expression