2017-03-13 14:49:55 +01:00
|
|
|
import Base: rationalize
|
|
|
|
|
2017-06-25 09:28:44 +02:00
|
|
|
using IntervalArithmetic
|
|
|
|
|
2017-07-26 12:58:39 +02:00
|
|
|
IntervalArithmetic.setrounding(Interval, :tight)
|
2017-10-27 18:25:13 +02:00
|
|
|
IntervalArithmetic.setformat(sigfigs=12)
|
2017-06-25 09:28:44 +02:00
|
|
|
|
|
|
|
import IntervalArithmetic.±
|
2017-03-14 23:34:47 +01:00
|
|
|
|
2017-06-25 09:31:52 +02:00
|
|
|
function (±){T<:Number}(X::AbstractArray{T}, tol::Real)
|
|
|
|
r{T}(x::T) = (x == zero(T)? @interval(0) : x ± tol)
|
|
|
|
return r.(X)
|
|
|
|
end
|
|
|
|
|
|
|
|
(±)(X::GroupRingElem, tol::Real) = GroupRingElem(X.coeffs ± tol, parent(X))
|
|
|
|
|
2017-07-21 17:11:25 +02:00
|
|
|
EOI{T<:Number}(Δ::GroupRingElem{T}, λ::T) = Δ*Δ - λ*Δ
|
2017-03-13 14:49:55 +01:00
|
|
|
|
2017-08-04 20:33:02 +02:00
|
|
|
function groupring_square(vect::AbstractVector, l, pm)
|
|
|
|
zzz = zeros(eltype(vect), l)
|
2017-11-03 16:54:41 +01:00
|
|
|
return GroupRings.mul!(zzz, vect, vect, pm)
|
2017-03-13 14:49:55 +01:00
|
|
|
end
|
|
|
|
|
2017-10-27 18:29:19 +02:00
|
|
|
function compute_SOS(Q::AbstractArray, pm::Array{Int,2}, l::Int)
|
2017-03-14 16:40:35 +01:00
|
|
|
|
2017-10-27 18:29:19 +02:00
|
|
|
# result = zeros(eltype(Q), l)
|
2017-11-03 16:54:41 +01:00
|
|
|
# r = similar(result)
|
|
|
|
# for i in 1:size(Q,2)
|
|
|
|
# print(" $i")
|
|
|
|
# result += GroupRings.mul!(r, view(Q,:,i), view(Q,:,i), pm)
|
2017-08-27 18:30:49 +02:00
|
|
|
# end
|
|
|
|
|
2017-10-09 18:08:22 +02:00
|
|
|
@everywhere groupring_square = PropertyT.groupring_square
|
|
|
|
|
2017-11-03 16:54:41 +01:00
|
|
|
result = @parallel (+) for i in 1:size(Q,2)
|
2017-10-27 18:29:19 +02:00
|
|
|
print(" $i")
|
|
|
|
groupring_square(view(Q,:,i), l, pm)
|
2017-10-09 18:08:22 +02:00
|
|
|
end
|
|
|
|
|
2017-11-03 16:54:41 +01:00
|
|
|
println("")
|
|
|
|
|
2017-10-27 18:29:19 +02:00
|
|
|
return result
|
2017-03-13 14:49:55 +01:00
|
|
|
end
|
|
|
|
|
2017-10-27 18:33:18 +02:00
|
|
|
function compute_SOS(Q::AbstractArray, RG::GroupRing, l::Int)
|
|
|
|
result = compute_SOS(Q, RG.pm, l)
|
|
|
|
return GroupRingElem(result, RG)
|
2017-03-13 14:49:55 +01:00
|
|
|
end
|
|
|
|
|
2017-11-02 13:48:20 +01:00
|
|
|
function distance_to_cone{T<:Interval}(elt::GroupRingElem, Q::AbstractArray{T,2}, wlen::Int)
|
2017-10-27 18:30:59 +02:00
|
|
|
SOS = compute_SOS(Q, parent(elt), length(elt.coeffs))
|
|
|
|
SOS_diff = elt - SOS
|
2017-03-13 14:49:55 +01:00
|
|
|
|
2017-10-27 18:30:59 +02:00
|
|
|
ɛ_dist = GroupRings.augmentation(SOS_diff)
|
|
|
|
info(logger, "ɛ(∑ξᵢ*ξᵢ) ∈ $(ɛ_dist)")
|
2017-03-15 17:51:13 +01:00
|
|
|
|
2017-10-27 18:30:59 +02:00
|
|
|
eoi_SOS_L1_dist = norm(SOS_diff,1)
|
|
|
|
info(logger, "‖Δ² - λΔ - ∑ξᵢ*ξᵢ‖₁ ∈ $(eoi_SOS_L1_dist)")
|
2017-03-13 14:49:55 +01:00
|
|
|
|
2017-10-27 18:30:59 +02:00
|
|
|
dist = 2^(wlen-1)*eoi_SOS_L1_dist
|
|
|
|
return dist
|
2017-03-13 14:49:55 +01:00
|
|
|
end
|
|
|
|
|
2017-10-27 18:30:59 +02:00
|
|
|
function distance_to_cone{T}(elt::GroupRingElem, Q::AbstractArray{T,2}, wlen::Int)
|
|
|
|
SOS = compute_SOS(Q, parent(elt), length(elt.coeffs))
|
|
|
|
SOS_diff = elt - SOS
|
2017-03-14 23:35:52 +01:00
|
|
|
|
2017-10-27 18:30:59 +02:00
|
|
|
ɛ_dist = GroupRings.augmentation(SOS_diff)
|
|
|
|
info(logger, "ɛ(Δ² - λΔ - ∑ξᵢ*ξᵢ) ≈ $(@sprintf("%.10f", ɛ_dist))")
|
2017-03-14 23:35:52 +01:00
|
|
|
|
2017-10-27 18:30:59 +02:00
|
|
|
eoi_SOS_L1_dist = norm(SOS_diff,1)
|
|
|
|
info(logger, "‖Δ² - λΔ - ∑ξᵢ*ξᵢ‖₁ ≈ $(@sprintf("%.10f", eoi_SOS_L1_dist))")
|
|
|
|
|
|
|
|
dist = 2^(wlen-1)*eoi_SOS_L1_dist
|
|
|
|
return dist
|
|
|
|
end
|
2017-03-15 17:51:13 +01:00
|
|
|
|
2017-10-27 18:32:58 +02:00
|
|
|
function augIdproj{T, I<:AbstractInterval}(S::Type{I}, Q::AbstractArray{T,2})
|
|
|
|
l = size(Q, 2)
|
2017-11-03 16:42:53 +01:00
|
|
|
R = zeros(S, (l,l))
|
2017-10-27 18:32:58 +02:00
|
|
|
Threads.@threads for j in 1:l
|
|
|
|
col = sum(view(Q, :,j))/l
|
|
|
|
for i in 1:l
|
|
|
|
R[i,j] = Q[i,j] - col ± eps(0.0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return R
|
2017-03-14 23:35:52 +01:00
|
|
|
end
|
2017-03-13 14:49:55 +01:00
|
|
|
|
2017-10-27 18:32:58 +02:00
|
|
|
function augIdproj{T}(Q::AbstractArray{T,2}, logger)
|
|
|
|
info(logger, "Projecting columns of Q to the augmentation ideal...")
|
2017-11-03 16:42:53 +01:00
|
|
|
@logtime logger Q = augIdproj(Interval{T}, Q)
|
2017-08-04 20:36:25 +02:00
|
|
|
|
2017-08-27 18:50:56 +02:00
|
|
|
info(logger, "Checking that sum of every column contains 0.0... ")
|
2017-10-09 18:06:29 +02:00
|
|
|
check = all([0.0 in sum(view(Q, :, i)) for i in 1:size(Q, 2)])
|
2017-08-27 18:50:56 +02:00
|
|
|
info(logger, (check? "They do." : "FAILED!"))
|
|
|
|
|
2017-08-27 18:46:31 +02:00
|
|
|
@assert check
|
2017-08-04 20:36:25 +02:00
|
|
|
|
2017-10-09 18:06:29 +02:00
|
|
|
return Q
|
2017-08-04 20:36:25 +02:00
|
|
|
end
|
|
|
|
|
2017-10-27 18:34:07 +02:00
|
|
|
function distance_to_positive_cone(Δ::GroupRingElem, λ, Q, wlen::Int)
|
2017-03-15 17:56:01 +01:00
|
|
|
info(logger, "------------------------------------------------------------")
|
2017-10-27 18:34:07 +02:00
|
|
|
info(logger, "λ = $λ")
|
2017-03-15 17:51:13 +01:00
|
|
|
info(logger, "Checking in floating-point arithmetic...")
|
2017-10-27 18:34:07 +02:00
|
|
|
Δ²_λΔ = EOI(Δ, λ)
|
2017-11-02 13:45:12 +01:00
|
|
|
@logtime logger fp_distance = λ - distance_to_cone(Δ²_λΔ, Q, wlen)
|
2017-03-16 10:07:35 +01:00
|
|
|
info(logger, "Floating point distance (to positive cone) ≈ $(@sprintf("%.10f", fp_distance))")
|
2017-03-15 17:56:01 +01:00
|
|
|
info(logger, "------------------------------------------------------------")
|
2017-03-13 14:49:55 +01:00
|
|
|
|
2017-04-17 15:22:33 +02:00
|
|
|
if fp_distance ≤ 0
|
2017-05-28 20:06:21 +02:00
|
|
|
return fp_distance
|
2017-04-17 15:22:33 +02:00
|
|
|
end
|
2017-03-31 22:38:18 +02:00
|
|
|
|
2017-06-05 17:29:22 +02:00
|
|
|
info(logger, "")
|
2017-10-27 18:34:07 +02:00
|
|
|
Q = augIdproj(Q, logger)
|
2017-03-13 14:49:55 +01:00
|
|
|
|
2017-03-15 17:51:13 +01:00
|
|
|
info(logger, "Checking in interval arithmetic")
|
2017-10-27 18:34:07 +02:00
|
|
|
λ = @interval(λ)
|
|
|
|
Δ = GroupRingElem([@interval(c) for c in Δ.coeffs], parent(Δ))
|
|
|
|
Δ²_λΔ = EOI(Δ, λ)
|
2017-08-04 20:36:25 +02:00
|
|
|
|
2017-11-02 13:45:12 +01:00
|
|
|
@logtime logger Interval_dist_to_ΣSq = λ - distance_to_cone(Δ²_λΔ, Q, wlen)
|
2017-06-05 17:24:45 +02:00
|
|
|
info(logger, "The Augmentation-projected actual distance (to positive cone) ∈ $(Interval_dist_to_ΣSq)")
|
2017-03-15 17:56:01 +01:00
|
|
|
info(logger, "------------------------------------------------------------")
|
2017-03-13 14:49:55 +01:00
|
|
|
|
2017-10-27 18:34:07 +02:00
|
|
|
return Interval_dist_to_ΣSq
|
2017-03-13 14:49:55 +01:00
|
|
|
end
|