Skip to content

Commit 7f20f38

Browse files
committed
Reformat using Black
1 parent cfd5397 commit 7f20f38

13 files changed

+264
-324
lines changed

angular_momentum/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .angular_momentum import *
2-
from .ladder_ops import *
2+
from .ladder_ops import *

angular_momentum/angular_momentum.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import numpy as np
22
from .ladder_ops import J_plus, J_minus
33

4+
45
def J_x(j: int) -> np.ndarray:
56
return (J_plus(j) + J_minus(j)) / 2
67

8+
79
def J_y(j: int) -> np.ndarray:
810
return (J_plus(j) - J_minus(j)) / 2j
911

12+
1013
def J_z(j: int) -> np.ndarray:
1114
return np.identity(int(2 * j + 1))
1215

1316

1417
def Jx(j: int) -> np.ndarray:
1518
return J_x(j)
1619

20+
1721
def Jy(j: int) -> np.ndarray:
1822
return J_y(j)
1923

24+
2025
def Jz(j: int) -> np.ndarray:
2126
return J_z(j)
22-

angular_momentum/ladder_ops.py

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import numpy as np
22
import math
33

4+
45
def J_plus(j: int) -> np.ndarray:
56
if int(2 * j + 1) != 2 * j + 1:
6-
raise ValueError(f'j must be a half integer. Found: {j}')
7+
raise ValueError(f"j must be a half integer. Found: {j}")
78

89
dim = int(2 * j + 1)
910

@@ -21,7 +22,7 @@ def J_plus(j: int) -> np.ndarray:
2122

2223
def J_minus(j: int) -> np.ndarray:
2324
if int(2 * j + 1) != 2 * j + 1:
24-
raise ValueError(f'j must be a half integer. Found: {j}')
25+
raise ValueError(f"j must be a half integer. Found: {j}")
2526

2627
dim = int(2 * j + 1)
2728

@@ -37,46 +38,41 @@ def J_minus(j: int) -> np.ndarray:
3738
return mat
3839

3940

40-
def J_plus_component(
41-
j_prime: int,
42-
m_prime: int,
43-
j: int,
44-
m: int
45-
) -> float:
46-
'''
41+
def J_plus_component(j_prime: int, m_prime: int, j: int, m: int) -> float:
42+
"""
4743
Get the matrix element of the raising operator
4844
..math::
4945
\langle j_\prime, m_\prime | J_+ | j, m \rangle
5046
\sqrt{(j - m) * (j + m + 1)} \delta_{j_\prime, j} \delta_{m_\prime, m + 1}.
51-
'''
47+
"""
5248
if (j_prime != j) or (m_prime != m + 1):
5349
return 0
5450
return J_plus_coefficient(j, m)
5551

5652

5753
def J_minus_component(j_prime: int, m_prime: int, j: int, m: int) -> float:
58-
'''
54+
"""
5955
Get the matrix element of the lowering operator
6056
..math::
6157
\langle j_\prime, m_\prime | J_+ | j, m \rangle
6258
\sqrt{(j + m) * (j - m + 1)} \delta_{j_\prime, j} \delta_{m_\prime, m - 1}.
63-
'''
59+
"""
6460
if (j_prime != j) or (m_prime != m - 1):
6561
return 0
6662
return J_minus_coefficient(j, m)
6763

6864

6965
def J_plus_coefficient(j: int, m: int) -> float:
70-
'''
66+
"""
7167
Applies raising operator on the state :math:`|j, m \rangle`
7268
and returns the coefficient (:math:`\sqrt{(j + m) (j - m + 1)}`).
73-
'''
69+
"""
7470
return math.sqrt((j - m) * (j + m + 1))
7571

7672

