Skip to content

Commit dff23d9

Browse files
authored
Merge pull request #54 from JuliaAlgebra/bl/copy
Copy internal vectors instead of giving the same instance
2 parents 8301aba + ebf9a1d commit dff23d9

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1515

1616
[compat]
1717
DataStructures = "0.17"
18-
MultivariatePolynomials = "~0.3.3"
18+
MultivariatePolynomials = "0.3.4"
1919
MutableArithmetics = "0.1.1"
2020
Reexport = "0.2"
2121
julia = "1"

src/cmult.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ function multdivmono!(output_variables::Vector{PolyVar{true}},
5757
end
5858
function multdivmono(v::Vector{PolyVar{true}}, x::Monomial{true}, op)
5959
if v == x.vars
60-
# /!\ no copy done here for efficiency, do not mess up with vars
61-
w = v
60+
w = copy(v)
6261
updatez = z -> op.(z, x.z)
6362
else
6463
w, maps = mergevars([v, x.vars])

src/mult.jl

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ function multiplyexistingvar(v::Vector{PolyVar{C}}, x::PolyVar{C}, i::Int) where
44
newz[i] += 1
55
newz
66
end
7-
# /!\ v not copied for efficiency, do not mess up with vars
8-
v, updatez
7+
copy(v), updatez
98
end
109
function insertvar(v::Vector{PolyVar{C}}, x::PolyVar{C}, i::Int) where {C}
1110
n = length(v)
@@ -29,8 +28,8 @@ end
2928
include("cmult.jl")
3029
include("ncmult.jl")
3130

32-
MP.multconstant(α, x::Monomial) = Term(α, x)
33-
MP.mapcoefficientsnz(f::Function, p::Polynomial) = Polynomial(f.(p.a), p.x)
31+
MP.multconstant(α, x::Monomial) = Term(α, MA.mutable_copy(x))
32+
MP.mapcoefficientsnz(f::Function, p::Polynomial) = Polynomial(map(f, p.a), MA.mutable_copy(p.x))
3433
function MP.mapcoefficientsnz_to!(output::Polynomial, f::Function, t::MP.AbstractTermLike)
3534
MP.mapcoefficientsnz_to!(output, f, polynomial(t))
3635
end
@@ -48,17 +47,14 @@ end
4847

4948
# I do not want to cast x to TermContainer because that would force the promotion of eltype(q) with Int
5049
function Base.:(*)(x::DMonomialLike, p::Polynomial)
51-
# /!\ No copy of a is done
52-
Polynomial(p.a, x*p.x)
50+
Polynomial(MA.mutable_copy(p.a), x*p.x)
5351
end
5452
function Base.:(*)(x::DMonomialLike{false}, p::Polynomial)
55-
# /!\ No copy of a is done
5653
# Order may change, e.g. y * (x + y) = y^2 + yx
57-
Polynomial(monovec(p.a, [x*m for m in p.x])...)
54+
Polynomial(monovec(MA.mutable_copy(p.a), [x*m for m in p.x])...)
5855
end
5956
function Base.:(*)(p::Polynomial, x::DMonomialLike)
60-
# /!\ No copy of a is done
61-
Polynomial(p.a, p.x*x)
57+
Polynomial(MA.mutable_copy(p.a), p.x*x)
6258
end
6359

6460
function _term_poly_mult(t::Term{C, S}, p::Polynomial{C, T}, op::Function) where {C, S, T}
@@ -87,7 +83,7 @@ end
8783
function _mul(::Type{T}, p::Polynomial{true}, q::Polynomial{true}) where T
8884
samevars = _vars(p) == _vars(q)
8985
if samevars
90-
allvars = _vars(p)
86+
allvars = copy(_vars(p))
9187
else
9288
allvars, maps = mergevars([_vars(p), _vars(q)])
9389
end

src/operators.jl

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ end
1515
Base.:(+)(x::DMonomialLike, y::DMonomialLike) = Term(x) + Term(y)
1616
Base.:(-)(x::DMonomialLike, y::DMonomialLike) = Term(x) - Term(y)
1717

18-
# `+(::UniformScaling)` is not defined...
19-
_unary(::typeof(+), x) = MA.copy_if_mutable(x)
20-
_unary(::typeof(-), x) = -x
21-
2218
_getindex(p::Polynomial, i) = p[i]
2319
_getindex(t::Term, i) = t
2420
function _plusorminus_to!(a::Vector{U}, Z::Vector{Vector{Int}}, op::Function, p::TermPoly{C}, q::TermPoly{C}, maps, nvars) where {C, U}
@@ -72,6 +68,11 @@ function MA.mutable_operate_to!(output::Polynomial{C}, op::Union{typeof(+), type
7268
return output
7369
end
7470

71+
# TODO replace by MA.operate with MA v0.2
72+
# `+(::UniformScaling)` is not defined...
73+
_unary(::typeof(+), x) = MA.copy_if_mutable(x)
74+
_unary(::typeof(-), x) = -x
75+
7576
function MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::Polynomial,
7677
q::Union{PolyVar, Monomial, Term})
7778
return MA.mutable_operate!(op, p, polynomial(q))
@@ -98,7 +99,7 @@ function MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::Polynomial{true
9899
return MA.mutable_operate!(op, p, rhs)
99100
end
100101
get1(i) = (p.a[i], p.x.Z[i])
101-
get2(i) = (MA.scaling_convert(eltype(p.a), MA.copy_if_mutable(q.a[i])), copy(q.x.Z[i]))
102+
get2(i) = (MA.scaling_convert(eltype(p.a), _unary(op, q.a[i])), copy(q.x.Z[i]))
102103
function set(i, t::_NoVarTerm)
103104
p.a[i] = t[1]
104105
p.x.Z[i] = t[2]
@@ -133,6 +134,11 @@ Base.:(+)(x::Union{Monomial,PolyVar}, y::TermPoly{C}) where C = Term{C}(x) + y
133134
Base.:(-)(x::TermPoly{T}, y::DMonomialLike) where T = x - Term{T}(y)
134135
Base.:(-)(x::DMonomialLike, y::TermPoly{T}) where T = Term{T}(x) - y
135136

136-
Base.:(-)(p::Polynomial) = Polynomial(-p.a, p.x)
137+
# `MA.operate(-, p)` redirects to `-p` as it assumes that `-p` can be modified
138+
# through the MA API without modifying `p`. We should either copy `p.x` here
139+
# or implement a `MA.operate(-, p)` that copies it. We choose the first option.
140+
Base.:(-)(p::Polynomial) = Polynomial(-p.a, copy(p.x))
141+
# TODO use that with MA v0.2
142+
#Base.:(-)(p::Polynomial) = Polynomial(map(α -> MA.operate(-, α), p.a), copy(p.x))
137143

138144
include("mult.jl")

0 commit comments

Comments
 (0)