Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for BitVector and BitMatrix Indexing #708

Merged
merged 6 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/atoms/IndexAtom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,17 @@ function Base.getindex(x::AbstractExpr, rows::AbstractVector{<:Real}, col::Real)
return getindex(x, rows, col:col)
end

function Base.getindex(x::AbstractExpr, I::AbstractMatrix{Bool})
function Base.getindex(
x::AbstractExpr,
I::Union{AbstractMatrix{Bool},<:BitMatrix},
)
return [xi for (xi, ii) in zip(x, I) if ii]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. So these methods have been there forever:
92c93fd
but they don't really with the vibe of Convex because they return vector/matrices instead of a new atom.

end

function Base.getindex(x::AbstractExpr, I::AbstractVector{Bool})
function Base.getindex(
x::AbstractExpr,
I::Union{<:AbstractVector{Bool},<:BitVector},
)
return [xi for (xi, ii) in zip(x, I) if ii]
end

Expand Down
9 changes: 9 additions & 0 deletions test/test_atoms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,15 @@ function test_IndexAtom()
y = [true, false, true]
x = Variable(3)
@test string(x[y]) == string([x[1], x[3]])
target = """
variables: x1, x2, x3
minobjective: [1.0 * x1, 1.0 * x3]
"""
_test_atom(target) do context
x = Variable(3)
y = BitVector([true, false, true])
return x[y]
end
return
end

Expand Down
Loading