@@ -488,11 +488,14 @@ def __add__(self, other: SideLike) -> LinearExpression:
488488 Note: If other is a numpy array or pandas object without axes names,
489489 dimension names of self will be filled in other
490490 """
491- if np .isscalar (other ):
492- return self .assign (const = self .const + other )
491+ try :
492+ if np .isscalar (other ):
493+ return self .assign (const = self .const + other )
493494
494- other = as_expression (other , model = self .model , dims = self .coord_dims )
495- return merge ([self , other ], cls = self .__class__ )
495+ other = as_expression (other , model = self .model , dims = self .coord_dims )
496+ return merge ([self , other ], cls = self .__class__ )
497+ except TypeError :
498+ return NotImplemented
496499
497500 def __radd__ (self , other : int ) -> LinearExpression | NotImplementedType :
498501 # This is needed for using python's sum function
@@ -505,11 +508,14 @@ def __sub__(self, other: SideLike) -> LinearExpression:
505508 Note: If other is a numpy array or pandas object without axes names,
506509 dimension names of self will be filled in other
507510 """
508- if np .isscalar (other ):
509- return self .assign_multiindex_safe (const = self .const - other )
511+ try :
512+ if np .isscalar (other ):
513+ return self .assign_multiindex_safe (const = self .const - other )
510514
511- other = as_expression (other , model = self .model , dims = self .coord_dims )
512- return merge ([self , - other ], cls = self .__class__ )
515+ other = as_expression (other , model = self .model , dims = self .coord_dims )
516+ return merge ([self , - other ], cls = self .__class__ )
517+ except TypeError :
518+ return NotImplemented
513519
514520 def __neg__ (self ) -> LinearExpression | QuadraticExpression :
515521 """
@@ -524,19 +530,22 @@ def __mul__(
524530 """
525531 Multiply the expr by a factor.
526532 """
527- if isinstance (other , QuadraticExpression ):
528- raise TypeError (
529- "unsupported operand type(s) for *: "
530- f"{ type (self )} and { type (other )} . "
531- "Higher order non-linear expressions are not yet supported."
532- )
533- elif isinstance (other , (variables .Variable , variables .ScalarVariable )):
534- other = other .to_linexpr ()
533+ try :
534+ if isinstance (other , QuadraticExpression ):
535+ raise TypeError (
536+ "unsupported operand type(s) for *: "
537+ f"{ type (self )} and { type (other )} . "
538+ "Higher order non-linear expressions are not yet supported."
539+ )
540+ elif isinstance (other , (variables .Variable , variables .ScalarVariable )):
541+ other = other .to_linexpr ()
535542
536- if isinstance (other , (LinearExpression , ScalarLinearExpression )):
537- return self ._multiply_by_linear_expression (other )
538- else :
539- return self ._multiply_by_constant (other )
543+ if isinstance (other , (LinearExpression , ScalarLinearExpression )):
544+ return self ._multiply_by_linear_expression (other )
545+ else :
546+ return self ._multiply_by_constant (other )
547+ except TypeError :
548+ return NotImplemented
540549
541550 def _multiply_by_linear_expression (
542551 self , other : LinearExpression | ScalarLinearExpression
@@ -599,15 +608,18 @@ def __matmul__(
599608 def __div__ (
600609 self , other : Variable | ConstantLike
601610 ) -> LinearExpression | QuadraticExpression :
602- if isinstance (
603- other , (LinearExpression , variables .Variable , variables .ScalarVariable )
604- ):
605- raise TypeError (
606- "unsupported operand type(s) for /: "
607- f"{ type (self )} and { type (other )} "
608- "Non-linear expressions are not yet supported."
609- )
610- return self .__mul__ (1 / other )
611+ try :
612+ if isinstance (
613+ other , (LinearExpression , variables .Variable , variables .ScalarVariable )
614+ ):
615+ raise TypeError (
616+ "unsupported operand type(s) for /: "
617+ f"{ type (self )} and { type (other )} "
618+ "Non-linear expressions are not yet supported."
619+ )
620+ return self .__mul__ (1 / other )
621+ except TypeError :
622+ return NotImplemented
611623
612624 def __truediv__ (
613625 self , other : Variable | ConstantLike
@@ -1557,13 +1569,17 @@ def __add__(
15571569 Note: If other is a numpy array or pandas object without axes names,
15581570 dimension names of self will be filled in other
15591571 """
1560- if np .isscalar (other ):
1561- return self .assign (const = self .const + other )
1572+ try :
1573+ if np .isscalar (other ):
1574+ return self .assign (const = self .const + other )
15621575
1563- other = as_expression (other , model = self .model , dims = self .coord_dims )
1564- if type (other ) is LinearExpression :
1565- other = other .to_quadexpr ()
1566- return merge ([self , other ], cls = self .__class__ ) # type: ignore
1576+ other = as_expression (other , model = self .model , dims = self .coord_dims )
1577+
1578+ if type (other ) is LinearExpression :
1579+ other = other .to_quadexpr ()
1580+ return merge ([self , other ], cls = self .__class__ ) # type: ignore
1581+ except TypeError :
1582+ return NotImplemented
15671583
15681584 def __radd__ (
15691585 self , other : LinearExpression | int
@@ -1586,13 +1602,16 @@ def __sub__(self, other: SideLike | QuadraticExpression) -> QuadraticExpression:
15861602 Note: If other is a numpy array or pandas object without axes names,
15871603 dimension names of self will be filled in other
15881604 """
1589- if np .isscalar (other ):
1590- return self .assign (const = self .const - other )
1591-
1592- other = as_expression (other , model = self .model , dims = self .coord_dims )
1593- if type (other ) is LinearExpression :
1594- other = other .to_quadexpr ()
1595- return merge ([self , - other ], cls = self .__class__ ) # type: ignore
1605+ try :
1606+ if np .isscalar (other ):
1607+ return self .assign (const = self .const - other )
1608+
1609+ other = as_expression (other , model = self .model , dims = self .coord_dims )
1610+ if type (other ) is LinearExpression :
1611+ other = other .to_quadexpr ()
1612+ return merge ([self , - other ], cls = self .__class__ ) # type: ignore
1613+ except TypeError :
1614+ return NotImplemented
15961615
15971616 def __rsub__ (self , other : LinearExpression ) -> QuadraticExpression :
15981617 """
0 commit comments