diff --git a/Project.toml b/Project.toml index 714ae70..0331698 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "DynamicPolynomials" uuid = "7c1d4256-1411-5781-91ec-d7bc3513ac07" repo = "https://github.com/JuliaAlgebra/DynamicPolynomials.jl.git" -version = "0.4.0" +version = "0.4.1" [deps] DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" @@ -15,7 +15,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [compat] DataStructures = "0.17, 0.18" -MultivariatePolynomials = "0.4" +MultivariatePolynomials = "0.4.1" MutableArithmetics = "0.3" Reexport = "0.2, 1.0" julia = "1" diff --git a/src/mult.jl b/src/mult.jl index 38a6c77..1321f53 100644 --- a/src/mult.jl +++ b/src/mult.jl @@ -49,22 +49,6 @@ function MP._multconstant(α::T, f, p::Polynomial{C,S} ) where {T, C, S} end end -MP.mapcoefficientsnz(f::Function, p::Polynomial) = Polynomial(map(f, p.a), MA.mutable_copy(p.x)) -function MP.mapcoefficientsnz_to!(output::Polynomial, f::Function, t::MP.AbstractTermLike) - MP.mapcoefficientsnz_to!(output, f, polynomial(t)) -end -function MP.mapcoefficientsnz_to!(output::Polynomial, f::Function, p::Polynomial) - resize!(output.a, length(p.a)) - @. output.a = f(p.a) - Future.copy!(output.x.vars, p.x.vars) - # TODO reuse the part of `Z` that is already in `output`. - resize!(output.x.Z, length(p.x.Z)) - for i in eachindex(p.x.Z) - output.x.Z[i] = copy(p.x.Z[i]) - end - return output -end - # I do not want to cast x to TermContainer because that would force the promotion of eltype(q) with Int function Base.:(*)(x::DMonomialLike, p::Polynomial) Polynomial(MA.mutable_copy(p.a), x*p.x) diff --git a/src/poly.jl b/src/poly.jl index 87bb474..99e2725 100644 --- a/src/poly.jl +++ b/src/poly.jl @@ -271,3 +271,33 @@ function MA.operate!(::typeof(one), p::Polynomial{C, T}) where {C, T} end return p end + +function MP.mapcoefficients(f::Function, p::Polynomial; nonzero = false) + return Polynomial(map(f, p.a), MA.mutable_copy(p.x)) +end + +function MP.mapcoefficients!(f::Function, p::Polynomial; nonzero = false) + map!(f, p.a, p.a) + if !nonzero + _remove_zeros!(p) + end + return p +end + +function MP.mapcoefficients_to!(output::Polynomial, f::Function, t::MP.AbstractTermLike; nonzero = false) + return MP.mapcoefficients_to!(output, f, polynomial(t); nonzero = nonzero) +end +function MP.mapcoefficients_to!(output::Polynomial, f::Function, p::Polynomial; nonzero = false) + resize!(output.a, length(p.a)) + map!(f, output.a, p.a) + Future.copy!(output.x.vars, p.x.vars) + # TODO reuse the part of `Z` that is already in `output`. + resize!(output.x.Z, length(p.x.Z)) + for i in eachindex(p.x.Z) + output.x.Z[i] = copy(p.x.Z[i]) + end + if !nonzero + _remove_zeros!(output) + end + return output +end