Skip to content

Add support for monomial ordering #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 5 additions & 36 deletions src/operators.jl
Original file line number Diff line number Diff line change
@@ -1,51 +1,20 @@
# Graded Lexicographic order
# First compare total degree, then lexicographic order
function Base.isless(m1::Monomial{V}, m2::Monomial{V}) where {V}
d1 = degree(m1)
d2 = degree(m2)
if d1 < d2
return true
elseif d1 > d2
return false
else
return exponents(m1) < exponents(m2)
end
function MP.compare(m1::Monomial{V}, m2::Monomial{V}, ::Type{O}) where {V,O<:MP.AbstractMonomialOrder}
return MP.compare(MP.exponents(m1), MP.exponents(m2), O)
end

function _compare(a::Tuple, b::Tuple, ::Type{MP.LexOrder})
if a == b
return 0
elseif a < b
return -1
else
return 1
end
end
function _compare(a::Tuple, b::Tuple, ::Type{MP.InverseLexOrder})
return _compare(reverse(a), reverse(b), MP.LexOrder)
end

function MP.compare(m1::Monomial{V}, m2::Monomial{V}, ::Type{O}) where {V,O<:Union{MP.LexOrder,MP.InverseLexOrder}}
return _compare(MP.exponents(m1), MP.exponents(m2), O)
end

function MP.compare(m1::Monomial, m2::Monomial, ::Type{O}) where {O<:Union{MP.LexOrder,MP.InverseLexOrder}}
function MP.compare(m1::Monomial, m2::Monomial, ::Type{O}) where {O<:MP.AbstractMonomialOrder}
return MP.compare(promote(m1, m2)..., O)
end

function MP.compare(m1::Monomial, m2::Monomial)
return MP.compare(m1, m2, MP.Graded{MP.LexOrder})
end


(==)(::Variable{N}, ::Variable{N}) where {N} = true
(==)(::Variable, ::Variable) = false
(==)(m1::Monomial{V}, m2::Monomial{V}) where {V} = exponents(m1) == exponents(m2)

# Multiplication is handled as a special case so that we can write these
# definitions without resorting to promotion:

(*)(v1::V, v2::V) where {V <: Variable} = Monomial{(V(),), 1}((2,))
(*)(::V, ::V) where {V <: Variable} = Monomial{(V(),), 1}((2,))
(*)(v1::Variable, v2::Variable) = (*)(promote(v1, v2)...)

function MP.divides(m1::Monomial{V, N}, m2::Monomial{V, N}) where {V, N}
Expand All @@ -68,7 +37,7 @@ end
# We could remove these methods since it is the default.
MA.mutability(::Type{<:Monomial}) = MA.IsNotMutable()

^(v::V, x::Integer) where {V <: Variable} = Monomial{(V(),), 1}((x,))
^(::V, x::Integer) where {V <: Variable} = Monomial{(V(),), 1}((x,))

# dot(v1::AbstractVector{<:TermLike}, v2::AbstractVector) = dot(v1, v2)
# dot(v1::AbstractVector, v2::AbstractVector{<:TermLike}) = dot(v1, v2)
Expand Down
25 changes: 15 additions & 10 deletions src/types.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
struct Variable{Name} <: AbstractVariable
"""
Variable{Name,M} <: AbstractVariable

Variable of name `Name` and monomial order `M`.
"""
struct Variable{Name,M} <: AbstractVariable
end

MP.name(::Type{Variable{N}}) where {N} = N
MP.name(::Type{<:Variable{N}}) where {N} = N
MP.name(v::Variable) = name(typeof(v))
MP.name_base_indices(v::Variable) = name_base_indices(typeof(v))
function MP.name_base_indices(v::Type{Variable{N}}) where N
function MP.name_base_indices(::Type{<:Variable{N}}) where N
name = string(N)
splits = split(string(N), r"[\[,\]]\s*", keepempty=false)
if length(splits) == 1
Expand All @@ -23,15 +28,15 @@ checksorted(x::Tuple{Any}, cmp) = true
checksorted(x::Tuple{}, cmp) = true
checksorted(x::Tuple, cmp) = cmp(x[1], x[2]) && checksorted(Base.tail(x), cmp)

struct Monomial{V, N} <: AbstractMonomial
struct Monomial{V, M, N} <: AbstractMonomial
exponents::NTuple{N, Int}

function Monomial{V, N}(exponents::NTuple{N, Int}=ntuple(_ -> 0, Val{N}())) where {V, N}
function Monomial{V, M, N}(exponents::NTuple{N, Int}=ntuple(_ -> 0, Val{N}())) where {V, M, N}
@assert checksorted(V, >)
new{V, N}(exponents)
new{V, M, N}(exponents)
end
Monomial{V}(exponents::NTuple{N, Integer}=()) where {V, N} = Monomial{V, N}(exponents)
Monomial{V}(exponents::AbstractVector{<:Integer}) where {V} = Monomial{V}(NTuple{length(V), Int}(exponents))
Monomial{V, M}(exponents::NTuple{N, Integer}=()) where {V, N} = Monomial{V, M, N}(exponents)
Monomial{V, M}(exponents::AbstractVector{<:Integer}) where {V} = Monomial{V, M}(NTuple{length(V), Int}(exponents))
end

Monomial(v::Variable) = monomial_type(v)((1,))
Expand All @@ -46,8 +51,8 @@ MP.monomial_type(v::Variable) = Monomial{(v,), 1}

MP.exponents(m::Monomial) = m.exponents
MP.exponent(m::Monomial, i::Integer) = m.exponents[i]
_exponent(v::V, p1::Tuple{V, Integer}, p2...) where {V <: Variable} = p1[2]
_exponent(v::Variable, p1::Tuple{Variable, Integer}, p2...) = _exponent(v, p2...)
_exponent(::V, p1::Tuple{V, Integer}, p2...) where {V <: Variable} = p1[2]
_exponent(v::Variable, ::Tuple{Variable, Integer}, p2...) = _exponent(v, p2...)
_exponent(v::Variable) = 0
MP.degree(m::Monomial, v::Variable) = _exponent(v, powers(m)...)

Expand Down
Loading