Arith: Methods for Function Arith in package clock

Description Details Author(s) Examples

Description

Methods for Arithmetic functions in the clock package: +, -, *, /, ^.

Details

The unary arithmetic functions (viz “+” and “-”) do no coercion.

The binary arithmetic functions coerce numeric <op> clock and clock <op> numeric to clock, except for ^, for which only clock^numeric is defined.

Multiplicative inverses are calculated using Fermat's theorem: if p is prime and (a,p)=1 then a^(p-1)=1 (mod p). From this we deduce that a.a^(p-2)=1 (mod p), or a^-1=a^(p-2) (mod p). It might be slightly more efficient to use a look-up table but the bookkeeping overhead for dealing with zero and NA would be large. Multiplicative inverses are not defined for composite moduli.

Powers are calculated using Euler's generalization of Fermat: if (a,n)=1 then a^phi(n)=1 (mod n). So if m=p.phi(n)+q we would have a^n=a^q (mod n), and this is used in the package. It might be possible to use the multiplicative order of a for composite n but again overheads would be considerable.

Author(s)

Robin K. S. Hankin

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
modulus(11)
a <- as.mod(sample(11))
b <- as.mod(sample(11))
a+b
a-b

a*b
a/b   # NB division by zero gives NA
a^2   # NB 2^a and a^a not defined

## Negative powers work as expected:
stopifnot(all(a^-1 == 1/a,na.rm=TRUE))

RobinHankin/clock documentation built on Nov. 8, 2021, 6:17 p.m.