From ab44000ac6ba89364b204ec3a9cf8b9063b92ce7 Mon Sep 17 00:00:00 2001 From: Royi Date: Sun, 27 Oct 2024 09:21:13 +0200 Subject: [PATCH 1/6] Add Support for `BitVector` and `BitMatrix` Indexing Fixes #707 . --- src/atoms/IndexAtom.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/atoms/IndexAtom.jl b/src/atoms/IndexAtom.jl index 9196c7a93..225189446 100644 --- a/src/atoms/IndexAtom.jl +++ b/src/atoms/IndexAtom.jl @@ -117,6 +117,14 @@ 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, I::BitMatrix) + return [xi for (xi, ii) in zip(x, I) if ii] +end + +function Base.getindex(x::AbstractExpr, I::BitVector) + return [xi for (xi, ii) in zip(x, I) if ii] +end + # All rows and columns Base.getindex(x::AbstractExpr, ::Colon, ::Colon) = x From 0d5b3aa66258438a7343b50744a9a7104f83f7c3 Mon Sep 17 00:00:00 2001 From: Royi Date: Sun, 27 Oct 2024 09:30:19 +0200 Subject: [PATCH 2/6] Add Support for `BitVector` and `BitMatrix` Indexing Fixes #707. --- test/test_atoms.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/test_atoms.jl b/test/test_atoms.jl index 2fa320be3..1b5e910e2 100644 --- a/test/test_atoms.jl +++ b/test/test_atoms.jl @@ -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 From e84d666050baa2883e10a8e400416723cb67bbce Mon Sep 17 00:00:00 2001 From: Royi Date: Sun, 27 Oct 2024 14:32:11 +0200 Subject: [PATCH 3/6] Add Support for `BitVector` and `BitMatrix` Indexing Fixes #707. --- src/atoms/IndexAtom.jl | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/atoms/IndexAtom.jl b/src/atoms/IndexAtom.jl index 225189446..b8d8743c8 100644 --- a/src/atoms/IndexAtom.jl +++ b/src/atoms/IndexAtom.jl @@ -109,19 +109,11 @@ 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] end -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, I::BitMatrix) - return [xi for (xi, ii) in zip(x, I) if ii] -end - -function Base.getindex(x::AbstractExpr, I::BitVector) +function Base.getindex(x::AbstractExpr, I::Union{<:AbstractVector{Bool}, <:BitVector}) return [xi for (xi, ii) in zip(x, I) if ii] end From 3ade074f195f53c9a4c282bcb7411f6008943c8a Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Mon, 28 Oct 2024 09:06:13 +1300 Subject: [PATCH 4/6] Update IndexAtom.jl --- src/atoms/IndexAtom.jl | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/atoms/IndexAtom.jl b/src/atoms/IndexAtom.jl index b8d8743c8..49162e000 100644 --- a/src/atoms/IndexAtom.jl +++ b/src/atoms/IndexAtom.jl @@ -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::Union{AbstractMatrix{Bool}, <:BitMatrix}) +function Base.getindex( + x::AbstractExpr, + I::Union{AbstractMatrix{Bool},<:BitMatrix}, +) 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::Union{<:AbstractVector{Bool},<:BitVector}, +) return [xi for (xi, ii) in zip(x, I) if ii] end From 52fc1f93a286b205e34019782dbfb309ea0175f5 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Mon, 28 Oct 2024 09:06:48 +1300 Subject: [PATCH 5/6] Update test_atoms.jl --- test/test_atoms.jl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/test_atoms.jl b/test/test_atoms.jl index 1b5e910e2..6c218b8ef 100644 --- a/test/test_atoms.jl +++ b/test/test_atoms.jl @@ -521,14 +521,14 @@ function test_IndexAtom() 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 + 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 From 3af9f284657acca387709a901f37b2f9de9980d6 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Mon, 28 Oct 2024 09:44:43 +1300 Subject: [PATCH 6/6] Update test_atoms.jl --- test/test_atoms.jl | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/test/test_atoms.jl b/test/test_atoms.jl index 6c218b8ef..6e38b06a0 100644 --- a/test/test_atoms.jl +++ b/test/test_atoms.jl @@ -517,18 +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]]) - 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 + 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