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-06-25 09:28:44 +02:00
|
|
|
|
IntervalArithmetic.setformat(sigfigs=10)
|
|
|
|
|
IntervalArithmetic.setprecision(Interval, 53) # slightly faster than 256
|
|
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
function Base.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
|
|
|
|
|
|
|
|
|
|
ℚ(x, tol::Real) = rationalize(BigInt, x, tol=tol)
|
|
|
|
|
|
2017-07-21 17:11:25 +02:00
|
|
|
|
EOI{T<:Number}(Δ::GroupRingElem{T}, λ::T) = Δ*Δ - λ*Δ
|
2017-03-13 14:49:55 +01:00
|
|
|
|
|
2017-07-21 16:07:08 +02:00
|
|
|
|
function groupring_square(vect::AbstractVector, elt::GroupRingElem)
|
2017-07-05 12:28:29 +02:00
|
|
|
|
zzz = zeros(eltype(vect), length(elt.coeffs))
|
2017-07-21 17:11:58 +02:00
|
|
|
|
zzz[1:length(vect)] .= vect
|
2017-07-21 16:07:08 +02:00
|
|
|
|
return GroupRings.mul!(similar(zzz), zzz, zzz, parent(elt).pm)
|
2017-03-13 14:49:55 +01:00
|
|
|
|
end
|
|
|
|
|
|
2017-03-14 16:40:35 +01:00
|
|
|
|
function compute_SOS(sqrt_matrix, elt)
|
2017-03-13 14:49:55 +01:00
|
|
|
|
n = size(sqrt_matrix,2)
|
2017-03-14 16:40:35 +01:00
|
|
|
|
|
2017-07-21 17:11:58 +02:00
|
|
|
|
result = zeros(eltype(sqrt_matrix), length(elt.coeffs))
|
|
|
|
|
for i in 1:n
|
|
|
|
|
result .+= (groupring_square(view(sqrt_matrix,:,i), elt))
|
2017-03-13 14:49:55 +01:00
|
|
|
|
end
|
2017-07-21 17:11:58 +02:00
|
|
|
|
|
|
|
|
|
# result = @parallel (+) for i in 1:n
|
|
|
|
|
# groupring_square(sqrt_matrix[:,i], elt)
|
|
|
|
|
# end
|
|
|
|
|
|
2017-05-16 18:53:25 +02:00
|
|
|
|
return GroupRingElem(result, parent(elt))
|
2017-03-13 14:49:55 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function correct_to_augmentation_ideal{T<:Rational}(sqrt_matrix::Array{T,2})
|
2017-08-04 15:30:12 +02:00
|
|
|
|
l = size(sqrt_matrix, 2)
|
|
|
|
|
sqrt_corrected = copy(sqrt_matrix)
|
|
|
|
|
Threads.@threads for j in 1:l
|
|
|
|
|
col = sum(view(sqrt_matrix, :,j))//l
|
|
|
|
|
for i in 1:l
|
|
|
|
|
sqrt_corrected[i,j] -= col
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return sqrt_corrected
|
2017-03-13 14:49:55 +01:00
|
|
|
|
end
|
|
|
|
|
|
2017-06-09 09:35:33 +02:00
|
|
|
|
function distance_to_cone{T<:Rational}(λ::T, sqrt_matrix::Array{T,2}, Δ::GroupRingElem{T}, wlen)
|
2017-03-14 23:35:52 +01:00
|
|
|
|
SOS = compute_SOS(sqrt_matrix, Δ)
|
2017-03-13 14:49:55 +01:00
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
SOS_diff = EOI(Δ, λ) - SOS
|
2017-06-04 20:30:34 +02:00
|
|
|
|
eoi_SOS_L1_dist = norm(SOS_diff,1)
|
2017-03-13 14:49:55 +01:00
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "λ = $λ (≈$(@sprintf("%.10f", float(λ)))")
|
2017-05-28 20:01:03 +02:00
|
|
|
|
ɛ_dist = GroupRings.augmentation(SOS_diff)
|
2017-03-15 17:51:13 +01:00
|
|
|
|
if ɛ_dist ≠ 0//1
|
2017-06-04 20:30:34 +02:00
|
|
|
|
warn(logger, "The SOS is not in the augmentation ideal, numbers below are meaningless!")
|
2017-03-13 14:49:55 +01:00
|
|
|
|
end
|
2017-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "ɛ(Δ² - λΔ - ∑ξᵢ*ξᵢ) = $ɛ_dist")
|
2017-06-04 20:30:34 +02:00
|
|
|
|
info(logger, "‖Δ² - λΔ - ∑ξᵢ*ξᵢ‖₁ = $(@sprintf("%.10f", float(eoi_SOS_L1_dist)))")
|
2017-03-15 17:51:13 +01:00
|
|
|
|
|
2017-06-09 09:35:33 +02:00
|
|
|
|
distance_to_cone = λ - 2^(wlen-1)*eoi_SOS_L1_dist
|
2017-03-13 14:49:55 +01:00
|
|
|
|
return distance_to_cone
|
|
|
|
|
end
|
|
|
|
|
|
2017-06-25 09:33:25 +02:00
|
|
|
|
function distance_to_cone{T<:Rational, S<:Interval}(λ::T, sqrt_matrix::AbstractArray{S,2}, Δ::GroupRingElem{T}, wlen)
|
2017-03-14 23:35:52 +01:00
|
|
|
|
SOS = compute_SOS(sqrt_matrix, Δ)
|
2017-05-28 20:01:03 +02:00
|
|
|
|
info(logger, "ɛ(∑ξᵢ*ξᵢ) ∈ $(GroupRings.augmentation(SOS))")
|
2017-06-04 20:30:34 +02:00
|
|
|
|
λ_int = @interval(λ)
|
2017-06-05 17:27:55 +02:00
|
|
|
|
Δ_int = GroupRingElem([@interval(c) for c in Δ.coeffs], parent(Δ))
|
2017-06-04 20:30:34 +02:00
|
|
|
|
SOS_diff = EOI(Δ_int, λ_int) - SOS
|
|
|
|
|
eoi_SOS_L1_dist = norm(SOS_diff,1)
|
2017-03-13 14:49:55 +01:00
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "λ = $λ (≈≥$(@sprintf("%.10f",float(λ))))")
|
2017-05-28 20:01:03 +02:00
|
|
|
|
ɛ_dist = GroupRings.augmentation(SOS_diff)
|
2017-03-15 17:51:13 +01:00
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "ɛ(Δ² - λΔ - ∑ξᵢ*ξᵢ) ∈ $(ɛ_dist)")
|
2017-06-04 20:30:34 +02:00
|
|
|
|
info(logger, "‖Δ² - λΔ - ∑ξᵢ*ξᵢ‖₁ ∈ $(eoi_SOS_L1_dist)")
|
2017-03-13 14:49:55 +01:00
|
|
|
|
|
2017-06-09 09:35:33 +02:00
|
|
|
|
distance_to_cone = λ - 2^(wlen-1)*eoi_SOS_L1_dist
|
2017-03-14 23:35:52 +01:00
|
|
|
|
return distance_to_cone
|
2017-03-13 14:49:55 +01:00
|
|
|
|
end
|
|
|
|
|
|
2017-06-25 09:33:25 +02:00
|
|
|
|
function distance_to_cone(λ, sqrt_matrix::AbstractArray, Δ::GroupRingElem, wlen)
|
2017-03-14 23:35:52 +01:00
|
|
|
|
SOS = compute_SOS(sqrt_matrix, Δ)
|
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
SOS_diff = EOI(Δ, λ) - SOS
|
2017-06-05 17:24:45 +02:00
|
|
|
|
eoi_SOS_L1_dist = norm(SOS_diff,1)
|
2017-03-14 23:35:52 +01:00
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "λ = $λ")
|
2017-05-28 20:01:03 +02:00
|
|
|
|
ɛ_dist = GroupRings.augmentation(SOS_diff)
|
2017-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "ɛ(Δ² - λΔ - ∑ξᵢ*ξᵢ) ≈ $(@sprintf("%.10f", ɛ_dist))")
|
2017-06-05 17:24:45 +02:00
|
|
|
|
info(logger, "‖Δ² - λΔ - ∑ξᵢ*ξᵢ‖₁ ≈ $(@sprintf("%.10f", eoi_SOS_L1_dist))")
|
2017-03-15 17:51:13 +01:00
|
|
|
|
|
2017-06-09 09:35:33 +02:00
|
|
|
|
distance_to_cone = λ - 2^(wlen-1)*eoi_SOS_L1_dist
|
2017-03-14 23:35:52 +01:00
|
|
|
|
return distance_to_cone
|
|
|
|
|
end
|
2017-03-13 14:49:55 +01:00
|
|
|
|
|
2017-06-22 11:56:46 +02:00
|
|
|
|
function check_distance_to_positive_cone(Δ::GroupRingElem, λ, Q, wlen;
|
2017-07-05 13:03:25 +02:00
|
|
|
|
tol=1e-14, rational=false)
|
2017-03-13 14:49:55 +01:00
|
|
|
|
|
2017-03-15 17:56:01 +01:00
|
|
|
|
info(logger, "------------------------------------------------------------")
|
2017-03-15 17:51:13 +01:00
|
|
|
|
info(logger, "")
|
|
|
|
|
info(logger, "Checking in floating-point arithmetic...")
|
2017-06-09 09:35:33 +02:00
|
|
|
|
t = @timed fp_distance = distance_to_cone(λ, Q, Δ, wlen)
|
2017-03-15 20:13:53 +01:00
|
|
|
|
info(logger, timed_msg(t))
|
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-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "Projecting columns of rationalized Q to the augmentation ideal...")
|
2017-06-23 13:30:37 +02:00
|
|
|
|
δ = tol
|
2017-04-01 15:21:57 +02:00
|
|
|
|
Q_ℚ = ℚ(Q, δ)
|
|
|
|
|
t = @timed Q_ℚω = correct_to_augmentation_ideal(Q_ℚ)
|
2017-03-31 15:21:45 +02:00
|
|
|
|
info(logger, timed_msg(t))
|
2017-04-01 15:21:57 +02:00
|
|
|
|
λ_ℚ = ℚ(λ, δ)
|
2017-03-14 16:40:53 +01:00
|
|
|
|
Δ_ℚ = ℚ(Δ, δ)
|
2017-03-13 14:49:55 +01:00
|
|
|
|
|
2017-03-15 17:51:13 +01:00
|
|
|
|
info(logger, "Checking in interval arithmetic")
|
2017-06-04 20:30:34 +02:00
|
|
|
|
Q_ℚω_int = Float64.(Q_ℚω) ± δ
|
2017-06-09 09:35:33 +02:00
|
|
|
|
t = @timed Interval_dist_to_ΣSq = distance_to_cone(λ_ℚ, Q_ℚω_int, Δ_ℚ, wlen)
|
2017-03-15 20:13:53 +01:00
|
|
|
|
info(logger, timed_msg(t))
|
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-06-05 17:24:45 +02:00
|
|
|
|
if Interval_dist_to_ΣSq.lo ≤ 0 || !rational
|
|
|
|
|
return Interval_dist_to_ΣSq
|
2017-03-13 14:49:55 +01:00
|
|
|
|
else
|
2017-03-15 17:51:13 +01:00
|
|
|
|
info(logger, "Checking Projected SOS decomposition in exact rational arithmetic...")
|
2017-06-09 09:35:33 +02:00
|
|
|
|
t = @timed ℚ_dist_to_ΣSq = distance_to_cone(λ_ℚ, Q_ℚω, Δ_ℚ, wlen)
|
2017-03-15 20:13:53 +01:00
|
|
|
|
info(logger, timed_msg(t))
|
2017-06-05 17:24:45 +02:00
|
|
|
|
@assert isa(ℚ_dist_to_ΣSq, Rational)
|
|
|
|
|
info(logger, "Augmentation-projected rational distance (to positive cone) ≥ $(Float64(trunc(ℚ_dist_to_ΣSq,8)))")
|
2017-03-15 17:56:01 +01:00
|
|
|
|
info(logger, "------------------------------------------------------------")
|
2017-06-05 17:24:45 +02:00
|
|
|
|
return ℚ_dist_to_ΣSq
|
2017-03-13 14:49:55 +01:00
|
|
|
|
end
|
|
|
|
|
end
|