Skip to content

Commit

Permalink
[BREAKING] Remove term iterator behavior of polynomial (#161)
Browse files Browse the repository at this point in the history
* Remove term iterator behavior of polynomial

* Fix

* Fixes

* Add bounds to MP
  • Loading branch information
blegat authored Jun 13, 2024
1 parent 469c47c commit 01869ba
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
MultivariatePolynomials = "0.5.3"
MultivariatePolynomials = "0.5.6"
MutableArithmetics = "1"
Reexport = "1"
julia = "1"
Expand Down
7 changes: 2 additions & 5 deletions src/comp.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import Base.==

#Base.iszero(t::Term) = iszero(MP.coefficient(t))
Base.iszero(p::Polynomial) = isempty(p)

# TODO This should be in Base with T instead of Variable{V,M}.
# See https://github.com/blegat/MultivariatePolynomials.jl/issues/3
function (==)(x::Vector{Variable{V,M}}, y::Vector{Variable{V,M}}) where {V,M}
Expand Down Expand Up @@ -162,10 +159,10 @@ end
# Comparison of Term
function (==)(p::Polynomial{V,M}, q::Polynomial{V,M}) where {V,M}
# terms should be sorted and without zeros
if length(p) != length(q)
if MP.nterms(p) != MP.nterms(q)
return false
end
for i in 1:length(p)
for i in eachindex(p.a)
if p.x[i] != q.x[i]
# There should not be zero terms
@assert p.a[i] != 0
Expand Down
6 changes: 3 additions & 3 deletions src/mult.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ function _mul(
else
allvars, maps = mergevars([MP.variables(p), MP.variables(q)])
end
N = length(p) * length(q)
N = MP.nterms(p) * MP.nterms(q)
Z = Vector{Vector{Int}}(undef, N)
a = Vector{T}(undef, N)
i = 0
for u in p
for v in q
for u in MP.terms(p)
for v in MP.terms(q)
if samevars
z = MP.monomial(u).z + MP.monomial(v).z
else
Expand Down
2 changes: 1 addition & 1 deletion src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ end
Base.:(+)(x::DMonomialLike, y::DMonomialLike) = MP.term(x) + MP.term(y)
Base.:(-)(x::DMonomialLike, y::DMonomialLike) = MP.term(x) - MP.term(y)

_getindex(p::Polynomial, i::Int) = p[i]
_getindex(p::Polynomial, i::Int) = MP.terms(p)[i]
_getindex(t::_Term, ::Int) = t
function _plusorminus_to!(
a::Vector{U},
Expand Down
10 changes: 1 addition & 9 deletions src/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,6 @@ end
#Base.convert(::Type{term_type{V,M}}, p::TermContainer{V,M}) where {V,M} = p
#Base.convert(::Type{term_type{V,M,T}}, p::TermContainer{V,M,T}) where {V,M,T} = p

Base.length(p::Polynomial) = length(p.a)
Base.isempty(p::Polynomial) = isempty(p.a)
Base.iterate(p::Polynomial) = isempty(p) ? nothing : (p[1], 1)
function Base.iterate(p::Polynomial, state::Int)
return state < length(p) ? (p[state+1], state + 1) : nothing
end
#eltype(::Type{Polynomial{V,M,T}}) where {V,M,T} = T
Base.getindex(p::Polynomial, I::Int) = MP.term(p.a[I[1]], p.x[I[1]])

#Base.transpose(p::Polynomial) = Polynomial(map(transpose, p.a), p.x) # FIXME invalid age range update

Expand Down Expand Up @@ -192,7 +184,7 @@ function MP.remove_monomials(p::Polynomial, x::MonomialVector)
# use the fact that monomials are sorted to do this O(n) instead of O(n^2)
j = 1
I = Int[]
for (i, t) in enumerate(p)
for (i, t) in enumerate(MP.terms(p))
while j <= length(x) && x[j] < MP.monomial(t)
j += 1
end
Expand Down

0 comments on commit 01869ba

Please sign in to comment.