@@ -212,6 +212,9 @@ cdef class Variable(object):
212212 """
213213 return Linear_Expression(self ) + Linear_Expression(other)
214214
215+ def __radd__ (self , other ):
216+ return Linear_Expression(self ) + Linear_Expression(other)
217+
215218 def __sub__ (self , other ):
216219 r """
217220 Return the difference ``self`` - ``other``.
@@ -235,7 +238,10 @@ cdef class Variable(object):
235238 >>> 15 - y
236239 -x1+ 15
237240 """
238- return Linear_Expression(self )- Linear_Expression(other)
241+ return Linear_Expression(self ) - Linear_Expression(other)
242+
243+ def __rsub__ (self , other ):
244+ return Linear_Expression(other) - Linear_Expression(self )
239245
240246 def __mul__ (self , other ):
241247 r """
@@ -259,10 +265,10 @@ cdef class Variable(object):
259265 >>> 15 * y
260266 15* x1
261267 """
262- if isinstance (self , Variable):
263- return Linear_Expression( self ) * other
264- else :
265- return Linear_Expression(other ) * self
268+ return Linear_Expression (self ) * other
269+
270+ def __rmul__ ( self , other ) :
271+ return Linear_Expression(self ) * other
266272
267273 def __pos__ (self ):
268274 r """
@@ -1043,13 +1049,30 @@ cdef class Linear_Expression(object):
10431049 >>> from gmpy2 import mpz
10441050 >>> mpz( 3) + x + mpz( 5) + y + mpz( 7)
10451051 x0+ x1+ 15
1052+ >>> from gmpy2 import mpz
1053+ >>> x + mpz( 5)
1054+ x0+ 5
10461055 """
10471056 cdef Linear_Expression lhs, rhs
10481057
1049- if isinstance (self , Linear_Expression):
1050- lhs = < Linear_Expression> self
1058+ lhs = < Linear_Expression> self
1059+
1060+ if isinstance (other, Linear_Expression):
1061+ rhs = < Linear_Expression> other
10511062 else :
1052- lhs = Linear_Expression(self )
1063+ rhs = Linear_Expression(other)
1064+
1065+ cdef Linear_Expression result = Linear_Expression()
1066+ result.thisptr[0 ] = lhs.thisptr[0 ] + rhs.thisptr[0 ]
1067+ return result
1068+
1069+ def __radd__ (self , other ):
1070+ r """
1071+ Add ``self`` and ``other``.
1072+ """
1073+ cdef Linear_Expression lhs, rhs
1074+
1075+ lhs = < Linear_Expression> self
10531076
10541077 if isinstance (other, Linear_Expression):
10551078 rhs = < Linear_Expression> other
@@ -1138,12 +1161,22 @@ cdef class Linear_Expression(object):
11381161 """
11391162 cdef Linear_Expression e
11401163 cdef c
1141- if isinstance (self , Linear_Expression):
1142- e = < Linear_Expression> self
1143- c = other
1144- else :
1145- e = < Linear_Expression> other
1146- c = self
1164+ e = < Linear_Expression> self
1165+ c = other
1166+
1167+ cdef PPL_Coefficient cc = PPL_Coefficient_from_pyobject(c)
1168+ cdef Linear_Expression result = Linear_Expression()
1169+ result.thisptr[0 ] = e.thisptr[0 ] * cc
1170+ return result
1171+
1172+ def __rmul__ (self , other ):
1173+ r """
1174+ Multiply ``self`` with ``other``
1175+ """
1176+ cdef Linear_Expression e
1177+ cdef c
1178+ e = < Linear_Expression> self
1179+ c = other
11471180
11481181 cdef PPL_Coefficient cc = PPL_Coefficient_from_pyobject(c)
11491182 cdef Linear_Expression result = Linear_Expression()
0 commit comments