1
0
mirror of https://github.com/kalmarek/Groups.jl.git synced 2024-12-04 18:11:26 +01:00

throw uniformly DomainError on check

This commit is contained in:
kalmarek 2018-07-30 14:59:11 +02:00
parent 0de75f1262
commit 192230c8d3
4 changed files with 32 additions and 17 deletions

View File

@ -37,7 +37,8 @@ AdditiveGroup = AddGrp
function (G::MltGrp)(g) function (G::MltGrp)(g)
r = (G.obj)(g) r = (G.obj)(g)
isunit(r) || throw(ArgumentError("Cannot coerce to multplicative group: $r is not invertible!")) isunit(r) || throw(DomainError(
"Cannot coerce to multplicative group: $r is not invertible!"))
return MltGrpElem(r) return MltGrpElem(r)
end end
@ -55,7 +56,8 @@ for (Elem, op) in ([:MltGrpElem, :*], [:AddGrpElem, :+])
^(g::$Elem, n::Integer) = $Elem(op(g.elt, n)) ^(g::$Elem, n::Integer) = $Elem(op(g.elt, n))
function *(g::$Elem, h::$Elem) function *(g::$Elem, h::$Elem)
parent(g) == parent(h) || throw("Cannot multiply elements of different parents") parent(g) == parent(h) || throw(DomainError(
"Cannot multiply elements of different parents"))
return $Elem($op(g.elt,h.elt)) return $Elem($op(g.elt,h.elt))
end end
end end
@ -120,8 +122,10 @@ parent(g::DirectProductGroupElem) =
Base.size(g::DirectProductGroupElem) = size(g.elts) Base.size(g::DirectProductGroupElem) = size(g.elts)
Base.IndexStyle(::Type{DirectProductGroupElem}) = Base.LinearFast() Base.IndexStyle(::Type{DirectProductGroupElem}) = Base.LinearFast()
Base.getindex(g::DirectProductGroupElem, i::Int) = g.elts[i] Base.getindex(g::DirectProductGroupElem, i::Int) = g.elts[i]
function Base.setindex!(g::DirectProductGroupElem{T}, v::T, i::Int) where {T} function Base.setindex!(g::DirectProductGroupElem{T}, v::T, i::Int) where {T}
parent(v) == parent(first(g.elts)) || throw("$g is not an element of $i-th factor of $(parent(G))") parent(v) == parent(g.elts[i]) || throw(DomainError(
"$g is not an element of $i-th factor of $(parent(G))"))
g.elts[i] = v g.elts[i] = v
return g return g
end end
@ -138,7 +142,8 @@ end
############################################################################### ###############################################################################
function ×(G::Group, H::Group) function ×(G::Group, H::Group)
G == H || throw("Direct products are defined only for the same groups") G == H || throw(DomainError(
"Direct Powers are defined only for the same groups"))
return DirectProductGroup(G,2) return DirectProductGroup(G,2)
end end
@ -154,7 +159,8 @@ DirectProductGroup(R::T, n::Int) where {T<:AbstractAlgebra.Ring} =
DirectProductGroup(AdditiveGroup(R), n) DirectProductGroup(AdditiveGroup(R), n)
function ×(G::DirectProductGroup{T}, H::Group) where T <: Union{AdditiveGroup, MultiplicativeGroup} function ×(G::DirectProductGroup{T}, H::Group) where T <: Union{AdditiveGroup, MultiplicativeGroup}
G.group == T(H) || throw(ArgumentError("Direct products are defined only for the same groups")) G.group == T(H) || throw(DomainError(
"Direct products are defined only for the same groups"))
return DirectProductGroup(G.group,G.n+1) return DirectProductGroup(G.group,G.n+1)
end end
@ -172,8 +178,9 @@ doc"""
""" """
function (G::DirectProductGroup)(a::Vector, check::Bool=true) function (G::DirectProductGroup)(a::Vector, check::Bool=true)
if check if check
G.n == length(a) || throw("Can not coerce to DirectProductGroup: lengths differ") G.n == length(a) || throw(DomainError(
a = G.group.(a) "Can not coerce to DirectProductGroup: lengths differ"))
a = (G.group).(a)
end end
return DirectProductGroupElem(a) return DirectProductGroupElem(a)
end end
@ -250,7 +257,8 @@ doc"""
""" """
function *(g::DirectProductGroupElem{T}, h::DirectProductGroupElem{T}, check::Bool=true) where {T} function *(g::DirectProductGroupElem{T}, h::DirectProductGroupElem{T}, check::Bool=true) where {T}
if check if check
parent(g) == parent(h) || throw("Can not multiply elements of different groups!") parent(g) == parent(h) || throw(DomainError(
"Can not multiply elements of different groups!"))
end end
return DirectProductGroupElem([a*b for (a,b) in zip(g.elts,h.elts)]) return DirectProductGroupElem([a*b for (a,b) in zip(g.elts,h.elts)])
end end

