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?

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?

Would this be a good way of implementing a noncommutative product -

from sympy import *

Expr_mul = Expr.__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.

--
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.

Reply via email to