@@ -83,14 +83,20 @@ function MA.mutable_operate!(op::Union{typeof(+), typeof(-)}, p::Polynomial{true
83
83
if _vars (p) != _vars (q)
84
84
varsvec = [_vars (p), _vars (q)]
85
85
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
94
100
return MA. mutable_operate! (op, p, rhs)
95
101
end
96
102
get1 (i) = (p. a[i], p. x. Z[i])
0 commit comments