How do you disable expression evaluation of matrix multiplication?

79 views
Skip to first unread message

Jonathan Crall

unread,
Jul 28, 2015, 3:31:07 PM7/28/15
to sympy
I'm using sympy to generate some latex output, and I would like to create an expression by multiplying several matrices, but I don't want it to simply the expression. 


A simple example: 

I have 

x, y, ori = sympy.symbols('x y ori')


T = Matrix([
[1, 0, -x],
[0, 1, -y],
[0, 0,  1]])

R = Matrix([
[ cos(ori), sin(ori), 0],
[-sin(ori), cos(ori), 0],
[        0,        0, 1]])

A = R.multiply(T)

Currently sympy.latex(A) looks something like: 

[[ cos(ori), sin(ori), -x*cos(ori) - y*sin(ori)],
[-sin(ori), cos(ori),  x*sin(ori) - y*cos(ori)],
[        0,        0,                        1]])

But I would like something more like this: 

[[ cos(ori), sin(ori), 0],       [1, 0, -x],
 [-sin(ori), cos(ori), 0],       [0, 1, -y],
 [        0,        0, 1]])      *    [0, 0,  1]]

Is it possible to disable the automatic expression evaluation?


Francesco Bonazzi

unread,
Jul 29, 2015, 4:29:36 AM7/29/15
to sympy, erot...@gmail.com
Yes, it is. Type:

MatMul(R, T, hold=True)

In [1]: x, y, ori = symbols('x y ori')

In [2]: T = Matrix([
   
...: [1, 0, -x],
   
...: [0, 1, -y],
   
...: [0, 0,  1]])

In [3]: R = Matrix([
   
...: [ cos(ori), sin(ori), 0],
   
...: [-sin(ori), cos(ori), 0],
   
...: [        0,        0, 1]])

In [4]: MatMul(R, T, hold=True)
Out[4]:
[cos(ori)   sin(ori)  0]*[1  0  -x]
[                      ] [        ]
[-sin(ori)  cos(ori)  0] [0  1  -y]
[                      ] [        ]
[    0         0      1] [0  0  1 ]

Reply all
Reply to author
Forward
0 new messages