Rename coordinates to coefficients

This commit is contained in:
kalmar 2016-12-23 00:51:06 +01:00
parent 2476101af3
commit 723957f415
2 changed files with 24 additions and 25 deletions

View File

@ -8,16 +8,16 @@ export GroupAlgebraElement
immutable GroupAlgebraElement{T<:Number} immutable GroupAlgebraElement{T<:Number}
coordinates::Vector{T} coefficients::Vector{T}
product_matrix::Array{Int,2} product_matrix::Array{Int,2}
# basis::Array{Any,1} # basis::Array{Any,1}
function GroupAlgebraElement(coordinates::Vector{T}, function GroupAlgebraElement(coefficients::Vector{T},
product_matrix::Array{Int,2}) product_matrix::Array{Int,2})
size(product_matrix, 1) == size(product_matrix, 2) || size(product_matrix, 1) == size(product_matrix, 2) ||
throw(ArgumentError("Product matrix has to be square")) throw(ArgumentError("Product matrix has to be square"))
new(coordinates, product_matrix) new(coefficients, product_matrix)
end end
end end
@ -25,10 +25,10 @@ end
GroupAlgebraElement{T}(c::Vector{T},pm) = GroupAlgebraElement{T}(c,pm) GroupAlgebraElement{T}(c::Vector{T},pm) = GroupAlgebraElement{T}(c,pm)
convert{T<:Number}(::Type{T}, X::GroupAlgebraElement) = convert{T<:Number}(::Type{T}, X::GroupAlgebraElement) =
GroupAlgebraElement(convert(Vector{T}, X.coordinates), X.product_matrix) GroupAlgebraElement(convert(Vector{T}, X.coefficients), X.product_matrix)
show{T}(io::IO, X::GroupAlgebraElement{T}) = print(io, 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 ", T, "of length $(length(X)):\n", X.coefficients)
function isequal{T, S}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{S}) function isequal{T, S}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{S})
@ -36,7 +36,7 @@ function isequal{T, S}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{S})
warn("Comparing elements with different coefficients Rings!") warn("Comparing elements with different coefficients Rings!")
end end
X.product_matrix == Y.product_matrix || return false X.product_matrix == Y.product_matrix || return false
X.coordinates == Y.coordinates || return false X.coefficients == Y.coefficients || return false
return true return true
end end
@ -45,28 +45,28 @@ end
function add{T<:Number}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{T}) function add{T<:Number}(X::GroupAlgebraElement{T}, Y::GroupAlgebraElement{T})
X.product_matrix == Y.product_matrix || throw(ArgumentError( X.product_matrix == Y.product_matrix || throw(ArgumentError(
"Elements don't seem to belong to the same Group Algebra!")) "Elements don't seem to belong to the same Group Algebra!"))
return GroupAlgebraElement(X.coordinates+Y.coordinates, X.product_matrix) return GroupAlgebraElement(X.coefficients+Y.coefficients, X.product_matrix)
end end
function add{T<:Number, S<:Number}(X::GroupAlgebraElement{T}, function add{T<:Number, S<:Number}(X::GroupAlgebraElement{T},
Y::GroupAlgebraElement{S}) Y::GroupAlgebraElement{S})
warn("Adding elements with different base rings!") warn("Adding elements with different base rings!")
return GroupAlgebraElement(+(promote(X.coordinates, Y.coordinates)...), return GroupAlgebraElement(+(promote(X.coefficients, Y.coefficients)...),
X.product_matrix) X.product_matrix)
end end
(+)(X::GroupAlgebraElement, Y::GroupAlgebraElement) = add(X,Y) (+)(X::GroupAlgebraElement, Y::GroupAlgebraElement) = add(X,Y)
(-)(X::GroupAlgebraElement) = GroupAlgebraElement(-X.coordinates, X.product_matrix) (-)(X::GroupAlgebraElement) = GroupAlgebraElement(-X.coefficients, X.product_matrix)
(-)(X::GroupAlgebraElement, Y::GroupAlgebraElement) = add(X,-Y) (-)(X::GroupAlgebraElement, Y::GroupAlgebraElement) = add(X,-Y)
function group_star_multiplication{T<:Number}(X::GroupAlgebraElement{T}, function group_star_multiplication{T<:Number}(X::GroupAlgebraElement{T},
Y::GroupAlgebraElement{T}) Y::GroupAlgebraElement{T})
X.product_matrix == Y.product_matrix || ArgumentError( X.product_matrix == Y.product_matrix || ArgumentError(
"Elements don't seem to belong to the same Group Algebra!") "Elements don't seem to belong to the same Group Algebra!")
result = zeros(X.coordinates) result = zeros(X.coefficients)
for (i,x) in enumerate(X.coordinates) for (i,x) in enumerate(X.coefficients)
if x != 0 if x != 0
for (j,y) in enumerate(Y.coordinates) for (j,y) in enumerate(Y.coefficients)
if y != 0 if y != 0
index = X.product_matrix[i,j] index = X.product_matrix[i,j]
if index == 0 if index == 0
@ -92,25 +92,24 @@ end
Y::GroupAlgebraElement{S}) = group_star_multiplication(X,Y); Y::GroupAlgebraElement{S}) = group_star_multiplication(X,Y);
(*){T<:Number}(a::T, X::GroupAlgebraElement{T}) = GroupAlgebraElement( (*){T<:Number}(a::T, X::GroupAlgebraElement{T}) = GroupAlgebraElement(
a*X.coordinates, X.product_matrix) a*X.coefficients, X.product_matrix)
function scalar_multiplication{T<:Number, S<:Number}(a::T, function scalar_multiplication{T<:Number, S<:Number}(a::T,
X::GroupAlgebraElement{S}) X::GroupAlgebraElement{S})
if T!=S promote_type(T,S) == S || warn("Scalar and coefficients are in different rings! Promoting result to $(promote_type(T,S))")
warn("Scalars and coefficients ring are not the same! Trying to promote...") return GroupAlgebraElement(a*X.coefficients, X.product_matrix)
end
return GroupAlgebraElement(a*X.coordinates, X.product_matrix)
end end
(*){T<:Number}(a::T,X::GroupAlgebraElement) = scalar_multiplication(a, X) (*){T<:Number}(a::T,X::GroupAlgebraElement) = scalar_multiplication(a, X)
//{T<:Rational, S<:Rational}(X::GroupAlgebraElement{T}, a::S) = //{T<:Rational, S<:Rational}(X::GroupAlgebraElement{T}, a::S) =
GroupAlgebraElement(X.coordinates//a, X.product_matrix) GroupAlgebraElement(X.coefficients//a, X.product_matrix)
//{T<:Rational, S<:Integer}(X::GroupAlgebraElement{T}, a::S) = //{T<:Rational, S<:Integer}(X::GroupAlgebraElement{T}, a::S) =
X//convert(T,a) X//convert(T,a)
length(X::GroupAlgebraElement) = length(X.coordinates) length(X::GroupAlgebraElement) = length(X.coefficients)
size(X::GroupAlgebraElement) = size(X.coordinates) size(X::GroupAlgebraElement) = size(X.coefficients)
norm(X::GroupAlgebraElement, p=2) = norm(X.coordinates, p) norm(X::GroupAlgebraElement, p=2) = norm(X.coefficients, p)
end end

View File

@ -81,20 +81,20 @@ function create_SDP_problem(matrix_constraints,
@variable(m, κ >= 0.0) @variable(m, κ >= 0.0)
@objective(m, Max, κ) @objective(m, Max, κ)
for (pairs, δ², δ) in zip(matrix_constraints, Δ².coordinates, Δ.coordinates) for (pairs, δ², δ) in zip(matrix_constraints, Δ².coefficients, Δ.coefficients)
@constraint(m, sum(A[i,j] for (i,j) in pairs) == δ² - κ*δ) @constraint(m, sum(A[i,j] for (i,j) in pairs) == δ² - κ*δ)
end end
return m return m
end 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{T})
result = zeros(elt.coordinates) result = zeros(elt.coefficients)
zzz = zeros(elt.coordinates) zzz = zeros(elt.coefficients)
L = size(sqrt_matrix,2) L = size(sqrt_matrix,2)
for i in 1:L for i in 1:L
zzz[1:L] = view(sqrt_matrix, :,i) zzz[1:L] = view(sqrt_matrix, :,i)
new_base = GroupAlgebraElement(zzz, elt.product_matrix) new_base = GroupAlgebraElement(zzz, elt.product_matrix)
result += (new_base*new_base).coordinates result += (new_base*new_base).coefficients
end end
return GroupAlgebraElement{T}(result, elt.product_matrix) return GroupAlgebraElement{T}(result, elt.product_matrix)
end end