Skip to content

Commit 1df9935

Browse files
committed
Only call _add_variables if needed
1 parent 7fdae0a commit 1df9935

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/operators.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,20 @@ function MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::Polynomial{true
8383
if _vars(p) != _vars(q)
8484
varsvec = [_vars(p), _vars(q)]
8585
allvars, maps = mergevars(varsvec)
86-
_add_variables!(p.x, allvars, maps[1])
87-
# We could avoid promoting `q` to the same variables
88-
# like in `plusorminus` to avoid extra allocation but it then
89-
# gives slower comparison. There is a tradeoff and the approach used here
90-
# should be better of `q` has less terms and then the same term is compared
91-
# many times.
92-
rhs = Polynomial(q.a, copy(q.x))
93-
_add_variables!(rhs.x, allvars, maps[2])
86+
if length(allvars) != length(_vars(p))
87+
_add_variables!(p.x, allvars, maps[1])
88+
end
89+
if length(allvars) == length(_vars(q))
90+
rhs = q
91+
else
92+
# We could avoid promoting `q` to the same variables
93+
# like in `plusorminus` to avoid extra allocation but it then
94+
# gives slower comparison. There is a tradeoff and the approach used here
95+
# should be better of `q` has less terms and then the same term is compared
96+
# many times.
97+
rhs = Polynomial(q.a, copy(q.x))
98+
_add_variables!(rhs.x, allvars, maps[2])
99+
end
94100
return MA.mutable_operate!(op, p, rhs)
95101
end
96102
get1(i) = (p.a[i], p.x.Z[i])

0 commit comments

Comments
 (0)