@@ -248,10 +248,6 @@ cdef class Expr:
248248 if not isinstance (other, EXPR_OP_TYPES):
249249 return NotImplemented
250250
251- if _is_number(other):
252- f = float (other)
253- return Expr({v: f * c for v, c in self .terms.items()})
254-
255251 cdef dict res = {}
256252 cdef Py_ssize_t pos1 = < Py_ssize_t> 0 , pos2 = < Py_ssize_t> 0
257253 cdef PyObject * k1_ptr = NULL
@@ -260,25 +256,31 @@ cdef class Expr:
260256 cdef PyObject * v2_ptr = NULL
261257 cdef PyObject * old_v_ptr = NULL
262258 cdef Term child
263- cdef double prod_v
264-
265- while PyDict_Next(self .terms, & pos1, & k1_ptr, & v1_ptr):
266- pos2 = < Py_ssize_t> 0
267- while PyDict_Next(other.terms, & pos2, & k2_ptr, & v2_ptr):
268- child = (< Term> k1_ptr) * (< Term> k2_ptr)
269- prod_v = (< double > (< object > v1_ptr)) * (< double > (< object > v2_ptr))
270- if (old_v_ptr := PyDict_GetItem(res, child)) != NULL :
271- res[child] = < double > (< object > old_v_ptr) + prod_v
272- else :
273- res[child] = prod_v
259+ cdef double coef
260+
261+ if _is_number(other):
262+ coef = < double > other
263+ while PyDict_Next(self .terms, & pos1, & k1_ptr, & v1_ptr):
264+ res[< Term> k1_ptr] = < double > (< object > v1_ptr) * coef
265+
266+ elif isinstance (other, Expr):
267+ while PyDict_Next(self .terms, & pos1, & k1_ptr, & v1_ptr):
268+ pos2 = < Py_ssize_t> 0
269+ while PyDict_Next(other.terms, & pos2, & k2_ptr, & v2_ptr):
270+ child = (< Term> k1_ptr) * (< Term> k2_ptr)
271+ coef = (< double > (< object > v1_ptr)) * (< double > (< object > v2_ptr))
272+ if (old_v_ptr := PyDict_GetItem(res, child)) != NULL :
273+ res[child] = < double > (< object > old_v_ptr) + coef
274+ else :
275+ res[child] = coef
274276 return Expr(res)
275277
276278 def __truediv__ (self , other ):
277279 if not isinstance (other, EXPR_OP_TYPES):
278280 return NotImplemented
279281
280282 if _is_number(other):
281- return 1.0 / other * self
283+ return 1.0 / < double > other * self
282284 return buildGenExprObj(self ) / other
283285
284286 def __rtruediv__ (self , other ):
0 commit comments