Skip to content

Commit

Permalink
Fix copy of term
Browse files Browse the repository at this point in the history
  • Loading branch information
blegat committed Oct 3, 2024
1 parent 7570617 commit 2e02a3f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/default_term.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)),
Expand Down
10 changes: 10 additions & 0 deletions test/substitution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 2e02a3f

Please sign in to comment.