From 0d5a2c0bc551bf769ee7cdb5cd390623e69f7e73 Mon Sep 17 00:00:00 2001 From: kalmar Date: Thu, 22 Dec 2016 20:25:55 +0100 Subject: [PATCH] Move parametrising GroupAlgebraElements by the Vector-type --- GroupAlgebras.jl | 51 ++++++++++++++++++++++++++---------------------- property(T).jl | 4 ++-- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/GroupAlgebras.jl b/GroupAlgebras.jl index 4494935..ea4793a 100644 --- a/GroupAlgebras.jl +++ b/GroupAlgebras.jl @@ -6,13 +6,14 @@ import Base: size, length, norm export GroupAlgebraElement +typealias CoordinateVector{T<:Number} AbstractVector{T} -immutable GroupAlgebraElement{T<:Number} - coordinates::Vector{T} +immutable GroupAlgebraElement{T<:CoordinateVector} + coordinates::T product_matrix::Array{Int,2} # basis::Array{Any,1} - function GroupAlgebraElement(coordinates::Vector{T}, + function GroupAlgebraElement(coordinates::T, product_matrix::Array{Int,2}) size(product_matrix, 1) == size(product_matrix, 2) || @@ -22,13 +23,13 @@ immutable GroupAlgebraElement{T<:Number} end # GroupAlgebraElement(c,pm,b) = GroupAlgebraElement(c,pm) -GroupAlgebraElement{T}(c::Vector{T},pm) = GroupAlgebraElement{T}(c,pm) +GroupAlgebraElement{T}(c::T,pm) = GroupAlgebraElement{T}(c,pm) convert{T<:Number}(::Type{T}, X::GroupAlgebraElement) = - GroupAlgebraElement(convert(Vector{T}, X.coordinates), X.product_matrix) + GroupAlgebraElement(convert(CoordinateVector{T}, X.coordinates), X.product_matrix) show{T}(io::IO, X::GroupAlgebraElement{T}) = print(io, - "Element of Group Algebra over ", T, "of length $(length(X)):\n", X.coordinates) + "Element of Group Algebra over $(typeofelt(X)), of length $(length(X)):\n", X.coordinates) function isequal{T, S}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{S}) @@ -42,13 +43,13 @@ end (==)(X::GroupAlgebraElement, Y::GroupAlgebraElement) = isequal(X,Y) -function add{T<:Number}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{T}) +function add{T<:CoordinateVector}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{T}) X.product_matrix == Y.product_matrix || throw(ArgumentError( "Elements don't seem to belong to the same Group Algebra!")) return GroupAlgebraElement(X.coordinates+Y.coordinates, X.product_matrix) end -function add{T<:Number, S<:Number}(X::GroupAlgebraElement{T}, +function add{T<:CoordinateVector, S<:CoordinateVector}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{S}) warn("Adding elements with different base rings!") return GroupAlgebraElement(+(promote(X.coordinates, Y.coordinates)...), @@ -59,7 +60,7 @@ end (-)(X::GroupAlgebraElement) = GroupAlgebraElement(-X.coordinates, X.product_matrix) (-)(X::GroupAlgebraElement, Y::GroupAlgebraElement) = add(X,-Y) -function group_star_multiplication{T<:Number}(X::GroupAlgebraElement{T}, +function group_star_multiplication{T<:CoordinateVector}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{T}) X.product_matrix == Y.product_matrix || ArgumentError( "Elements don't seem to belong to the same Group Algebra!") @@ -81,33 +82,37 @@ function group_star_multiplication{T<:Number}(X::GroupAlgebraElement{T}, return GroupAlgebraElement(result, X.product_matrix) end -function group_star_multiplication{T<:Number, S<:Number}( +function group_star_multiplication{T<:CoordinateVector, S<:CoordinateVector}( 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}, +(*){T<:CoordinateVector, S<:CoordinateVector}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{S}) = group_star_multiplication(X,Y); -(*){T<:Number}(a::T, X::GroupAlgebraElement{T}) = GroupAlgebraElement( - a*X.coordinates, X.product_matrix) +typeofelt{T<:Number}(X::AbstractVector{T}) = T +typeofelt{S<:CoordinateVector}(X::GroupAlgebraElement{S}) = typeofelt(X.coordinates) -function scalar_multiplication{T<:Number, S<:Number}(a::T, - X::GroupAlgebraElement{S}) - if T!=S - warn("Scalars and coefficients ring are not the same! Trying to promote...") - end +function (*){T<:Number, S<:CoordinateVector}(a::T, X::GroupAlgebraElement{S}) + W = typeofelt(X) + promote_type(T,W) == W || warn("Scalar and coordinates are in different rings! Promoting result to $(promote_type(T,W))") return GroupAlgebraElement(a*X.coordinates, X.product_matrix) end -(*){T<:Number}(a::T,X::GroupAlgebraElement) = scalar_multiplication(a, X) -//{T<:Rational, S<:Rational}(X::GroupAlgebraElement{T}, a::S) = - GroupAlgebraElement(X.coordinates//a, X.product_matrix) +(*){T<:Number, S<:CoordinateVector}(X::GroupAlgebraElement{S}, a::T) = (*)(a, X) -//{T<:Rational, S<:Integer}(X::GroupAlgebraElement{T}, a::S) = - X//convert(T,a) +function rational_division{T<:CoordinateVector, S<:Rational}(X::GroupAlgebraElement{T}, a::S) + if typeofelt(X) <: Rational + return GroupAlgebraElement(X.coordinates//a, X.product_matrix) + else + throw(ArgumentError("Rational division attempt on a GroupAlgebraElement of non-rational coefficients!")) + end +end + +(//)(X,a) = rational_division(X,a) +(//){S<:Integer}(X::GroupAlgebraElement, a::S) = //(X, Rational{S}(a)) length(X::GroupAlgebraElement) = length(X.coordinates) size(X::GroupAlgebraElement) = size(X.coordinates) diff --git a/property(T).jl b/property(T).jl index c7c6ad0..3613119 100644 --- a/property(T).jl +++ b/property(T).jl @@ -87,7 +87,7 @@ function create_SDP_problem(matrix_constraints, return m end -function resulting_SOS{T<:Number}(sqrt_matrix::Array{T,2}, elt::GroupAlgebraElement{T}) +function resulting_SOS{T<:Number}(sqrt_matrix::Array{T,2}, elt::GroupAlgebraElement) result = zeros(elt.coordinates) zzz = zeros(elt.coordinates) L = size(sqrt_matrix,2) @@ -96,7 +96,7 @@ function resulting_SOS{T<:Number}(sqrt_matrix::Array{T,2}, elt::GroupAlgebraElem new_base = GroupAlgebraElement(zzz, elt.product_matrix) result += (new_base*new_base).coordinates end - return GroupAlgebraElement{T}(result, elt.product_matrix) + return GroupAlgebraElement(result, elt.product_matrix) end function correct_to_augmentation_ideal{T<:Rational}(sqrt_matrix::Array{T,2})