Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transpose a sympy matrix

Tags:

python

sympy

I have a problem using the sympy library. I want to transpose a matrix but it does not work. My code is

x = Symbol('x')
y = Symbol('y')

J = Matrix([[1+x,2+y,3*x],[4*x,5+x,6*y],[7+y,8*y,9+x]])


Jtrans = Transpose(J)

print(J)
print(Jtrans)

Any suggestions?

like image 847
python_beginner Avatar asked Oct 15 '25 15:10

python_beginner


2 Answers

Use the shorthand J.T.

>>> J
Matrix([
[x + 1, y + 2,   3*x],
[  4*x, x + 5,   6*y],
[y + 7,   8*y, x + 9]])
>>> Transpose(J)
Matrix([
[x + 1, y + 2,   3*x],
[  4*x, x + 5,   6*y],
[y + 7,   8*y, x + 9]])'
>>> #'
>>> J.T
Matrix([
[x + 1,   4*x, y + 7],
[y + 2, x + 5,   8*y],
[  3*x,   6*y, x + 9]])
like image 169
kennytm Avatar answered Oct 18 '25 19:10

kennytm


it is working fine for me:

Matrix([[x + 1, y + 2, 3*x], [4*x, x + 5, 6*y], [y + 7, 8*y, x + 9]])
Matrix([
[x + 1, y + 2,   3*x],
[  4*x, x + 5,   6*y],
[y + 7,   8*y, x + 9]])'

Note the small ' after the second matrix? That's your transpose operation.

like image 41
Nils Werner Avatar answered Oct 18 '25 18:10

Nils Werner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!