diff --git a/src/comparison.jl b/src/comparison.jl index aa6092f8..ad13aff2 100644 --- a/src/comparison.jl +++ b/src/comparison.jl @@ -513,6 +513,23 @@ end nvariables(it::ExponentsIterator) = length(it.object) +function Base.in(x, it::ExponentsIterator) + if length(x) != nvariables(it) + return false + end + if any(xi -> xi < 0, x) + return false + end + deg = sum(x) + if deg < it.mindegree + return false + end + if it.maxdegree !== nothing && deg > it.maxdegree + return false + end + return true +end + _last_lex_index(n, ::Type{LexOrder}) = n _prev_lex_index(i, ::Type{LexOrder}) = i - 1 _not_first_indices(n, ::Type{LexOrder}) = n:-1:2 diff --git a/test/comparison.jl b/test/comparison.jl index 401f7146..d1641202 100644 --- a/test/comparison.jl +++ b/test/comparison.jl @@ -26,6 +26,21 @@ function test_equal() @test exp != ExponentsIterator{LexOrder}([0], maxdegree = 2) end +function test_in() + # Unbounded iterator (no maxdegree) + it = ExponentsIterator{Graded{LexOrder}}([0, 0]) + @test [0, 0] in it + @test [1, 2] in it + @test [3] ∉ it # wrong number of variables + @test [0, 0, 0] ∉ it # wrong number of variables + # Bounded iterator + it2 = ExponentsIterator{LexOrder}([0, 0], mindegree = 1, maxdegree = 3) + @test [1, 0] in it2 + @test [1, 2] in it2 + @test [0, 0] ∉ it2 # degree too low + @test [2, 2] ∉ it2 # degree too high +end + function test_errors() err = ArgumentError( "The `mindegree` of `ExponentsIterator` cannot be negative.",