Skip to content

Commit b5923f9

Browse files
authored
Add Support for BitVector and BitMatrix Indexing (#708)
1 parent cc71b51 commit b5923f9

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/atoms/IndexAtom.jl

+8-2
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,17 @@ function Base.getindex(x::AbstractExpr, rows::AbstractVector{<:Real}, col::Real)
109109
return getindex(x, rows, col:col)
110110
end
111111

112-
function Base.getindex(x::AbstractExpr, I::AbstractMatrix{Bool})
112+
function Base.getindex(
113+
x::AbstractExpr,
114+
I::Union{AbstractMatrix{Bool},<:BitMatrix},
115+
)
113116
return [xi for (xi, ii) in zip(x, I) if ii]
114117
end
115118

116-
function Base.getindex(x::AbstractExpr, I::AbstractVector{Bool})
119+
function Base.getindex(
120+
x::AbstractExpr,
121+
I::Union{<:AbstractVector{Bool},<:BitVector},
122+
)
117123
return [xi for (xi, ii) in zip(x, I) if ii]
118124
end
119125

test/test_atoms.jl

+32-1
Original file line numberDiff line numberDiff line change
@@ -517,9 +517,40 @@ function test_IndexAtom()
517517
_test_atom(target) do context
518518
return Variable(2, 2)[:, 2]
519519
end
520+
# Base.getindex(x::AbstractExpr, I::AbstractVector{Bool})
520521
y = [true, false, true]
521522
x = Variable(3)
522-
@test string(x[y]) == string([x[1], x[3]])
523+
z = x[y]
524+
@test string(z) == string([x[1], x[3]])
525+
@test z isa Vector{Convex.IndexAtom}
526+
@test length(z) == 2
527+
Convex.set_value!(x, [1, 2, 3])
528+
@test Convex.evaluate.(z) == [1, 3]
529+
# Base.getindex(x::AbstractExpr, I::AbstractMatrix{Bool})
530+
y = [true false; true true]
531+
x = Variable(2, 2)
532+
z = x[y]
533+
@test z isa Vector{Convex.IndexAtom}
534+
@test length(z) == 3
535+
Convex.set_value!(x, [1 3; 2 4])
536+
@test Convex.evaluate.(z) == [1, 2, 4]
537+
# Base.getindex(x::AbstractExpr, I::BitVector)
538+
y = BitVector([true, false, true])
539+
x = Variable(3)
540+
z = x[y]
541+
@test string(z) == string([x[1], x[3]])
542+
@test z isa Vector{Convex.IndexAtom}
543+
@test length(z) == 2
544+
Convex.set_value!(x, [1, 2, 3])
545+
@test Convex.evaluate.(z) == [1, 3]
546+
# Base.getindex(x::AbstractExpr, I::BitMatrix)
547+
y = BitMatrix([true false; true true])
548+
x = Variable(2, 2)
549+
z = x[y]
550+
@test z isa Vector{Convex.IndexAtom}
551+
@test length(z) == 3
552+
Convex.set_value!(x, [1 3; 2 4])
553+
@test Convex.evaluate.(z) == [1, 2, 4]
523554
return
524555
end
525556

0 commit comments

Comments
 (0)