1
0
mirror of https://github.com/kalmarek/PropertyT.jl.git synced 2024-11-23 00:10:28 +01:00

Using ValidatedNumerics (Interval Arithmetic)

This commit is contained in:
kalmar 2017-03-13 11:33:40 +01:00
parent a1d1c1ca4f
commit 0588eedd7f

View File

@ -4,6 +4,7 @@ import Base: rationalize
using GroupAlgebras
using ProgressMeter
using ValidatedNumerics
function create_product_matrix(basis, limit)
product_matrix = zeros(Int, (limit,limit))
@ -125,7 +126,14 @@ end
function check_solution{T<:Number}(κ::T, sqrt_matrix::Array{T,2}, Δ::GroupAlgebraElement{T}; verbose=true, augmented=false)
result = compute_SOS(sqrt_matrix, Δ)
if augmented
@assert GroupAlgebras.ɛ(result) == 0//1
epsilon = GroupAlgebras.ɛ(result)
if isa(epsilon, Interval)
@assert 0 in epsilon
elseif isa(epsilon, Rational)
@assert epsilon == 0//1
else
warn("Does checking for augmentation has meaning for $(typeof(epsilon))?")
end
end
SOS_diff = EOI(Δ, κ) - result
@ -136,56 +144,100 @@ function check_solution{T<:Number}(κ::T, sqrt_matrix::Array{T,2}, Δ::GroupAlge
if augmented
println("ɛ(Δ² - κΔ - ∑ξᵢ*ξᵢ) = ", GroupAlgebras.ɛ(SOS_diff))
else
ɛ_dist = Float64(round(GroupAlgebras.ɛ(SOS_diff),12))
println("ɛ(Δ² - κΔ - ∑ξᵢ*ξᵢ) ≈ $ɛ_dist")
ɛ_dist = GroupAlgebras.ɛ(SOS_diff)
if typeof(ɛ_dist) <: Interval
ɛ_dist = ɛ_dist.lo
end
L₁_dist = Float64(round(eoi_SOS_L₁_dist, 12))
println("‖Δ² - κΔ - ∑ξᵢ*ξᵢ‖₁ ≈ $L₁_dist")
@printf("ɛ(Δ² - κΔ - ∑ξᵢ*ξᵢ) ≈ %.10f\n", ɛ_dist)
end
L₁_dist = eoi_SOS_L₁_dist
if typeof(L₁_dist) <: Interval
L₁_dist = L₁_dist.lo
end
@printf("‖Δ² - κΔ - ∑ξᵢ*ξᵢ‖₁ ≈ %.10f\n", L₁_dist)
end
distance_to_cone = κ - 2^2*eoi_SOS_L₁_dist
return distance_to_cone
end
function rationalize{T<:Integer, S<:Real}(::Type{T},
import ValidatedNumerics
function (±)(X::AbstractArray, tol::Real)
r{T}(x::T) = ( x==zero(T) ? @interval(x) : 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;
end
(x, tol::Real) = rationalize(BigInt, x, tol=tol)
function _distance_to_positive_cone(Δ::GroupAlgebraElement, κ, A;
tol=10.0^-7, verbose=true)
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))
println("")
println("Checking in floating-point arithmetic...")
@time fp_distance = check_solution(κ, A_sqrt, Δ, verbose=verbose)
println("Floating point distance (to positive cone) ≈ $(Float64(trunc(fp_distance,8)))")
# println("")
# println("Checking in floating-point arithmetic...")
# @time fp_distance = check_solution(κ, A_sqrt, Δ, verbose=verbose)
# println("Floating point distance (to positive cone) ≈ $(Float64(trunc(fp_distance,8)))")
# println("-------------------------------------------------------------")
# println("")
#
# if fp_distance ≤ 0
# return fp_distance
# end
println("Checking in interval arithmetic...")
A_sqrtᴵ = A_sqrt ± tol
κᴵ = κ ± tol
Δᴵ = Δ ± tol
@time Interval_distance = check_solution(κᴵ, A_sqrtᴵ, Δᴵ, verbose=verbose)
# @assert isa(_distance, Rational)
println("The actual distance (to positive cone) is contained in $Interval_distance")
println("-------------------------------------------------------------")
println("")
if fp_distance 0
return fp_distance
if Interval_distance.lo 0
return Interval_distance.lo
end
println("Checking in rational arithmetic...")
κ_ = (trunc(κ,Int(abs(log10(tol)))), tol)
A_sqrt_, Δ_ = (A_sqrt, tol), (Δ, tol)
@time _distance = check_solution(κ_, A_sqrt_, Δ_, verbose=verbose)
@assert isa(_distance, Rational)
println("Rational distance (to positive cone) ≈ $(Float64(trunc(_distance,8)))")
println("Projecting columns of A_sqrt to the augmentation ideal...")
A_sqrt_ = (A_sqrt, tol)
A_sqrt__aug = correct_to_augmentation_ideal(A_sqrt_)
κ_ = (κ, tol)
Δ_ = (Δ, tol)
A_sqrt__augᴵ = A_sqrt__aug ± tol
κᴵ = κ_ ± tol
Δᴵ = Δ_ ± tol
@time Interval_dist_to_Σ² = check_solution(κᴵ, A_sqrt__augᴵ, Δᴵ, verbose=verbose, augmented=true)
println("The Augmentation-projected actual distance (to positive cone) is contained in $Interval_dist_to_Σ²")
println("-------------------------------------------------------------")
println("")
if _distance 0
return _distance
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)
println("Augmentation-projected rational distance (to positive cone) ≥ $(Float64(trunc(_dist_to_Σ²,8)))")
println("-------------------------------------------------------------")
return _dist_to_Σ²
end
end
function pmΔfilenames(name::String)
if !isdir(name)