From d185eb8404248dc0820baccb1803cfcea0bbb84c Mon Sep 17 00:00:00 2001 From: Alexander Demin Date: Wed, 14 Feb 2024 19:10:40 +0300 Subject: [PATCH 1/4] fix for DP.jl orderings --- Project.toml | 2 +- src/input-output/DynamicPolynomials.jl | 54 +++++++++++++++++++++---- test/input-output/DynamicPolynomials.jl | 25 ++++++++++++ 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/Project.toml b/Project.toml index fc7c4c80..7008b09b 100644 --- a/Project.toml +++ b/Project.toml @@ -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" PrecompileTools = "1" PrettyTables = "2" diff --git a/src/input-output/DynamicPolynomials.jl b/src/input-output/DynamicPolynomials.jl index 88c4c08e..016295f2 100644 --- a/src/input-output/DynamicPolynomials.jl +++ b/src/input-output/DynamicPolynomials.jl @@ -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} @@ -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 @@ -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) diff --git a/test/input-output/DynamicPolynomials.jl b/test/input-output/DynamicPolynomials.jl index bfc4159b..b13484ee 100644 --- a/test/input-output/DynamicPolynomials.jl +++ b/test/input-output/DynamicPolynomials.jl @@ -100,4 +100,29 @@ 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 + gb = Groebner.groebner([x^2 * y + x, x * y^2 + y^3], ordering=Groebner.DegLex()) + @test gb == [x * y + x^2, y^3 + x, -x + x * y^2] + gb = Groebner.groebner([x^2 * y + x, x * y^2 + y^3], ordering=Groebner.Lex()) + @test gb == [-y^3 + y^5, y^3 + x] + + @polyvar x y monomial_order = Graded{Reverse{InverseLexOrder}} + gb = Groebner.groebner([x^2 * y + x, x * y^2 + y^3]) + @test gb == [x * y + x^2, y^3 + x, -x + x * y^2] + gb = Groebner.groebner([x^2 * y + x, x * y^2 + y^3], ordering=Groebner.Lex()) + @test gb == [-y^3 + y^5, y^3 + x] + + for dp_ord in [LexOrder, Graded{LexOrder}, Graded{Reverse{InverseLexOrder}}] + @polyvar x y monomial_order = dp_ord + for gb_ord in + [Groebner.Lex(), Groebner.DegLex(), Groebner.DegRevLex(), Groebner.Lex(x, y)] + gb = Groebner.groebner([2x^2 * y + 3x, 4x * y^2 + 5y^3], ordering=gb_ord) + @test Groebner.isgroebner(gb, ordering=gb_ord) + end + end + + @polyvar x y monomial_order = InverseLexOrder + @test_throws DomainError Groebner.groebner([2x^2 * y + 3x, 4x * y^2 + 5y^3]) end From 75a1e961bc1a9cd7c5f2d434e5c42605e9194ee3 Mon Sep 17 00:00:00 2001 From: Alexander Demin Date: Wed, 14 Feb 2024 21:12:31 +0300 Subject: [PATCH 2/4] Bump nemo --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 7008b09b..687b3b63 100644 --- a/Project.toml +++ b/Project.toml @@ -27,7 +27,7 @@ Combinatorics = "1" ExprTools = "0.1" HostCPUFeatures = "0.1" MultivariatePolynomials = "0.5" -Nemo = "0.38.3, 0.39, 0.40, 0.41" +Nemo = "0.38.3, 0.39, 0.40, 0.41, 0.42" PrecompileTools = "1" PrettyTables = "2" Primes = "0.5" From 18cbe722219d78231ae8b810588e6f6e1b1a3cfa Mon Sep 17 00:00:00 2001 From: Alexander Demin Date: Thu, 15 Feb 2024 15:53:38 +0300 Subject: [PATCH 3/4] seems to work --- test/input-output/DynamicPolynomials.jl | 39 +++++++++++++++++-------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/test/input-output/DynamicPolynomials.jl b/test/input-output/DynamicPolynomials.jl index b13484ee..ba3b9cae 100644 --- a/test/input-output/DynamicPolynomials.jl +++ b/test/input-output/DynamicPolynomials.jl @@ -103,26 +103,41 @@ using DynamicPolynomials # Test for different DynamicPolynomials.jl orderings @polyvar x y monomial_order = LexOrder - gb = Groebner.groebner([x^2 * y + x, x * y^2 + y^3], ordering=Groebner.DegLex()) - @test gb == [x * y + x^2, y^3 + x, -x + x * y^2] - gb = Groebner.groebner([x^2 * y + x, x * y^2 + y^3], ordering=Groebner.Lex()) - @test gb == [-y^3 + y^5, y^3 + x] + 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}} - gb = Groebner.groebner([x^2 * y + x, x * y^2 + y^3]) - @test gb == [x * y + x^2, y^3 + x, -x + x * y^2] - gb = Groebner.groebner([x^2 * y + x, x * y^2 + y^3], ordering=Groebner.Lex()) - @test gb == [-y^3 + y^5, y^3 + x] + 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 - for gb_ord in - [Groebner.Lex(), Groebner.DegLex(), Groebner.DegRevLex(), Groebner.Lex(x, y)] - gb = Groebner.groebner([2x^2 * y + 3x, 4x * y^2 + 5y^3], ordering=gb_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 - @test_throws DomainError Groebner.groebner([2x^2 * y + 3x, 4x * y^2 + 5y^3]) + F = [2x^2 * y + 3x, 4x * y^2 + 5y^3] + @test_throws DomainError Groebner.groebner(F) end From 366d47f7b7c73f27f789c410c1a246479a73268f Mon Sep 17 00:00:00 2001 From: Alexander Demin Date: Thu, 15 Feb 2024 15:54:07 +0300 Subject: [PATCH 4/4] bump --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index 687b3b63..2abbff31 100644 --- a/Project.toml +++ b/Project.toml @@ -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"