2016-12-19 15:44:52 +01:00
|
|
|
using JuMP
|
2017-01-09 00:59:40 +01:00
|
|
|
import Base: rationalize
|
2016-12-19 15:44:52 +01:00
|
|
|
|
2016-12-21 10:00:22 +01:00
|
|
|
function products{T<:Real}(S1::Array{Array{T,2},1}, S2::Array{Array{T,2},1})
|
|
|
|
result = [0*similar(S1[1])]
|
|
|
|
for x in S1
|
|
|
|
for y in S2
|
|
|
|
push!(result, x*y)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return unique(result[2:end])
|
|
|
|
end
|
2016-12-19 15:44:52 +01:00
|
|
|
|
2017-01-09 01:01:31 +01:00
|
|
|
function generate_B₂_and_B₄(identity, S₁)
|
|
|
|
S₂ = products(S₁, S₁);
|
|
|
|
S₃ = products(S₁, S₂);
|
|
|
|
S₄ = products(S₂, S₂);
|
|
|
|
|
|
|
|
B₂ = unique(vcat([identity],S₁,S₂));
|
|
|
|
B₄ = unique(vcat(B₂, S₃, S₄));
|
|
|
|
@assert B₄[1:length(B₂)] == B₂
|
|
|
|
return B₂, B₄;
|
|
|
|
end
|
|
|
|
|
2016-12-19 15:44:52 +01:00
|
|
|
function read_GAP_raw_list(filename::String)
|
|
|
|
return eval(parse(String(read(filename))))
|
|
|
|
end
|
|
|
|
|
|
|
|
function create_product_matrix(matrix_constraints)
|
|
|
|
l = length(matrix_constraints)
|
|
|
|
product_matrix = zeros(Int, (l, l))
|
|
|
|
for (index, pairs) in enumerate(matrix_constraints)
|
|
|
|
for (i,j) in pairs
|
|
|
|
product_matrix[i,j] = index
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return product_matrix
|
|
|
|
end
|
|
|
|
|
2016-12-21 10:00:22 +01:00
|
|
|
function create_product_matrix(basis::Array{Array{Float64,2},1}, limit::Int)
|
|
|
|
|
2017-01-13 18:04:20 +01:00
|
|
|
product_matrix = zeros(Int, (limit,limit))
|
2016-12-21 16:03:19 +01:00
|
|
|
constraints = [Array{Int,1}[] for x in 1:length(basis)]
|
2016-12-21 10:00:22 +01:00
|
|
|
|
|
|
|
for i in 1:limit
|
|
|
|
x_inv = inv(basis[i])
|
|
|
|
for j in 1:limit
|
2017-01-13 18:02:34 +01:00
|
|
|
w = x_inv*basis[j]
|
2016-12-21 10:00:22 +01:00
|
|
|
|
2017-01-13 18:02:34 +01:00
|
|
|
index = findfirst(basis, w)
|
|
|
|
if 0 < index ≤ limit
|
|
|
|
product_matrix[i,j] = index
|
|
|
|
push!(constraints[index],[i,j])
|
2016-12-21 10:00:22 +01:00
|
|
|
end
|
2016-12-19 15:44:52 +01:00
|
|
|
end
|
|
|
|
end
|
2016-12-21 10:00:22 +01:00
|
|
|
return product_matrix, constraints
|
2016-12-19 15:44:52 +01:00
|
|
|
end
|
|
|
|
|
2016-12-21 16:02:03 +01:00
|
|
|
function Laplacian_sparse(S::Array{Array{Float64,2},1},
|
|
|
|
basis::Array{Array{Float64,2},1})
|
|
|
|
|
|
|
|
squares = unique(vcat([basis[1]], S, products(S,S)))
|
|
|
|
|
|
|
|
result = spzeros(length(basis))
|
|
|
|
result[1] = length(S)
|
|
|
|
for s in S
|
|
|
|
ind = find(x -> x==s, basis)
|
|
|
|
result[ind] += -1
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
|
|
|
function Laplacian(S::Array{Array{Float64,2},1},
|
|
|
|
basis:: Array{Array{Float64,2},1})
|
|
|
|
|
|
|
|
return full(Laplacian_sparse(S,basis))
|
|
|
|
end
|
|
|
|
|
2017-01-09 01:01:31 +01:00
|
|
|
function prepare_Laplacian_and_constraints{T}(S::Vector{Array{T,2}};)
|
|
|
|
|
|
|
|
identity = eye(S[1])
|
|
|
|
B₂, B₄ = generate_B₂_and_B₄(identity, S)
|
|
|
|
product_matrix, matrix_constraints = create_product_matrix(B₄,length(B₂));
|
|
|
|
|
|
|
|
L= Laplacian(S, B₄);
|
|
|
|
|
2017-01-13 18:03:08 +01:00
|
|
|
return GroupAlgebraElement(L, product_matrix), matrix_constraints
|
2017-01-09 01:01:31 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function create_SDP_problem(matrix_constraints, Δ::GroupAlgebraElement)
|
2016-12-21 16:03:19 +01:00
|
|
|
N = size(Δ.product_matrix,1)
|
2017-01-09 01:01:31 +01:00
|
|
|
const Δ² = Δ*Δ
|
2016-12-19 15:44:52 +01:00
|
|
|
@assert length(Δ) == length(matrix_constraints)
|
|
|
|
m = Model();
|
|
|
|
@variable(m, A[1:N, 1:N], SDP)
|
|
|
|
@SDconstraint(m, A >= zeros(size(A)))
|
|
|
|
@variable(m, κ >= 0.0)
|
|
|
|
@objective(m, Max, κ)
|
|
|
|
|
2016-12-23 00:51:06 +01:00
|
|
|
for (pairs, δ², δ) in zip(matrix_constraints, Δ².coefficients, Δ.coefficients)
|
2016-12-19 15:44:52 +01:00
|
|
|
@constraint(m, sum(A[i,j] for (i,j) in pairs) == δ² - κ*δ)
|
|
|
|
end
|
|
|
|
return m
|
|
|
|
end
|
|
|
|
|
2017-01-09 01:01:31 +01:00
|
|
|
function solve_for_property_T{T}(S₁::Vector{Array{T,2}}, solver; verbose=true)
|
|
|
|
|
|
|
|
Δ, matrix_constraints = prepare_Laplacian_and_constraints(S₁)
|
|
|
|
|
|
|
|
problem = create_SDP_problem(matrix_constraints, Δ);
|
|
|
|
@show solver
|
|
|
|
|
|
|
|
setsolver(problem, solver);
|
|
|
|
verbose && @show problem
|
|
|
|
|
|
|
|
solution_status = solve(problem);
|
|
|
|
verbose && @show solution_status
|
|
|
|
|
|
|
|
if solution_status != :Optimal
|
|
|
|
throw(ExceptionError("The solver did not solve the problem successfully!"))
|
|
|
|
else
|
|
|
|
κ = SL_3ZZ.objVal;
|
|
|
|
A = getvalue(getvariable(SL_3ZZ, :A));;
|
|
|
|
end
|
|
|
|
|
|
|
|
return κ, A
|
|
|
|
end
|
|
|
|
|
|
|
|
function EOI{T<:Number}(Δ::GroupAlgebraElement{T}, κ::T)
|
|
|
|
return Δ*Δ - κ*Δ
|
|
|
|
end
|
|
|
|
|
|
|
|
function resulting_SOS{T<:Number}(sqrt_matrix::Array{T,2},
|
|
|
|
elt::GroupAlgebraElement{T})
|
2016-12-23 00:51:06 +01:00
|
|
|
result = zeros(elt.coefficients)
|
|
|
|
zzz = zeros(elt.coefficients)
|
2016-12-22 02:39:18 +01:00
|
|
|
L = size(sqrt_matrix,2)
|
|
|
|
for i in 1:L
|
|
|
|
zzz[1:L] = view(sqrt_matrix, :,i)
|
|
|
|
new_base = GroupAlgebraElement(zzz, elt.product_matrix)
|
2016-12-23 00:51:06 +01:00
|
|
|
result += (new_base*new_base).coefficients
|
2016-12-19 15:44:52 +01:00
|
|
|
end
|
2016-12-22 22:12:52 +01:00
|
|
|
return GroupAlgebraElement{T}(result, elt.product_matrix)
|
2016-12-19 15:44:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
function correct_to_augmentation_ideal{T<:Rational}(sqrt_matrix::Array{T,2})
|
|
|
|
sqrt_corrected = similar(sqrt_matrix)
|
|
|
|
l = size(sqrt_matrix,2)
|
|
|
|
for i in 1:l
|
|
|
|
col = view(sqrt_matrix,:,i)
|
|
|
|
sqrt_corrected[:,i] = col - sum(col)//l
|
2017-01-09 01:01:31 +01:00
|
|
|
# @assert sum(sqrt_corrected[:,i]) == 0
|
2016-12-19 15:44:52 +01:00
|
|
|
end
|
|
|
|
return sqrt_corrected
|
|
|
|
end
|
2017-01-09 01:01:31 +01:00
|
|
|
|
|
|
|
function check_solution{T<:Number}(κ::T,
|
|
|
|
sqrt_matrix::Array{T,2},
|
|
|
|
Δ::GroupAlgebraElement{T})
|
|
|
|
eoi = EOI(Δ, κ)
|
|
|
|
result = resulting_SOS(sqrt_matrix, Δ)
|
|
|
|
return sum(abs.((result - eoi).coefficients)), sum(result.coefficients)
|
|
|
|
end
|
|
|
|
|
2017-01-09 00:59:40 +01:00
|
|
|
function rationalize{T<:Integer, S<:Real}(::Type{T},
|
|
|
|
X::AbstractArray{S}; tol::Real=eps(eltype(X)))
|
|
|
|
|
|
|
|
r(x) = rationalize(T, x, tol=tol)
|
|
|
|
return r.(X)
|
|
|
|
end
|