PropertyT.jl/src/checksolution.jl

137 lines
4.5 KiB
Julia
Raw Normal View History

2017-03-13 14:49:55 +01:00
using ProgressMeter
import Base: rationalize
using ValidatedNumerics
setrounding(Interval, :narrow)
setdisplay(:standard)
2017-03-13 14:49:55 +01:00
function EOI{T<:Number}(Δ::GroupAlgebraElement{T}, κ::T)
return Δ*Δ - κ*Δ
end
2017-03-14 16:40:35 +01:00
function algebra_square(vector, elt)
2017-03-13 14:49:55 +01:00
zzz = zeros(elt.coefficients)
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
import ValidatedNumerics
function (±){T<:Number}(X::AbstractArray{T}, tol::Real)
r{T}(x::T) = (x == zero(T)? @biginterval(0) : x ± tol)
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-13 14:49:55 +01:00
function check_solution{T<:Number}(κ::T, sqrt_matrix::Array{T,2}, Δ::GroupAlgebraElement{T}; verbose=true, augmented=false)
result = compute_SOS(sqrt_matrix, Δ)
if augmented
epsilon = GroupAlgebras.ɛ(result)
2017-03-14 16:40:53 +01:00
@show epsilon
2017-03-13 14:49:55 +01:00
end
2017-03-14 16:40:53 +01:00
SOS_diff = EOI(Δ, κ) - result
2017-03-13 14:49:55 +01:00
eoi_SOS_L₁_dist = norm(SOS_diff,1)
if verbose
@show κ
ɛ_dist = GroupAlgebras.ɛ(SOS_diff)
2017-03-14 16:40:53 +01:00
if isa(ɛ_dist, Interval)
ɛ_dist = ɛ_dist.lo
L₁_dist = eoi_SOS_L₁_dist.lo
else
L₁_dist = eoi_SOS_L₁_dist
end
@printf("ɛ(Δ² - κΔ - ∑ξᵢ*ξᵢ) ≈ %.10f\n", float(ɛ_dist))
@printf("‖Δ² - κΔ - ∑ξᵢ*ξᵢ‖₁ ≈ %.10f\n", float(L₁_dist))
2017-03-13 14:49:55 +01:00
end
distance_to_cone = κ - 2^3*eoi_SOS_L₁_dist
return distance_to_cone
end
end
function _distance_to_positive_cone(Δ::GroupAlgebraElement, κ, A;
tol=1e-7, verbose=true, rational=false)
isapprox(eigvals(A), abs(eigvals(A)), atol=tol) ||
warn("The solution matrix doesn't seem to be positive definite!")
@assert A == Symmetric(A)
A_sqrt = real(sqrtm(A))
2017-03-14 16:40:53 +01:00
println("-------------------------------------------------------------")
println("")
println("Checking in floating-point arithmetic...")
@time fp_distance = check_solution(κ, A_sqrt, Δ, verbose=verbose)
println("Floating point distance (to positive cone)\n$(Float64(trunc(fp_distance,8)))")
2017-03-13 14:49:55 +01:00
println("-------------------------------------------------------------")
println("")
2017-03-14 16:40:53 +01:00
println("Projecting columns of rationalized A_sqrt to the augmentation ideal...")
δ = eps(κ)
A_sqrt_ = (A_sqrt, δ)
2017-03-13 14:49:55 +01:00
A_sqrt__aug = correct_to_augmentation_ideal(A_sqrt_)
2017-03-14 16:40:53 +01:00
κ_ = (κ, δ)
Δ_ = (Δ, δ)
2017-03-13 14:49:55 +01:00
2017-03-14 16:40:53 +01:00
println("Checking in interval arithmetic")
A_sqrt__augᴵ = A_sqrt__aug ± δ
κᴵ = κ_ ± δ
Δᴵ = Δ_ ± δ
2017-03-13 14:49:55 +01:00
@time Interval_dist_to_Σ² = check_solution(κᴵ, A_sqrt__augᴵ, Δᴵ, verbose=verbose, augmented=true)
2017-03-14 16:40:53 +01:00
println("The Augmentation-projected actual distance (to positive cone) belongs to \n$Interval_dist_to_Σ²")
2017-03-13 14:49:55 +01:00
println("-------------------------------------------------------------")
println("")
if Interval_dist_to_Σ².lo 0 || !rational
return Interval_dist_to_Σ².lo
else
println("Checking Projected SOS decomposition in exact rational arithmetic...")
@time _dist_to_Σ² = check_solution(κ_, A_sqrt__aug, Δ_, verbose=verbose, augmented=true)
@assert isa(_dist_to_Σ², Rational)
2017-03-14 16:40:53 +01:00
println("Augmentation-projected rational distance (to positive cone)\n$(Float64(trunc(_dist_to_Σ²,8)))")
2017-03-13 14:49:55 +01:00
println("-------------------------------------------------------------")
return _dist_to_Σ²
end
end