1
0
mirror of https://github.com/kalmarek/PropertyT.jl.git synced 2024-11-19 07:20:28 +01:00
PropertyT.jl/GroupAlgebras.jl

134 lines
4.5 KiB
Julia
Raw Normal View History

2016-12-19 15:44:52 +01:00
module GroupAlgebras
import Base: convert, show, isequal, ==
import Base: +, -, *, //
2017-01-09 00:59:40 +01:00
import Base: size, length, norm, rationalize
2016-12-19 15:44:52 +01:00
export GroupAlgebraElement
immutable GroupAlgebraElement{T<:Number}
coefficients::AbstractVector{T}
2016-12-19 15:44:52 +01:00
product_matrix::Array{Int,2}
# basis::Array{Any,1}
function GroupAlgebraElement(coefficients::AbstractVector,
2016-12-19 15:44:52 +01:00
product_matrix::Array{Int,2})
size(product_matrix, 1) == size(product_matrix, 2) ||
throw(ArgumentError("Product matrix has to be square"))
2016-12-23 00:51:06 +01:00
new(coefficients, product_matrix)
2016-12-19 15:44:52 +01:00
end
end
# GroupAlgebraElement(c,pm,b) = GroupAlgebraElement(c,pm)
GroupAlgebraElement{T}(c::AbstractVector{T},pm) = GroupAlgebraElement{T}(c,pm)
2016-12-19 15:44:52 +01:00
convert{T<:Number}(::Type{T}, X::GroupAlgebraElement) =
GroupAlgebraElement(convert(AbstractVector{T}, X.coefficients), X.product_matrix)
2016-12-19 15:44:52 +01:00
show{T}(io::IO, X::GroupAlgebraElement{T}) = print(io,
"Element of Group Algebra over $T of length $(length(X)):\n $(X.coefficients)")
2016-12-19 15:44:52 +01:00
function isequal{T, S}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{S})
if T != S
warn("Comparing elements with different coefficients Rings!")
end
X.product_matrix == Y.product_matrix || return false
2016-12-23 00:51:06 +01:00
X.coefficients == Y.coefficients || return false
2016-12-19 15:44:52 +01:00
return true
end
(==)(X::GroupAlgebraElement, Y::GroupAlgebraElement) = isequal(X,Y)
function add{T<:Number}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{T})
X.product_matrix == Y.product_matrix || throw(ArgumentError(
2016-12-19 15:44:52 +01:00
"Elements don't seem to belong to the same Group Algebra!"))
2016-12-23 00:51:06 +01:00
return GroupAlgebraElement(X.coefficients+Y.coefficients, X.product_matrix)
2016-12-19 15:44:52 +01:00
end
function add{T<:Number, S<:Number}(X::GroupAlgebraElement{T},
2016-12-19 15:44:52 +01:00
Y::GroupAlgebraElement{S})
warn("Adding elements with different base rings!")
2016-12-23 00:51:06 +01:00
return GroupAlgebraElement(+(promote(X.coefficients, Y.coefficients)...),
2016-12-19 15:44:52 +01:00
X.product_matrix)
end
(+)(X::GroupAlgebraElement, Y::GroupAlgebraElement) = add(X,Y)
2016-12-23 00:51:06 +01:00
(-)(X::GroupAlgebraElement) = GroupAlgebraElement(-X.coefficients, X.product_matrix)
2016-12-19 15:44:52 +01:00
(-)(X::GroupAlgebraElement, Y::GroupAlgebraElement) = add(X,-Y)
function algebra_multiplication{T<:Number}(X::AbstractVector{T}, Y::AbstractVector{T}, pm::Array{Int,2})
result = zeros(X)
2017-03-13 10:28:49 +01:00
for (j,y) in enumerate(Y)
if y != zero(T)
for (i, index) in enumerate(pm[:,j])
if X[i] != zero(T)
index == 0 && throw(ArgumentError("The product don't seem to belong to the span of basis!"))
2017-03-13 10:28:49 +01:00
result[index] += X[i]*y
end
end
2016-12-19 15:44:52 +01:00
end
end
return result
end
function group_star_multiplication{T<:Number}(X::GroupAlgebraElement{T},
Y::GroupAlgebraElement{T})
X.product_matrix == Y.product_matrix || ArgumentError(
"Elements don't seem to belong to the same Group Algebra!")
result = algebra_multiplication(X.coefficients, Y.coefficients, X.product_matrix)
2016-12-19 15:44:52 +01:00
return GroupAlgebraElement(result, X.product_matrix)
end
function group_star_multiplication{T<:Number, S<:Number}(
2016-12-19 15:44:52 +01:00
X::GroupAlgebraElement{T},
Y::GroupAlgebraElement{S})
S == T || warn("Multiplying elements with different base rings!")
return group_star_multiplication(promote(X,Y)...)
end
(*){T<:Number, S<:Number}(X::GroupAlgebraElement{T},
2016-12-19 15:44:52 +01:00
Y::GroupAlgebraElement{S}) = group_star_multiplication(X,Y);
(*){T<:Number}(a::T, X::GroupAlgebraElement{T}) = GroupAlgebraElement(
2016-12-23 00:51:06 +01:00
a*X.coefficients, X.product_matrix)
2016-12-19 15:44:52 +01:00
function scalar_multiplication{T<:Number, S<:Number}(a::T,
X::GroupAlgebraElement{S})
2016-12-23 00:51:06 +01:00
promote_type(T,S) == S || warn("Scalar and coefficients are in different rings! Promoting result to $(promote_type(T,S))")
return GroupAlgebraElement(a*X.coefficients, X.product_matrix)
2016-12-19 15:44:52 +01:00
end
2016-12-23 00:51:06 +01:00
(*){T<:Number}(a::T,X::GroupAlgebraElement) = scalar_multiplication(a, X)
2016-12-19 15:44:52 +01:00
//{T<:Rational, S<:Rational}(X::GroupAlgebraElement{T}, a::S) =
2016-12-23 00:51:06 +01:00
GroupAlgebraElement(X.coefficients//a, X.product_matrix)
2016-12-19 15:44:52 +01:00
//{T<:Rational, S<:Integer}(X::GroupAlgebraElement{T}, a::S) =
X//convert(T,a)
2016-12-19 15:44:52 +01:00
2016-12-23 00:51:06 +01:00
length(X::GroupAlgebraElement) = length(X.coefficients)
size(X::GroupAlgebraElement) = size(X.coefficients)
function norm(X::GroupAlgebraElement, p=2)
if p == 1
return sum(abs(X.coefficients))
elseif p == Inf
return max(abs(X.coefficients))
else
return norm(X.coefficients, p)
end
end
2017-01-13 17:59:30 +01:00
ɛ(X::GroupAlgebraElement) = sum(X.coefficients)
2016-12-19 15:44:52 +01:00
2017-02-11 13:27:14 +01:00
function rationalize{T<:Integer, S<:Number}(
::Type{T}, X::GroupAlgebraElement{S}; tol=eps(S))
v = rationalize(T, X.coefficients, tol=tol)
return GroupAlgebraElement(v, X.product_matrix)
end
2017-01-09 00:59:40 +01:00
2016-12-19 15:44:52 +01:00
end