diff --git a/src/default_term.jl b/src/default_term.jl index 7a91d5a..5e7b799 100644 --- a/src/default_term.jl +++ b/src/default_term.jl @@ -25,9 +25,6 @@ coefficient(t::Term) = t.coefficient monomial(t::Term) = t.monomial term_type(::Type{<:Term{C,M}}, ::Type{T}) where {C,M,T} = Term{T,M} monomial_type(::Type{<:Term{C,M}}) where {C,M} = M -function Base.copy(t::Term) - return Term(copy(t.coefficient), copy(t.monomial)) -end (t::Term)(s...) = substitute(Eval(), t, s) @@ -84,6 +81,10 @@ function MA.mutability(::Type{Term{C,M}}) where {C,M} end end +# `Base.power_by_squaring` calls `Base.copy` and we want +# `t^1` to be a mutable copy of `t` so `copy` needs to be +# the same as `mutable_copy`. +Base.copy(t::Term) = MA.mutable_copy(t) function MA.mutable_copy(t::Term) return Term( MA.copy_if_mutable(coefficient(t)), diff --git a/test/substitution.jl b/test/substitution.jl index 79ccb55..452ddf5 100644 --- a/test/substitution.jl +++ b/test/substitution.jl @@ -97,4 +97,14 @@ import MutableArithmetics as MA @test MA.promote_operation(substitute, Subs, typeof(p), typeof(s)) == typeof(subs(p, s)) end + + @testset "Issue #302 $T" for T in [BigFloat, BigInt] + t = T(1) * x[1] + @test copy(t).coefficient !== t.coefficient + @test mutable_copy(t).coefficient !== t.coefficient + @test copy_if_mutable(t).coefficient !== t.coefficient + F = T(5) * x[1] * x[2] * x[3] + T(1) * x[1] * x[2]^2 + @test subs(F, c => T(0)) == a * b^2 + @test subs(F, c => 0) == a * b^2 + end end