GroupRings.jl/src/GroupRings.jl

490 lines
14 KiB
Julia
Raw Normal View History

2017-05-16 21:06:44 +02:00
module GroupRings
2017-05-16 18:27:32 +02:00
using Nemo
2017-07-11 15:58:45 +02:00
import Nemo: Group, GroupElem, Ring, RingElem, parent, elem_type, parent_type, mul!, addeq!, divexact
2017-05-16 18:27:32 +02:00
2017-07-06 10:09:43 +02:00
import Base: convert, show, hash, ==, +, -, *, //, /, length, norm, rationalize, deepcopy_internal, getindex, setindex!, eltype, one, zero
2017-05-16 18:47:34 +02:00
###############################################################################
#
# GroupRings / GroupRingsElem
#
###############################################################################
2017-03-13 19:44:26 +01:00
type GroupRing{Gr<:Group, T<:GroupElem} <: Ring
group::Gr
basis::Vector{T}
basis_dict::Dict{T, Int}
pm::Array{Int,2}
2017-03-13 19:44:26 +01:00
function GroupRing(G::Gr; initialise=true)
A = new(G)
if initialise
complete(A)
end
return A
end
function GroupRing(G::Gr, basis::Vector{T}, basis_dict::Dict{T,Int}, pm::Array{Int,2})
return new(G, basis, basis_dict, pm)
end
2017-05-16 18:28:32 +02:00
end
2017-03-13 19:44:26 +01:00
GroupRing{Gr<:Group}(G::Gr;initialise=true) = GroupRing{Gr, elem_type(G)}(G, initialise=initialise)
GroupRing{Gr<:Group, T<:GroupElem}(G::Gr, b::Vector{T}, b_d::Dict{T,Int}, pm::Array{Int,2}) = GroupRing{Gr, T}(G, b, b_d, pm)
type GroupRingElem{T<:Number} <: RingElem
2017-05-16 18:29:14 +02:00
coeffs::AbstractVector{T}
parent::GroupRing
function GroupRingElem(c::AbstractVector{T}, RG::GroupRing, check=true)
if check
if isdefined(RG, :basis)
length(c) == length(RG.basis) || throw(
"Can't create GroupRingElem -- lengths differ: length(c) =
$(length(c)) != $(length(RG.basis)) = length(RG.basis)")
else
warn("Basis of the GroupRing is not defined.")
end
end
return new(c, RG)
end
2017-03-13 19:44:26 +01:00
end
2017-05-17 17:45:03 +02:00
export GroupRing, GroupRingElem, complete, create_pm
2017-03-13 19:44:26 +01:00
2017-05-16 18:47:34 +02:00
###############################################################################
#
# Type and parent object methods
#
###############################################################################
2017-03-13 19:44:26 +01:00
2017-05-16 18:34:43 +02:00
elem_type(::GroupRing) = GroupRingElem
2017-07-06 10:06:17 +02:00
parent_type(::GroupRingElem) = GroupRing
parent_type(::Type{GroupRingElem}) = GroupRing
2017-05-16 18:34:43 +02:00
eltype(X::GroupRingElem) = eltype(X.coeffs)
2017-05-17 11:30:35 +02:00
parent{T}(g::GroupRingElem{T}) = g.parent
2017-03-13 19:44:26 +01:00
Base.promote_rule{T<:Number,S<:Number}(::Type{GroupRingElem{T}}, ::Type{GroupRingElem{S}}) = GroupRingElem{promote_type(T,S)}
function convert{T<:Number}(::Type{T}, X::GroupRingElem)
return GroupRingElem(convert(AbstractVector{T}, X.coeffs), parent(X))
end
2017-05-16 18:47:34 +02:00
###############################################################################
#
# GroupRing / GroupRingElem constructors
#
###############################################################################
2017-03-13 19:44:26 +01:00
function GroupRingElem{T<:Number}(c::AbstractVector{T}, RG::GroupRing)
return GroupRingElem{T}(c, RG)
end
2017-05-16 18:31:26 +02:00
2017-05-16 18:32:12 +02:00
function GroupRing(G::Group, pm::Array{Int,2})
2017-05-17 17:43:37 +02:00
size(pm,1) == size(pm,2) || throw("pm must be square, got $(size(pm))")
RG = GroupRing(G, initialise=false)
RG.pm = pm
return RG
2017-05-16 18:32:12 +02:00
end
function GroupRing(G::Group, basis::Vector)
basis_dict = reverse_dict(basis)
pm = try
create_pm(basis, basis_dict)
catch err
isa(err, KeyError) && throw("Products are not supported on basis")
throw(err)
end
return GroupRing(G, basis, basis_dict, pm)
end
function GroupRing(G::Group, basis::Vector, pm::Array{Int,2})
2017-05-16 18:32:12 +02:00
size(pm,1) == size(pm,2) || throw("pm must be of size (n,n), got
$(size(pm))")
eltype(basis) == elem_type(G) || throw("basis must consist of elements of $G")
2017-05-17 11:34:03 +02:00
basis_dict = reverse_dict(basis)
return GroupRing(G, basis, basis_dict, pm)
2017-05-16 18:32:12 +02:00
end
2017-05-16 18:47:34 +02:00
###############################################################################
#
# Parent object call overloads
#
###############################################################################
2017-07-10 19:26:44 +02:00
zero(RG::GroupRing, T::Type=Int) = RG(T)
one(RG::GroupRing, T::Type=Int) = RG(RG.group(), T)
2017-07-17 09:32:38 +02:00
function (RG::GroupRing)(i::Int, T::Type=Int)
elt = RG(T)
elt[RG.group()] = i
return elt
end
2017-07-10 19:26:44 +02:00
function (RG::GroupRing)(T::Type=Int)
2017-05-17 11:34:39 +02:00
isdefined(RG, :basis) || throw("Complete the definition of GroupRing first")
return GroupRingElem(spzeros(T,length(RG.basis)), RG)
2017-05-17 11:34:39 +02:00
end
function (RG::GroupRing)(g::GroupElem, T::Type=Int)
2017-07-16 21:44:42 +02:00
g = RG.group(g)
result = RG(T)
result[g] = one(T)
return result
end
function (RG::GroupRing)(x::AbstractVector)
length(x) == length(RG.basis) || throw("Can not coerce to $RG: lengths differ")
result = RG(eltype(x))
result.coeffs = x
return result
end
function (RG::GroupRing)(X::GroupRingElem)
RG == parent(X) || throw("Can not coerce!")
return RG(X.coeffs)
end
function (RG::GroupRing)(X::GroupRingElem, emb::Function)
2017-07-12 20:54:20 +02:00
result = RG(eltype(X.coeffs))
T = typeof(X.coeffs)
result.coeffs = T(result.coeffs)
for g in parent(X).basis
result[emb(g)] = X[g]
end
return result
end
2017-05-16 18:47:34 +02:00
###############################################################################
#
# Basic manipulation && Array protocol
2017-05-16 18:47:34 +02:00
#
###############################################################################
2017-05-16 18:35:37 +02:00
function deepcopy_internal(X::GroupRingElem, dict::ObjectIdDict)
return GroupRingElem(deepcopy(X.coeffs), parent(X))
end
function hash(X::GroupRingElem, h::UInt)
return hash(full(X.coeffs), hash(parent(X), hash(GroupRingElem, h)))
2017-05-16 18:35:37 +02:00
end
function getindex(X::GroupRingElem, n::Int)
return X.coeffs[n]
end
function getindex(X::GroupRingElem, g::GroupElem)
return X.coeffs[parent(X).basis_dict[g]]
end
2017-05-17 12:49:45 +02:00
function setindex!(X::GroupRingElem, value, n::Int)
X.coeffs[n] = value
end
2017-05-17 12:49:45 +02:00
function setindex!(X::GroupRingElem, value, g::GroupElem)
RG = parent(X)
typeof(g) == elem_type(RG.group) || throw("$g is not an element of $(RG.group)")
if !(g in keys(RG.basis_dict))
g = (RG.group)(g)
end
X.coeffs[RG.basis_dict[g]] = value
end
Base.size(X::GroupRingElem) = size(X.coeffs)
Base.linearindexing{T<:GroupRingElem}(::Type{T}) = Base.LinearFast()
2017-05-18 12:19:12 +02:00
2017-05-16 18:47:34 +02:00
###############################################################################
#
# String I/O
#
###############################################################################
2017-05-16 18:34:21 +02:00
function show(io::IO, A::GroupRing)
2017-07-10 19:21:38 +02:00
print(io, "Group Ring of $(A.group)")
2017-05-16 18:34:21 +02:00
end
function show(io::IO, X::GroupRingElem)
2017-05-17 14:38:00 +02:00
RG = parent(X)
if X.coeffs == zero(X.coeffs)
T = eltype(X.coeffs)
2017-05-17 14:38:00 +02:00
print(io, "$(zero(T))*$((RG.group)())")
elseif isdefined(RG, :basis)
2017-05-19 10:32:09 +02:00
non_zeros = ((X.coeffs[i], RG.basis[i]) for i in findn(X.coeffs))
elts = ("$(sign(c)> 0? " + ": " - ")$(abs(c))*$g" for (c,g) in non_zeros)
2017-07-11 18:44:08 +02:00
str = join(elts, "")[2:end]
2017-07-10 19:21:38 +02:00
if sign(first(non_zeros)[1]) > 0
2017-07-11 18:44:08 +02:00
str = str[3:end]
2017-07-10 19:21:38 +02:00
end
print(io, str)
else
warn("Basis of the parent Group is not defined, showing coeffs")
2017-07-10 19:21:38 +02:00
show(io, MIME("text/plain"), X.coeffs)
2017-05-17 12:31:04 +02:00
end
2017-05-16 18:34:21 +02:00
end
2017-05-16 18:47:34 +02:00
###############################################################################
#
# Comparison
#
###############################################################################
2017-05-16 18:36:00 +02:00
function (==)(X::GroupRingElem, Y::GroupRingElem)
parent(X) == parent(Y) || return false
2017-05-17 11:36:21 +02:00
if eltype(X.coeffs) != eltype(Y.coeffs)
2017-05-16 18:36:00 +02:00
warn("Comparing elements with different coeffs Rings!")
end
all(X.coeffs .== Y.coeffs) || return false
2017-05-16 18:36:00 +02:00
return true
end
function (==)(A::GroupRing, B::GroupRing)
A.group == B.group || return false
if isdefined(A, :basis) && isdefined(B, :basis)
A.basis == B.basis || return false
else
warn("Bases of GroupRings are not defined, comparing products mats.")
end
A.pm == B.pm || return false
return true
2017-05-16 18:36:00 +02:00
end
2017-05-16 18:47:34 +02:00
###############################################################################
#
# Scalar operators
#
###############################################################################
(-)(X::GroupRingElem) = GroupRingElem(-X.coeffs, parent(X))
2017-06-06 22:29:02 +02:00
mul{T<:Number}(a::T, X::GroupRingElem{T}) = GroupRingElem(a*X.coeffs, parent(X))
2017-06-06 22:29:02 +02:00
function mul{T<:Number, S<:Number}(a::T, X::GroupRingElem{S})
2017-05-17 14:38:00 +02:00
promote_type(T,S) == S || warn("Scalar and coeffs are in different rings! Promoting result to $(promote_type(T,S))")
2017-05-17 11:36:47 +02:00
return GroupRingElem(a*X.coeffs, parent(X))
2017-03-13 19:44:26 +01:00
end
2017-06-06 22:29:02 +02:00
(*)(a, X::GroupRingElem) = mul(a,X)
(*)(X::GroupRingElem, a) = mul(a,X)
# disallow Nemo.Rings to hijack *(::Integer, ::RingElem)
2017-06-06 22:29:02 +02:00
(*){T<:Integer}(a::T, X::GroupRingElem) = mul(a,X)
(/)(X::GroupRingElem, a) = 1/a*X
function (//){T<:Integer, S<:Integer}(X::GroupRingElem{T}, a::S)
U = typeof(X[1]//a)
warn("Rational division: promoting result to $U")
return convert(U, X)//a
end
(//){T<:Rational, S<:Rational}(X::GroupRingElem{T}, a::S) =
2017-05-17 11:36:47 +02:00
GroupRingElem(X.coeffs//a, parent(X))
2017-05-17 11:36:47 +02:00
(//){T<:Rational, S<:Integer}(X::GroupRingElem{T}, a::S) = X//convert(T,a)
2017-03-13 19:44:26 +01:00
2017-05-16 18:47:34 +02:00
###############################################################################
#
# Binary operators
#
###############################################################################
2017-05-16 18:37:55 +02:00
function addeq!{T}(X::GroupRingElem{T}, Y::GroupRingElem{T})
X.coeffs .+= Y.coeffs
return X
end
function add{T<:Number}(X::GroupRingElem{T}, Y::GroupRingElem{T}, check::Bool=true)
if check
parent(X) == parent(Y) || throw("Elements don't seem to belong to the same Group Ring!")
end
return GroupRingElem(X.coeffs+Y.coeffs, parent(X))
2017-03-13 19:44:26 +01:00
end
2017-05-16 18:37:55 +02:00
function add{T<:Number, S<:Number}(X::GroupRingElem{T},
Y::GroupRingElem{S}, check::Bool=true)
if check
parent(X) == parent(Y) || throw("Elements don't seem to belong to the same Group Ring!")
end
warn("Adding elements with different base rings!")
return GroupRingElem(+(promote(X.coeffs, Y.coeffs)...), parent(X))
2017-03-13 19:44:26 +01:00
end
2017-05-16 18:37:55 +02:00
(+)(X::GroupRingElem, Y::GroupRingElem) = add(X,Y)
(-)(X::GroupRingElem, Y::GroupRingElem) = add(X,-Y)
2017-03-13 19:44:26 +01:00
2017-07-19 22:19:58 +02:00
function mul!{T}(result::AbstractVector{T},
X::AbstractVector,
Y::AbstractVector,
pm::Array{Int,2})
2017-07-10 19:25:52 +02:00
z = zero(T)
result .= z
2017-07-12 20:43:12 +02:00
for j in eachindex(Y)
if Y[j] != z
for i in 1:size(pm,1)
2017-07-10 19:25:52 +02:00
if X[i] != z
2017-07-12 20:43:12 +02:00
pm[i,j] == 0 && throw(ArgumentError("The product don't seem to be supported on basis!"))
result[pm[i,j]] += X[i]*Y[j]
2017-03-13 19:44:26 +01:00
end
end
end
end
2017-03-13 19:44:26 +01:00
end
function mul!{T}(result::GroupRingElem{T}, X::GroupRingElem, Y::GroupRingElem)
if result === X
result = deepcopy(result)
end
z = zero(T)
result.coeffs .= z
for j in eachindex(Y.coeffs)
if Y.coeffs[j] != z
for i in eachindex(X.coeffs)
if X.coeffs[i] != z
result.coeffs[parent(X).pm[i,j]] += X[i]*Y[j]
end
end
end
end
return result
end
function mul!{T<:Number}(result::GroupRingElem{T}, X::GroupRingElem, Y::GroupRingElem)
if result === X
result = deepcopy(result)
end
2017-07-12 20:56:59 +02:00
TT = typeof(first(X.coeffs)*first(Y.coeffs))
if TT != T
warn("Type of the result $T does not contain type of the product ($TT), promoting.")
result = convert(TT, result)
end
mul!(result.coeffs, X.coeffs, Y.coeffs, parent(X).pm)
return result
end
function *{T<:Number, S<:Number}(X::GroupRingElem{T}, Y::GroupRingElem{S}, check::Bool=true)
if true
parent(X) == parent(Y) || throw("Elements don't seem to belong to the same Group Ring!")
end
TT = typeof(first(X.coeffs)*first(Y.coeffs))
warn("Multiplying elements with different base rings! Promoting the result to $TT.")
result = mul!(result, X, Y)
return result
end
2017-06-06 18:44:53 +02:00
function mul(X::AbstractVector, Y::AbstractVector, pm::Array{Int,2})
T = promote_type(eltype(X), eltype(Y))
result = zeros(T, X)
2017-07-10 19:28:19 +02:00
mul!(result, Vector{T}(X), Vector{T}(Y), pm)
2017-05-17 12:32:46 +02:00
return result
end
2017-03-13 19:44:26 +01:00
2017-07-11 15:58:45 +02:00
function divexact{T}(X::GroupRingElem{T}, Y::GroupRingElem{T})
if length(Y) != 1
throw("Can not divide by a non-primitive element $(Y)!")
else
idx = findfirst(Y)
c = Y[idx]
2017-07-11 22:37:53 +02:00
c == 0 || throw("Can not invert")
2017-07-11 15:58:45 +02:00
g = parent(Y).basis[idx]
return X*1//c*parent(Y)(inv(g))
end
end
2017-07-11 22:37:53 +02:00
2017-05-17 12:34:24 +02:00
###############################################################################
#
# *-involution
#
###############################################################################
function star{T}(X::GroupRingElem{T})
2017-05-17 12:34:24 +02:00
RG = parent(X)
isdefined(RG, :basis) || complete(RG)
result = RG(T)
2017-05-17 12:34:24 +02:00
for (i,c) in enumerate(X.coeffs)
if c != zero(T)
2017-05-17 12:34:24 +02:00
g = inv(RG.basis[i])
result[g] = c
end
end
return result
end
2017-05-16 18:47:34 +02:00
###############################################################################
#
# Misc
#
###############################################################################
2017-03-13 19:44:26 +01:00
length(X::GroupRingElem) = countnz(X.coeffs)
2017-03-13 19:44:26 +01:00
2017-05-16 18:45:31 +02:00
norm(X::GroupRingElem, p=2) = norm(X.coeffs, p)
2017-03-13 19:44:26 +01:00
2017-05-16 18:45:31 +02:00
augmentation(X::GroupRingElem) = sum(X.coeffs)
2017-03-13 19:44:26 +01:00
2017-05-18 17:58:13 +02:00
function rationalize{T<:Integer, S<:Integer}(::Type{T}, X::GroupRingElem{S})
return convert(Rational{T}, X)
end
2017-05-16 18:45:56 +02:00
function rationalize{T<:Integer, S<:Number}(::Type{T}, X::GroupRingElem{S};
tol=eps(S))
v = rationalize(T, X.coeffs, tol=tol)
return GroupRingElem(v, parent(X))
end
2017-03-13 19:44:26 +01:00
function reverse_dict(iter)
T = eltype(iter)
return Dict{T, Int}(x => i for (i,x) in enumerate(iter))
2017-03-13 19:44:26 +01:00
end
function create_pm{T<:GroupElem}(basis::Vector{T}, basis_dict::Dict{T, Int},
2017-05-17 11:45:56 +02:00
limit=length(basis); twisted=false)
product_matrix = zeros(Int, (limit,limit))
for i in 1:limit
2017-05-17 11:45:56 +02:00
x = basis[i]
if twisted
x = inv(x)
end
for j in 1:limit
2017-05-17 11:45:56 +02:00
w = x*(basis[j])
product_matrix[i,j] = basis_dict[w]
end
end
return product_matrix
end
2017-05-17 17:44:51 +02:00
create_pm{T<:GroupElem}(b::Vector{T}) = create_pm(b, reverse_dict(b))
function complete(A::GroupRing)
2017-05-17 11:47:59 +02:00
if !isdefined(A, :basis)
A.basis = [elements(A.group)...]
2017-05-17 11:47:59 +02:00
end
if !isdefined(A, :basis_dict)
A.basis_dict = reverse_dict(A.basis)
end
if !isdefined(A, :pm)
A.pm = try
2017-05-17 11:47:59 +02:00
create_pm(A.basis, A.basis_dict)
catch err
2017-05-17 11:47:59 +02:00
isa(err, KeyError) && throw("Product is not supported on basis")
throw(err)
end
2017-05-17 11:47:59 +02:00
end
return A
2017-03-13 19:44:26 +01:00
end
2017-05-16 18:49:40 +02:00
end # of module GroupRings