Skip to content

Commit 9100d52

Browse files
committed
Refactor normalization in ProdExpr and PowExpr
Replaces direct zero/one constant checks with calls to the _normalize() method in ProdExpr and PowExpr arithmetic operations. This centralizes normalization logic and improves code maintainability.
1 parent 7e8cd99 commit 9100d52

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

src/pyscipopt/expr.pxi

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -564,37 +564,37 @@ cdef class ProdExpr(FuncExpr):
564564
if self._is_child_equal(_other):
565565
res = <ProdExpr>Expr._copy(self, ProdExpr, copy=True)
566566
res.coef += (<ProdExpr>_other).coef
567-
return ConstExpr(0.0) if res.coef == 0 else res
567+
return res._normalize()
568568
return super().__add__(_other)
569569

570570
def __iadd__(self, other: Union[Number, Variable, Expr]) -> Expr:
571571
cdef Expr _other = Expr._from_other(other)
572572
if self._is_child_equal(_other):
573573
self.coef += (<ProdExpr>_other).coef
574-
return ConstExpr(0.0) if self.coef == 0 else self
574+
return self._normalize()
575575
return super().__iadd__(_other)
576576

577577
def __mul__(self, other: Union[Number, Variable, Expr]) -> Expr:
578578
cdef Expr _other = Expr._from_other(other)
579579
if Expr._is_const(_other):
580580
res = <ProdExpr>Expr._copy(self, ProdExpr, copy=True)
581581
res.coef *= _other[CONST]
582-
return ConstExpr(0.0) if res.coef == 0 else res
582+
return res._normalize()
583583
return super().__mul__(_other)
584584

585585
def __imul__(self, other: Union[Number, Variable, Expr]) -> Expr:
586586
cdef Expr _other = Expr._from_other(other)
587587
if Expr._is_const(_other):
588588
self.coef *= _other[CONST]
589-
return ConstExpr(0.0) if self.coef == 0 else self
589+
return self._normalize()
590590
return super().__imul__(_other)
591591

592592
def __truediv__(self, other: Union[Number, Variable, Expr]) -> Expr:
593593
cdef Expr _other = Expr._from_other(other)
594594
if Expr._is_const(_other):
595595
res = <ProdExpr>Expr._copy(self, ProdExpr, copy=True)
596596
res.coef /= _other[CONST]
597-
return ConstExpr(0.0) if res.coef == 0 else res
597+
return res._normalize()
598598
return super().__truediv__(_other)
599599

600600
def __richcmp__(self, other: Union[Number, Variable, Expr], int op) -> ExprCons:
@@ -632,22 +632,22 @@ cdef class PowExpr(FuncExpr):
632632
if self._is_child_equal(_other):
633633
res = <PowExpr>Expr._copy(self, PowExpr, copy=True)
634634
res.expo += (<PowExpr>_other).expo
635-
return ConstExpr(1.0) if res.expo == 0 else res
635+
return res._normalize()
636636
return super().__mul__(_other)
637637

638638
def __imul__(self, other: Union[Number, Variable, Expr]) -> Expr:
639639
cdef Expr _other = Expr._from_other(other)
640640
if self._is_child_equal(_other):
641641
self.expo += (<PowExpr>_other).expo
642-
return ConstExpr(1.0) if self.expo == 0 else self
642+
return self._normalize()
643643
return super().__imul__(_other)
644644

645645
def __truediv__(self, other: Union[Number, Variable, Expr]) -> Expr:
646646
cdef Expr _other = Expr._from_other(other)
647647
if self._is_child_equal(_other):
648648
res = <PowExpr>Expr._copy(self, PowExpr, copy=True)
649649
res.expo -= (<PowExpr>_other).expo
650-
return ConstExpr(1.0) if res.expo == 0 else res
650+
return res._normalize()
651651
return super().__truediv__(_other)
652652

653653
def __richcmp__(self, other: Union[Number, Variable, Expr], int op) -> ExprCons:
@@ -657,7 +657,7 @@ cdef class PowExpr(FuncExpr):
657657
return f"PowExpr({self._fchild()}, {self.expo})"
658658

659659
def _normalize(self) -> Expr:
660-
if self.expo == 0:
660+
if not self or self.expo == 0:
661661
return ConstExpr(1.0)
662662
elif self.expo == 1:
663663
return (

0 commit comments

Comments
 (0)