From 32f82274e9e6b0c026663784e1ce7a67ce034a2d Mon Sep 17 00:00:00 2001 From: schillic Date: Wed, 19 Apr 2023 21:39:44 +0200 Subject: [PATCH 1/3] improve rare algorithm for support vector of UnionSet --- src/LazyOperations/UnionSet.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LazyOperations/UnionSet.jl b/src/LazyOperations/UnionSet.jl index 9c1b2fb409..6679ff8ea8 100644 --- a/src/LazyOperations/UnionSet.jl +++ b/src/LazyOperations/UnionSet.jl @@ -124,8 +124,8 @@ function σ(d::AbstractVector, cup::UnionSet; algorithm="support_vector") return dot(d, σX) > dot(d, σY) ? σX : σY elseif algorithm == "support_function" - m = argmax([ρ(d, X), ρ(d, Y)]) - return m == 1 ? σ(d, X) : σ(d, Y) + ρX, ρY = ρ(d, X), ρ(d, Y) + return ρX > ρY ? σ(d, X) : σ(d, Y) else error("algorithm $algorithm for the support vector of a `UnionSet` is" * From 1d61444e283847a22ebfea52a8361cf54520ae9f Mon Sep 17 00:00:00 2001 From: schillic Date: Wed, 19 Apr 2023 22:02:12 +0200 Subject: [PATCH 2/3] faster support vector of UnionSetArray --- src/LazyOperations/UnionSetArray.jl | 30 +++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/LazyOperations/UnionSetArray.jl b/src/LazyOperations/UnionSetArray.jl index 2262e106cd..4e73743746 100644 --- a/src/LazyOperations/UnionSetArray.jl +++ b/src/LazyOperations/UnionSetArray.jl @@ -109,18 +109,14 @@ support function of each set directly and then calls only the support vector of one of the ``Xᵢ``. """ function σ(d::AbstractVector, cup::UnionSetArray; algorithm="support_vector") - A = array(cup) + arr = array(cup) if algorithm == "support_vector" - σarray = map(Xi -> σ(d, Xi), A) - ρarray = map(vi -> dot(d, vi), σarray) - m = argmax(ρarray) - return σarray[m] + return _σ_union(d, arr) elseif algorithm == "support_function" - ρarray = map(Xi -> ρ(d, Xi), A) - m = argmax(ρarray) - return σ(d, A[m]) + m = argmax(i -> ρ(d, @inbounds arr[i]), eachindex(arr)) + return σ(d, arr[m]) else error("algorithm $algorithm for the support vector of a " * @@ -128,6 +124,21 @@ function σ(d::AbstractVector, cup::UnionSetArray; algorithm="support_vector") end end +function _σ_union(d::AbstractVector, sets) + σmax = d + N = eltype(d) + ρmax = N(-Inf) + for Xi in sets + σX = σ(d, Xi) + ρX = dot(d, σX) + if ρX > ρmax + ρmax = ρX + σmax = σX + end + end + return σmax +end + """ ρ(d::AbstractVector, cup::UnionSetArray) @@ -149,8 +160,7 @@ The support function of the union of a finite number of sets ``X₁, X₂, ...`` can be obtained as the maximum of ``ρ(d, X₂), ρ(d, X₂), ...``. """ function ρ(d::AbstractVector, cup::UnionSetArray) - A = array(cup) - return maximum(Xi -> ρ(d, Xi), A) + return maximum(Xi -> ρ(d, Xi), array(cup)) end """ From e5ee8ffd36288bb2e52cda8c22e8fc140f8d5ea8 Mon Sep 17 00:00:00 2001 From: schillic Date: Wed, 19 Apr 2023 22:22:51 +0200 Subject: [PATCH 3/3] share duplicate code --- src/LazyOperations/ConvexHullArray.jl | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/src/LazyOperations/ConvexHullArray.jl b/src/LazyOperations/ConvexHullArray.jl index e27a68adf8..76d986ce30 100644 --- a/src/LazyOperations/ConvexHullArray.jl +++ b/src/LazyOperations/ConvexHullArray.jl @@ -110,18 +110,7 @@ A support vector in the given direction. """ function σ(d::AbstractVector, cha::ConvexHullArray) @assert !isempty(cha.array) "an empty convex hull is not allowed" - svec = d - N = eltype(d) - rmax = N(-Inf) - for chi in cha.array - si = σ(d, chi) - ri = dot(d, si) - if ri > rmax - rmax = ri - svec = si - end - end - return svec + return _σ_union(d, array(cha)) end """