Skip to content

Commit

Permalink
Simpler side_dimension_for_vectorized_dimension
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Dec 10, 2023
1 parent 55d96d8 commit c96d5ad
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
12 changes: 5 additions & 7 deletions src/Utilities/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,19 +128,17 @@ function is_diagonal_vectorized_index(index::Base.Integer)
return isqrt(perfect_square)^2 == perfect_square
end

# We have `d*(d+1)/2 = n` so `2n = d^2 + d` hence
# `d^2 ≤ 2n < d^2 + 2d + 1 = (d + 1)^2`
# this means that `d` is the largest natural number `d`
# such that `d^2 ≤ 2n` hence `d = isqrt(2n)`.
"""
side_dimension_for_vectorized_dimension(n::Integer)
Return the dimension `d` such that
`MOI.dimension(MOI.PositiveSemidefiniteConeTriangle(d))` is `n`.
"""
function side_dimension_for_vectorized_dimension(n::Base.Integer)
# We have `d*(d+1)/2 = n` so
# `d² + d - 2n = 0` hence `d = (-1 ± √(1 + 8n)) / 2`
# The integer `√(1 + 8n)` is odd and `√(1 + 8n) - 1` is even.
# We can drop the `- 1` as `div` already discards it.
return div(isqrt(1 + 8n), 2)
end
side_dimension_for_vectorized_dimension(n::Base.Integer) = isqrt(2n)

"""
trimap(row::Integer, column::Integer)
Expand Down
9 changes: 9 additions & 0 deletions test/Utilities/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ function test_diagonal_element()
vec_dim = MOI.dimension(set)
@test MOIU.side_dimension_for_vectorized_dimension(vec_dim) == side_dim
end
# There is an alternative way to compute it so let's check that
# they match.
# We have `d*(d+1)/2 = n` so
# `d² + d - 2n = 0` hence `d = (-1 ± √(1 + 8n)) / 2`
# The integer `√(1 + 8n)` is odd and `√(1 + 8n) - 1` is even.
# We can drop the `- 1` as `div` already discards it.
for n in 1:100
@test div(isqrt(1 + 8n), 2) == MOIU.side_dimension_for_vectorized_dimension(vec_dim)
end
return
end

Expand Down

0 comments on commit c96d5ad

Please sign in to comment.