GroupRings.jl/src/GroupRings.jl

443 lines
13 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
import Nemo: Group, GroupElem, Ring, RingElem, parent, elem_type, parent_type, mul!, addeq!
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)
2017-05-17 14:32:17 +02:00
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
2017-05-17 11:30:35 +02:00
parent{T}(g::GroupRingElem{T}) = g.parent
2017-03-13 19:44:26 +01:00
2017-05-16 18:47:34 +02:00
###############################################################################
#
# GroupRing / GroupRingElem constructors
#
###############################################################################
2017-03-13 19:44:26 +01:00
2017-05-17 12:30:33 +02: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-17 17:43:37 +02:00
function convert{T<:Number}(::Type{T}, X::GroupRingElem)
return GroupRingElem(convert(AbstractVector{T}, X.coeffs), parent(X))
end
2017-05-17 12:28:59 +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
#
###############################################################################
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)
g = try
RG.group(g)
catch
throw("Can't coerce $g to the underlying group of $RG")
end
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)
result = RG(eltype(X.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
#
###############################################################################
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(X.coeffs, hash(parent(X), h))
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)
else
X.coeffs[RG.basis_dict[g]] = value
end
end
2017-05-18 12:19:12 +02:00
eltype(X::GroupRingElem) = eltype(X.coeffs)
one(RG::GroupRing) = RG(RG.group())
2017-07-06 10:09:43 +02:00
zero(RG::GroupRing) = RG()
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-05-17 12:31:16 +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)
join(io, elts, "")
else
warn("Basis of the parent Group is not defined, showing coeffs")
print(io, 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})
parent(X) == parent(Y) || throw(ArgumentError(
"Elements don't seem to belong to the same Group Ring!"))
X.coeffs .+= Y.coeffs
return X
end
2017-05-16 18:37:55 +02:00
function add{T<:Number}(X::GroupRingElem{T}, Y::GroupRingElem{T})
parent(X) == parent(Y) || throw(ArgumentError(
2017-05-17 12:31:49 +02:00
"Elements don't seem to belong to the same Group Ring!"))
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})
parent(X) == parent(Y) || throw(ArgumentError(
2017-05-17 12:31:49 +02:00
"Elements don't seem to belong to the same Group Ring!"))
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
function mul!{T}(X::AbstractVector{T}, Y::AbstractVector{T},
2017-06-06 11:46:15 +02:00
pm::Array{Int,2}, result::AbstractVector{T})
for (j,y) in enumerate(Y)
2017-05-17 11:37:48 +02:00
if y != zero(eltype(Y))
for (i, index) in enumerate(pm[:,j])
2017-05-17 11:37:48 +02:00
if X[i] != zero(eltype(X))
index == 0 && throw(ArgumentError("The product don't seem to belong to the span of basis!"))
result[index] += X[i]*y
2017-03-13 19:44:26 +01:00
end
end
end
end
2017-03-13 19:44:26 +01:00
end
function mul!(result::GroupRingElem, X::GroupRingElem, Y::GroupRingElem)
mul!(X.coeffs, Y.coeffs, parent(X).pm, result.coeffs)
return result
end
2017-06-06 18:44:53 +02:00
function mul{T<:Number}(X::AbstractVector{T}, Y::AbstractVector{T},
pm::Array{Int,2})
result = zeros(X)
2017-06-06 18:44:53 +02:00
mul!(X,Y,pm,result)
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)
mul!(Vector{T}(X), Vector{T}(Y), pm, result)
2017-05-17 12:32:46 +02:00
return result
end
function *{T<:Number}(X::GroupRingElem{T}, Y::GroupRingElem{T})
2017-05-16 18:38:37 +02:00
parent(X) == parent(Y) || throw(ArgumentError(
2017-05-17 12:31:49 +02:00
"Elements don't seem to belong to the same Group Ring!"))
RG = parent(X)
isdefined(RG, :pm) || complete(RG)
2017-06-06 18:44:53 +02:00
result = mul(X.coeffs, Y.coeffs, RG.pm)
return GroupRingElem(result, RG)
2017-03-13 19:44:26 +01:00
end
function *{T<:Number, S<:Number}(X::GroupRingElem{T}, Y::GroupRingElem{S})
parent(X) == parent(Y) || throw("Elements don't seem to belong to the same
Group Ring!")
2017-05-16 18:38:37 +02:00
warn("Multiplying elements with different base rings!")
RG = parent(X)
isdefined(RG, :pm) || complete(RG)
2017-06-06 18:44:53 +02:00
result = mul(X.coeffs, Y.coeffs, RG.pm)
return GroupRingElem(result, RG)
2017-03-13 19:44:26 +01:00
end
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