View File

@ -75,8 +75,10 @@ function (G::FPGroup)(w::GWord)
if eltype(w.symbols) == FPSymbol if eltype(w.symbols) == FPSymbol
for s in w.symbols for s in w.symbols
i = findfirst(g -> g.str == s.str, G.gens) i = findfirst(g -> g.str == s.str, G.gens)
i == 0 && throw("Symbol $s does not belong to $G.") i == 0 && throw(DomainError(
s.pow % G.gens[i].pow == 0 || throw("Symbol $s doesn't belong to $G.") "Symbol $s does not belong to $G."))
s.pow % G.gens[i].pow == 0 || throw(DomainError(
"Symbol $s doesn't belong to $G."))
end end
end end
w.parent = G w.parent = G
@ -168,7 +170,8 @@ end
function /(G::FPGroup, newrels::Vector{FPGroupElem}) function /(G::FPGroup, newrels::Vector{FPGroupElem})
for r in rels for r in rels
parent(r) == G || throw("Can not form quotient group: $r is not an element of $G") parent(r) == G || throw(DomainError(
"Can not form quotient group: $r is not an element of $G"))
end end
H = deepcopy(G) H = deepcopy(G)
newrels = Dict(H(r) => H() for r in newrels) newrels = Dict(H(r) => H() for r in newrels)
@ -178,7 +181,8 @@ end
function /(G::FreeGroup, rels::Vector{FreeGroupElem}) function /(G::FreeGroup, rels::Vector{FreeGroupElem})
for r in rels for r in rels
parent(r) == G || throw("Can not form quotient group: $r is not an element of $G") parent(r) == G || throw(DomainError(
"Can not form quotient group: $r is not an element of $G"))
end end
H = FPGroup(G) H = FPGroup(G)
H.rels = Dict(H(rel) => H() for rel in unique(rels)) H.rels = Dict(H(rel) => H() for rel in unique(rels))

View File

@ -61,8 +61,10 @@ function (G::FreeGroup)(w::GroupWord{FreeSymbol})
if length(w) > 0 if length(w) > 0
for s in w.symbols for s in w.symbols
i = findfirst(g -> g.str == s.str, G.gens) i = findfirst(g -> g.str == s.str, G.gens)
i == 0 && throw("Symbol $s does not belong to $G.") i == 0 && throw(DomainError(
s.pow % G.gens[i].pow == 0 || throw("Symbol $s doesn't belong to $G.") "Symbol $s does not belong to $G."))
s.pow % G.gens[i].pow == 0 || throw(DomainError(
"Symbol $s doesn't belong to $G."))
end end
end end
w.parent = G w.parent = G

View File

@ -37,7 +37,8 @@ struct WreathProductElem{T<:GroupElem, I<:Integer} <: GroupElem
function WreathProductElem{T, I}(n::DirectProductGroupElem{T}, p::Generic.perm{I}, function WreathProductElem{T, I}(n::DirectProductGroupElem{T}, p::Generic.perm{I},
check::Bool=true) where {T, I} check::Bool=true) where {T, I}
if check if check
length(n.elts) == length(p) || throw("Can't form WreathProductElem: lengths differ") length(n.elts) == length(p) || throw(DomainError(
"Can't form WreathProductElem: lengths differ"))
end end
return new(n, p) return new(n, p)
end end
@ -80,12 +81,12 @@ function (G::WreathProduct)(g::WreathProductElem)
n = try n = try
G.N(g.n) G.N(g.n)
catch catch
throw("Can't coerce $(g.n) to $(G.N) factor of $G") throw(DomainError("Can't coerce $(g.n) to $(G.N) factor of $G"))
end end
p = try p = try
G.P(g.p) G.P(g.p)
catch catch
throw("Can't coerce $(g.p) to $(G.P) factor of $G") throw(DomainError("Can't coerce $(g.p) to $(G.P) factor of $G"))
end end
return WreathProductElem(n, p) return WreathProductElem(n, p)
end end