2017-06-22 15:04:51 +02:00
|
|
|
|
import Base: ×
|
2017-06-22 14:21:25 +02:00
|
|
|
|
|
|
|
|
|
export DirectProductGroup, DirectProductGroupElem
|
2018-07-30 14:03:04 +02:00
|
|
|
|
export MultiplicativeGroup, MltGrp, MltGrpElem
|
|
|
|
|
export AdditiveGroup, AddGrp, AddGrpElem
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
#
|
|
|
|
|
# MltGrp/MltGrpElem & AddGrp/AddGrpElem
|
|
|
|
|
# (a thin wrapper for multiplicative/additive group of a Ring)
|
|
|
|
|
#
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
for (Gr, Elem) in [(:MltGrp, :MltGrpElem), (:AddGrp, :AddGrpElem)]
|
|
|
|
|
@eval begin
|
|
|
|
|
struct $Gr{T<:AbstractAlgebra.Ring} <: AbstractAlgebra.Group
|
|
|
|
|
obj::T
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
struct $Elem{T<:AbstractAlgebra.RingElem} <: AbstractAlgebra.GroupElem
|
|
|
|
|
elt::T
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
==(g::$Elem, h::$Elem) = g.elt == h.elt
|
|
|
|
|
==(G::$Gr, H::$Gr) = G.obj == H.obj
|
|
|
|
|
|
|
|
|
|
elem_type(::Type{$Gr{T}}) where T = $Elem{elem_type(T)}
|
|
|
|
|
parent_type(::Type{$Elem{T}}) where T = $Gr{parent_type(T)}
|
|
|
|
|
parent(g::$Elem) = $Gr(parent(g.elt))
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
MultiplicativeGroup = MltGrp
|
|
|
|
|
AdditiveGroup = AddGrp
|
|
|
|
|
|
|
|
|
|
(G::MltGrp)(g::MltGrpElem) = MltGrpElem(G.obj(g.elt))
|
|
|
|
|
|
|
|
|
|
function (G::MltGrp)(g)
|
|
|
|
|
r = (G.obj)(g)
|
2018-07-30 14:59:11 +02:00
|
|
|
|
isunit(r) || throw(DomainError(
|
|
|
|
|
"Cannot coerce to multplicative group: $r is not invertible!"))
|
2018-07-30 14:03:04 +02:00
|
|
|
|
return MltGrpElem(r)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
(G::AddGrp)(g) = AddGrpElem((G.obj)(g))
|
|
|
|
|
|
|
|
|
|
(G::MltGrp)() = MltGrpElem(G.obj(1))
|
|
|
|
|
(G::AddGrp)() = AddGrpElem(G.obj())
|
|
|
|
|
|
|
|
|
|
inv(g::MltGrpElem) = MltGrpElem(inv(g.elt))
|
|
|
|
|
inv(g::AddGrpElem) = AddGrpElem(-g.elt)
|
|
|
|
|
|
|
|
|
|
for (Elem, op) in ([:MltGrpElem, :*], [:AddGrpElem, :+])
|
|
|
|
|
@eval begin
|
|
|
|
|
|
|
|
|
|
^(g::$Elem, n::Integer) = $Elem(op(g.elt, n))
|
|
|
|
|
|
|
|
|
|
function *(g::$Elem, h::$Elem)
|
2018-07-30 14:59:11 +02:00
|
|
|
|
parent(g) == parent(h) || throw(DomainError(
|
|
|
|
|
"Cannot multiply elements of different parents"))
|
2018-07-30 14:03:04 +02:00
|
|
|
|
return $Elem($op(g.elt,h.elt))
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Base.show(io::IO, G::MltGrp) = print(io, "The multiplicative group of $(G.obj)")
|
|
|
|
|
Base.show(io::IO, G::AddGrp) = print(io, "The additive group of $(G.obj)")
|
|
|
|
|
|
|
|
|
|
Base.show(io::IO, g::Union{MltGrpElem, AddGrpElem}) = show(io, g.elt)
|
|
|
|
|
|
|
|
|
|
gens(F::AbstractAlgebra.Field) = elem_type(F)[gen(F)]
|
|
|
|
|
|
|
|
|
|
order(G::AddGrp{<:AbstractAlgebra.GFField}) = order(G.obj)
|
|
|
|
|
elements(G::AddGrp{F}) where F <: AbstractAlgebra.GFField = (G((i-1)*G.obj(1)) for i in 1:order(G))
|
|
|
|
|
|
|
|
|
|
order(G::MltGrp{<:AbstractAlgebra.GFField}) = order(G.obj) - 1
|
|
|
|
|
elements(G::MltGrp{F}) where F <: AbstractAlgebra.GFField = (G(i*G.obj(1)) for i in 1:order(G))
|
|
|
|
|
|
2017-06-22 14:21:25 +02:00
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
#
|
|
|
|
|
# DirectProductGroup / DirectProductGroupElem
|
|
|
|
|
#
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
doc"""
|
2018-07-30 14:05:47 +02:00
|
|
|
|
DirectProductGroup(G::Group, n::Int) <: Group
|
2017-07-21 13:19:56 +02:00
|
|
|
|
Implements `n`-fold direct product of `G`. The group operation is
|
2017-06-22 14:21:25 +02:00
|
|
|
|
`*` distributed component-wise, with component-wise identity as neutral element.
|
|
|
|
|
"""
|
|
|
|
|
|
2017-09-13 16:47:31 +02:00
|
|
|
|
struct DirectProductGroup{T<:Group} <: Group
|
2017-07-21 13:19:56 +02:00
|
|
|
|
group::T
|
|
|
|
|
n::Int
|
2017-06-22 14:21:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
2017-09-13 16:47:31 +02:00
|
|
|
|
struct DirectProductGroupElem{T<:GroupElem} <: GroupElem
|
2017-07-12 21:05:21 +02:00
|
|
|
|
elts::Vector{T}
|
2017-06-22 14:21:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
#
|
|
|
|
|
# Type and parent object methods
|
|
|
|
|
#
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
2018-07-30 14:07:42 +02:00
|
|
|
|
elem_type(::Type{DirectProductGroup{T}}) where {T} =
|
|
|
|
|
DirectProductGroupElem{elem_type(T)}
|
2017-06-22 14:21:25 +02:00
|
|
|
|
|
2017-09-13 16:47:31 +02:00
|
|
|
|
parent_type(::Type{DirectProductGroupElem{T}}) where {T} =
|
2017-07-21 13:20:31 +02:00
|
|
|
|
DirectProductGroup{parent_type(T)}
|
2017-06-22 14:21:25 +02:00
|
|
|
|
|
2017-07-21 13:21:38 +02:00
|
|
|
|
parent(g::DirectProductGroupElem) =
|
|
|
|
|
DirectProductGroup(parent(first(g.elts)), length(g.elts))
|
2017-06-22 14:21:25 +02:00
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
#
|
2017-07-12 21:10:01 +02:00
|
|
|
|
# AbstractVector interface
|
2017-06-22 14:21:25 +02:00
|
|
|
|
#
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
2017-07-12 21:10:01 +02:00
|
|
|
|
Base.size(g::DirectProductGroupElem) = size(g.elts)
|
2017-09-13 15:43:56 +02:00
|
|
|
|
Base.IndexStyle(::Type{DirectProductGroupElem}) = Base.LinearFast()
|
2017-07-12 21:10:01 +02:00
|
|
|
|
Base.getindex(g::DirectProductGroupElem, i::Int) = g.elts[i]
|
2018-07-30 14:59:11 +02:00
|
|
|
|
|
2017-09-13 16:47:31 +02:00
|
|
|
|
function Base.setindex!(g::DirectProductGroupElem{T}, v::T, i::Int) where {T}
|
2018-07-30 14:59:11 +02:00
|
|
|
|
parent(v) == parent(g.elts[i]) || throw(DomainError(
|
|
|
|
|
"$g is not an element of $i-th factor of $(parent(G))"))
|
2017-07-23 03:24:05 +02:00
|
|
|
|
g.elts[i] = v
|
|
|
|
|
return g
|
2017-06-22 14:21:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
2018-07-30 14:54:54 +02:00
|
|
|
|
function Base.setindex!(g::DirectProductGroupElem{T}, v::S, i::Int) where {T, S}
|
|
|
|
|
g.elts[i] = parent(g.elts[i])(v)
|
|
|
|
|
return g
|
|
|
|
|
end
|
|
|
|
|
|
2017-07-12 21:10:01 +02:00
|
|
|
|
###############################################################################
|
|
|
|
|
#
|
|
|
|
|
# DirectProductGroup / DirectProductGroupElem constructors
|
|
|
|
|
#
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
2017-07-21 13:23:16 +02:00
|
|
|
|
function ×(G::Group, H::Group)
|
2018-07-30 14:59:11 +02:00
|
|
|
|
G == H || throw(DomainError(
|
|
|
|
|
"Direct Powers are defined only for the same groups"))
|
2017-07-21 13:23:16 +02:00
|
|
|
|
return DirectProductGroup(G,2)
|
|
|
|
|
end
|
2017-06-22 14:21:25 +02:00
|
|
|
|
|
2018-07-30 14:56:28 +02:00
|
|
|
|
×(H::Group, G::DirectProductGroup) = G×H
|
|
|
|
|
|
|
|
|
|
function ×(G::DirectProductGroup, H::Group)
|
|
|
|
|
G.group == H || throw(DomainError(
|
|
|
|
|
"Direct products are defined only for the same groups"))
|
|
|
|
|
return DirectProductGroup(G.group,G.n+1)
|
|
|
|
|
end
|
|
|
|
|
|
2018-07-30 14:03:04 +02:00
|
|
|
|
DirectProductGroup(R::T, n::Int) where {T<:AbstractAlgebra.Ring} =
|
|
|
|
|
DirectProductGroup(AdditiveGroup(R), n)
|
|
|
|
|
|
|
|
|
|
function ×(G::DirectProductGroup{T}, H::Group) where T <: Union{AdditiveGroup, MultiplicativeGroup}
|
2018-07-30 14:59:11 +02:00
|
|
|
|
G.group == T(H) || throw(DomainError(
|
|
|
|
|
"Direct products are defined only for the same groups"))
|
2018-07-30 14:03:04 +02:00
|
|
|
|
return DirectProductGroup(G.group,G.n+1)
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-22 14:21:25 +02:00
|
|
|
|
###############################################################################
|
|
|
|
|
#
|
|
|
|
|
# Parent object call overloads
|
|
|
|
|
#
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
doc"""
|
2017-07-21 13:25:34 +02:00
|
|
|
|
(G::DirectProductGroup)(a::Vector, check::Bool=true)
|
|
|
|
|
> Constructs element of the $n$-fold direct product group `G` by coercing each
|
2017-07-23 17:04:22 +02:00
|
|
|
|
> element of vector `a` to `G.group`. If `check` flag is set to `false` neither
|
|
|
|
|
> check on the correctness nor coercion is performed.
|
2017-06-22 14:21:25 +02:00
|
|
|
|
"""
|
2017-07-21 13:25:34 +02:00
|
|
|
|
function (G::DirectProductGroup)(a::Vector, check::Bool=true)
|
|
|
|
|
if check
|
2018-07-30 14:59:11 +02:00
|
|
|
|
G.n == length(a) || throw(DomainError(
|
|
|
|
|
"Can not coerce to DirectProductGroup: lengths differ"))
|
|
|
|
|
a = (G.group).(a)
|
2017-07-21 13:25:34 +02:00
|
|
|
|
end
|
2017-07-12 21:07:05 +02:00
|
|
|
|
return DirectProductGroupElem(a)
|
2017-06-22 14:21:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
2017-07-21 13:27:00 +02:00
|
|
|
|
(G::DirectProductGroup)() = DirectProductGroupElem([G.group() for _ in 1:G.n])
|
|
|
|
|
|
|
|
|
|
(G::DirectProductGroup)(g::DirectProductGroupElem) = G(g.elts)
|
|
|
|
|
|
2018-07-30 14:56:28 +02:00
|
|
|
|
(G::DirectProductGroup)(a::Vararg{T, N}) where {T, N} = G([a...])
|
2017-07-21 13:27:27 +02:00
|
|
|
|
|
2017-06-22 14:21:25 +02:00
|
|
|
|
###############################################################################
|
|
|
|
|
#
|
|
|
|
|
# Basic manipulation
|
|
|
|
|
#
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
function hash(G::DirectProductGroup, h::UInt)
|
2017-07-21 13:27:54 +02:00
|
|
|
|
return hash(G.group, hash(G.n, hash(DirectProductGroup,h)))
|
2017-06-22 14:21:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function hash(g::DirectProductGroupElem, h::UInt)
|
2017-07-12 21:10:31 +02:00
|
|
|
|
return hash(g.elts, hash(parent(g), hash(DirectProductGroupElem, h)))
|
2017-06-22 14:21:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
#
|
|
|
|
|
# String I/O
|
|
|
|
|
#
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
function show(io::IO, G::DirectProductGroup)
|
2018-07-30 14:05:47 +02:00
|
|
|
|
print(io, "$(G.n)-fold direct product of $(G.group)")
|
2017-06-22 14:21:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function show(io::IO, g::DirectProductGroupElem)
|
2018-07-30 14:05:47 +02:00
|
|
|
|
print(io, "[$(join(g.elts,","))]")
|
2017-06-22 14:21:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
#
|
|
|
|
|
# Comparison
|
|
|
|
|
#
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
2017-07-21 13:28:19 +02:00
|
|
|
|
doc"""
|
|
|
|
|
==(g::DirectProductGroup, h::DirectProductGroup)
|
|
|
|
|
> Checks if two direct product groups are the same.
|
|
|
|
|
"""
|
2017-06-22 14:21:25 +02:00
|
|
|
|
function (==)(G::DirectProductGroup, H::DirectProductGroup)
|
2018-07-30 14:05:47 +02:00
|
|
|
|
G.group == H.group || return false
|
|
|
|
|
G.n == G.n || return false
|
|
|
|
|
return true
|
2017-06-22 14:21:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
doc"""
|
|
|
|
|
==(g::DirectProductGroupElem, h::DirectProductGroupElem)
|
2017-07-21 13:28:19 +02:00
|
|
|
|
> Checks if two direct product group elements are the same.
|
2017-06-22 14:21:25 +02:00
|
|
|
|
"""
|
|
|
|
|
function (==)(g::DirectProductGroupElem, h::DirectProductGroupElem)
|
2018-07-30 14:05:47 +02:00
|
|
|
|
g.elts == h.elts || return false
|
|
|
|
|
return true
|
2017-06-22 14:21:25 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
|
#
|
2017-07-23 03:24:40 +02:00
|
|
|
|
# Group operations
|
2017-06-22 14:21:25 +02:00
|
|
|
|
#
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
|
|
|
|
doc"""
|
|
|
|
|
*(g::DirectProductGroupElem, h::DirectProductGroupElem)
|
|
|
|
|
> Return the direct-product group operation of elements, i.e. component-wise
|
|
|
|
|
> operation as defined by `operations` field of the parent object.
|
|
|
|
|
"""
|
2017-09-13 16:47:31 +02:00
|
|
|
|
function *(g::DirectProductGroupElem{T}, h::DirectProductGroupElem{T}, check::Bool=true) where {T}
|
2017-07-21 13:30:35 +02:00
|
|
|
|
if check
|
2018-07-30 14:59:11 +02:00
|
|
|
|
parent(g) == parent(h) || throw(DomainError(
|
|
|
|
|
"Can not multiply elements of different groups!"))
|
2017-07-21 13:30:35 +02:00
|
|
|
|
end
|
|
|
|
|
return DirectProductGroupElem([a*b for (a,b) in zip(g.elts,h.elts)])
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-22 14:21:25 +02:00
|
|
|
|
doc"""
|
|
|
|
|
inv(g::DirectProductGroupElem)
|
|
|
|
|
> Return the inverse of the given element in the direct product group.
|
|
|
|
|
"""
|
2017-09-13 16:47:31 +02:00
|
|
|
|
function inv(g::DirectProductGroupElem{T}) where {T<:GroupElem}
|
2017-07-21 13:30:35 +02:00
|
|
|
|
return DirectProductGroupElem([inv(a) for a in g.elts])
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-22 14:21:25 +02:00
|
|
|
|
###############################################################################
|
|
|
|
|
#
|
|
|
|
|
# Misc
|
|
|
|
|
#
|
|
|
|
|
###############################################################################
|
|
|
|
|
|
2018-08-08 14:12:55 +02:00
|
|
|
|
import Base: size, length, start, next, done, eltype
|
|
|
|
|
|
|
|
|
|
struct DirectPowerIter{Gr<:AbstractAlgebra.Group, GrEl<:AbstractAlgebra.GroupElem}
|
|
|
|
|
G::Gr
|
|
|
|
|
N::Int
|
|
|
|
|
elts::Vector{GrEl}
|
|
|
|
|
totalorder::Int
|
|
|
|
|
orderG::Int
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function DirectPowerIter(G::Gr, N::Integer) where {Gr<:AbstractAlgebra.Group}
|
|
|
|
|
return DirectPowerIter{Gr, elem_type(G)}(
|
|
|
|
|
G,
|
|
|
|
|
N,
|
|
|
|
|
vec(collect(elements(G))),
|
|
|
|
|
Int(order(G))^N,
|
|
|
|
|
Int(order(G))
|
|
|
|
|
)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Base.size(DPIter::DirectPowerIter) = ntuple(i -> DPIter.orderG, DPIter.N)
|
|
|
|
|
Base.length(DPIter::DirectPowerIter) = DPIter.totalorder
|
|
|
|
|
Base.start(::DirectPowerIter) = 0
|
|
|
|
|
|
|
|
|
|
function Base.next(DPIter::DirectPowerIter, state)
|
|
|
|
|
idx = ind2sub(size(DPIter), state+1)
|
|
|
|
|
return DirectProductGroupElem([DPIter.elts[i] for i in idx]), state+1
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Base.done(DPIter::DirectPowerIter, state) = DPIter.totalorder <= state
|
|
|
|
|
|
|
|
|
|
Base.eltype(::Type{DirectPowerIter{Gr, GrEl}}) where {Gr, GrEl} = DirectProductGroupElem{GrEl}
|
|
|
|
|
|
2017-06-22 14:21:25 +02:00
|
|
|
|
doc"""
|
|
|
|
|
elements(G::DirectProductGroup)
|
2017-07-21 13:31:42 +02:00
|
|
|
|
> Returns `generator` that produces all elements of group `G` (provided that
|
|
|
|
|
> `G.group` implements the `elements` method).
|
2017-06-22 14:21:25 +02:00
|
|
|
|
"""
|
2018-08-08 14:12:55 +02:00
|
|
|
|
elements(G::DirectProductGroup) = DirectPowerIter(G.group, G.n)
|
2017-06-22 14:21:25 +02:00
|
|
|
|
|
|
|
|
|
doc"""
|
|
|
|
|
order(G::DirectProductGroup)
|
|
|
|
|
> Returns the order (number of elements) in the group.
|
|
|
|
|
|
|
|
|
|
"""
|
2017-07-21 13:31:42 +02:00
|
|
|
|
order(G::DirectProductGroup) = order(G.group)^G.n
|