Integer ring

# Integer ring

AbstractAlgebra.jl provides a module, implemented in src/julia/Integer.jl for making Julia BigInts conform to the AbstractAlgebra.jl Ring interface.

In addition to providing a parent object ZZ for Julia BigInts, we implement any additional functionality required by AbstractAlgebra.jl.

Because BigInt cannot be directly included in the AbstractAlgebra.jl abstract type hierarchy, we achieve integration of Julia BigInts by introducing a type union, called RingElement, which is a union of AbstractAlgebra.RingElem and a number of Julia types, including BigInt. Everywhere that RingElem is notionally used in AbstractAlgebra.jl, we are in fact using RingElement, with additional care being taken to avoid ambiguities.

The details of how this is done are technical, and we refer the reader to the implementation for details. For most intents and purposes, one can think of the Julia BigInt type as belonging to AbstractAlgebra.RingElem.

One other technicality is that Julia defines certain functions for BigInt, such as sqrt and exp differently to what AbstractAlgebra.jl requires. To get around this, we redefine these functions internally to AbstractAlgebra.jl, without redefining them for users of AbstractAlgebra.jl. This allows the internals of AbstractAlgebra.jl to function correctly, without broadcasting pirate definitions of already defined Julia functions to the world.

To access the internal definitions, one can use AbstractAlgebra.sqrt and AbstractAlgebra.exp, etc.

## Types and parent objects

Integers have type BigInt, as in Julia itself. We simply supplement the functionality for this type as required for computer algebra.

The parent objects of such integers has type Integers{BigInt}.

For convenience, we also make Int a part of the AbstractAlgebra.jl type hierarchy and its parent object (accessible as zz) has type Integers{Int}. But we caution that this type is not particularly useful as a model of the integers and may not function as expected within AbstractAlgebra.jl.

## Integer constructors

In order to construct integers in AbstractAlgebra.jl, one can first construct the integer ring itself. This is accomplished using the following constructor.

Integers{BigInt}()

This gives the unique object of type Integers{BigInt} representing the ring of integers in AbstractAlgebra.jl.

In practice, one simply uses ZZ which is assigned to be the return value of the above constructor. There is no need to call the constructor in practice.

Here are some examples of creating the integer ring and making use of the resulting parent object to coerce various elements into the ring.

Examples

julia> f = ZZ()
0

julia> g = ZZ(123)
123

julia> h = ZZ(BigInt(1234))
1234


## Basic ring functionality

The integer ring in AbstractAlgebra.jl implements the full Ring interface and the Euclidean Ring interface.

We give some examples of such functionality.

Examples

julia> f = ZZ(12)
12

julia> h = zero(ZZ)
0

julia> k = one(ZZ)
1

julia> isone(k)
true

julia> iszero(f)
false

julia> T = parent(f)
Integers

julia> f == deepcopy(f)
true

julia> g = f + 12
24

julia> h = powmod(f, 12, ZZ(17))
4

julia> flag, q = divides(f, ZZ(3))
(true, 4)


## Integer functionality provided by AbstractAlgebra.jl

The functionality below supplements that provided by Julia itself for its BigInt type.

### Basic functionality

isunit(a::Integer)

Return true if $a$ is $1$ or $-1$.

Examples

julia> r = ZZ(-1)
-1

julia> isunit(r)
true


### Square root

sqrt(a::T) where T <: Integer

Return the integer square root of $a$. If $a$ is not a perfect square an error is thrown.

sqrt(a::Rational{T}) where T <: Integer

Return the square root of $a$ if it is the square of a rational, otherwise throw an error.

exp(a::T) where T <: Integer

Return $1$ if $a = 0$, otherwise throw an exception. This function is not generally of use to the user, but is used internally in AbstractAlgebra.jl.

exp(a::Rational{T}) where T <: Integer

Return $1$ if $a = 0$, otherwise throw an exception.

Examples

julia> d = AbstractAlgebra.sqrt(ZZ(36))
6

julia> m = AbstractAlgebra.exp(ZZ(0))
1


### Coprime bases

ppio(a::T, b::T)

Split $a$ into $c*d$ where $c = gcd(a, b^\infty)$.

Examples

julia> c, n = ppio(ZZ(12), ZZ(26))
(4, 3)