Matrices with noncommutative symbols

52 views
Skip to first unread message

Carsten Knoll

unread,
May 11, 2015, 4:22:01 PM5/11/15
to sy...@googlegroups.com
Hello,

I wonder about the following behavior


In [1]: import sympy as sp

In [2]: x, y = sp.symbols('x, y', commutative = False)

In [3]: M1 = x*sp.eye(2)

In [4]: M2 = y*x*sp.eye(2)

In [5]: y*M1-M2
Out[5]:
Matrix([
[x*y - y*x, 0],
[ 0, x*y - y*x]])



I would have expected that the two matrices are equal.

However, multiplying y from the right to M1 results in products where y
is on the left side:

In [6]: y*M1
Out[6]:
Matrix([
[x*y, 0],
[ 0, x*y]])


For reference two other results:

In [7]: M2
Out[7]:
Matrix([
[y*x, 0],
[ 0, y*x]])

In [8]: M1*y
Out[8]:
Matrix([
[x*y, 0],
[ 0, x*y]])


Questions:

1. Is this behavior intended?
2. Is there any (easy) way to achieve my intended behavior, i.e.
respecting multiplication order when using matrix multiplication.


Thanks,
Carsten.

Gaurav Dhingra

unread,
May 23, 2015, 3:31:50 AM5/23/15
to sy...@googlegroups.com
I think you are right this is a bug. 
>>> x, y = symbols('x y', commutative=False)
>>> M1 = x*eye(2)
>>> y * (x*M1)
Matrix( [ [x*y, 0], [x*y, 0] ] )

# i think the output should be Matrix( [ [y*x, 0], [0, y*x] ] )
Reply all
Reply to author
Forward
0 new messages