Skip to content

Commit

Permalink
Fix copy of term (#303)
Browse files Browse the repository at this point in the history
* Fix copy of term

* Fix

* Fix

* Fix
  • Loading branch information
blegat authored Oct 9, 2024
1 parent 7570617 commit 9d3f0d3
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]
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

0 comments on commit 9d3f0d3

Please sign in to comment.