Skip to content

Commit 71172d3

Browse files
committed
Implement MutableArithmetics
1 parent 361c8d3 commit 71172d3

File tree

6 files changed

+34
-1
lines changed

6 files changed

+34
-1
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,16 @@ version = "0.3.2"
66

77
[deps]
88
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
9+
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
910

1011
[compat]
1112
julia = "1"
1213

1314
[extras]
1415
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
1516
DynamicPolynomials = "7c1d4256-1411-5781-91ec-d7bc3513ac07"
16-
TypedPolynomials = "afbbf031-7a57-5f58-a1b9-b774a0fad08d"
1717
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
18+
TypedPolynomials = "afbbf031-7a57-5f58-a1b9-b774a0fad08d"
1819

1920
[targets]
2021
test = ["BenchmarkTools", "DynamicPolynomials", "Test", "TypedPolynomials"]

src/MultivariatePolynomials.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ module MultivariatePolynomials
22

33
import LinearAlgebra
44

5+
import MutableArithmetics
6+
const MA = MutableArithmetics
7+
58
export AbstractPolynomialLike, AbstractTermLike, AbstractMonomialLike
69
"""
710
AbstractPolynomialLike{T}

src/monomial.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ mapexponents(f, m1::AbstractMonomialLike, m2::AbstractMonomialLike) = mapexponen
124124

125125
Base.one(::Type{TT}) where {TT<:AbstractMonomialLike} = constantmonomial(TT)
126126
Base.one(t::AbstractMonomialLike) = constantmonomial(t)
127+
MA.promote_operation(::typeof(one), MT::Type{<:AbstractMonomialLike}) = monomialtype(MT)
127128
# See https://github.com/JuliaAlgebra/MultivariatePolynomials.jl/issues/82
128129
# By default, Base do oneunit(v::VT) = VT(one(v)).
129130
# This tries to convert a monomial to a variable which does not work.

src/operators.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ for (op, fun) in [(:+, :plusconstant), (:-, :minusconstant), (:*, :multconstant)
2828
@eval Base.$op(α, p::APL) = $fun(α, p)
2929
end
3030

31+
MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::AbstractPolynomial, q::AbstractPolynomial) = MA.mutable_operate_to!(p, op, p, q)
32+
MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::AbstractPolynomial, q::AbstractPolynomialLike) = MA.mutable_operate!(op, p, polynomial(q))
33+
3134
# Special case AbstractArrays of APLs
3235
# We add these instead of relying on the broadcasting API since the above method definitions are very wide.
3336
# In particular, there is support for Matrices as coefficents. In order to avoid issues like #104 we therefore

src/promote.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,25 @@ Base.promote_rule(::Type{RT}, ::Type{PT}) where {PT<:APL, RT<:RationalPoly} = pr
1919
#promote_rule(::Type{RationalPoly{C, S, T}}, ::Type{Term{C, U}}) where {C, S, T, U} = RationalPoly{C, promote_type(U, S), T}
2020
#promote_rule(::Type{Polynomial{C, U}}, ::Type{RationalPoly{C, S, T}}) where {C, S, T, U} = RationalPoly{C, promote_type(U, S), T}
2121
#promote_rule(::Type{RationalPoly{C, S, T}}, ::Type{Polynomial{C, U}}) where {C, S, T, U} = RationalPoly{C, promote_type(U, S), T}
22+
23+
function MA.promote_operation(
24+
op::Union{typeof(+), typeof(-)}, PT::Type{<:APL{S}},
25+
::Type{<:APL{T}}) where {S, T}
26+
27+
U = MA.promote_operation(op, S, T)
28+
return polynomialtype(PT, U)
29+
end
30+
function MA.promote_operation(::typeof(*), MT::Type{<:AbstractMonomialLike},
31+
::Type{<:AbstractMonomialLike})
32+
return monomialtype(MT)
33+
end
34+
function MA.promote_operation(::typeof(*), TT::Type{<:AbstractTermLike{S}},
35+
::Type{<:AbstractTermLike{T}}) where {S, T}
36+
U = MA.promote_operation(*, S, T)
37+
return termtype(TT, U)
38+
end
39+
function MA.promote_operation(::typeof(*), PT::Type{<:APL{S}},
40+
::Type{<:APL{T}}) where {S, T}
41+
U = MA.promote_operation(MA.add_mul, S, T)
42+
return polynomialtype(PT, U)
43+
end

src/term.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,5 +158,8 @@ zeroterm(p::APL{T}) where {T} = constantterm(zero(T), p)
158158

159159
Base.zero(::Type{TT}) where {T, TT<:AbstractTermLike{T}} = zero(polynomialtype(TT))
160160
Base.zero(t::AbstractTermLike{T}) where {T} = zero(polynomialtype(t))
161+
MA.promote_operation(::typeof(zero), PT::Type{<:AbstractTermLike}) = polynomialtype(PT)
161162
Base.one(::Type{TT}) where {T, TT<:AbstractTermLike{T}} = one(T) * constantmonomial(TT)
162163
Base.one(t::AbstractTermLike{T}) where {T} = one(T) * constantmonomial(t)
164+
MA.promote_operation(::typeof(one), TT::Type{<:AbstractTermLike}) = termtype(TT)
165+
MA.promote_operation(::typeof(one), PT::Type{<:AbstractPolynomialLike}) = polynomialtype(PT)

0 commit comments

Comments
 (0)