Skip to content

Commit 9d3f0d3

Browse files
authored
Fix copy of term (#303)
* Fix copy of term * Fix * Fix * Fix
1 parent 7570617 commit 9d3f0d3

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/default_term.jl

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ coefficient(t::Term) = t.coefficient
2525
monomial(t::Term) = t.monomial
2626
term_type(::Type{<:Term{C,M}}, ::Type{T}) where {C,M,T} = Term{T,M}
2727
monomial_type(::Type{<:Term{C,M}}) where {C,M} = M
28-
function Base.copy(t::Term)
29-
return Term(copy(t.coefficient), copy(t.monomial))
30-
end
3128

3229
(t::Term)(s...) = substitute(Eval(), t, s)
3330

@@ -84,6 +81,10 @@ function MA.mutability(::Type{Term{C,M}}) where {C,M}
8481
end
8582
end
8683

84+
# `Base.power_by_squaring` calls `Base.copy` and we want
85+
# `t^1` to be a mutable copy of `t` so `copy` needs to be
86+
# the same as `mutable_copy`.
87+
Base.copy(t::Term) = MA.mutable_copy(t)
8788
function MA.mutable_copy(t::Term)
8889
return Term(
8990
MA.copy_if_mutable(coefficient(t)),

test/substitution.jl

+10
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,14 @@ import MutableArithmetics as MA
9797
@test MA.promote_operation(substitute, Subs, typeof(p), typeof(s)) ==
9898
typeof(subs(p, s))
9999
end
100+
101+
@testset "Issue #302 $T" for T in [BigFloat, BigInt]
102+
Mod.@polyvar x[1:3]
103+
t = T(1) * x[1]
104+
@test copy(t).coefficient !== t.coefficient
105+
@test MA.mutable_copy(t).coefficient !== t.coefficient
106+
F = T(5) * x[1] * x[2] * x[3] + T(1) * x[1] * x[2]^2
107+
@test subs(F, x[3] => T(0)) == x[1] * x[2]^2
108+
@test subs(F, x[3] => 0) == x[1] * x[2]^2
109+
end
100110
end

0 commit comments

Comments
 (0)