Skip to content

Commit 0799b12

Browse files
committed
Fix
1 parent 89471bb commit 0799b12

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/Utilities/distance_to_set.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,20 @@ function distance_to_set(
531531
},
532532
) where {T<:Real}
533533
_check_dimension(x, set)
534-
λ, U = LinearAlgebra.eigen(_reshape(x, set))
535-
if minimum(λ) >= zero(T)
536-
return 0.0
534+
# Let
535+
# 1) `U` be the matrix of eigenvectors.
536+
# 2) `λ_negative = LinearAlgebra.Diagonal(min.(zero(T), λ))`
537+
# 3) `A = LinearAlgebra.Symmetric(U * λ_negative * U')`
538+
# The upper bound distance is the Frobenius norm of `A` which is the square
539+
# root of the sum of the squares of the negative eigenvalues.
540+
# Indeed:
541+
# `⟨U * λ_negative * U', U * λ_negative * U'⟩`
542+
# which is equal to
543+
# `⟨λ_negative * U' * U, U' * U * λ_negative⟩`
544+
# Since `U'U = I` this is equal to
545+
# `⟨λ_negative, λ_negative⟩`
546+
# So we need the return:
547+
return sum(LinearAlgebra.eigvals(_reshape(x, set))) do λ
548+
min(λ, zero(T))^2
537549
end
538-
λ_negative = LinearAlgebra.Diagonal(min.(zero(T), λ))
539-
A = LinearAlgebra.Symmetric(U * λ_negative * U')
540-
# Σ A^2 is needed to match the definition of Utilities.set_dot
541-
return sum(Base.Fix2(^, 2), A)
542550
end

test/Utilities/distance_to_set.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ function test_positivesemidefiniteconesquare()
314314
[1.0, 0.0, 0.0, 1.0] => 0.0,
315315
[1.0, -1.0, -1.0, 1.0] => 0.0,
316316
[1.0, -2.0, -2.0, 1.0] => 1.0,
317-
[1.0, 1.1, 1.1, -2.3] => 6.9329523025;
317+
[1.0, 1.1, 1.1, -2.3] => 2.633053201505194;
318318
mismatch = [1.0],
319319
)
320320
return
@@ -326,7 +326,7 @@ function test_positivesemidefiniteconetriangle()
326326
[1.0, 0.0, 1.0] => 0.0,
327327
[1.0, -1.0, 1.0] => 0.0,
328328
[1.0, -2.0, 1.0] => 1.0,
329-
[1.0, 1.1, -2.3] => 6.9329523025;
329+
[1.0, 1.1, -2.3] => 2.633053201505194;
330330
mismatch = [1.0],
331331
)
332332
return

0 commit comments

Comments
 (0)