On Dec 28, 2007 10:18 AM, Pearu Peterson <[EMAIL PROTECTED]> wrote: > > > On Dec 28, 9:11 am, kent-and <[EMAIL PROTECTED]> wrote: > > > class Foo: > > > > def __init__(self): > > self.field = 1.0 > > > > def __mul__(self, other): > > print "Multiplication in Foo from right" > > self.field = self.field * other > > > > def __rmul__(self, other): > > print "Multiplication in Foo from left" > > self.field = other * self.field > > > > f = Foo() > > > > f*x # works > > x*f # works > > > > f*y # works > > y*f # does not work > > > > What should I do about this ? > > Try deriving Foo from sympy.Basic. Actually the __mul__ code in sympy > is broken as it should return NotImplemented when it gets an > unknown (to sympy) object as an right hand side.
It raises: Traceback (most recent call last): File "t.py", line 21, in ? y*f # does not work File "/home/ondra/sympy/sympy/core/methods.py", line 32, in __mul__ return Basic.Mul(self, other) File "sympy/core/basic_methods.py", line 122, in wrapper func_cache_it_cache[k] = r = func(*args, **kw_args) File "/home/ondra/sympy/sympy/core/operations.py", line 19, in __new__ c_part, nc_part, lambda_args, order_symbols = cls.flatten(map(Basic.sympify, args)) File "sympy/core/basic.py", line 270, in sympify raise ValueError("%r is NOT a valid SymPy expression" % a) ValueError: '<__main__.Foo instance at 0xb7dbf14c>' is NOT a valid SymPy expression So maybe we could check in the mul, if a ValueError exception gets raised in sympify and reraise NotImplemented instead? > Btw, arithmetic operations in sympycore are about 4x faster than > in sympy and I am currently working on sexpr support (based on > Fredrik's research) which should give speedups upto 10x in arithmetic > operations. Excellent. Now we need to port this to SymPy: http://code.google.com/p/sympy/issues/detail?id=486 You may also be interested in some timings I did: http://groups.google.com/group/sage-support/msg/8f1a7ed5a49df1a0 where sympycore was 3x faster than SymPy. Ondrej --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sympy?hl=en -~----------~----~----~----~------~----~------~--~---