4
4
from typing import Dict , Optional , Tuple , Union , List
5
5
from .methods import *
6
6
7
+
7
8
class ClebschGordanCoefficients :
8
9
def __init__ (self , jmax : int ):
9
10
if int (2 * jmax ) != 2 * jmax :
10
11
jmax = int (2 * jmax ) / 2
11
-
12
+
12
13
self .__jmax = jmax
13
14
jmin = 1 / 2
14
15
self .__j_list = np .linspace (jmin , jmax , int (2 * (jmax - jmin ) + 1 ))
@@ -23,133 +24,129 @@ def __getitem__(self, *args) -> Union[pd.DataFrame, Dict[float, pd.DataFrame]]:
23
24
if j1 > self .jmax or j2 > self .jmax :
24
25
self .set_jmax (max (j1 , j2 ))
25
26
return self .__cg_dict .get ((j1 , j2 ))
26
-
27
+
27
28
elif len (* args ) == 3 :
28
29
j1 , j2 , m = args [0 ]
29
30
if j1 > self .jmax or j2 > self .jmax :
30
31
self .set_jmax (max (j1 , j2 ))
31
32
return self .__cg_dict .get ((j1 , j2 )).get (m )
32
-
33
+
33
34
else :
34
- raise IndexError (f' Invalid number of indices: { len (* args )} ' )
35
-
35
+ raise IndexError (f" Invalid number of indices: { len (* args )} " )
36
+
36
37
def cg_matrix (
37
- self ,
38
- j1 : int ,
39
- j2 : int ,
40
- m : int ,
41
- return_indices : bool = False
38
+ self , j1 : int , j2 : int , m : int , return_indices : bool = False
42
39
) -> Union [Tuple [np .ndarray , np .ndarray , np .ndarray ], np .ndarray ]:
43
40
cg_dict = self .__cg_dict .get ((j1 , j2 ))
44
41
if cg_dict is None :
45
42
self .update (max (j1 , j2 ))
46
-
43
+
47
44
mat = cg_dict .get (m )
48
-
45
+
49
46
if mat is None :
50
47
return None
51
-
48
+
52
49
if return_indices :
53
- return mat .to_numpy (), mat .index .to_numpy (), mat .columns .to_numpy ()
54
-
50
+ return mat .to_numpy (), mat .index .to_numpy (), mat .columns .to_numpy ()
51
+
55
52
return mat .to_numpy ()
56
-
57
- def cg_matrices_all_m (self , j1 : int , j2 : int ) -> Tuple [
58
- List [np .ndarray ],
59
- List [Tuple [
60
- Tuple [float , float ], # (j1, j2)
61
- float # m
62
- ]]
53
+
54
+ def cg_matrices_all_m (
55
+ self , j1 : int , j2 : int
56
+ ) -> Tuple [
57
+ List [np .ndarray ], List [Tuple [Tuple [float , float ], float ]] # (j1, j2) # m
63
58
]:
64
- '''
59
+ """
65
60
Returns the Clebsch-Gordan coefficients for all m for the given j1, j2 in matrix forms
66
61
along with the index labels of the matrix elements.
67
- '''
62
+ """
68
63
cg_mats = []
69
64
cg_indices = []
70
-
65
+
71
66
if j1 > self .jmax or j2 > self .jmax :
72
67
self .set_jmax (max (j1 , j2 ))
73
-
68
+
74
69
m_list = self .__cg_dict [(j1 , j2 )].keys ()
75
70
for m in m_list :
76
71
mat , row , col = self .cg_matrix (j1 , j2 , m , return_indices = True )
77
72
cg_mats .append (mat )
78
73
cg_indices .append ((row , col ))
79
74
return cg_mats , cg_indices
80
-
81
-
75
+
82
76
def set_jmax (self , jmax : int ):
83
- ''' Updates jmax and appends larger CG coefficients'''
77
+ """ Updates jmax and appends larger CG coefficients"""
84
78
if jmax <= self .__jmax :
85
79
return
86
-
80
+
87
81
jmax_old = self .__jmax
88
- j_new_list = np .linspace (jmax_old , jmax , int (2 * (jmax - jmax_old ) + 1 ))[1 :] # exclude jmax_old
89
-
82
+ j_new_list = np .linspace (jmax_old , jmax , int (2 * (jmax - jmax_old ) + 1 ))[
83
+ 1 :
84
+ ] # exclude jmax_old
85
+
90
86
new_dict = dict ()
91
- new_dict .update ({
92
- (j1 , j2 ): cg_tables_all_m (j1 , j2 )
93
- for j1 , j2 in itertools .product (self .__j_list , j_new_list )
94
- })
95
- new_dict .update ({
96
- (j1 , j2 ): cg_tables_all_m (j1 , j2 )
97
- for j1 , j2 in itertools .product (j_new_list , self .__j_list )
98
- })
99
- new_dict .update ({
100
- (j1 , j2 ): cg_tables_all_m (j1 , j2 )
101
- for j1 , j2 in itertools .product (j_new_list , j_new_list )
102
- })
103
-
87
+ new_dict .update (
88
+ {
89
+ (j1 , j2 ): cg_tables_all_m (j1 , j2 )
90
+ for j1 , j2 in itertools .product (self .__j_list , j_new_list )
91
+ }
92
+ )
93
+ new_dict .update (
94
+ {
95
+ (j1 , j2 ): cg_tables_all_m (j1 , j2 )
96
+ for j1 , j2 in itertools .product (j_new_list , self .__j_list )
97
+ }
98
+ )
99
+ new_dict .update (
100
+ {
101
+ (j1 , j2 ): cg_tables_all_m (j1 , j2 )
102
+ for j1 , j2 in itertools .product (j_new_list , j_new_list )
103
+ }
104
+ )
105
+
104
106
self .__cg_dict .update (new_dict )
105
107
self .__j_list = np .concatenate ((self .__j_list , j_new_list ))
106
108
self .__jmax = jmax
107
-
109
+
108
110
return True
109
-
111
+
110
112
def __repr__ (self ) -> str :
111
- return f' ClebschGordanCoefficients(jmax={ self .__jmax } )'
112
-
113
+ return f" ClebschGordanCoefficients(jmax={ self .__jmax } )"
114
+
113
115
def __str__ (self ) -> str :
114
116
return self .__repr__ ()
115
-
117
+
116
118
def display_all_tables (
117
- self ,
118
- jmin : Optional [int ] = None ,
119
- jmax : Optional [int ] = None
119
+ self , jmin : Optional [int ] = None , jmax : Optional [int ] = None
120
120
) -> None :
121
- '''
121
+ """
122
122
Displays all CG coefficients stored in the object
123
- If the bounds jmin and/or jmax are/is specified, display within the given bounds
124
- '''
123
+ If the bounds jmin and/or jmax are/is specified, display within the given bounds
124
+ """
125
125
if jmax is not None and jmax > self .jmax :
126
126
self .set_jmax (jmax )
127
-
127
+
128
128
for key , tables in self .__cg_dict .items ():
129
129
j1 , j2 = key
130
130
if (jmax is not None and jmax > 0 ) and (jmax < j1 or jmax < j2 ):
131
131
continue
132
-
132
+
133
133
if (jmin is not None and jmin > 0 ) and (jmin > j1 or jmin > j2 ):
134
134
continue
135
-
136
- print (f' { j1 = } , { j2 = } ' )
135
+
136
+ print (f" { j1 = } , { j2 = } " )
137
137
for m , table in tables .items ():
138
- print (f' { m = } :' )
138
+ print (f" { m = } :" )
139
139
display (table )
140
140
return
141
-
141
+
142
142
@property
143
- def dicts (self ) -> Dict [
144
- Tuple [float , float ],
145
- Dict [float , pd .DataFrame ]
146
- ]:
143
+ def dicts (self ) -> Dict [Tuple [float , float ], Dict [float , pd .DataFrame ]]:
147
144
return self .__cg_dict
148
-
145
+
149
146
@property
150
147
def jmax (self ):
151
148
return self .__jmax
152
-
149
+
153
150
@property
154
151
def keys (self ):
155
152
return self .__cg_dict .keys ()
0 commit comments