Skip to content

Commit cc75c46

Browse files
committed
Vectorize generate_flm function
1 parent 7cf80bb commit cc75c46

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

s2fft/utils/signal_generator.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import numpy as np
24
import torch
35

@@ -12,7 +14,7 @@ def generate_flm(
1214
spin: int = 0,
1315
reality: bool = False,
1416
using_torch: bool = False,
15-
) -> np.ndarray:
17+
) -> np.ndarray | torch.Tensor:
1618
r"""
1719
Generate a 2D set of random harmonic coefficients.
1820
@@ -38,18 +40,22 @@ def generate_flm(
3840
"""
3941
flm = np.zeros(samples.flm_shape(L), dtype=np.complex128)
4042

41-
for el in range(max(L_lower, abs(spin)), L):
42-
if reality:
43-
flm[el, 0 + L - 1] = rng.normal()
44-
else:
45-
flm[el, 0 + L - 1] = rng.normal() + 1j * rng.normal()
46-
47-
for m in range(1, el + 1):
48-
flm[el, m + L - 1] = rng.normal() + 1j * rng.normal()
49-
if reality:
50-
flm[el, -m + L - 1] = (-1) ** m * np.conj(flm[el, m + L - 1])
51-
else:
52-
flm[el, -m + L - 1] = rng.normal() + 1j * rng.normal()
43+
min_el = max(L_lower, abs(spin))
44+
flm[min_el:L, L - 1] = rng.standard_normal(L - min_el)
45+
if not reality:
46+
flm[min_el:L, L - 1] += 1j * rng.standard_normal(L - min_el)
47+
m_indices, el_indices = np.triu_indices(n=L, k=1, m=L) + np.array([[1], [0]])
48+
if min_el > 0:
49+
in_range_el = el_indices >= min_el
50+
m_indices = m_indices[in_range_el]
51+
el_indices = el_indices[in_range_el]
52+
len_indices = len(m_indices)
53+
flm[el_indices, L - 1 - m_indices] = rng.standard_normal(
54+
len_indices
55+
) + 1j * rng.standard_normal(len_indices)
56+
flm[el_indices, L - 1 + m_indices] = (-1) ** m_indices * np.conj(
57+
flm[el_indices, L - 1 - m_indices]
58+
)
5359

5460
return torch.from_numpy(flm) if using_torch else flm
5561

@@ -61,7 +67,7 @@ def generate_flmn(
6167
L_lower: int = 0,
6268
reality: bool = False,
6369
using_torch: bool = False,
64-
) -> np.ndarray:
70+
) -> np.ndarray | torch.Tensor:
6571
r"""
6672
Generate a 3D set of random Wigner coefficients.
6773

0 commit comments

Comments
 (0)