You can get a compact representation since there are many repeated
expressions (though not appearing in a simple factorable manner):
>>> tr.count_ops()
271
>>> c1=cse(tr)
>>> count_ops(c1)
74
>>> c1[1] # the expression with subexpressions extracted
[(x11*x24 + x23*x8)*(x2*x25 + x26*x9) + (x11*x9 + x2*x8)*(x14*x2 + x16*x9)
+ (x14*x23 + x16*x24)*(x2*x21 + x22*x9) + (x21*x23 + x22*x24)*(x23*x25 +
x24*x26)]
On Thursday, March 19, 2020 at 10:10:28 AM UTC-5, Ilshat Garipov wrote:
>
> 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/cb2e2f76-22e3-46a4-af8c-274bc598ac00%40googlegroups.com.