Skip to content

Commit 49e248e

Browse files
authored
Fix indexing with vector of boolean (#112)
1 parent 2ca5cd5 commit 49e248e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/monovec.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ end
3939
# `mutable_copy` recursively copies the vector or vector of integers.
4040
MA.mutable_copy(m::MV) where {MV<:MonomialVector} = MV(copy(m.vars), MA.mutable_copy(m.Z))
4141
Base.copy(m::MonomialVector) = MA.mutable_copy(m)
42-
function Base.getindex(x::MonomialVector, I)
42+
function Base.getindex(x::MonomialVector, I::AbstractVector{Bool})
43+
return typeof(x)(x.vars, x.Z[I])
44+
end
45+
function Base.getindex(x::MonomialVector, I::AbstractVector{<:Integer})
4346
return typeof(x)(x.vars, x.Z[sort(I)])
4447
end
4548
Base.getindex(x::MonomialVector, i::Integer) = Monomial(x.vars, x.Z[i])

test/mono.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,4 +142,13 @@ using Test
142142
@test y == DynamicPolynomials.MP.mapexponents_to!(z, -, x * y^2, x * y)
143143
end
144144
end
145+
# TODO add to MP
146+
@testset "Indexing with boolean" begin
147+
@polyvar x y
148+
X = monomials([x, y], 2)
149+
@test X[[true, false, true]] == monovec([x^2, y^2])
150+
X = monomials([x, y], 0:1)
151+
@test filter(mono -> degree(mono) == 1, X) == monovec([x, y])
152+
@test filter(mono -> degree(mono) == 0, X) == monovec([x^0])
153+
end
145154
end

0 commit comments

Comments
 (0)