https://github.com/sympy/sympy/wiki/canonicalization (that also has links to mailing list discussions).
If that's not a concern, then don't worry about it. Otherwise, you might want to make a function that just parses the expression and replaces the multiplications with what you want (until we get that issue fixed). Aaron Meurer On Tue, Mar 13, 2012 at 10:36 AM, Alan Bromborsky <[email protected]> wrote: > On 03/13/2012 09:02 AM, Alan Bromborsky wrote: >> >> First a question on noncommutative multiplication. The following code - >> >> from sympy import * >> >> (a1,a2,b1,b2) = symbols('a1 a2 b1 b2') >> (e1,e2) = symbols('e1 e2',commutative=False) >> >> x = expand(a1*(e1+e2)*a2*b1*(e1+e2)*b2) >> >> print x >> >> gives - >> >> a1*a2*b1*b2*e1*e2 + a1*a2*b1*b2*e1**2 + a1*a2*b1*b2*e2*e1 + >> a1*a2*b1*b2*e2**2 >> >> Are the products of the noncommutative symbols always grouped together? I don't get the question. You called expand() and that's exactly what it did. What did you expect? >> >> This is important so that if one defines a multiplication table for the >> noncommutative symbols a complex product could be evaluated with a simple >> >> x = x.subs(mul_table) >> >> where the keys of the mul_table dictionary are e1**2, e1*e2, e2*e1, e2**2. >> Would e1*e1 work as a key as well as e1**2? >> >> The second question is if one uses a function rather than a table to >> calculate the product of noncommuting symbols what class should be >> subclassed in order to overload the __mul__() operator? Should it be the >> Basic class? Expr, if I understand your question correctly. Or Symbol if the object is explicitly a Symbol. >> > Would this be a good way of implementing a noncommutative product - > > from sympy import * > > Expr_mul = Expr.__mul__ Just us Mul(). > > def mymul(a,b): > if not a.is_commutative and not b.is_commutative: > if isinstance(a,Symbol) and isinstance(b,Symbol): > print 'a =',a,'b =',b > return(Expr_mul(a,b)) > else: > return(Expr_mul(a,b)) > else: > return(Expr_mul(a,b)) > > Expr.__mul__ = mymul > > > (a1,a2,b1,b2) = symbols('a1 a2 b1 b2') > (e1,e2) = symbols('e1 e2',commutative=False) > > print expand(a1*(e1+e2)*a2*b1*(e1+e2)*b2) > > the output is - > > a = e2 b = e2 > a = e2 b = e1 > a = e1 b = e2 > a = e1 b = e1 > > a1*a2*b1*b2*e1*e2 + a1*a2*b1*b2*e1**2 + a1*a2*b1*b2*e2*e1 + > a1*a2*b1*b2*e2**2 > > In the real implementation there would be no print statments a and b and the > first instance of Expr_mul(a,b) would be replaced by the actual function for > multiplying noncommutative symbols. It wouldn't work if you injected a non-commutative Symbol in the middle, like e1*x*e2, or if the multiplication happened out of order due to association, like e1*(e2*e3). This is related to the problem we've been discussing in another thread. See > > > -- > You received this message because you are subscribed to the Google Groups > "sympy" group. > To post to this group, send email to [email protected]. > 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. > -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to [email protected]. 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.
