Skip to content

Commit 02c68c6

Browse files
authored
Merge pull request #127 from JuliaAlgebra/bl/MAv0.2
Update to MA v0.2
2 parents 9646c61 + f8070e3 commit 02c68c6

File tree

3 files changed

+36
-24
lines changed

3 files changed

+36
-24
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "MultivariatePolynomials"
22
uuid = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3"
33
license = "MIT"
44
repo = "https://github.com/JuliaAlgebra/MultivariatePolynomials.jl"
5-
version = "0.3.4"
5+
version = "0.3.5"
66

77
[deps]
88
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
@@ -11,7 +11,7 @@ MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
1111

1212
[compat]
1313
DataStructures = "0.17"
14-
MutableArithmetics = "0.1.1"
14+
MutableArithmetics = "0.2"
1515
julia = "1"
1616

1717
[extras]

src/operators.jl

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,19 @@ end
3737
# Fix ambiguity between above methods and methods in MA
3838
Base.:+(::MA.Zero, p::APL) = MA.copy_if_mutable(p)
3939
Base.:+(p::APL, ::MA.Zero) = MA.copy_if_mutable(p)
40+
41+
# Special case AbstractArrays of APLs
42+
# We add these instead of relying on the broadcasting API since the above method definitions are very wide.
43+
# In particular, there is support for Matrices as coefficents. In order to avoid issues like #104 we therefore
44+
# explicitly define this (instead of implictly getting unexpected results).
45+
for op in [:+, :-]
46+
@eval Base.$op(p::APL, A::AbstractArray{<:APL}) = map(f -> $op(p, f), A)
47+
@eval Base.$op(A::AbstractArray{<:APL}, p::APL) = map(f -> $op(f, p), A)
48+
end
49+
Base.:*(p::APL, A::AbstractArray) = map(f -> p * f, A)
50+
Base.:*(A::AbstractArray, p::APL) = map(f -> f * p, A)
51+
Base.:/(A::AbstractArray, p::APL) = map(f -> f / p, A)
52+
4053
constant_function(::typeof(+)) = plusconstant
4154
constant_function(::typeof(-)) = minusconstant
4255
MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::APL, α) = MA.mutable_operate!(constant_function(op), p, α)
@@ -163,16 +176,6 @@ end
163176
#MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::AbstractPolynomial, q::AbstractPolynomial) = MA.mutable_operate_to!(p, op, p, q)
164177
MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::AbstractPolynomial, q::AbstractPolynomialLike) = MA.mutable_operate!(op, p, polynomial(q))
165178

166-
# Special case AbstractArrays of APLs
167-
# We add these instead of relying on the broadcasting API since the above method definitions are very wide.
168-
# In particular, there is support for Matrices as coefficents. In order to avoid issues like #104 we therefore
169-
# explicitly define this (instead of implictly getting unexpected results).
170-
for op in [:+, :-, :*]
171-
@eval Base.$op(p::APL, A::AbstractArray{<:APL}) = map(f -> $op(p, f), A)
172-
@eval Base.$op(A::AbstractArray{<:APL}, p::APL) = map(f -> $op(f, p), A)
173-
end
174-
Base.:/(A::AbstractArray{<:APL}, p::APL) = map(f -> f / p, A)
175-
176179
function mul_to_terms!(ts::Vector{<:AbstractTerm}, p1::APL, p2::APL)
177180
for t1 in terms(p1)
178181
for t2 in terms(p2)
@@ -188,8 +191,12 @@ end
188191
Base.isapprox(p::APL, α; kwargs...) = isapprox(promote(p, α)...; kwargs...)
189192
Base.isapprox(α, p::APL; kwargs...) = isapprox(promote(p, α)...; kwargs...)
190193

191-
Base.:-(m::AbstractMonomialLike) = (-1) * m
192-
Base.:-(t::AbstractTermLike) = (-coefficient(t)) * monomial(t)
194+
# `MA.operate(-, p)` redirects to `-p` as it assumes that `-p` can be modified
195+
# through the MA API without modifying `p`. We should either copy the monomial
196+
# here or implement a `MA.operate(-, p)` that copies it. We choose the first
197+
# option.
198+
Base.:-(m::AbstractMonomialLike) = (-1) * MA.copy_if_mutable(m)
199+
Base.:-(t::AbstractTermLike) = MA.operate(-, coefficient(t)) * MA.copy_if_mutable(monomial(t))
193200
Base.:-(p::APL) = polynomial!((-).(terms(p)))
194201
Base.:+(p::Union{APL, RationalPoly}) = p
195202
Base.:*(p::Union{APL, RationalPoly}) = p
@@ -206,6 +213,12 @@ function MA.mutable_operate!(::typeof(plusconstant), p::APL, α)
206213
end
207214
minusconstant(p::APL{S}, α::T) where {S, T} = iszero(α) ? polynomial( p, MA.promote_operation(-, S, T)) : p - constantterm(α, p)
208215
minusconstant::S, p::APL{T}) where {S, T} = iszero(α) ? polynomial(-p, MA.promote_operation(-, S, T)) : constantterm(α, p) - p
216+
function MA.mutable_operate!(::typeof(minusconstant), p::APL, α)
217+
if !iszero(α)
218+
MA.mutable_operate!(-, p, constantterm(α, p))
219+
end
220+
return p
221+
end
209222

