1
0
mirror of https://github.com/kalmarek/PropertyT.jl.git synced 2024-11-19 15:25:29 +01:00
PropertyT.jl/property(T).jl

138 lines
4.0 KiB
Julia
Raw Normal View History

2016-12-19 15:44:52 +01:00
using JuMP
2017-01-09 00:59:40 +01:00
import Base: rationalize
2017-01-14 15:24:16 +01:00
using GroupAlgebras
2016-12-19 15:44:52 +01:00
2017-02-11 13:31:01 +01:00
function products{T}(U::AbstractVector{T}, V::AbstractVector{T})
result = Vector{T}()
for u in U
for v in V
push!(result, u*v)
end
end
return unique(result)
end
2016-12-19 15:44:52 +01:00
2017-02-11 13:33:35 +01:00
function create_product_matrix(basis, limit)
2017-01-13 18:04:20 +01:00
product_matrix = zeros(Int, (limit,limit))
for i in 1:limit
2017-02-11 13:33:35 +01:00
x_inv::eltype(basis) = inv(basis[i])
for j in 1:limit
w = x_inv*basis[j]
index = findfirst(basis, w)
2017-02-11 13:33:35 +01:00
index 0 || throw(ArgumentError("Product is not supported on basis: $w"))
product_matrix[i,j] = index
2016-12-19 15:44:52 +01:00
end
end
2017-02-11 13:33:35 +01:00
return product_matrix
2016-12-19 15:44:52 +01:00
end
function constraints_from_pm(pm, total_length=maximum(pm))
n = size(pm,1)
constraints = constraints = [Array{Int,1}[] for x in 1:total_length]
for j in 1:n
Threads.@threads for i in 1:n
idx = pm[i,j]
push!(constraints[idx], [i,j])
end
end
return constraints
end
function splaplacian_coeff(S, basis, n=length(basis))
result = spzeros(n)
result[1] = length(S)
for s in S
ind = findfirst(basis, s)
result[ind] += -1
end
return result
end
function laplacian_coeff(S, basis)
return full(splaplacian_coeff(S,basis))
end
2017-01-09 01:01:31 +01:00
function create_SDP_problem(matrix_constraints, Δ::GroupAlgebraElement)
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 = JuMP.Model();
JuMP.@variable(m, A[1:N, 1:N], SDP)
JuMP.@SDconstraint(m, A >= zeros(size(A)))
JuMP.@variable(m, κ >= 0.0)
JuMP.@objective(m, Max, κ)
2016-12-19 15:44:52 +01:00
2016-12-23 00:51:06 +01:00
for (pairs, δ², δ) in zip(matrix_constraints, Δ².coefficients, Δ.coefficients)
JuMP.@constraint(m, sum(A[i,j] for (i,j) in pairs) == δ² - κ*δ)
2016-12-19 15:44:52 +01:00
end
return m
end
2017-02-11 13:41:03 +01:00
function solve_SDP(sdp_constraints, Δ, solver; verbose=true)
SDP_problem = create_SDP_problem(sdp_constraints, Δ);
verbose && @show solver
JuMP.setsolver(SDP_problem, solver);
verbose && @show SDP_problem
# @time MathProgBase.writeproblem(SDP_problem, "/tmp/SDP_problem")
solution_status = JuMP.solve(SDP_problem);
2017-01-09 01:01:31 +01:00
verbose && @show solution_status
if solution_status != :Optimal
throw(ExceptionError("The solver did not solve the problem successfully!"))
else
2017-02-11 13:41:03 +01:00
κ = SDP_problem.objVal;
A = JuMP.getvalue(JuMP.getvariable(SDP_problem, :A));;
2017-01-09 01:01:31 +01:00
end
return κ, A
end
function EOI{T<:Number}(Δ::GroupAlgebraElement{T}, κ::T)
return Δ*Δ - κ*Δ
end
function square_as_elt(vector, elt)
2016-12-23 00:51:06 +01:00
zzz = zeros(elt.coefficients)
2017-01-14 15:24:16 +01:00
zzz[1:length(vector)] = vector
# new_base_elt = GroupAlgebraElement(zzz, elt.product_matrix)
# return (new_base_elt*new_base_elt).coefficients
return GroupAlgebras.algebra_multiplication(zzz, zzz, elt.product_matrix)
end
function compute_SOS{T<:Number}(sqrt_matrix::Array{T,2},
elt::GroupAlgebraElement{T})
2017-02-11 13:44:51 +01:00
n = size(sqrt_matrix,2)
# result = zeros(T, length(elt.coefficients))
result = @parallel (+) for i in 1:n
square_as_elt(sqrt_matrix[:,i], elt)
2016-12-19 15:44:52 +01:00
end
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(Δ, κ)
2017-01-14 15:24:16 +01:00
result = compute_SOS(sqrt_matrix, Δ)
L₁_dist = norm(result - eoi,1)
return eoi - result, L₁_dist
2017-01-09 01:01:31 +01:00
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;