@@ -284,7 +284,11 @@ cdef class Variable(object):
284284 ...
285285 TypeError: ppl coefficients must be integral
286286 """
287- return Linear_Expression(self ) * other
287+ if isinstance (self , Variable):
288+ return Linear_Expression(self ) * other
289+ else :
290+ # NOTE: this code path will only be executed when compiled with cython < 3.0.0
291+ return Linear_Expression(other) * self
288292
289293 def __rmul__ (self , other ):
290294 return Linear_Expression(self ) * other
@@ -641,11 +645,12 @@ cdef class Linear_Expression(object):
641645 a = args[0 ]
642646 b = args[1 ]
643647 self .thisptr = new PPL_Linear_Expression()
644- if isinstance (a, dict ) and a:
645- self .thisptr.set_space_dimension(1 + max (a))
646- for i, coeff in a.items():
647- self .thisptr.set_coefficient(PPL_Variable(i), PPL_Coefficient_from_pyobject(coeff))
648- elif a:
648+ if isinstance (a, dict ):
649+ if a:
650+ self .thisptr.set_space_dimension(1 + max (a))
651+ for i, coeff in a.items():
652+ self .thisptr.set_coefficient(PPL_Variable(i), PPL_Coefficient_from_pyobject(coeff))
653+ else :
649654 self .thisptr.set_space_dimension(len (a))
650655 for i, coeff in enumerate (a):
651656 self .thisptr.set_coefficient(PPL_Variable(i), PPL_Coefficient_from_pyobject(coeff))
@@ -1088,7 +1093,11 @@ cdef class Linear_Expression(object):
10881093 """
10891094 cdef Linear_Expression lhs, rhs
10901095
1091- lhs = < Linear_Expression> self
1096+ if isinstance (self , Linear_Expression):
1097+ lhs = < Linear_Expression> self
1098+ else :
1099+ # NOTE: this code path will only be executed when compiled with cython < 3.0.0
1100+ lhs = Linear_Expression(self )
10921101
10931102 if isinstance (other, Linear_Expression):
10941103 rhs = < Linear_Expression> other
@@ -1181,9 +1190,9 @@ cdef class Linear_Expression(object):
11811190 >>> from ppl import Variable
11821191 >>> x = Variable( 0)
11831192 >>> y = Variable( 1)
1184- >>> 8* ( x + 1)
1193+ >>> 8 * ( x + 1)
11851194 8* x0+ 8
1186- >>> y* 8
1195+ >>> y * 8
11871196 8* x1
11881197 >>> 2** 128 * x
11891198 340282366920938463463374607431768211456* x0
@@ -1194,8 +1203,14 @@ cdef class Linear_Expression(object):
11941203 """
11951204 cdef Linear_Expression e
11961205 cdef c
1197- e = < Linear_Expression> self
1198- c = other
1206+
1207+ if isinstance (self , Linear_Expression):
1208+ e = < Linear_Expression> self
1209+ c = other
1210+ else :
1211+ # NOTE: this code path will only be executed when compiled with cython < 3.0.0
1212+ e = < Linear_Expression> other
1213+ c = self
11991214
12001215 cdef PPL_Coefficient cc = PPL_Coefficient_from_pyobject(c)
12011216 cdef Linear_Expression result = Linear_Expression()
0 commit comments