Skip to content

Commit

Permalink
Merge pull request #121 from sumiya11/fix-dynamic-polys
Browse files Browse the repository at this point in the history
Fix for DynamicPolynomials.jl orderings
  • Loading branch information
sumiya11 authored Feb 15, 2024
2 parents 0cb3174 + 84bf0dd commit df183ee
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Groebner"
uuid = "0b43b601-686d-58a3-8a1c-6623616c7cd4"
authors = ["sumiya11"]
version = "0.7.0"
version = "0.7.1"

[deps]
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"
Expand All @@ -26,7 +26,7 @@ BenchmarkTools = "1"
Combinatorics = "1"
ExprTools = "0.1"
HostCPUFeatures = "0.1"
MultivariatePolynomials = "0.4, 0.5"
MultivariatePolynomials = "0.5"
Nemo = "0.38.3, 0.39, 0.40, 0.41, 0.42"
PrecompileTools = "1"
PrettyTables = "2"
Expand Down
54 changes: 46 additions & 8 deletions src/input-output/DynamicPolynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,52 @@
###
# Conversion from DynamicPolynomials.jl to internal representation and back.

const _DP_supported_orderings_symbols = (:lex, :deglex, :degrevlex)

function dp_ordering_sym2typed(ord::Symbol)
if !(ord in _DP_supported_orderings_symbols)
__throw_input_not_supported(ord, "Not a supported ordering.")
end
if ord === :lex
Lex()
elseif ord === :deglex
DegLex()
elseif ord === :degrevlex
DegRevLex()
end
end

function dp_ord_to_symbol(ord)
if ord === MultivariatePolynomials.LexOrder
:lex
elseif ord === MultivariatePolynomials.Graded{MultivariatePolynomials.LexOrder}
:deglex
elseif ord === MultivariatePolynomials.Graded{
MultivariatePolynomials.Reverse{MultivariatePolynomials.InverseLexOrder}
}
:degrevlex
else
__throw_input_not_supported(
"""This ordering from DynamicPolynomials.jl is not supported by Groebner.jl.
Consider opening a Github issue if you need it.""",
ord
)
end
end

function peek_at_polynomials(polynomials::Vector{<:AbstractPolynomialLike{T}}) where {T}
@assert !isempty(polynomials) "Input must not be empty"
nv = Groebner.MultivariatePolynomials.nvariables(polynomials)
:dynamicpolynomials, length(polynomials), UInt(0), nv, :deglex
nv = MultivariatePolynomials.nvariables(polynomials)
ord = dp_ord_to_symbol(MultivariatePolynomials.ordering(polynomials[1]))
@assert length(unique(MultivariatePolynomials.ordering, polynomials)) == 1
:dynamicpolynomials, length(polynomials), UInt(0), nv, ord
end

function extract_ring(orig_polys::Vector{<:AbstractPolynomialLike{T}}) where {T}
nv = Groebner.MultivariatePolynomials.nvariables(orig_polys)
ord = DegLex()
PolyRing{typeof(ord), UInt}(nv, ord, UInt(0))
nv = MultivariatePolynomials.nvariables(orig_polys)
ord = dp_ord_to_symbol(MultivariatePolynomials.ordering(orig_polys[1]))
ord_typed = dp_ordering_sym2typed(ord)
PolyRing{typeof(ord_typed), UInt}(nv, ord_typed, UInt(0))
end

function _io_check_input(polynomials::Vector{<:AbstractPolynomialLike{T}}, kws) where {T}
Expand Down Expand Up @@ -47,7 +83,7 @@ end

function exponents_wrt_vars(t, var2idx)
exp = zeros(Int, length(var2idx))
@inbounds for (v, p) in Groebner.MultivariatePolynomials.powers(t)
@inbounds for (v, p) in MultivariatePolynomials.powers(t)
exp[var2idx[v]] = p
end
exp
Expand Down Expand Up @@ -124,10 +160,12 @@ function _io_convert_to_output(
gbcoeffs::Vector{Vector{I}},
params::AlgorithmParameters
) where {M <: Monom, P <: AbstractPolynomialLike{J}, I <: Coeff} where {J}
if params.target_ord != DegLex()
ord_dp = MultivariatePolynomials.ordering(origpolys[1])
_ord_dp = dp_ordering_sym2typed(dp_ord_to_symbol(ord_dp))
if params.target_ord != _ord_dp
@log level = -1 """
Basis is computed in $(params.target_ord).
Terms in the output are in $(DegLex())"""
Terms in the output are in $(_ord_dp)"""
end

origvars = MultivariatePolynomials.variables(origpolys)
Expand Down
40 changes: 40 additions & 0 deletions test/input-output/DynamicPolynomials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,44 @@ using DynamicPolynomials
gb = Groebner.groebner(fs, ordering=gb_ord)
@test gb == result
end

# Test for different DynamicPolynomials.jl orderings
@polyvar x y monomial_order = LexOrder
F = [2x^2 * y + 3x, 4x * y^2 + 5y^3]
gb = Groebner.groebner(F, ordering=Groebner.DegLex())
@test gb == [5 * x * y + 4 * x^2, 25 * y^3 + 24 * x, -6 * x + 5 * x * y^2]
gb = Groebner.groebner(F, ordering=Groebner.DegRevLex())
@test gb == [5 * x * y + 4 * x^2, 25 * y^3 + 24 * x, -6 * x + 5 * x * y^2]
gb = Groebner.groebner(F, ordering=Groebner.Lex())
@test gb == [-6 * y^3 + 5 * y^5, 25 * y^3 + 24 * x]
gb = Groebner.groebner(F, ordering=Groebner.Lex())
@test gb == [-6 * y^3 + 5 * y^5, 25 * y^3 + 24 * x]
gb = Groebner.groebner(F, ordering=Groebner.Lex(y, x))
@test gb == [-15 * x + 8 * x^3, 5 * x * y + 4 * x^2, 24 * x + 25 * y^3]

@polyvar x y monomial_order = Graded{Reverse{InverseLexOrder}}
F = [2x^2 * y + 3x, 4x * y^2 + 5y^3]
gb = Groebner.groebner(F)
@test gb == [5 * x * y + 4 * x^2, 25 * y^3 + 24 * x, -6 * x + 5 * x * y^2]
gb = Groebner.groebner(F, ordering=Groebner.Lex())
@test gb == [-6 * y^3 + 5 * y^5, 25 * y^3 + 24 * x]

for dp_ord in [LexOrder, Graded{LexOrder}, Graded{Reverse{InverseLexOrder}}]
@polyvar x y monomial_order = dp_ord
F = [2x^2 * y + 3x, 4x * y^2 + 5y^3]
for gb_ord in [
Groebner.Lex(),
Groebner.DegLex(),
Groebner.DegRevLex(),
Groebner.Lex(x, y),
Groebner.Lex(y, x)
]
gb = Groebner.groebner(F, ordering=gb_ord)
@test Groebner.isgroebner(gb, ordering=gb_ord)
end
end

@polyvar x y monomial_order = InverseLexOrder
F = [2x^2 * y + 3x, 4x * y^2 + 5y^3]
@test_throws DomainError Groebner.groebner(F)
end

2 comments on commit df183ee

@sumiya11
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/100939

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.7.1 -m "<description of version>" df183eed4f23c5ec2349a9e5f1a77c2630d44f7c
git push origin v0.7.1

Please sign in to comment.