2017-03-13 14:49:55 +01:00
|
|
|
|
import Base: rationalize
|
|
|
|
|
|
2017-03-14 23:34:47 +01:00
|
|
|
|
using ValidatedNumerics
|
2017-03-20 21:41:34 +01:00
|
|
|
|
ValidatedNumerics.setrounding(Interval, :correct)
|
2017-03-31 22:38:51 +02:00
|
|
|
|
# ValidatedNumerics.setrounding(Interval, :fast) #which is slower??
|
2017-03-20 21:41:34 +01:00
|
|
|
|
ValidatedNumerics.setformat(:standard)
|
2017-03-31 22:38:51 +02:00
|
|
|
|
# setprecision(Interval, 53) # slightly faster than 256
|
2017-03-14 23:34:47 +01:00
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
function EOI{T<:Number}(Δ::GroupAlgebraElement{T}, λ::T)
|
|
|
|
|
return Δ*Δ - λ*Δ
|
2017-03-13 14:49:55 +01:00
|
|
|
|
end
|
|
|
|
|
|
2017-03-14 16:40:35 +01:00
|
|
|
|
function algebra_square(vector, elt)
|
2017-03-14 23:36:59 +01:00
|
|
|
|
zzz = zeros(eltype(vector), elt.coefficients)
|
2017-03-13 14:49:55 +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
|
|
|
|
|
|
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
|
|
|
|
T = eltype(sqrt_matrix)
|
|
|
|
|
|
|
|
|
|
# result = zeros(T, length(elt.coefficients))
|
|
|
|
|
# for i in 1:n
|
|
|
|
|
# result += algebra_square(sqrt_matrix[:,i], elt)
|
|
|
|
|
# end
|
|
|
|
|
|
|
|
|
|
result = @parallel (+) for i in 1:n
|
|
|
|
|
PropertyT.algebra_square(sqrt_matrix[:,i], elt)
|
2017-03-13 14:49:55 +01:00
|
|
|
|
end
|
2017-03-14 16:40:35 +01:00
|
|
|
|
|
|
|
|
|
return GroupAlgebraElement(result, elt.product_matrix)
|
2017-03-13 14:49:55 +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
|
|
|
|
|
# @assert sum(sqrt_corrected[:,i]) == 0
|
|
|
|
|
end
|
|
|
|
|
return sqrt_corrected
|
|
|
|
|
end
|
|
|
|
|
|
2017-03-14 23:34:47 +01:00
|
|
|
|
import ValidatedNumerics.±
|
|
|
|
|
|
|
|
|
|
function (±){T<:Number}(X::AbstractArray{T}, tol::Real)
|
2017-03-31 22:35:30 +02:00
|
|
|
|
r{T}(x::T) = (x == zero(T)? @interval(0) : x ± tol)
|
2017-03-14 23:34:47 +01:00
|
|
|
|
return r.(X)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
(±)(X::GroupAlgebraElement, tol::Real) = GroupAlgebraElement(X.coefficients ± tol, X.product_matrix)
|
|
|
|
|
|
|
|
|
|
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-03-14 23:35:52 +01:00
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
function distance_to_cone{T<:Rational}(λ::T, sqrt_matrix::Array{T,2}, Δ::GroupAlgebraElement{T})
|
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-03-13 14:49:55 +01:00
|
|
|
|
eoi_SOS_L₁_dist = norm(SOS_diff,1)
|
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "λ = $λ (≈$(@sprintf("%.10f", float(λ)))")
|
2017-03-15 17:51:13 +01:00
|
|
|
|
ɛ_dist = GroupAlgebras.ɛ(SOS_diff)
|
|
|
|
|
if ɛ_dist ≠ 0//1
|
|
|
|
|
warn(logger, "The SOS is not in the augmentation ideal, number below are meaningless!")
|
2017-03-13 14:49:55 +01:00
|
|
|
|
end
|
2017-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "ɛ(Δ² - λΔ - ∑ξᵢ*ξᵢ) = $ɛ_dist")
|
|
|
|
|
info(logger, "‖Δ² - λΔ - ∑ξᵢ*ξᵢ‖₁ = $(@sprintf("%.10f", float(eoi_SOS_L₁_dist)))")
|
2017-03-15 17:51:13 +01:00
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
distance_to_cone = λ - 2^3*eoi_SOS_L₁_dist
|
2017-03-13 14:49:55 +01:00
|
|
|
|
return distance_to_cone
|
|
|
|
|
end
|
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
function distance_to_cone{T<:Rational, S<:Interval}(λ::T, sqrt_matrix::Array{S,2}, Δ::GroupAlgebraElement{T})
|
2017-03-14 23:35:52 +01:00
|
|
|
|
SOS = compute_SOS(sqrt_matrix, Δ)
|
2017-03-15 17:51:13 +01:00
|
|
|
|
info(logger, "ɛ(∑ξᵢ*ξᵢ) ∈ $(GroupAlgebras.ɛ(SOS))")
|
2017-04-01 15:21:57 +02:00
|
|
|
|
λⁱⁿᵗ = @interval(λ)
|
2017-04-01 14:22:01 +02:00
|
|
|
|
Δⁱⁿᵗ = GroupAlgebraElement([@interval(c) for c in Δ.coefficients], Δ.product_matrix)
|
2017-04-01 15:21:57 +02:00
|
|
|
|
SOS_diff = EOI(Δⁱⁿᵗ, λⁱⁿᵗ) - SOS
|
2017-03-14 23:35:52 +01:00
|
|
|
|
eoi_SOS_L₁_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-03-15 17:51:13 +01:00
|
|
|
|
ɛ_dist = GroupAlgebras.ɛ(SOS_diff)
|
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "ɛ(Δ² - λΔ - ∑ξᵢ*ξᵢ) ∈ $(ɛ_dist)")
|
|
|
|
|
info(logger, "‖Δ² - λΔ - ∑ξᵢ*ξᵢ‖₁ ∈ $(eoi_SOS_L₁_dist)")
|
2017-03-13 14:49:55 +01:00
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
distance_to_cone = λ - 2^3*eoi_SOS_L₁_dist
|
2017-03-14 23:35:52 +01:00
|
|
|
|
return distance_to_cone
|
2017-03-13 14:49:55 +01:00
|
|
|
|
end
|
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
function distance_to_cone{T<:AbstractFloat}(λ::T, sqrt_matrix::Array{T,2}, Δ::GroupAlgebraElement{T})
|
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-03-14 23:35:52 +01:00
|
|
|
|
eoi_SOS_L₁_dist = norm(SOS_diff,1)
|
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "λ = $λ")
|
2017-03-15 17:51:13 +01:00
|
|
|
|
ɛ_dist = GroupAlgebras.ɛ(SOS_diff)
|
2017-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "ɛ(Δ² - λΔ - ∑ξᵢ*ξᵢ) ≈ $(@sprintf("%.10f", ɛ_dist))")
|
|
|
|
|
info(logger, "‖Δ² - λΔ - ∑ξᵢ*ξᵢ‖₁ ≈ $(@sprintf("%.10f", eoi_SOS_L₁_dist))")
|
2017-03-15 17:51:13 +01:00
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
distance_to_cone = λ - 2^3*eoi_SOS_L₁_dist
|
2017-03-14 23:35:52 +01:00
|
|
|
|
return distance_to_cone
|
|
|
|
|
end
|
2017-03-13 14:49:55 +01:00
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
function check_distance_to_positive_cone(Δ::GroupAlgebraElement, λ, P;
|
2017-03-15 17:51:13 +01:00
|
|
|
|
tol=1e-7, rational=false)
|
2017-03-13 14:49:55 +01:00
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
isapprox(eigvals(P), abs(eigvals(P)), atol=tol) ||
|
2017-03-13 14:49:55 +01:00
|
|
|
|
warn("The solution matrix doesn't seem to be positive definite!")
|
2017-04-01 15:21:57 +02:00
|
|
|
|
@assert P == Symmetric(P)
|
|
|
|
|
Q = real(sqrtm(P))
|
2017-03-14 23:35:52 +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-04-01 15:21:57 +02:00
|
|
|
|
t = @timed fp_distance = distance_to_cone(λ, Q, Δ)
|
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-03-31 22:38:18 +02:00
|
|
|
|
# if fp_distance ≤ 0
|
|
|
|
|
# return fpdistance
|
|
|
|
|
# end
|
|
|
|
|
|
2017-04-01 15:21:57 +02:00
|
|
|
|
info(logger, "Projecting columns of rationalized Q to the augmentation ideal...")
|
|
|
|
|
δ = eps(λ)
|
|
|
|
|
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-04-01 15:21:57 +02:00
|
|
|
|
Q_ℚωⁱⁿᵗ = Float64.(Q_ℚω) ± δ
|
|
|
|
|
t = @timed Interval_dist_to_Σ² = distance_to_cone(λ_ℚ, Q_ℚωⁱⁿᵗ, Δ_ℚ)
|
2017-03-15 20:13:53 +01:00
|
|
|
|
info(logger, timed_msg(t))
|
2017-04-01 14:21:01 +02:00
|
|
|
|
info(logger, "The Augmentation-projected actual distance (to positive cone) ∈ $(Interval_dist_to_Σ²)")
|
2017-03-15 17:56:01 +01:00
|
|
|
|
info(logger, "------------------------------------------------------------")
|
2017-03-13 14:49:55 +01:00
|
|
|
|
|
2017-03-16 14:29:11 +01:00
|
|
|
|
if Interval_dist_to_Σ².lo ≤ 0 || !rational
|
2017-04-01 14:21:01 +02:00
|
|
|
|
return Interval_dist_to_Σ²
|
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-04-01 15:21:57 +02:00
|
|
|
|
t = @timed ℚ_dist_to_Σ² = distance_to_cone(λ_ℚ, Q_ℚω, Δ_ℚ)
|
2017-03-15 20:13:53 +01:00
|
|
|
|
info(logger, timed_msg(t))
|
2017-03-13 14:49:55 +01:00
|
|
|
|
@assert isa(ℚ_dist_to_Σ², Rational)
|
2017-03-15 17:56:01 +01:00
|
|
|
|
info(logger, "Augmentation-projected rational distance (to positive cone) ≥ $(Float64(trunc(ℚ_dist_to_Σ²,8)))")
|
|
|
|
|
info(logger, "------------------------------------------------------------")
|
2017-03-13 14:49:55 +01:00
|
|
|
|
return ℚ_dist_to_Σ²
|
|
|
|
|
end
|
|
|
|
|
end
|