Skip to content

Commit 91f41e5

Browse files
authored
Add tests for mapcoefficients (#186)
* Add tests for mapcoefficients * Fix tests * Fix
1 parent 52fbfc0 commit 91f41e5

File tree

5 files changed

+38
-33
lines changed

5 files changed

+38
-33
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
1111

1212
[compat]
1313
DataStructures = "0.17.7, 0.18"
14-
DynamicPolynomials = "0.4"
14+
DynamicPolynomials = "0.4.1"
1515
MutableArithmetics = "0.3"
16-
TypedPolynomials = "0.3"
16+
TypedPolynomials = "0.3.1"
1717
julia = "1"
1818

1919
[extras]

src/division.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
export divides
22

3-
Base.round(t::AbstractTermLike; args...) = term(round(coefficient(t); args...), monomial(t))
43
function Base.round(p::APL; args...)
5-
# round(0.1) is zero so we cannot use SortedUniqState
6-
polynomial!(round.(terms(p); args...), SortedState())
4+
# round(0.1) is zero so we cannot use `mapcoefficientsnz`
5+
return mapcoefficients(p) do term
6+
round(term; args...)
7+
end
78
end
89

9-
Base.div(t::AbstractTermLike, α::Number, args...) = term(div(coefficient(t), α, args...), monomial(t))
1010
function Base.div(p::APL, α::Number, args...)
11-
polynomial!(div.(terms(p), α, args...), SortedState())
11+
return mapcoefficients(p) do term
12+
div(term, α, args...)
13+
end
1214
end
1315

1416
"""

src/operators.jl

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ Base.isapprox(α, p::APL; kwargs...) = isapprox(promote(p, α)...; kwargs...)
216216
# option.
217217
Base.:-(m::AbstractMonomialLike) = _term(-1, MA.copy_if_mutable(m))
218218
Base.:-(t::AbstractTermLike) = _term(MA.operate(-, coefficient(t)), monomial(t))
219-
Base.:-(p::APL) = polynomial!((-).(terms(p)))
219+
Base.:-(p::APL) = mapcoefficients(-, p)
220220
Base.:+(p::Union{APL, RationalPoly}) = p
221221
Base.:*(p::Union{APL, RationalPoly}) = p
222222

@@ -243,39 +243,25 @@ end
243243
multconstant(α, v::AbstractVariable) = multconstant(α, monomial(v)) # TODO linear term
244244
multconstant(m::AbstractMonomialLike, α) = multconstant(α, m)
245245

246-
_multconstant(α, f, t::AbstractTermLike) = mapcoefficientsnz(f, t)
247-
function _multconstant::T, f, p::AbstractPolynomial{S}) where {S, T}
248-
if iszero(α)
249-
zero(polynomialtype(p, MA.promote_operation(*, T, S)))
250-
else
251-
mapcoefficientsnz(f, p)
252-
end
253-
end
254-
_multconstant(α, f, p::AbstractPolynomialLike) = _multconstant(α, f, polynomial(p))
246+
# TODO delete once DynamicPolynomials stops using it
247+
function _multconstant end
248+
function _multconstant_to! end
255249

256-
multconstant(α, p::AbstractPolynomialLike) = _multconstant(α, β -> α*β, p)
257-
multconstant(p::AbstractPolynomialLike, α) = _multconstant(α, β -> β*α, p)
250+
multconstant(α, p::AbstractPolynomialLike) = mapcoefficients(Base.Fix1(*, α), p)
251+
multconstant(p::AbstractPolynomialLike, α) = mapcoefficients(Base.Fix2(*, α), p)
258252

259-
# TODO delete once DynamicPolynomials stops using it
260-
function _multconstant_to!(output, α, f, p)
261-
if iszero(α)
262-
MA.operate!(zero, output)
263-
else
264-
mapcoefficientsnz_to!(output, f, p)
265-
end
253+
function MA.operate_to!(output, ::typeof(multconstant), α, p::APL)
254+
return mapcoefficients_to!(output, Base.Fix1(*, α), p)
266255
end
267256
function MA.operate_to!(output, ::typeof(multconstant), p::APL, α)
268-
return _multconstant_to!(output, α, β -> β*α, p)
257+
return mapcoefficients_to!(output, Base.Fix2(*, α), p)
269258
end
270-
function MA.operate_to!(output, ::typeof(multconstant), α, p::APL)
271-
return _multconstant_to!(output, α, β -> α*β, p)
259+
function MA.operate!(::typeof(multconstant), α, p::APL)
260+
return mapcoefficients!(Base.Fix1(*, α), p)
272261
end
273262
function MA.operate!(::typeof(multconstant), p::APL, α)
274263
return mapcoefficients!(Base.Fix2(*, α), p)
275264
end
276-
function MA.operate!(::typeof(multconstant), α, p::APL)
277-
return mapcoefficients!(Base.Fix1(*, α), p)
278-
end
279265

280266
MA.operate_to!(output::AbstractMonomial, ::typeof(*), m1::AbstractMonomialLike, m2::AbstractMonomialLike) = mapexponents_to!(output, +, m1, m2)
281267
MA.operate!(::typeof(*), m1::AbstractMonomial, m2::AbstractMonomialLike) = mapexponents!(+, m1, m2)

src/polynomial.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ See also [`mapcoefficients!`](@ref) and [`mapcoefficients_to!`](@ref).
409409
Calling `mapcoefficients(α -> mod(3α, 6), 2x*y + 3x + 1)` should return `3x + 3`.
410410
"""
411411
function mapcoefficients end
412-
function mapcoefficients(f::Function, p::AbstractPolynomialLike; nonzero = false) # TODO remove
412+
function mapcoefficients(f::Function, p::AbstractPolynomialLike; nonzero = false) # Not used by either TypedPolynomials or DynamicPolynomials but used by CustomPoly in tests. FIXME Remove in a breaking release
413413
# Invariant: p has only nonzero coefficient
414414
# therefore f(α) will be nonzero for every coefficient α of p
415415
# hence we can use Uniq

test/polynomial.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,21 @@ const MP = MultivariatePolynomials
192192
q = MA.add!!(q, 2y)
193193
@test p == 2x + 1
194194
end
195+
196+
@testset "mapcoefficients $nz" for nz in [false, true]
197+
p = 2x + 1
198+
@test mapcoefficients(x -> x / 2, p, nonzero = nz) == 1.0x + 0.5
199+
@test mapcoefficients(x -> x / 2, 3x, nonzero = nz) == 1.5x
200+
@test p === mapcoefficients!(x -> x + 1, p, nonzero = nz)
201+
@test p == 3x + 2
202+
q = zero(p)
203+
@test q === mapcoefficients_to!(q, x -> 2x, p, nonzero = nz)
204+
@test q == 6x + 4
205+
@test q === mapcoefficients_to!(q, x -> 2x, 3x, nonzero = nz)
206+
@test q == 6x
207+
@test q === mapcoefficients_to!(q, x -> 2x, x, nonzero = nz)
208+
@test q == 2x
209+
@test q === mapcoefficients_to!(q, x -> 2x, x^2, nonzero = nz)
210+
@test q == 2x^2
211+
end
195212
end

0 commit comments

Comments
 (0)