210223
# Coefficients and variables commute
211224
multconstant(α, v::AbstractVariable) = multconstant(α, monomial(v)) # TODO linear term
@@ -325,18 +338,18 @@ Base.vec(vars::Tuple{Vararg{AbstractVariable}}) = [vars...]
325338
# https://github.com/JuliaLang/julia/pull/23332
326339
Base.:^(x::AbstractPolynomialLike, p::Integer) = Base.power_by_squaring(x, p)
327340

328-
function MA.mutable_operate_to!(output::AbstractPolynomial, ::typeof(MA.add_mul), x, args::Vararg{Any, N}) where N
329-
return MA.mutable_operate_to!(output, +, x, *(args...))
341+
function MA.mutable_operate_to!(output::AbstractPolynomial, op::MA.AddSubMul, x, args::Vararg{Any, N}) where N
342+
return MA.mutable_operate_to!(output, MA.add_sub_op(op), x, *(args...))
330343
end
331-
function MA.mutable_operate!(::typeof(MA.add_mul), x, y, z, args::Vararg{Any, N}) where N
332-
return MA.mutable_operate!(+, x, *(y, z, args...))
344+
function MA.mutable_operate!(op::MA.AddSubMul, x, y, z, args::Vararg{Any, N}) where N
345+
return MA.mutable_operate!(MA.add_sub_op(op), x, *(y, z, args...))
333346
end
334-
MA.buffer_for(::typeof(MA.add_mul), ::Type{<:AbstractPolynomial}, args::Vararg{Type, N}) where {N} = zero(MA.promote_operation(*, args...))
335-
function MA.mutable_buffered_operate_to!(buffer::AbstractPolynomial, output::AbstractPolynomial, ::typeof(MA.add_mul), x, y, z, args::Vararg{Any, N}) where N
347+
MA.buffer_for(::MA.AddSubMul, ::Type{<:AbstractPolynomial}, args::Vararg{Type, N}) where {N} = zero(MA.promote_operation(*, args...))
348+
function MA.mutable_buffered_operate_to!(buffer::AbstractPolynomial, output::AbstractPolynomial, op::MA.AddSubMul, x, y, z, args::Vararg{Any, N}) where N
336349
product = MA.operate_to!(buffer, *, y, z, args...)
337-
return MA.mutable_operate_to!(output, +, x, product)
350+
return MA.mutable_operate_to!(output, MA.add_sub_op(op), x, product)
338351
end
339-
function MA.mutable_buffered_operate!(buffer::AbstractPolynomial, ::typeof(MA.add_mul), x, y, z, args::Vararg{Any, N}) where N
352+
function MA.mutable_buffered_operate!(buffer::AbstractPolynomial, op::MA.AddSubMul, x, y, z, args::Vararg{Any, N}) where N
340353
product = MA.operate_to!(buffer, *, y, z, args...)
341-
return MA.mutable_operate!(+, x, product)
354+
return MA.mutable_operate!(MA.add_sub_op(op), x, product)
342355
end

test/show.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
@test sprint(show, -(1.0 + 3.1im) * z*x) == "(-1.0 - 3.1im)xz"
2626
@test sprint(show, x^2 + (1.0 + 3.1im) * x) == "x² + (1.0 + 3.1im)x"
2727
@test sprint(show, x^2 - (1.0 + 3.1im) * x) == "x² + (-1.0 - 3.1im)x"
28-
@test sprint(show, [1.0, 2.0] * x) == "([1.0, 2.0])x"
2928

3029
Mod.@polyvar x[0:9]
3130
@test sprint(show, sum(i*x[i]^i for i=1:10)) == "10x₉¹⁰ + 9x₈⁹ + 8x₇⁸ + 7x₆⁷ + 6x₅⁶ + 5x₄⁵ + 4x₃⁴ + 3x₂³ + 2x₁² + x₀"

0 commit comments

Comments
 (0)