Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests for ordering #146

Merged
merged 2 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The following types are defined:

All common algebraic operations between those types are designed to be as efficient as possible without doing any assumption on `T`.
Typically, one imagine `T` to be a subtype of `Number` but it can be anything.
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.
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.
The commutativity of `T` with `*` is not assumed, even if it is the coefficient of a monomial of commutative variables.
However, commutativity of `T` and of the variables `+` is always assumed.
This allows to keep the terms sorted (Graded Lexicographic order is used) in polynomial and measure which enables more efficient operations.
Expand Down
1 change: 1 addition & 0 deletions src/DynamicPolynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function MP.constant_monomial(p::PolyType)
end
MP.monomial_type(::Type{<:PolyType{V,M}}) where {V,M} = Monomial{V,M}
MP.monomial_type(::PolyType{V,M}) where {V,M} = Monomial{V,M}
MP.ordering(p::PolyType) = MP.ordering(MP.variable_union_type(p))
#function MP.constant_monomial(::Type{Monomial{V,M}}, vars=Variable{V,M}[]) where {V,M}
# return Monomial{V,M}(vars, zeros(Int, length(vars)))
#end
Expand Down
1 change: 0 additions & 1 deletion src/monomial_vector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ struct MonomialVector{V,M} <: AbstractVector{Monomial{V,M}}
_isless = let M = M
(a, b) -> MP.compare(a, b, M) < 0
end
@assert issorted(Z, lt = _isless)
return new{V,M}(vars, Z)
end
end
Expand Down
9 changes: 5 additions & 4 deletions src/var.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ function buildpolyvar(var, variable_order, monomial_order)
varname,
:(
$(esc(varname)) = polyarrayvar(
$variable_order,
$monomial_order,
$(variable_order),
$(monomial_order),
$prefix,
$(esc.(var.args[2:end])...),
)
Expand All @@ -53,9 +53,9 @@ function _extract_kw_args(args, variable_order)
for arg in args
if Base.Meta.isexpr(arg, :(=))
if arg.args[1] == :variable_order
variable_order = arg.args[2]
variable_order = esc(arg.args[2])
elseif arg.args[1] == :monomial_order
monomial_order = arg.args[2]
monomial_order = esc(arg.args[2])
else
error("Unrecognized keyword argument `$(arg.args[1])`")
end
Expand Down Expand Up @@ -145,6 +145,7 @@ end

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

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

Expand Down
14 changes: 14 additions & 0 deletions test/comp.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
using Test
using DynamicPolynomials
import DynamicPolynomials: Commutative, CreationOrder # to test hygiene
@testset "Variable order" begin
@polyvar x
z = x
@polyvar x
@test z != x
order = Commutative{CreationOrder}
@polyvar x variable_order = order
@test z != x
end
@testset "README example" begin
function p(x, y, z)
Expand All @@ -19,3 +25,11 @@ end
@polyvar x y z monomial_order = Graded{Reverse{InverseLexOrder}}
@test p(x, y, z) == "4z² - 5x³ + 7x²z² + 4xy²z"
end
# See https://github.com/JuliaAlgebra/DynamicPolynomials.jl/issues/138
# Also tests `ordering`
@testset "InverseLexOrder" begin
order = Graded{InverseLexOrder}
@polyvar x[1:2] monomial_order = order
@test ordering(x[1]) == order
@test issorted(monomials(x[1], 0:2))
end
Loading