-
Notifications
You must be signed in to change notification settings - Fork 0
/
wigner.py
213 lines (177 loc) · 6.18 KB
/
wigner.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""
Code to calculate Clebsch-Gordan coefficients, Wigner3j symbols, and Wigner6j symbols
Most functions here were derived from Matlab code written by others
"""
from scipy.special import factorial
import numpy as np
def clebsch_gordan(j1, j2, j, m1, m2, m, print_warnings=False):
"""
Compute Clebsch gordan coefficient <(j1 m1) (j2 m2) | j m >
:param j1:
:param j2:
:param j:
:param m1:
:param m2:
:param m:
:param print_warnings:
:return:
"""
return (-1)**(j1-j2+m) * np.sqrt(2*j + 1) * wigner3j(j1, j2, j, m1, m2, -m, print_warnings)
def wigner3j(j1, j2, j3, m1, m2, m3, print_warnings=False):
"""
Wigner3j.m by David Terr, Raytheon, 6-17-04
Compute the Wigner 3j symbol using the Racah formula. See e.g.
Wigner 3j-Symbol entry of Eric Weinstein's Mathworld:
http://mathworld.wolfram.com/Wigner3j-Symbol.html
/ j1 j2 j3 \
| |
\ m1 m2 m3 /
:param j1:
:param j2:
:param j3:
:param m1:
:param m2:
:param m3:
:param print_warnings:
:return:
"""
# Error checking
if ((2*j1 != np.floor(2*j1)) |
(2*j2 != np.floor(2*j2)) |
(2*j3 != np.floor(2*j3)) |
(2*m1 != np.floor(2*m1)) |
(2*m2 != np.floor(2*m2)) |
(2*m3 != np.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:
if print_warnings:
print('3j-Symbol unphysical')
return 0
if j1 - m1 != np.floor(j1 - m1):
if print_warnings:
print('2*j1 and 2*m1 must have the same parity')
return 0
if j2 - m2 != np.floor(j2 - m2):
if print_warnings:
print('2*j2 and 2*m2 must have the same parity')
return 0
if (j3 - m3) != np.floor(j3 - m3):
if print_warnings:
print('2*j3 and 2*m3 must have the same parity')
return 0
if (j3 > j1 + j2) | (j3 < np.abs(j1 - j2)):
if print_warnings:
print('j3 is out of bounds.')
return 0
if abs(m1) > j1:
if print_warnings:
print('m1 is out of bounds.')
return 0
if abs(m2) > j2:
if print_warnings:
print('m2 is out of bounds.')
return 0
if abs(m3) > j3:
if print_warnings:
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 = np.arange(tmin, tmax+1, 1)
wigner = 0
for t in tvec:
wigner += (-1)**t / (factorial(t) * factorial(t - t1) *
factorial(t - t2) * factorial(t3 - t) *
factorial(t4 - t) * factorial(t5 - t))
w3j = wigner * (-1) ** (j1-j2-m3) * np.sqrt(factorial(j1 + j2 - j3) * factorial(j1 - j2 + j3) *
factorial(-j1 + j2 + j3) / factorial(j1 + j2 + j3 + 1) *
factorial(j1 + m1) * factorial(j1 - m1) *
factorial(j2 + m2) * factorial(j2 - m2) *
factorial(j3 + m3) * factorial(j3 - m3))
return w3j
def wigner6j(j1, j2, j3, J1, J2, J3, print_warnings=False):
"""
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
/ j1 j2 j3 \
< >
\ J1 J2 J3 /
:param j1:
:param j2:
:param j3:
:param J1:
:param J2:
:param J3:
:param print_warnings:
:return:
"""
# 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)):
if print_warnings:
print('6j-Symbol is not triangular!')
return 0
# Check if the sum of the elements of each triad 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)))):
if print_warnings:
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 = np.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
w6j = WignerReturn * np.sqrt(triangle_coeff(j1, j2, j3) *
triangle_coeff(j1, J2, J3) *
triangle_coeff(J1, j2, J3) *
triangle_coeff(J1, J2, j3))
return w6j
def triangle_coeff(a, b, c):
"""
:param a:
:param b:
:param c:
:return:
"""
# Calculating the triangle coefficient
return factorial(a + b - c) * factorial(a - b + c) * factorial(-a + b + c) / (factorial(a + b + c + 1))