-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathangular.py
More file actions
296 lines (240 loc) · 8.1 KB
/
angular.py
File metadata and controls
296 lines (240 loc) · 8.1 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from __future__ import division
import numpy as np
from scipy import special
from .. import util
from warnings import warn
try:
import quadpy.u3._lebedev as _lebedev # only for grid_lebedev()
except ImportError:
pass
def sht_matrix(N, azi, colat, weights=None):
r"""Matrix of spherical harmonics up to order N for given angles.
Computes a matrix of spherical harmonics up to order :math:`N`
for the given angles/grid.
.. math::
\mathbf{Y} = \left[ \begin{array}{cccccc}
Y_0^0(\theta[0], \phi[0]) & Y_1^{-1}(\theta[0], \phi[0]) & Y_1^0(\theta[0], \phi[0]) & Y_1^1(\theta[0], \phi[0]) & \dots & Y_N^N(\theta[0], \phi[0]) \\
Y_0^0(\theta[1], \phi[1]) & Y_1^{-1}(\theta[1], \phi[1]) & Y_1^0(\theta[1], \phi[1]) & Y_1^1(\theta[1], \phi[1]) & \dots & Y_N^N(\theta[1], \phi[1]) \\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\
Y_0^0(\theta[Q-1], \phi[Q-1]) & Y_1^{-1}(\theta[Q-1], \phi[Q-1]) & Y_1^0(\theta[Q-1], \phi[Q-1]) & Y_1^1(\theta[Q-1], \phi[Q-1]) & \dots & Y_N^N(\theta[Q-1], \phi[Q-1])
\end{array} \right]
where
.. math::
Y_n^m(\theta, \phi) = \sqrt{\frac{2n + 1}{4 \pi} \frac{(n-m)!}{(n+m)!}} P_n^m(\cos \theta) e^{i m \phi}
(Note: :math:`\mathbf{Y}` is interpreted as the inverse transform (or synthesis)
matrix in examples and documentation.)
cf. eq. (1.9), (3.29) :cite:`Rafaely2019_book`
Parameters
----------
N : int
Maximum order.
azi : (Q,) array_like
Azimuth.
colat : (Q,) array_like
Colatitude.
weights : (Q,) array_like, optional
Quadrature weights.
Returns
-------
Ymn : (Q, (N+1)**2) numpy.ndarray
Matrix of spherical harmonics.
"""
azi = util.asarray_1d(azi)
colat = util.asarray_1d(colat)
if azi.ndim == 0:
Q = 1
else:
Q = len(azi)
if weights is None:
weights = np.ones(Q)
Ymn = np.zeros([Q, (N+1)**2], dtype=complex)
i = 0
for n in range(N+1):
for m in range(-n, n+1):
Ymn[:, i] = weights * special.sph_harm(m, n, azi, colat)
i += 1
return Ymn
def Legendre_matrix(N, ctheta):
r"""Matrix of weighted Legendre Polynominals.
Computes a matrix of weighted Legendre polynominals up to order N for
the given angles
.. math::
L_n(\theta) = \frac{2n+1}{4 \pi} P_n(\theta)
Parameters
----------
N : int
Maximum order.
ctheta : (Q,) array_like
Angles.
Returns
-------
Lmn : ((N+1), Q) numpy.ndarray
Matrix containing Legendre polynominals.
"""
if ctheta.ndim == 0:
M = 1
else:
M = len(ctheta)
Lmn = np.zeros([N+1, M], dtype=complex)
for n in range(N+1):
Lmn[n, :] = (2*n+1)/(4*np.pi) * np.polyval(special.legendre(n), ctheta)
return Lmn
def cht_matrix(N, pol, weights=None):
r"""Matrix of circular harmonics up to order N for given angles.
Computes a matrix of circular harmonics up to order :math:`N`
for the given angles/grid.
.. math::
\Psi = \left[ \begin{array}{ccccccc}
1 & e^{i\varphi[0]} & \cdots & e^{iN\varphi[0]} & e^{-iN\varphi[0]} & \cdots & e^{-i\varphi[0]} \\
1 & e^{i\varphi[1]} & \cdots & e^{iN\varphi[1]} & e^{-iN\varphi[1]} & \cdots & e^{-i\varphi[1]} \\
\vdots & \vdots & \vdots & \vdots & \vdots & \vdots & \vdots \\
1 & e^{i\varphi[Q-1]} & \cdots & e^{iN\varphi[Q-1]} & e^{-iN\varphi[Q-1]} & \cdots & e^{-i\varphi[Q-1]}
\end{array} \right]
(Note: :math:`\Psi` is interpreted as the inverse transform (or synthesis)
matrix in examples and documentation.)
Parameters
----------
N : int
Maximum order.
pol : (Q,) array_like
Polar angle.
weights : (Q,) array_like, optional
Weights.
Returns
-------
Psi : (Q, 2N+1) numpy.ndarray
Matrix of circular harmonics.
"""
pol = util.asarray_1d(pol)
if pol.ndim == 0:
Q = 1
else:
Q = len(pol)
if weights is None:
weights = np.ones(Q)
Psi = np.zeros([Q, (2*N+1)], dtype=complex)
order = np.roll(np.arange(-N, N+1), -N)
for i, n in enumerate(order):
Psi[:, i] = weights * np.exp(1j * n * pol)
return Psi
def grid_equal_angle(n):
"""Equi-angular sampling points on a sphere.
According to (cf. Rafaely book, sec.3.2)
Parameters
----------
n : int
Maximum order.
Returns
-------
azi : array_like
Azimuth.
colat : array_like
Colatitude.
weights : array_like
Quadrature weights.
"""
azi = np.linspace(0, 2*np.pi, 2*n+2, endpoint=False)
colat, d_colat = np.linspace(0, np.pi, 2*n+2, endpoint=False, retstep=True)
colat += d_colat/2
weights = np.zeros_like(colat)
p = np.arange(1, 2*n+2, 2)
for i, theta in enumerate(colat):
weights[i] = 2*np.pi/(n+1) * np.sin(theta) * np.sum(np.sin(p*theta)/p)
azi = np.tile(azi, 2*n+2)
colat = np.repeat(colat, 2*n+2)
weights = np.repeat(weights, 2*n+2)
weights /= n+1 # sum(weights) == 4pi
return azi, colat, weights
def grid_gauss(n):
""" Gauss-Legendre sampling points on sphere.
According to (cf. Rafaely book, sec.3.3)
Parameters
----------
n : int
Maximum order.
Returns
-------
azi : array_like
Azimuth.
colat : array_like
Colatitude.
weights : array_like
Quadrature weights.
"""
azi = np.linspace(0, 2*np.pi, 2*n+2, endpoint=False)
x, weights = np.polynomial.legendre.leggauss(n+1)
colat = np.arccos(x)
azi = np.tile(azi, n+1)
colat = np.repeat(colat, 2*n+2)
weights = np.repeat(weights, 2*n+2)
weights *= np.pi / (n+1) # sum(weights) == 4pi
return azi, colat, weights
def grid_equal_polar_angle(n):
"""Equi-angular sampling points on a circle.
Parameters
----------
n : int
Maximum order
Returns
-------
pol : array_like
Polar angle.
weights : array_like
Weights.
"""
num_mic = 2*n+1
pol = np.linspace(0, 2*np.pi, num=num_mic, endpoint=False)
weights = 1/num_mic * np.ones(num_mic)
return pol, weights
def grid_lebedev(n):
"""Lebedev sampling points on sphere.
(Maximum n is 65. We use what is available in quadpy, some n may not be
tight, others produce negative weights.
Parameters
----------
n : int
Maximum order.
Returns
-------
azi : array_like
Azimuth.
colat : array_like
Colatitude.
weights : array_like
Quadrature weights.
"""
def available_quadrature(d):
"""Get smallest availabe quadrature of of degree d.
see:
https://people.sc.fsu.edu/~jburkardt/datasets/sphere_lebedev_rule/sphere_lebedev_rule.html
"""
l = list(range(1, 32, 2)) + list(range(35, 132, 6))
matches = [x for x in l if x >= d]
return matches[0]
if n > 65:
print(n)
raise ValueError("Maximum available Lebedev grid order is 65. "
"(requested: {})".format(n))
# this needs https://pypi.python.org/pypi/quadpy
# currently validated with 0.16.5
degree = _lebedev.lebedev_003a() # tmp call to mute pep8 warning
degree = available_quadrature(2 * n)
# the nice API handling currently returns only up to lebedev_047:
# q = quadpy.u3.get_good_scheme(degree=degree)
# thus we call the appropriate lebedev function directly in low level,
# not nice, but then we are save until the quadpy API will be consistent:
if degree == 3:
fcn = '_lebedev.lebedev_00' + str(degree) + 'a()'
elif degree < 10:
fcn = '_lebedev.lebedev_00'+str(degree) + '()'
elif degree < 100:
fcn = '_lebedev.lebedev_0' + str(degree) + '()'
else:
fcn = '_lebedev.lebedev_' + str(degree) + '()'
q = eval(fcn)
if np.any(q.weights < 0):
warn("Lebedev grid of order {} has negative weights.".format(n))
azi, colat, r = util.cart2sph(q.points[0, :],
q.points[1, :],
q.points[2, :])
return azi, colat, 4*np.pi*q.weights