-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclebsch.py
185 lines (155 loc) · 6.87 KB
/
clebsch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from __future__ import division
from scipy import floor, sqrt, log, exp
from scipy.misc import factorial
from scipy.special import gammaln
from numpy import arange,zeros
import numpy as np
def Wigner3j(j1,j2,j3,m1,m2,m3):
#======================================================================
# Wigner3j.m by David Terr, Raytheon, 6-17-04
#
# Compute the Wigner 3j symbol using the Racah formula [1].
#
# Usage:
# from wigner import Wigner3j
# wigner = Wigner3j(j1,j2,j3,m1,m2,m3)
#
# / j1 j2 j3 \
# | |
# \ m1 m2 m3 /
#
# Reference: Wigner 3j-Symbol entry of Eric Weinstein's Mathworld:
# http://mathworld.wolfram.com/Wigner3j-Symbol.html
#======================================================================
# Error checking
if ( ( 2*j1 != floor(2*j1) ) | ( 2*j2 != floor(2*j2) ) | ( 2*j3 != floor(2*j3) ) | ( 2*m1 != floor(2*m1) ) | ( 2*m2 != floor(2*m2) ) | ( 2*m3 != floor(2*m3) ) ):
print 'All arguments must be integers or half-integers.'
return -1
# Additional check if the sum of the second row equals zero
if ( m1+m2+m3 != 0 ):
print '3j-Symbol unphysical'
return 0
if ( j1 - m1 != floor ( j1 - m1 ) ):
print '2*j1 and 2*m1 must have the same parity'
return 0
if ( j2 - m2 != floor ( j2 - m2 ) ):
print '2*j2 and 2*m2 must have the same parity'
return; 0
if ( j3 - m3 != floor ( j3 - m3 ) ):
print '2*j3 and 2*m3 must have the same parity'
return 0
if ( j3 > j1 + j2) | ( j3 < abs(j1 - j2) ):
print 'j3 is out of bounds.'
return 0
if abs(m1) > j1:
print 'm1 is out of bounds.'
return 0
if abs(m2) > j2:
print 'm2 is out of bounds.'
return 0
if abs(m3) > j3:
print 'm3 is out of bounds.'
return 0
t1 = j2 - m1 - j3
t2 = j1 + m2 - j3
t3 = j1 + j2 - j3
t4 = j1 - m1
t5 = j2 + m2
tmin = max( 0, max( t1, t2 ) )
tmax = min( t3, min( t4, t5 ) )
tvec = arange(tmin, tmax+1, 1)
wigner = 0
wignerlogvec = zeros((tmax-tmin+1),dtype=np.complex)
for t in tvec: #get the logarithm of each term
wignerlogvec[t-tmin] = log((-1)**t) - (gammaln(t+1) + gammaln(t - t1 + 1) + gammaln(t - t2 + 1) + gammaln(t3 - t + 1) + gammaln(t4 - t + 1) + gammaln(t5 - t + 1))
wignermeanpow = int(wignerlogvec.mean()) #finds the mean order of magnitude of the terms...
for t in tvec:
wigner += exp(wignerlogvec[t-tmin] - wignermeanpow) # ... so we can subtract it here (since to add these terms together
logwigner = log(wigner) + wignermeanpow # we need to exponentiate them). Then take the logarithm and add
# the order of magnitude back.
f12m3 = gammaln(j1+j2-j3+1)
f1m23 = gammaln(j1-j2+j3+1)
fm123 = gammaln(-j1+j2+j3+1)
f123p1 = gammaln(j1+j2+j3+1+1)
fj1pm1 = gammaln(j1+m1+1)
fj1mm1 = gammaln(j1-m1+1)
fj1pm2 = gammaln(j2+m2+1)
fj1mm2 = gammaln(j2-m2+1)
fj1pm3 = gammaln(j3+m3+1)
fj1mm3 = gammaln(j3-m3+1)
try:
logresult = logwigner + 0.5*(f12m3 + f1m23 + fm123 - f123p1 +
fj1pm1 + fj1mm1 + fj1pm2 + fj1mm2 +
fj1pm3 + fj1mm3)
result = ((-1)**(j1-j2-m3) * exp(logresult)).real #wigner * (-1)**(j1-j2-m3) * sqrt( ratio1 * f1m23 * fm123 * fj1pm1 * fj1mm1 * fj1pm2 * fj1mm2 * fj1pm3 * fj1mm3 )
except Exception as e:
print "Exception in clebsch.py Wigner3J: {}".format(e)
print "j1={}, j2={}, j3={}, m1={}, m2={}, m3={}".format(j1,j2,j3,m1,m2,m3)
print "f123p1: {}, f12m3: {}, f1m23: {}, fm123: {}".format(f123p1,f12m3,f1m23,fm123)
print "fj1pm1: {}, fj1mm1: {}".format(fj1pm1,fj1mm1)
print "fj1pm2: {}, fj1mm2: {}".format(fj1pm2,fj1mm2)
print "fj1pm3: {}, fj1mm3: {}".format(fj1pm3,fj1mm3)
print "wigner: {}".format(wigner)
quit()
#print "j1={}, j2={}, j3={}, m1={}, m2={}, m3={}".format(j1,j2,j3,m1,m2,m3)
#print "result: {}".format(result)
return result
def Wigner6j(j1,j2,j3,J1,J2,J3):
#======================================================================
# Calculating the Wigner6j-Symbols using the Racah-Formula
# Author: Ulrich Krohn
# Date: 13th November 2009
#
# Based upon Wigner3j.m from David Terr, Raytheon
# Reference: http://mathworld.wolfram.com/Wigner6j-Symbol.html
#
# Usage:
# from wigner import Wigner6j
# WignerReturn = Wigner6j(j1,j2,j3,J1,J2,J3)
#
# / j1 j2 j3 \
# < >
# \ J1 J2 J3 /
#
#======================================================================
# Check that the js and Js are only integer or half integer
if ( ( 2*j1 != round(2*j1) ) | ( 2*j2 != round(2*j2) ) | ( 2*j2 != round(2*j2) ) | ( 2*J1 != round(2*J1) ) | ( 2*J2 != round(2*J2) ) | ( 2*J3 != round(2*J3) ) ):
print 'All arguments must be integers or half-integers.'
return -1
# Check if the 4 triads ( (j1 j2 j3), (j1 J2 J3), (J1 j2 J3), (J1 J2 j3) ) satisfy the triangular inequalities
if ( ( abs(j1-j2) > j3 ) | ( j1+j2 < j3 ) | ( abs(j1-J2) > J3 ) | ( j1+J2 < J3 ) | ( abs(J1-j2) > J3 ) | ( J1+j2 < J3 ) | ( abs(J1-J2) > j3 ) | ( J1+J2 < j3 ) ):
print '6j-Symbol is not triangular!'
return 0
# Check if the sum of the elements of each traid is an integer
if ( ( 2*(j1+j2+j3) != round(2*(j1+j2+j3)) ) | ( 2*(j1+J2+J3) != round(2*(j1+J2+J3)) ) | ( 2*(J1+j2+J3) != round(2*(J1+j2+J3)) ) | ( 2*(J1+J2+j3) != round(2*(J1+J2+j3)) ) ):
print '6j-Symbol is not triangular!'
return 0
# Arguments for the factorials
t1 = j1+j2+j3
t2 = j1+J2+J3
t3 = J1+j2+J3
t4 = J1+J2+j3
t5 = j1+j2+J1+J2
t6 = j2+j3+J2+J3
t7 = j1+j3+J1+J3
# Finding summation borders
tmin = max(0, max(t1, max(t2, max(t3,t4))))
tmax = min(t5, min(t6,t7))
tvec = arange(tmin,tmax+1,1)
# Calculation the sum part of the 6j-Symbol
WignerReturn = 0
for t in tvec:
WignerReturn += (-1)**t*factorial(t+1)/( factorial(t-t1)*factorial(t-t2)*factorial(t-t3)*factorial(t-t4)*factorial(t5-t)*factorial(t6-t)*factorial(t7-t) )
# Calculation of the 6j-Symbol
return WignerReturn*sqrt( TriaCoeff(j1,j2,j3)*TriaCoeff(j1,J2,J3)*TriaCoeff(J1,j2,J3)*TriaCoeff(J1,J2,j3) )
def TriaCoeff(a,b,c):
# Calculating the triangle coefficient
return factorial(a+b-c)*factorial(a-b+c)*factorial(-a+b+c)/(factorial(a+b+c+1))
def clebsch(j1, j2, j3, m1, m2, m3):
try:
return pow(-1,j1-j2+m3)*sqrt(2*j3+1)*Wigner3j(j1,j2,j3,m1,m2,-m3)
except Exception as e:
print "Exception: {}".format(e)
print "j1={}, j2={}, j3={}".format(j1,j2,j3)
print "m1={}, m2={}, m3={}".format(m1,m2,m3)
quit()