Skip to content
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
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
33 changes: 32 additions & 1 deletion test/test_atoms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -517,9 +517,40 @@ function test_IndexAtom()
_test_atom(target) do context
return Variable(2, 2)[:, 2]
end
# Base.getindex(x::AbstractExpr, I::AbstractVector{Bool})
y = [true, false, true]
x = Variable(3)
@test string(x[y]) == string([x[1], x[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]
# Base.getindex(x::AbstractExpr, I::AbstractMatrix{Bool})
y = [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]
# 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]
# 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]
return
end

Expand Down
Loading