I recently faced with the need to do small calculations with matrices opf 
complex number in any symbolic math environment, so I decided to try with 
simpy. And here is an example of my code:

import sympy as sym
from sympy import *
from sympy.physics.quantum.dagger import Dagger

a1, a2, a3, a4 = sym.symbols('a1, a2, a3, a4', real=True)
b1, b2, b3, b4 = sym.symbols('b1, b2, b3, b4', real=True)
g1, g2, g3 = sym.symbols('g1, g2, g3', real=True)
h1, h2, h3 = sym.symbols('h1, h2, h3', real=True)

U = sym.Matrix([[a1 + b1*1j, a2 + b2*1j], [a3 + b3*1j, a4 + b4*1j]])
G = sym.Matrix([[g1, g2 + g3*1j], [g2 - g3*1j, -g1]])
H = sym.Matrix([[h1, h2 + h3*1j], [h2 - h3*1j, -h1]])
U1 = Dagger(U)

Rg= U*G*U1
Rh = U*H*U1
tr = (Rg*Rh).trace()

The problem is that result looks awesomely bulky:

((a1 - 1.0*I*b1)*(g1*(a1 + 1.0*I*b1) + (a2 + 1.0*I*b2)*(g2 - 1.0*I*g3)) + 
(a2 - 1.0*I*b2)*(-g1*(a2 + 1.0*I*b2) + (a1 + 1.0*I*b1)*(g2 + 
1.0*I*g3)))*((a1 - 1.0*I*b1)*(h1*(a1 + 1.0*I*b1) + (a2 + 1.0*I*b2)*(h2 - 
1.0*I*h3)) + (a2 - 1.0*I*b2)*(-h1*(a2 + 1.0*I*b2) + (a1 + 1.0*I*b1)*(h2 + 
1.0*I*h3))) + ((a1 - 1.0*I*b1)*(g1*(a3 + 1.0*I*b3) + (a4 + 1.0*I*b4)*(g2 - 
1.0*I*g3)) + (a2 - 1.0*I*b2)*(-g1*(a4 + 1.0*I*b4) + (a3 + 1.0*I*b3)*(g2 + 
1.0*I*g3)))*((a3 - 1.0*I*b3)*(h1*(a1 + 1.0*I*b1) + (a2 + 1.0*I*b2)*(h2 - 
1.0*I*h3)) + (a4 - 1.0*I*b4)*(-h1*(a2 + 1.0*I*b2) + (a1 + 1.0*I*b1)*(h2 + 
1.0*I*h3))) + ((a1 - 1.0*I*b1)*(h1*(a3 + 1.0*I*b3) + (a4 + 1.0*I*b4)*(h2 - 
1.0*I*h3)) + (a2 - 1.0*I*b2)*(-h1*(a4 + 1.0*I*b4) + (a3 + 1.0*I*b3)*(h2 + 
1.0*I*h3)))*((a3 - 1.0*I*b3)*(g1*(a1 + 1.0*I*b1) + (a2 + 1.0*I*b2)*(g2 - 
1.0*I*g3)) + (a4 - 1.0*I*b4)*(-g1*(a2 + 1.0*I*b2) + (a1 + 1.0*I*b1)*(g2 + 
1.0*I*g3))) + ((a3 - 1.0*I*b3)*(g1*(a3 + 1.0*I*b3) + (a4 + 1.0*I*b4)*(g2 - 
1.0*I*g3)) + (a4 - 1.0*I*b4)*(-g1*(a4 + 1.0*I*b4) + (a3 + 1.0*I*b3)*(g2 + 
1.0*I*g3)))*((a3 - 1.0*I*b3)*(h1*(a3 + 1.0*I*b3) + (a4 + 1.0*I*b4)*(h2 - 
1.0*I*h3)) + (a4 - 1.0*I*b4)*(-h1*(a4 + 1.0*I*b4) + (a3 + 1.0*I*b3)*(h2 + 
1.0*I*h3)))

And i am wondering if there are any built-in methods to shorten this 
expression? Am I doing any thing wrong in my code? I am new in python so 
there might be things I don't yet know.. Thanks in advance for helping.

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sympy/b6b78b13-f11d-427e-9633-26381d0f34fa%40googlegroups.com.

Reply via email to