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

Fix indexing AbstractExor with BitVector and BitMatrix #710

Merged
merged 3 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 10 additions & 8 deletions src/atoms/IndexAtom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,20 +109,22 @@ function Base.getindex(x::AbstractExpr, rows::AbstractVector{<:Real}, col::Real)
return getindex(x, rows, col:col)
end

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

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

function Base.getindex(x::AbstractExpr, ind::BitVector)
return getindex(x, findall(ind))
end

function Base.getindex(x::AbstractExpr, ind::BitMatrix)
return getindex(x, LinearIndices(ind)[ind])
end

# All rows and columns
Base.getindex(x::AbstractExpr, ::Colon, ::Colon) = x

Expand Down
6 changes: 5 additions & 1 deletion src/atoms/VcatAtom.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ function Base.getindex(x::VcatAtom, inds::AbstractVector{<:Real})
return IndexAtom(remaining, inds)
end

function Base.getindex(x::VcatAtom, inds::BitVector)
return getindex(x, findall(inds))
end

function Base.getindex(x::VcatAtom, inds::AbstractVector{Bool})
return getindex(x, first.(filter!(last, collect(enumerate(inds)))))
return getindex(x, convert(BitVector, inds))
end
33 changes: 18 additions & 15 deletions test/test_atoms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -535,22 +535,25 @@ function test_IndexAtom()
Convex.set_value!(x, [1 3; 2 4])
@test Convex.evaluate.(z) == [1, 2, 4]
# Base.getindex(x::AbstractExpr, I::BitVector)
y = BitVector([true, false, true])
x = Variable(3)
z = x[y]
@test string(z) == string([x[1], x[3]])
@test z isa Vector{Convex.IndexAtom}
@test length(z) == 2
Convex.set_value!(x, [1, 2, 3])
@test Convex.evaluate.(z) == [1, 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
# Base.getindex(x::AbstractExpr, I::BitMatrix)
y = BitMatrix([true false; true true])
x = Variable(2, 2)
z = x[y]
@test z isa Vector{Convex.IndexAtom}
@test length(z) == 3
Convex.set_value!(x, [1 3; 2 4])
@test Convex.evaluate.(z) == [1, 2, 4]
target = """
variables: x1, x2, x3, x4
minobjective: [1.0 * x1, 1.0 * x2, 1.0 * x4]
"""
_test_atom(target) do context
x = Variable(2, 2)
y = BitMatrix([true false; true true])
return x[y]
end
return
end

Expand Down
Loading