From 9d3f0d38deb2fb947f88c8a03ceec101af8675da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 9 Oct 2024 17:07:50 +0200 Subject: [PATCH] Fix copy of term (#303) * Fix copy of term * Fix * Fix * Fix --- src/default_term.jl | 7 ++++--- test/substitution.jl | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/default_term.jl b/src/default_term.jl index 7a91d5ad..5e7b7990 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 79ccb55d..dbc04921 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] + Mod.@polyvar x[1:3] + t = T(1) * x[1] + @test copy(t).coefficient !== t.coefficient + @test MA.mutable_copy(t).coefficient !== t.coefficient + F = T(5) * x[1] * x[2] * x[3] + T(1) * x[1] * x[2]^2 + @test subs(F, x[3] => T(0)) == x[1] * x[2]^2 + @test subs(F, x[3] => 0) == x[1] * x[2]^2 + end end