Skip to content

Commit 8c9d155

Browse files
committed
Speed up via avoid copying dict itself
Implements the __iadd__ method for PolynomialExpr, allowing in-place addition of polynomial expressions by updating child coefficients. Falls back to superclass behavior for non-polynomial operands.
1 parent a0f3f57 commit 8c9d155

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

src/pyscipopt/expr.pxi

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,14 @@ class PolynomialExpr(Expr):
275275
return PolynomialExpr.to_subclass(self.to_dict(other.children))
276276
return super().__add__(other)
277277

278+
def __iadd__(self, other):
279+
other = Expr.from_const_or_var(other)
280+
if isinstance(other, PolynomialExpr):
281+
for child, coef in other.children.items():
282+
self.children[child] = self.children.get(child, 0.0) + coef
283+
return self
284+
return super().__iadd__(other)
285+
278286
def __mul__(self, other):
279287
other = Expr.from_const_or_var(other)
280288
if isinstance(other, PolynomialExpr):

0 commit comments

Comments
 (0)