Skip to content

Commit 50bb63d

Browse files
authored
Implement mapcoefficient functions (#102)
* Implement mapcoefficient functions * Updates * Up
1 parent 52358e3 commit 50bb63d

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DynamicPolynomials"
22
uuid = "7c1d4256-1411-5781-91ec-d7bc3513ac07"
33
repo = "https://github.com/JuliaAlgebra/DynamicPolynomials.jl.git"
4-
version = "0.4.0"
4+
version = "0.4.1"
55

66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
@@ -15,7 +15,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1515

1616
[compat]
1717
DataStructures = "0.17, 0.18"
18-
MultivariatePolynomials = "0.4"
18+
MultivariatePolynomials = "0.4.1"
1919
MutableArithmetics = "0.3"
2020
Reexport = "0.2, 1.0"
2121
julia = "1"

src/mult.jl

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,6 @@ function MP._multconstant(α::T, f, p::Polynomial{C,S} ) where {T, C, S}
4949
end
5050
end
5151

52-
MP.mapcoefficientsnz(f::Function, p::Polynomial) = Polynomial(map(f, p.a), MA.mutable_copy(p.x))
53-
function MP.mapcoefficientsnz_to!(output::Polynomial, f::Function, t::MP.AbstractTermLike)
54-
MP.mapcoefficientsnz_to!(output, f, polynomial(t))
55-
end
56-
function MP.mapcoefficientsnz_to!(output::Polynomial, f::Function, p::Polynomial)
57-
resize!(output.a, length(p.a))
58-
@. output.a = f(p.a)
59-
Future.copy!(output.x.vars, p.x.vars)
60-
# TODO reuse the part of `Z` that is already in `output`.
61-
resize!(output.x.Z, length(p.x.Z))
62-
for i in eachindex(p.x.Z)
63-
output.x.Z[i] = copy(p.x.Z[i])
64-
end
65-
return output
66-
end
67-
6852
# I do not want to cast x to TermContainer because that would force the promotion of eltype(q) with Int
6953
function Base.:(*)(x::DMonomialLike, p::Polynomial)
7054
Polynomial(MA.mutable_copy(p.a), x*p.x)

src/poly.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,33 @@ function MA.operate!(::typeof(one), p::Polynomial{C, T}) where {C, T}
271271
end
272272
return p
273273
end
274+
275+
function MP.mapcoefficients(f::Function, p::Polynomial; nonzero = false)
276+
return Polynomial(map(f, p.a), MA.mutable_copy(p.x))
277+
end
278+
279+
function MP.mapcoefficients!(f::Function, p::Polynomial; nonzero = false)
280+
map!(f, p.a, p.a)
281+
if !nonzero
282+
_remove_zeros!(p)
283+
end
284+
return p
285+
end
286+
287+
function MP.mapcoefficients_to!(output::Polynomial, f::Function, t::MP.AbstractTermLike; nonzero = false)
288+
return MP.mapcoefficients_to!(output, f, polynomial(t); nonzero = nonzero)
289+
end
290+
function MP.mapcoefficients_to!(output::Polynomial, f::Function, p::Polynomial; nonzero = false)
291+
resize!(output.a, length(p.a))
292+
map!(f, output.a, p.a)
293+
Future.copy!(output.x.vars, p.x.vars)
294+
# TODO reuse the part of `Z` that is already in `output`.
295+
resize!(output.x.Z, length(p.x.Z))
296+
for i in eachindex(p.x.Z)
297+
output.x.Z[i] = copy(p.x.Z[i])
298+
end
299+
if !nonzero
300+
_remove_zeros!(output)
301+
end
302+
return output
303+
end

0 commit comments

Comments
 (0)