From b4ffdbafa8d61aae2806e286ebdde63821f23092 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Sat, 4 May 2024 17:54:27 +0200 Subject: Fix Expression._binary, avoid creating unnecessary nodes --- polymatrix/expression/expression.py | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/polymatrix/expression/expression.py b/polymatrix/expression/expression.py index edb0830..3c52297 100644 --- a/polymatrix/expression/expression.py +++ b/polymatrix/expression/expression.py @@ -117,31 +117,25 @@ class Expression(ExpressionBaseMixin, ABC): def _binary(op, left, right): stack = get_stack_lines() - if isinstance(left, Expression): - right = polymatrix.expression.init.init_from_expr_or_none(right) + if isinstance(left, Expression) and isinstance(right, Expression): + return left.copy(underlying=op(left.underlying, right.underlying, stack)) - # delegate to upper level + elif isinstance(left, Expression): + right = polymatrix.expression.init.init_from_expr_or_none(right) - # NP: what is upper level? Class inherits from ABC and base class, - # NP: neither has binary operators, also NotImplemented IIRC is only for __lt__ - # NP: and other comparison methods and not for overloadings like __add__ if right is None: return NotImplemented - return left.copy( - underlying=op(left, right, stack), - ) + return left.copy(underlying=op(left.underlying, right, stack)) + # else right is an Expression else: left = polymatrix.expression.init.init_from_expr_or_none(left) - # delegate to upper level if left is None: return NotImplemented - return right.copy( - underlying=op(left, right, stack), - ) + return right.copy( underlying=op(left, right.underlying, stack)) def cache(self) -> "Expression": return self.copy( -- cgit v1.2.1