Skip to content

Commit 1d73969

Browse files
committed
Fix tests
1 parent 5486184 commit 1d73969

File tree

3 files changed

+62
-52
lines changed

3 files changed

+62
-52
lines changed

src/comparison.jl

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -200,26 +200,7 @@ Springer Science & Business Media, **2013**.
200200
"""
201201
struct LexOrder <: AbstractMonomialOrdering end
202202

203-
function compare(
204-
exp1::AbstractVector{T},
205-
exp2::AbstractVector{T},
206-
::Type{LexOrder},
207-
) where {T}
208-
if eachindex(exp1) != eachindex(exp2)
209-
throw(
210-
ArgumentError(
211-
"Cannot compare exponent vectors `$exp1` and `$exp2` of different indices.",
212-
),
213-
)
214-
end
215-
@inbounds for i in eachindex(exp1)
216-
Δ = exp1[i] - exp2[i]
217-
if !iszero(Δ)
218-
return Δ
219-
end
220-
end
221-
return zero(T)
222-
end
203+
compare(exp1, exp2, ::Type{LexOrder}) = Base.cmp(exp1, exp2)
223204

224205
"""
225206
struct InverseLexOrder <: AbstractMonomialOrdering end
@@ -239,25 +220,12 @@ SIAM Journal on Matrix Analysis and Applications, 34(1), 102-125, *2013*.
239220
"""
240221
struct InverseLexOrder <: AbstractMonomialOrdering end
241222

242-
function compare(
243-
exp1::AbstractVector{T},
244-
exp2::AbstractVector{T},
245-
::Type{InverseLexOrder},
246-
) where {T}
247-
if eachindex(exp1) != eachindex(exp2)
248-
throw(
249-
ArgumentError(
250-
"Cannot compare exponent vectors `$exp1` and `$exp2` of different indices.",
251-
),
252-
)
253-
end
254-
@inbounds for i in Iterators.Reverse(eachindex(exp1))
255-
Δ = exp1[i] - exp2[i]
256-
if !iszero(Δ)
257-
return Δ
258-
end
259-
end
260-
return zero(T)
223+
# We can't use `Iterators.Reverse` because it's not an `AbstractVector`
224+
# so not `cmp` methods is defined for it.
225+
_rev(v::AbstractArray) = view(v, lastindex(v):-1:firstindex(v))
226+
_rev(t::Tuple) = reverse(t)
227+
function compare(exp1, exp2, ::Type{InverseLexOrder})
228+
return compare(_rev(exp1), _rev(exp2), LexOrder)
261229
end
262230

263231
"""
@@ -393,6 +361,11 @@ function ExponentsIterator{M}(
393361
maxdegree::Union{Nothing,Int} = nothing,
394362
inline::Bool = false,
395363
) where {M}
364+
if length(object) == 0 && isnothing(maxdegree)
365+
# Otherwise, it will incorrectly think that the iterator is infinite
366+
# while it actually has zero elements
367+
maxdegree = mindegree
368+
end
396369
return ExponentsIterator{M,typeof(maxdegree),typeof(object)}(
397370
object,
398371
mindegree,
@@ -416,6 +389,9 @@ end
416389
function Base.length(it::ExponentsIterator{M,Int}) where {M}
417390
len = binomial(nvariables(it) + it.maxdegree, nvariables(it))
418391
if it.mindegree > 0
392+
if it.maxdegree < it.mindegree
393+
return 0
394+
end
419395
len -= binomial(nvariables(it) + it.mindegree - 1, nvariables(it))
420396
end
421397
return len
@@ -479,15 +455,20 @@ function _iterate!(it::ExponentsIterator{M}, z, deg) where {M}
479455
end
480456

481457
function Base.iterate(it::ExponentsIterator{M}) where {M}
482-
if nvariables(it) == 0
483-
return
484-
end
485458
z = _zero(it.object)
486-
z = _setindex!(z, it.mindegree, _last_lex_index(nvariables(it), M))
459+
if it.mindegree > 0
460+
if nvariables(it) == 0 || (!isnothing(it.maxdegree) && it.maxdegree < it.mindegree)
461+
return
462+
end
463+
z = _setindex!(z, it.mindegree, _last_lex_index(nvariables(it), M))
464+
end
487465
return z, (z, it.mindegree)
488466
end
489467

490468
function Base.iterate(it::ExponentsIterator, state)
469+
if nvariables(it) == 0
470+
return # There cannot be more than 1 element
471+
end
491472
z, deg = state
492473
if !it.inline
493474
z = _copy(z)

test/comparison.jl

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
1-
module TestExponentsIterator
1+
module TestComparison
22

33
using Test
44
using MultivariatePolynomials
55

66
function _test(object, M; kws...)
77
it = ExponentsIterator{M}(object; kws...)
88
v = collect(Iterators.take(it, 20))
9-
@test issorted(v, lt = (a, b) -> compare(a, b, M))
9+
@test issorted(v, lt = (a, b) -> compare(a, b, M) < 0)
1010
end
1111

1212
function _test(nvars::Int, M; kws...)
13-
_test(zeros(Int, nvars); inline = false, kws...)
14-
_test(zeros(Int, nvars); inline = true, kws...)
15-
_test(ntuple(zero, nvars); inline = false, kws...)
13+
_test(zeros(Int, nvars), M; inline = false, kws...)
14+
_test(zeros(Int, nvars), M; inline = true, kws...)
15+
_test(ntuple(zero, nvars), M; inline = false, kws...)
1616
return
1717
end
1818

19-
function test_all()
19+
function test_exponents_iterator()
2020
@testset "nvariables = $nvars" for nvars in 0:3
2121
@testset "mindegree = $mindegree" for mindegree in 0:3
22-
@testset "maxdegree = $maxdegree" for maxdegree in
23-
vcat(nothing; 0:3)
22+
@testset "maxdegree = $maxdegree" for maxdegree in vcat(nothing, 0:3)
2423
for L in [LexOrder, InverseLexOrder]
2524
@testset "M = $M" for M in [L, Graded{L}]
2625
_test(nvars, M; mindegree, maxdegree)
@@ -31,6 +30,33 @@ function test_all()
3130
end
3231
end
3332

33+
function test_compare()
34+
lex = LexOrder
35+
grlex = Graded{lex}
36+
rinvlex = Reverse{InverseLexOrder}
37+
grevlex = Graded{rinvlex}
38+
@test compare([1, 0, 1], [1, 1, 0], grlex) == -1
39+
@test compare([1, 1, 0], [1, 0, 1], grlex) == 1
40+
# [CLO13, p. 58]
41+
@test compare(1:3, [3, 2, 0], lex) < 0
42+
@test compare(1:3, [3, 2, 0], grlex) > 0
43+
@test compare(1:3, [3, 2, 0], rinvlex) < 0
44+
@test compare(1:3, [3, 2, 0], grevlex) > 0
45+
@test compare([1, 2, 4], [1, 1, 5], lex) > 0
46+
@test compare([1, 2, 4], [1, 1, 5], grlex) > 0
47+
@test compare([1, 2, 4], [1, 1, 5], rinvlex) > 0
48+
@test compare([1, 2, 4], [1, 1, 5], grevlex) > 0
49+
# [CLO13, p. 59]
50+
@test compare((5, 1, 1), (4, 1, 2), lex) > 0
51+
@test compare((5, 1, 1), (4, 1, 2), grlex) > 0
52+
@test compare((5, 1, 1), (4, 1, 2), rinvlex) > 0
53+
@test compare((5, 1, 1), (4, 1, 2), grevlex) > 0
54+
# [CLO13] Cox, D., Little, J., & OShea, D.
55+
# *Ideals, varieties, and algorithms: an introduction to computational algebraic geometry and commutative algebra*.
56+
# Springer Science & Business Media, **2013**.
57+
58+
end
59+
3460
function runtests()
3561
for name in names(@__MODULE__; all = true)
3662
if startswith("$(name)", "test_")
@@ -44,4 +70,4 @@ end
4470

4571
end # module
4672

47-
TestExponentsIterator.runtests()
73+
TestComparison.runtests()

test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ using LinearAlgebra
44
using MultivariatePolynomials
55
const MP = MultivariatePolynomials
66

7+
# Tests that do not need any specific polynomial implementation
8+
include("exponents_iterator.jl")
9+
710
include("utils.jl")
811

912
# Taken from JuMP/test/solvers.jl

0 commit comments

Comments
 (0)