From 463b472a04885c10bfb709fbbcd17557b97306f1 Mon Sep 17 00:00:00 2001 From: Sasha Demin Date: Fri, 25 Oct 2024 15:15:09 +0200 Subject: [PATCH 1/3] in Groebner Ext, partially fix ordering --- Project.toml | 2 +- ext/SymbolicsGroebnerExt.jl | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index 6842dd311..21c089bc3 100644 --- a/Project.toml +++ b/Project.toml @@ -69,7 +69,7 @@ DocStringExtensions = "0.9" DomainSets = "0.6, 0.7" DynamicPolynomials = "0.5, 0.6" ForwardDiff = "0.10.36" -Groebner = "0.5, 0.6, 0.7" +Groebner = "0.8" IfElse = "0.1" LaTeXStrings = "1.3" LambertW = "1.0.0" diff --git a/ext/SymbolicsGroebnerExt.jl b/ext/SymbolicsGroebnerExt.jl index fa89330ee..f8edaa568 100644 --- a/ext/SymbolicsGroebnerExt.jl +++ b/ext/SymbolicsGroebnerExt.jl @@ -33,9 +33,16 @@ julia> @variables x y; julia> groebner_basis([x*y^2 + x, x^2*y + y]) ``` """ -function Symbolics.groebner_basis(polynomials::Vector{Num}; kwargs...) +function Symbolics.groebner_basis(polynomials::Vector{Num}; ordering=InputOrdering(), kwargs...) polynoms, pvar2sym, sym2term = Symbolics.symbol_to_poly(polynomials) - basis = Groebner.groebner(polynoms; kwargs...) + sym2term_for_groebner = Dict{Any,Any}(v1 => k for (k, (v1, v2)) in sym2term) + all_sym_vars = Groebner.ordering_variables(ordering) + missed = setdiff(all_sym_vars, Set(collect(keys(sym2term_for_groebner)))) + for k in missed + sym2term_for_groebner[k] = k + end + ordering = Groebner.ordering_transform(ordering, sym2term_for_groebner ) + basis = Groebner.groebner(polynoms; ordering=ordering, kwargs...) PolyType = symtype(first(polynomials)) Symbolics.poly_to_symbol(basis, pvar2sym, sym2term, PolyType) end From fed330e1576a16a9d7538b8bbe41d00f92d9338d Mon Sep 17 00:00:00 2001 From: Sasha Demin Date: Fri, 25 Oct 2024 17:07:26 +0200 Subject: [PATCH 2/3] add a test; run CI --- ext/SymbolicsGroebnerExt.jl | 4 ++-- test/extensions/groebner.jl | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/ext/SymbolicsGroebnerExt.jl b/ext/SymbolicsGroebnerExt.jl index f8edaa568..102cb67a8 100644 --- a/ext/SymbolicsGroebnerExt.jl +++ b/ext/SymbolicsGroebnerExt.jl @@ -38,8 +38,8 @@ function Symbolics.groebner_basis(polynomials::Vector{Num}; ordering=InputOrderi sym2term_for_groebner = Dict{Any,Any}(v1 => k for (k, (v1, v2)) in sym2term) all_sym_vars = Groebner.ordering_variables(ordering) missed = setdiff(all_sym_vars, Set(collect(keys(sym2term_for_groebner)))) - for k in missed - sym2term_for_groebner[k] = k + for var in missed + sym2term_for_groebner[var] = var end ordering = Groebner.ordering_transform(ordering, sym2term_for_groebner ) basis = Groebner.groebner(polynoms; ordering=ordering, kwargs...) diff --git a/test/extensions/groebner.jl b/test/extensions/groebner.jl index 93b9d02f9..505e42c95 100644 --- a/test/extensions/groebner.jl +++ b/test/extensions/groebner.jl @@ -72,5 +72,13 @@ truebasis = [ basis = groebner_basis(system) @test isequal(expand.(basis), truebasis) +# issues/1323 +@variables t S(t) R(t) +sys = [-12 + 5S, 2 + 5R] +@test isequal(groebner_basis(sys, ordering=Lex(S, R)), [2//5 + R, -12//5 + S]) +sys = [S^2 + 2*R^2 - 1, S*R - 1] +res = [(1//2) - (1//2)*(R^2) + R^4, S - R + (2//1)*(R^3)] +@test isequal(groebner_basis(sys, ordering=Lex(S, R), res) + # Groebner does not yet work with constant ideals @test_broken groebner_basis([1]) From 093b273dcc6f90ee6ef47efe122bf76ace7dfd72 Mon Sep 17 00:00:00 2001 From: Sasha Demin Date: Fri, 25 Oct 2024 17:18:00 +0200 Subject: [PATCH 3/3] CI passes, I think --- test/extensions/groebner.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/extensions/groebner.jl b/test/extensions/groebner.jl index 505e42c95..2b3a23358 100644 --- a/test/extensions/groebner.jl +++ b/test/extensions/groebner.jl @@ -75,10 +75,10 @@ basis = groebner_basis(system) # issues/1323 @variables t S(t) R(t) sys = [-12 + 5S, 2 + 5R] -@test isequal(groebner_basis(sys, ordering=Lex(S, R)), [2//5 + R, -12//5 + S]) +@test isequal(expand.(groebner_basis(sys, ordering=Lex(S, R))), [2//5 + R, -12//5 + S]) sys = [S^2 + 2*R^2 - 1, S*R - 1] res = [(1//2) - (1//2)*(R^2) + R^4, S - R + (2//1)*(R^3)] -@test isequal(groebner_basis(sys, ordering=Lex(S, R), res) +@test isequal(expand.(groebner_basis(sys, ordering=Lex(S, R))), res) # Groebner does not yet work with constant ideals @test_broken groebner_basis([1])