Skip to content

Commit

Permalink
Merge pull request #165 from sumiya11/patch-ordering
Browse files Browse the repository at this point in the history
Allow the ordering to contain more variables than what is present in the input polynomials
  • Loading branch information
sumiya11 authored Nov 23, 2024
2 parents ff30fd1 + 58ec3bc commit 26668d4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
24 changes: 17 additions & 7 deletions src/monomials/orderings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -328,24 +328,32 @@ ordering_make_not_simple(ord::DegRevLex{true}, n::Int) = DegRevLex(collect(1:n))
# Checking consistency against a polynomial ring
function ordering_check_consistency(nvars::Int, ord::AbstractMonomialOrdering)
isempty(ordering_variables(ord)) && return nothing
if nvars != length(ordering_variables(ord))
throw(DomainError("The monomial ordering is invalid."))
end
if !issubset(ordering_variables(ord), collect(1:nvars))
throw(DomainError("The monomial ordering is invalid."))
throw(
DomainError(
"Monomial ordering is invalid. The ring has variables $(collect(1:nvars)), the ordering has variables $(ordering_variables(ord)), which are incompatible."
)
)
end
nothing
end

# Transform orderings

function map_variables(vars, varmap)
isempty(vars) && return vars
if !(Set(vars) == Set(collect(keys(varmap))))
# Fallback to string representation
varmap_str = Dict(string(k) => v for (k, v) in varmap)
vars_str = map(string, vars)
!isempty(setdiff(vars_str, collect(keys(varmap_str)))) &&
throw(DomainError("Invalid monomial ordering."))
if !isempty(setdiff(collect(keys(varmap_str)), vars_str))
throw(
DomainError(
"Invalid monomial ordering. The ring has variables $(collect(keys(varmap_str))) and the ordering variables are $vars_str, which are incompatible."
)
)
end
vars_str = filter(var -> var in keys(varmap_str), vars_str)
return map(v -> varmap_str[v], vars_str)
end
map(v -> varmap[v], vars)
Expand All @@ -364,7 +372,9 @@ function ordering_transform(ord::WeightedOrdering, varmap::AbstractDict)
end

function ordering_transform(ord::ProductOrdering, varmap::AbstractDict)
ProductOrdering(ordering_transform(ord.ord1, varmap), ordering_transform(ord.ord2, varmap))
varmap1 = filter(key_val -> key_val[1] in Set(ordering_variables(ord.ord1)), varmap)
varmap2 = filter(key_val -> key_val[1] in Set(ordering_variables(ord.ord2)), varmap)
ProductOrdering(ordering_transform(ord.ord1, varmap1), ordering_transform(ord.ord2, varmap2))
end

function ordering_transform(ord::MatrixOrdering, varmap::AbstractDict)
Expand Down
3 changes: 2 additions & 1 deletion test/groebner.jl
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,8 @@ end

# ProductOrdering
ord = Groebner.Lex(x6, x2) * Groebner.Lex(x4, x1, x3)
@test_throws DomainError Groebner.groebner([x1], ordering=ord)
@test Groebner.groebner([x1, x2], ordering=ord) == [x1, x2]
@test Groebner.groebner([x4, x6, x3], ordering=ord) == [x3, x4, x6]
@test_throws DomainError Groebner.Lex() * Groebner.Lex(x4, x1, x3)

ord = Groebner.Lex(x6, x2, x5) * Groebner.Lex(x4, x1, x3)
Expand Down
5 changes: 5 additions & 0 deletions test/input_output/GroebnerDynamicPolynomialsExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ using DynamicPolynomials, Test
gb = Groebner.groebner(F, ordering=Groebner.Lex(y, x))
@test gb == [(-15 // 8) * x + x^3, x * y + (4 // 5) * x^2, (24 // 25) * x + y^3]

# It is possible to over-specify the ordering
@polyvar x y z
@test groebner([x, y], ordering=Lex(x, y, z)) == [y, x]
@test groebner([x, y], ordering=Lex(y, x, z)) == [x, y]

@polyvar x y monomial_order = Graded{Reverse{InverseLexOrder}}
F = [2x^2 * y + 3x, 4x * y^2 + 5y^3]
gb = Groebner.groebner(F)
Expand Down

0 comments on commit 26668d4

Please sign in to comment.