Reputation: 123
I have implemented the spin-weighted spherical harmonics (SWSH) in Sympy, which give the correct expressions but in an un-simplified form.
For example, the SWSH with spin=0 l=1 m=0 has in it the expression
(1/tan(0.5*th))**2.0 - 1
which can be simplified easily in for example Mathematica to
cos(th)*csc(0.5*th)**2
I have tried the following to reproduce this simplification using Sympy's functions:
simplify
and trigsimp
expr.rewrite(exp).simplify().expand().rewrite(sin).simplify()
expr.rewrite(exp).simplify().expand().rewrite(cos).simplify()
but unfortunately it just doesn't want to simplify, and I cannot see any other way.
At the end of the day I am writing a function as a sum of these things, and Ineed to integrate the result. The problem is because these expressions do not simplify, the resulting integrand is long and complicated and the integration takes forever.
I am not able to hardcode the expressions for the SWSH as there are too many that I am using, hence I must calculate them.
Thus if somebody could lead me toward a method of using built-in Sympy simplification functions to simplify this expression that would be great. Hopefully the method will also simplify other SWSH too.
Upvotes: 1
Views: 301
Reputation:
The module fu.py
contains several specialized trigonometric transformation procedures. For example, TR2 converts tan-cot to sin-cos.
from sympy.simplify.fu import *
expr = (1/tan(th/2))**2 - 1 # no floats please, we are symbolic
e2 = TR2(expr).trigsimp()
Now e2 is -2*cos(th)/(cos(th) - 1)
. It's no longer the sum of two terms, and there is cos(th)
in it; but the other thing is not yet what you wanted. It turns out to be hard to convince SymPy to turn 1-cos(th)
into 2*sin(th/2)**2
--- perhaps unsurprisingly, since the latter does look more complicated. I got it with
e3 = TR7(TR5(TR6(TR11(e2.subs(th, 2*th))))).subs(th, th/2)
which results in cos(th)/sin(th/2)**2
. The logic is:
-2*cos(2*th)/(cos(2*th) - 1)
-2*(-sin(th)**2 + cos(th)**2)/(-sin(th)**2 + cos(th)**2 - 1)
(-2*sin(th)**2 + 1)/sin(th)**2
. This achieved the desired form of the denominator; it remains to undo the collateral damage to the numerator.(2*cos(th)**2 - 1)/sin(th)**2
cos(2*th)/sin(th)**2
cos(th)/sin(th/2)**2
The steps 4, 5, 6 are essentially the inverses of 3, 2, 1.
Upvotes: 0