Skip to content

Commit 8767e3e

Browse files
authored
Add monomial order (#231)
* Abstract monomial ordering * MA term * DP#bl/order
1 parent 97c6859 commit 8767e3e

File tree

5 files changed

+134
-4
lines changed

5 files changed

+134
-4
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ jobs:
5151
shell: julia --project=@. {0}
5252
run: |
5353
using Pkg
54-
Pkg.add(PackageSpec(name="DynamicPolynomials", rev="bl/coefficient_type"))
54+
Pkg.add(PackageSpec(name="DynamicPolynomials", rev="bl/order"))
5555
- uses: julia-actions/julia-buildpkg@v1
5656
- uses: julia-actions/julia-runtest@v1
5757
with:

src/default_term.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,33 @@ function MA.mutability(::Type{Term{C,M}}) where {C,M}
8686
return MA.IsNotMutable()
8787
end
8888
end
89+
90+
function MA.mutable_copy(t::Term)
91+
return Term(
92+
MA.copy_if_mutable(coefficient(t)),
93+
MA.copy_if_mutable(monomial(t)),
94+
)
95+
end
96+
97+
function MA.operate_to!(
98+
t::Term,
99+
::typeof(*),
100+
t1::AbstractTermLike,
101+
t2::AbstractTermLike,
102+
)
103+
MA.operate_to!(t.coefficient, *, coefficient(t1), coefficient(t2))
104+
MA.operate_to!(t.monomial, *, monomial(t1), monomial(t2))
105+
return t
106+
end
107+
108+
function MA.operate!(::typeof(*), t1::Term, t2::AbstractTermLike)
109+
MA.operate!(*, t1.coefficient, coefficient(t2))
110+
MA.operate!(*, t1.monomial, monomial(t2))
111+
return t1
112+
end
113+
114+
function MA.operate!(::typeof(one), t::Term)
115+
MA.operate!(one, t.coefficient)
116+
MA.operate!(constant_monomial, t.monomial)
117+
return t
118+
end

src/monomial_vector.jl

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,106 @@ export monomial_vector,
44
sort_monomial_vector,
55
merge_monomial_vectors
66

7+
export LexOrder, InverseLexOrder, Reverse, Graded
8+
9+
"""
10+
abstract type AbstractMonomialOrdering end
11+
12+
Abstract type for monomial ordering as defined in [CLO13, Definition 2.2.1, p. 55]
13+
14+
[CLO13] Cox, D., Little, J., & OShea, D.
15+
*Ideals, varieties, and algorithms: an introduction to computational algebraic geometry and commutative algebra*.
16+
Springer Science & Business Media, **2013**.
17+
"""
18+
abstract type AbstractMonomialOrdering end
19+
20+
"""
21+
compare(a, b, order::AbstractMonomialOrdering)
22+
23+
Returns a negative number if `a < b`, a positive number if `a > b` and zero if `a == b`.
24+
"""
25+
function compare end
26+
27+
"""
28+
struct LexOrder <: AbstractMonomialOrdering end
29+
30+
Lexicographic (Lex for short) Order often abbreviated as *lex* order as defined in [CLO13, Definition 2.2.3, p. 56]
31+
32+
The [`Graded`](@ref) version is often abbreviated as *grlex* order and is defined in [CLO13, Definition 2.2.5, p. 58]
33+
34+
[CLO13] Cox, D., Little, J., & OShea, D.
35+
*Ideals, varieties, and algorithms: an introduction to computational algebraic geometry and commutative algebra*.
36+
Springer Science & Business Media, **2013**.
37+
"""
38+
struct LexOrder <: AbstractMonomialOrdering end
39+
40+
"""
41+
struct InverseLexOrder <: AbstractMonomialOrdering end
42+
43+
Inverse Lex Order defined in [CLO13, Exercise 2.2.6, p. 61] where it is abbreviated as *invlex*.
44+
It corresponds to [`LexOrder`](@ref) but with the variables in reverse order.
45+
46+
The [`Graded`](@ref) version can be abbreviated as *grinvlex* order.
47+
It is defined in [BDD13, Definition 2.1] where it is called *Graded xel order*.
48+
49+
[CLO13] Cox, D., Little, J., & OShea, D.
50+
*Ideals, varieties, and algorithms: an introduction to computational algebraic geometry and commutative algebra*.
51+
Springer Science & Business Media, **2013**.
52+
[BDD13] Batselier, K., Dreesen, P., & De Moor, B.
53+
*The geometry of multivariate polynomial division and elimination*.
54+
SIAM Journal on Matrix Analysis and Applications, 34(1), 102-125, *2013*.
55+
"""
56+
struct InverseLexOrder <: AbstractMonomialOrdering end
57+
58+
"""
59+
struct Graded{O<:AbstractMonomialOrdering} <: AbstractMonomialOrdering
60+
same_degree_ordering::O
61+
end
62+
63+
Monomial ordering defined by:
64+
* `degree(a) == degree(b)` then the ordering is determined by `same_degree_ordering`,
65+
* otherwise, it is the ordering between the integers `degree(a)` and `degree(b)`.
66+
"""
67+
struct Graded{O<:AbstractMonomialOrdering} <: AbstractMonomialOrdering
68+
same_degree_ordering::O
69+
end
70+
71+
_deg(exponents) = sum(exponents)
72+
_deg(mono::AbstractMonomial) = degree(mono)
73+
74+
function compare(a, b, ordering::Graded)
75+
deg_a = _deg(a)
76+
deg_b = _deg(b)
77+
if deg_a == deg_b
78+
return compare(a, b, ordering.same_degree_ordering)
79+
else
80+
return deg_a - deg_b
81+
end
82+
end
83+
84+
"""
85+
struct Reverse{O<:AbstractMonomialOrdering} <: AbstractMonomialOrdering
86+
reverse_order::O
87+
end
88+
89+
Monomial ordering defined by `compare(a, b, order) = compare(b, a, order.reverse_order)`..
90+
91+
Reverse Lex Order defined in [CLO13, Exercise 2.2.9, p. 61] where it is abbreviated as *rinvlex*.
92+
can be obtained as `Reverse(InverseLexOrder())`.
93+
94+
The Graded Reverse Lex Order often abbreviated as *grevlex* order defined in [CLO13, Definition 2.2.6, p. 58]
95+
can be obtained as `Graded(Reverse(InverseLexOrder()))`.
96+
97+
[CLO13] Cox, D., Little, J., & OShea, D.
98+
*Ideals, varieties, and algorithms: an introduction to computational algebraic geometry and commutative algebra*.
99+
Springer Science & Business Media, **2013**.
100+
"""
101+
struct Reverse{O<:AbstractMonomialOrdering} <: AbstractMonomialOrdering
102+
reverse_ordering::O
103+
end
104+
105+
compare(a, b, ordering::Reverse) = compare(b, a, ordering.reverse_ordering)
106+
7107
function monomials(v::AbstractVariable, degree, args...)
8108
return monomials((v,), degree, args...)
9109
end

src/variable.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ be the type of `p` but the supertype of all variables that could be created.
1313
1414
For `TypedPolynomials`, a variable of name `x` has type `Variable{:x}` so
1515
`variable_union_type` should return `Variable`.
16-
For `DynamicPolynomials`, all variables have the same type `PolyVar{C}` where
16+
For `DynamicPolynomials`, all variables have the same type `Variable{C}` where
1717
`C` is `true` for commutative variables and `false` for non-commutative ones so
18-
`variable_union_type` should return `PolyVar{C}`.
18+
`variable_union_type` should return `Variable{C}`.
1919
"""
2020
function variable_union_type end
2121
function variable_union_type(::Type{MT}) where {MT<:AbstractMonomialLike}

test/variable.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import MultivariatePolynomials:
99
@test length(z) == 2
1010
@test x[1] > x[2] > x[3] > y > z[1] > z[2]
1111
end
12-
@testset "PolyVar" begin
12+
@testset "Variable" begin
1313
Mod.@polyvar x
1414
alloc_test(() -> convert(typeof(x), x), 0)
1515
alloc_test(() -> convert(variable_union_type(x), x), 0)

0 commit comments

Comments
 (0)