Skip to content

Commit 6c40a39

Browse files
authored
Add tests for ordering (#146)
* Add tests for ordering * Update url
1 parent b8d6527 commit 6c40a39

File tree

5 files changed

+21
-6
lines changed

5 files changed

+21
-6
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The following types are defined:
1616

1717
All common algebraic operations between those types are designed to be as efficient as possible without doing any assumption on `T`.
1818
Typically, one imagine `T` to be a subtype of `Number` but it can be anything.
19-
This is useful for example in the package [PolyJuMP](https://github.com/JuliaOpt/PolyJuMP.jl) where `T` is often an affine expression of [JuMP](https://github.com/JuliaOpt/JuMP.jl) decision variables.
19+
This is useful for example in the package [PolyJuMP](https://github.com/jump-dev/PolyJuMP.jl) where `T` is often an affine expression of [JuMP](https://github.com/jump-dev/JuMP.jl) decision variables.
2020
The commutativity of `T` with `*` is not assumed, even if it is the coefficient of a monomial of commutative variables.
2121
However, commutativity of `T` and of the variables `+` is always assumed.
2222
This allows to keep the terms sorted (Graded Lexicographic order is used) in polynomial and measure which enables more efficient operations.

src/DynamicPolynomials.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ function MP.constant_monomial(p::PolyType)
3636
end
3737
MP.monomial_type(::Type{<:PolyType{V,M}}) where {V,M} = Monomial{V,M}
3838
MP.monomial_type(::PolyType{V,M}) where {V,M} = Monomial{V,M}
39+
MP.ordering(p::PolyType) = MP.ordering(MP.variable_union_type(p))
3940
#function MP.constant_monomial(::Type{Monomial{V,M}}, vars=Variable{V,M}[]) where {V,M}
4041
# return Monomial{V,M}(vars, zeros(Int, length(vars)))
4142
#end

src/monomial_vector.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ struct MonomialVector{V,M} <: AbstractVector{Monomial{V,M}}
1515
_isless = let M = M
1616
(a, b) -> MP.compare(a, b, M) < 0
1717
end
18-
@assert issorted(Z, lt = _isless)
1918
return new{V,M}(vars, Z)
2019
end
2120
end

src/var.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ function buildpolyvar(var, variable_order, monomial_order)
2626
varname,
2727
:(
2828
$(esc(varname)) = polyarrayvar(
29-
$variable_order,
30-
$monomial_order,
29+
$(variable_order),
30+
$(monomial_order),
3131
$prefix,
3232
$(esc.(var.args[2:end])...),
3333
)
@@ -53,9 +53,9 @@ function _extract_kw_args(args, variable_order)
5353
for arg in args
5454
if Base.Meta.isexpr(arg, :(=))
5555
if arg.args[1] == :variable_order
56-
variable_order = arg.args[2]
56+
variable_order = esc(arg.args[2])
5757
elseif arg.args[1] == :monomial_order
58-
monomial_order = arg.args[2]
58+
monomial_order = esc(arg.args[2])
5959
else
6060
error("Unrecognized keyword argument `$(arg.args[1])`")
6161
end
@@ -145,6 +145,7 @@ end
145145

146146
MP.monomial(v::Variable) = Monomial(v)
147147
MP.variables(v::Variable) = [v]
148+
MP.ordering(::Variable{V,M}) where {V,M} = M
148149

149150
iscomm(::Type{Variable{C}}) where {C} = C
150151

test/comp.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1+
using Test
2+
using DynamicPolynomials
3+
import DynamicPolynomials: Commutative, CreationOrder # to test hygiene
14
@testset "Variable order" begin
25
@polyvar x
36
z = x
47
@polyvar x
58
@test z != x
9+
order = Commutative{CreationOrder}
10+
@polyvar x variable_order = order
11+
@test z != x
612
end
713
@testset "README example" begin
814
function p(x, y, z)
@@ -19,3 +25,11 @@ end
1925
@polyvar x y z monomial_order = Graded{Reverse{InverseLexOrder}}
2026
@test p(x, y, z) == "4z² - 5x³ + 7x²z² + 4xy²z"
2127
end
28+
# See https://github.com/JuliaAlgebra/DynamicPolynomials.jl/issues/138
29+
# Also tests `ordering`
30+
@testset "InverseLexOrder" begin
31+
order = Graded{InverseLexOrder}
32+
@polyvar x[1:2] monomial_order = order
33+
@test ordering(x[1]) == order
34+
@test issorted(monomials(x[1], 0:2))
35+
end

0 commit comments

Comments
 (0)