Mul is a better model for a noncommutative expression, since Mul handles noncommutatives. In either case, Mul.match and Add.match are going to be complicated by the automatic collecting of terms (x + x -> 2*x or x*x -> x**2). Assumedly you don't want that on your object (if you do, you should just use Mul).
I'm not sure if match is needed to make subs work. For that you should only need _eval_subs. Aaron Meurer On Sat, Jan 23, 2016 at 4:13 PM, Rouslan Korneychuk < [email protected]> wrote: > I want to implement a new type of expression so that, given: > import sympy > > class MyExpr(sympy.Expr,...): > ... > > a,b,c,x = sympy.symbols('a b c x') > > e1 = MyExpr(a,b,c) > e2 = MyExpr(b,c) > > this would be so: > > e1.subs(e2,x) == MyExpr(a,x) > > I need a new kind of operation because I'm not working with numbers and > need a very specific set of operations. MyExpr needs to be associative but > not commutative. > > So far I have basically this (I'm working with Python 3): > class MyExpr(sympy.Expr): > @sympy.cacheit > def __new__(cls,*args): > tmp = [] > for a in args: > a = sympy.sympify(a,strict=True) > if type(a) is cls: > tmp.extend(a.args) > else: > tmp.append(a) > return super().__new__(cls,*tmp) > > is_commutative = False > > What do I need for the above to work? I looked at the source for sympy.Add > and some other classes and figure I need to implement match() and compare > contiguous slices, but the implementation for Add.match is far from > straight-forward. > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at https://groups.google.com/group/sympy. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/5cbbc510-eba7-4c66-9661-2531258a6f45%40googlegroups.com > <https://groups.google.com/d/msgid/sympy/5cbbc510-eba7-4c66-9661-2531258a6f45%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6JrZfQGNzh2VfmVgwm_SjmyR3GVH3yjVAegJ%2B9L9aVU0g%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
