Skip to content

Commit

Permalink
fix: force exact numeric sum - simplex projection
Browse files Browse the repository at this point in the history
Signed-off-by: hopeyen <[email protected]>
  • Loading branch information
hopeyen committed Apr 17, 2023
1 parent 0972935 commit 92c8f15
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/project.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ function σsimplex(x::AbstractVector{T}, σ::Real) where {T<:Real}
ρ = maximum((1:n)[μ - (cumsum(μ) .- σ) ./ (1:n) .> zero(T)])
θ = (sum(μ[1:ρ]) - σ) / ρ
w = max.(x .- θ, zero(T))
w[end] += σ - sum(w)
return w
end

Expand Down
4 changes: 4 additions & 0 deletions test/project.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
x = Float64[-1, 0, 0]
σ = 5
@test σsimplex(x, σ) [1, 2, 2] # Scale up

x = Float64[23133337391432116]
σ = 652174.7265297174
@test sum(σsimplex(x, σ)) σ # exact
end

@testset "gssp" begin
Expand Down

0 comments on commit 92c8f15

Please sign in to comment.