Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading