Skip to content

Commit

Permalink
Implement mapcoefficient functions (#102)
Browse files Browse the repository at this point in the history
* Implement mapcoefficient functions

* Updates

* Up
  • Loading branch information
blegat authored Nov 16, 2021
1 parent 52358e3 commit 50bb63d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
Expand Down
16 changes: 0 additions & 16 deletions src/mult.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 30 additions & 0 deletions src/poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 comments on commit 50bb63d

@blegat
Copy link
Member Author

@blegat blegat commented on 50bb63d Nov 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/48849

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.4.1 -m "<description of version>" 50bb63d3f7901a52131f2f5de7d58bbecc09c16d
git push origin v0.4.1

Please sign in to comment.