GroupRings.jl/src/GroupRings.jl

337 lines
9.8 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-05-17 11:26:35 +02:00
import Nemo: Group, GroupElem, Ring, parent, elem_type, parent_type
2017-05-16 18:27:32 +02:00
2017-05-17 12:28:22 +02:00
import Base: convert, show, hash, ==, +, -, *, //, /, length, norm, rationalize, deepcopy_internal, getindex, setindex!
2017-05-16 18:47:34 +02:00
###############################################################################
#
# GroupRings / GroupRingsElem
#
###############################################################################
2017-03-13 19:44:26 +01:00
2017-05-16 18:28:32 +02:00
type GroupRing <: Ring
group::Group
pm::Array{Int,2}
basis::Vector{GroupElem}
basis_dict::Dict{GroupElem, Int}
2017-03-13 19:44:26 +01:00
function GroupRing(G::Group; full=false)
A = new(G)
if full
complete(A)
end
return A
end
2017-05-16 18:28:32 +02:00
end
2017-03-13 19:44:26 +01:00
abstract AbstractGroupRingElem
type GroupRingElem{T<:Number} <: AbstractGroupRingElem
2017-05-16 18:29:14 +02:00
coeffs::AbstractVector{T}
parent::GroupRing
2017-03-13 19:44:26 +01:00
end
2017-05-16 18:31:45 +02:00
export GroupRing, GroupRingElem
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-05-17 11:30:35 +02:00
parent_type{T}(::GroupRingElem{T}) = 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)
isdefined(RG, :basis) || complete(RG)
length(c) == length(RG.basis) || throw("Can't create GroupRingElem -- lengths differ: length(c) = $(length(c)) != $(length(RG.basis)) = length(RG.basis)")
2017-05-17 12:30:33 +02:00
GroupRingElem{T}(c,RG)
end
2017-05-16 18:31:26 +02:00
convert{T<:Number}(::Type{T}, X::GroupRingElem) =
2017-05-17 11:33:01 +02:00
GroupRingElem(convert(AbstractVector{T}, X.coeffs), parent(X))
2017-05-16 18:32:12 +02:00
2017-05-17 12:28:59 +02:00
function (RG::GroupRing)(g::GroupElem, T::Type=Int)
typeof(g) == elem_type(RG.group) || throw("$g does not belong to $(RG.group), the underlying group of $RG")
g = try
RG.group(g)
catch
throw("Can't coerce $g to the underlying group of $RG")
end
c = spzeros(T, length(RG.basis))
c[RG.basis_dict[g]] = one(T)
return GroupRingElem(c, RG)
end
2017-05-16 18:32:12 +02:00
function GroupRing(G::Group, pm::Array{Int,2})
size(pm,1) == size(pm,2) || throw("pm must be of size (n,n), got
$(size(pm))")
2017-05-17 11:33:01 +02:00
return GroupRing(G, pm)
2017-05-16 18:32:12 +02:00
end
function GroupRing(G::Group, pm::Array{Int,2}, basis::Vector)
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)
2017-05-16 18:32:12 +02:00
return GroupRing(Group, pm, basis, basis_dict)
end
2017-05-16 18:47:34 +02:00
###############################################################################
#
# Parent object call overloads
#
###############################################################################
2017-05-17 11:34:39 +02:00
function (RG::GroupRing)(T::Type=Int)
isdefined(RG, :basis) || throw("Complete the definition of GroupRing first")
return GroupRingElem(spzeros(T,length(RG.basis)), RG)
end
function (A::GroupRing)(X::GroupRingElem)
length(X) == length(A.basis) || throw("Can not coerce to $A: lengths differ")
X.parent = A
return X
end
function (A::GroupRing)(x::AbstractVector)
length(x) == length(A.basis) || throw("Can not coerce to $A: lengths differ")
return GroupRingElem(x, A)
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
function setindex!(X::GroupRingElem, k, n::Int)
X.coeffs[n] = k
end
function setindex!(X::GroupRingElem, k, g::GroupElem)
RG = parent(X)
typeof(g) == elem_type(RG.group) || throw("$g is not an element of $(RG.group)")
g = (RG.group)(g)
X.coeffs[RG.basis_dict[g]] = k
end
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 12:31:04 +02:00
if X == parent(X)()
print(io, "0*$((parent(X).group)())")
else
T = eltype(X.coeffs)
elts = ("$(X.coeffs[i])*$(parent(X).basis[i])" for i in 1:length(X) if X.coeffs[i] != zero(T))
join(io, elts, " + ")
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
X.coeffs == Y.coeffs || return false
return true
end
function (==)(A::GroupRing, B::GroupRing)
return A.group == B.group
end
2017-05-16 18:47:34 +02:00
###############################################################################
#
# Scalar operators
#
###############################################################################
(-)(X::GroupRingElem) = GroupRingElem(-X.coeffs, parent(X))
(*){T<:Number}(a::T, X::GroupRingElem{T}) = GroupRingElem(a*X.coeffs, parent(X))
function scalar_multiplication{T<:Number, S<:Number}(a::T,
X::GroupRingElem{S})
2017-05-17 11:36:47 +02:00
promote_type(T,S) == S || warn("Scalar and coeffs are in different rings!
Promoting result to $(promote_type(T,S))")
return GroupRingElem(a*X.coeffs, parent(X))
2017-03-13 19:44:26 +01:00
end
(*){T<:Number}(a::T,X::GroupRingElem) = scalar_multiplication(a, X)
(/){T<:Number}(a::T, X::GroupRingElem) = scalar_multiplication(1/a, X)
(//){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 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
2017-05-17 11:37:48 +02:00
function groupring_mult!(X,Y,pm,result)
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
return result
2017-03-13 19:44:26 +01:00
end
function groupring_mult{T<:Number}(X::AbstractVector{T}, Y::AbstractVector{T},
pm::Array{Int,2})
result = zeros(X)
return groupring_mult!(X,Y,pm,result)
end
function groupring_mult(X::AbstractVector, Y::AbstractVector, pm::Array{Int,2})
T = promote_type(eltype(X), eltype(Y))
2017-05-17 12:32:46 +02:00
result = zeros(T, deepcopy(X))
groupring_mult!(X, Y, pm, result)
return result
end
function groupring_mult{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!"))
result = groupring_mult(X.coeffs, Y.coeffs, parent(X).pm)
2017-05-16 18:38:37 +02:00
return GroupRingElem(result, parent(X))
2017-03-13 19:44:26 +01:00
end
function groupring_mult{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!")
result = groupring_mult(promote(X.coeffs,Y.coeffs)..., parent(X).pm)
return GroupRingElem(result, parent(X))
2017-03-13 19:44:26 +01:00
end
(*)(X::GroupRingElem, Y::GroupRingElem) = groupring_mult(X,Y)
2017-03-13 19:44:26 +01:00
2017-05-16 18:47:34 +02:00
###############################################################################
#
# Misc
#
###############################################################################
2017-03-13 19:44:26 +01:00
2017-05-16 18:45:31 +02:00
length(X::GroupRingElem) = length(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-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(a::AbstractVector)
return Dict{eltype(a), Int}(x => i for (i,x) in enumerate(a))
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
function complete(A::GroupRing)
2017-05-17 11:47:59 +02:00
if !isdefined(A, :basis)
A.basis = collect(elements(A.group))
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