Skip to content
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

Implement maxdegree for vector of polynomials #279

Merged
merged 1 commit into from
Nov 23, 2023
Merged
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
29 changes: 19 additions & 10 deletions src/polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,23 +272,29 @@ end

#$(SIGNATURES)
"""
mindegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractTermLike}})
mindegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractPolynomialLike}})

Returns the minimal total degree of the monomials of `p`, i.e. `minimum(degree, terms(p))`.

mindegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractTermLike}}, v::AbstractVariable)
mindegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractPolynomialLike}}, v::AbstractVariable)

Returns the minimal degree of the monomials of `p` in the variable `v`, i.e. `minimum(degree.(terms(p), v))`.

### Examples
Calling `mindegree` on on ``4x^2y + xy + 2x`` should return 1, `mindegree(4x^2y + xy + 2x, x)` should return 1 and `mindegree(4x^2y + xy + 2x, y)` should return 0.
"""
function mindegree(X::AbstractVector{<:AbstractTermLike}, args...)
return isempty(X) ? 0 : minimum(t -> degree(t, args...), X)
function mindegree(
X::AbstractVector{<:AbstractPolynomialLike},
args::Vararg{Any,N},
) where {N}
return isempty(X) ? 0 : minimum(t -> mindegree(t, args...), X)
end
function mindegree(p::AbstractPolynomialLike, args...)
function mindegree(p::AbstractPolynomialLike, args::Vararg{Any,N}) where {N}
return mindegree(terms(p), args...)
end
function mindegree(t::AbstractTermLike, args::Vararg{Any,N}) where {N}
return degree(t, args...)
end

#$(SIGNATURES)
"""
Expand All @@ -304,30 +310,33 @@ Returns the maximal degree of the monomials of `p` in the variable `v`, i.e. `ma
Calling `maxdegree` on ``4x^2y + xy + 2x`` should return 3, `maxdegree(4x^2y + xy + 2x, x)` should return 2 and `maxdegree(4x^2y + xy + 2x, y)` should return 1.
"""
function maxdegree(
X::AbstractVector{<:AbstractTermLike},
X::AbstractVector{<:AbstractPolynomialLike},
args::Vararg{Any,N},
) where {N}
return mapreduce(t -> degree(t, args...), max, X, init = 0)
return mapreduce(t -> maxdegree(t, args...), max, X, init = 0)
end
function maxdegree(p::AbstractPolynomialLike, args::Vararg{Any,N}) where {N}
return maxdegree(terms(p), args...)
end
function maxdegree(t::AbstractTermLike, args::Vararg{Any,N}) where {N}
return degree(t, args...)
end

#$(SIGNATURES)
"""
extdegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractTermLike}})
extdegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractPolynomialLike}})

Returns the extremal total degrees of the monomials of `p`, i.e. `(mindegree(p), maxdegree(p))`.

extdegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractTermLike}}, v::AbstractVariable)
extdegree(p::Union{AbstractPolynomialLike, AbstractVector{<:AbstractPolynomialLike}}, v::AbstractVariable)

Returns the extremal degrees of the monomials of `p` in the variable `v`, i.e. `(mindegree(p, v), maxdegree(p, v))`.

### Examples
Calling `extdegree` on ``4x^2y + xy + 2x`` should return `(1, 3)`, `extdegree(4x^2y + xy + 2x, x)` should return `(1, 2)` and `maxdegree(4x^2y + xy + 2x, y)` should return `(0, 1)`.
"""
function extdegree(
p::Union{AbstractPolynomialLike,AbstractVector{<:AbstractTermLike}},
p::Union{AbstractPolynomialLike,AbstractVector{<:AbstractPolynomialLike}},
args...,
)
return (mindegree(p, args...), maxdegree(p, args...))
Expand Down
57 changes: 36 additions & 21 deletions test/polynomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,29 +72,44 @@ const MP = MultivariatePolynomials
@test Tuple(variables([x + 1, y^2])) == (x, y)
@test Tuple(variables([y^2, x + 1])) == (x, y)

@test maxdegree(x^2 - x^2) == 0
@test maxdegree(x^2 - x^2, x) == 0
@test maxdegree(x^2 - x^2, y) == 0
@test mindegree(x^2 - x^2) == 0
@test mindegree(x^2 - x^2, x) == 0
@test mindegree(x^2 - x^2, y) == 0
@test extdegree(x^2 - x^2) == (0, 0)
@test extdegree(x^2 - x^2, x) == (0, 0)
@test extdegree(x^2 - x^2, y) == (0, 0)
@test maxdegree(x * y + 2 + x^2 * y + x + y) == 3
@test maxdegree(x * y + 2 + x^2 * y + x + y, x) == 2
@test maxdegree(x * y + 2 + x^2 * y + x + y, y) == 1
@test mindegree(x * y + 2 + x^2 * y + x + y) == 0
@test mindegree(x * y + 2 + x^2 * y + x + y, x) == 0
@test mindegree(x * y + 2 + x^2 * y + x + y, y) == 0
@test extdegree(x * y + 2 + x^2 * y + x + y) == (0, 3)
@test extdegree(x * y + 2 + x^2 * y + x + y, x) == (0, 2)
@test extdegree(x * y + 2 + x^2 * y + x + y, y) == (0, 1)
@test extdegree(x * y + x^2 * y, x) == (1, 2)
@test extdegree(x * y + x^2 * y, y) == (1, 1)
@test leading_term(x * y + 2 + x^2 * y + x + y) == x^2 * y
p = x^2 - x^2
@test maxdegree(p) == 0
@test maxdegree(p, x) == 0
@test maxdegree(p, y) == 0
@test mindegree(p) == 0
@test mindegree(p, x) == 0
@test mindegree(p, y) == 0
@test extdegree(p) == (0, 0)
@test extdegree(p, x) == (0, 0)
@test extdegree(p, y) == (0, 0)
q = x * y + 2 + x^2 * y + x + y
@test maxdegree(q) == 3
@test maxdegree(q, x) == 2
@test maxdegree(q, y) == 1
@test mindegree(q) == 0
@test mindegree(q, x) == 0
@test mindegree(q, y) == 0
@test extdegree(q) == (0, 3)
@test extdegree(q, x) == (0, 2)
@test extdegree(q, y) == (0, 1)
r = x * y + x^2 * y
@test extdegree(r, x) == (1, 2)
@test extdegree(r, y) == (1, 1)
@test leading_term(q) == x^2 * y
@test nvariables(x + y - x) == 2
@test nvariables(x + x^2) == 1
@test mindegree([p, q, r]) == 0
@test mindegree([p, q, r], x) == 0
@test mindegree([p, q, r], y) == 0
@test mindegree([p, q], y) == 0
@test mindegree([q], y) == 0
@test mindegree([r], y) == 1
@test maxdegree([p, q, r]) == 3
@test maxdegree([p, q, r], x) == 2
@test maxdegree([p, q, r], y) == 1
@test extdegree([p, q, r]) == (0, 3)
@test extdegree([p, q, r], x) == (0, 2)
@test extdegree([p, q, r], y) == (0, 1)

@test collect(
coefficients(
Expand Down
Loading