From ab371612fe29e9482e002220f96150a382d8b4bc Mon Sep 17 00:00:00 2001 From: Michael Schneeberger Date: Tue, 27 Feb 2024 06:29:57 +0100 Subject: type check left argument of binary operation --- sumofsquares/sosexpr/mixins/sosexpropmixin.py | 33 +++++++++++++++------------ 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/sumofsquares/sosexpr/mixins/sosexpropmixin.py b/sumofsquares/sosexpr/mixins/sosexpropmixin.py index c410ea5..5d6e7d7 100644 --- a/sumofsquares/sosexpr/mixins/sosexpropmixin.py +++ b/sumofsquares/sosexpr/mixins/sosexpropmixin.py @@ -18,22 +18,25 @@ class SOSExprOPMixin(SOSExprMixin): right: typing.Union[polymatrix.Expression, 'SOSExprOPMixin'], ) -> 'SOSExprOPMixin': - # if not isinstance(left, SOSExprOPMixin): - # left = polymatrix.expression.from_.from_expr_or_none(left) + if not isinstance(left, SOSExprOPMixin): + if isinstance(left, tuple): + return NotImplemented + + left = polymatrix.expression.from_.from_expr_or_none(left) - # if left is None: - # return NotImplemented + if left is None: + return NotImplemented - # underlying = init_sos_expr_base( - # expr=op(polymatrix.from_(left), right.expr), - # variables=right.variables, - # dependence=right.dependence, - # ) + underlying = init_sos_expr_base( + expr=op(polymatrix.from_(left), right.expr), + variables=right.variables, + dependence=right.dependence, + ) - # return dataclasses.replace( - # right, - # underlying=underlying, - # ) + return dataclasses.replace( + right, + underlying=underlying, + ) if not isinstance(right, SOSExprOPMixin): if isinstance(right, tuple): @@ -92,13 +95,13 @@ class SOSExprOPMixin(SOSExprMixin): return self._unary(polymatrix.Expression.__neg__, self) def __radd__(self, other): - return self._binary(polymatrix.Expression.__add__, other, self) + return self._binary(polymatrix.Expression.__add__, self, other) def __rmatmul__(self, other: typing.Union[polymatrix.Expression, 'SOSExprOPMixin']) -> 'SOSExprOPMixin': return self._binary(polymatrix.Expression.__matmul__, other, self) def __rmul__(self, other): - return self._binary(polymatrix.Expression.__mul__, other, self) + return self._binary(polymatrix.Expression.__mul__, self, other) def __rsub__(self, other: typing.Union[polymatrix.Expression, 'SOSExprOPMixin']) -> 'SOSExprOPMixin': return self._binary(polymatrix.Expression.__sub__, other, self) -- cgit v1.2.1