7773
def J_minus_coefficient(j: int, m: int) -> float:
78-
'''
74+
"""
7975
Applies lowering operator on the state :math:`|j, m \rangle`
8076
and returns the coefficient (:math:`\sqrt{(j - m) (j + m + 1)}`).
81-
'''
77+
"""
8278
return math.sqrt((j + m) * (j - m + 1))

cg/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from .clebsch_gordan_coefficients import *
2-
from .methods import *
2+
from .methods import *

cg/clebsch_gordan_coefficients.py

+65-68
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
from typing import Dict, Optional, Tuple, Union, List
55
from .methods import *
66

7+
78
class ClebschGordanCoefficients:
89
def __init__(self, jmax: int):
910
if int(2 * jmax) != 2 * jmax:
1011
jmax = int(2 * jmax) / 2
11-
12+
1213
self.__jmax = jmax
1314
jmin = 1 / 2
1415
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]]:
2324
if j1 > self.jmax or j2 > self.jmax:
2425
self.set_jmax(max(j1, j2))
2526
return self.__cg_dict.get((j1, j2))
26-
27+
2728
elif len(*args) == 3:
2829
j1, j2, m = args[0]
2930
if j1 > self.jmax or j2 > self.jmax:
3031
self.set_jmax(max(j1, j2))
3132
return self.__cg_dict.get((j1, j2)).get(m)
32-
33+
3334
else:
34-
raise IndexError(f'Invalid number of indices: {len(*args)}')
35-
35+
raise IndexError(f"Invalid number of indices: {len(*args)}")
36+
3637
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
4239
) -> Union[Tuple[np.ndarray, np.ndarray, np.ndarray], np.ndarray]:
4340
cg_dict = self.__cg_dict.get((j1, j2))
4441
if cg_dict is None:
4542
self.update(max(j1, j2))
46-
43+
4744
mat = cg_dict.get(m)
48-
45+
4946
if mat is None:
5047
return None
51-
48+
5249
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+
5552
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
6358
]:
64-
'''
59+
"""
6560
Returns the Clebsch-Gordan coefficients for all m for the given j1, j2 in matrix forms
6661
along with the index labels of the matrix elements.
67-
'''
62+
"""
6863
cg_mats = []
6964
cg_indices = []
70-
65+
7166
if j1 > self.jmax or j2 > self.jmax:
7267
self.set_jmax(max(j1, j2))
73-
68+
7469
m_list = self.__cg_dict[(j1, j2)].keys()
7570
for m in m_list:
7671
mat, row, col = self.cg_matrix(j1, j2, m, return_indices=True)
7772
cg_mats.append(mat)
7873
cg_indices.append((row, col))
7974
return cg_mats, cg_indices
80-
81-
75+
8276
def set_jmax(self, jmax: int):
83-
'''Updates jmax and appends larger CG coefficients'''
77+
"""Updates jmax and appends larger CG coefficients"""
8478
if jmax <= self.__jmax:
8579
return
86-
80+
8781
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+
9086
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+
104106
self.__cg_dict.update(new_dict)
105107
self.__j_list = np.concatenate((self.__j_list, j_new_list))
106108
self.__jmax = jmax
107-
109+
108110
return True
109-
111+
110112
def __repr__(self) -> str:
111-
return f'ClebschGordanCoefficients(jmax={self.__jmax})'
112-
113+
return f"ClebschGordanCoefficients(jmax={self.__jmax})"
114+
113115
def __str__(self) -> str:
114116
return self.__repr__()
115-
117+
116118
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
120120
) -> None:
121-
'''
121+
"""
122122
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+
"""
125125
if jmax is not None and jmax > self.jmax:
126126
self.set_jmax(jmax)
127-
127+
128128
for key, tables in self.__cg_dict.items():
129129
j1, j2 = key
130130
if (jmax is not None and jmax > 0) and (jmax < j1 or jmax < j2):
131131
continue
132-
132+
133133
if (jmin is not None and jmin > 0) and (jmin > j1 or jmin > j2):
134134
continue
135-
136-
print(f'{j1 = }, {j2 = }')
135+
136+
print(f"{j1 = }, {j2 = }")
137137
for m, table in tables.items():
138-
print(f'{m = }:')
138+
print(f"{m = }:")
139139
display(table)
140140
return
141-
141+
142142
@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]]:
147144
return self.__cg_dict
148-
145+
149146
@property
150147
def jmax(self):
151148
return self.__jmax
152-
149+
153150
@property
154151
def keys(self):
155152
return self.__cg_dict.keys()

0 commit comments

Comments
 (0)