-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdisk_quadrature.py
More file actions
204 lines (161 loc) · 5.19 KB
/
disk_quadrature.py
File metadata and controls
204 lines (161 loc) · 5.19 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
#
# Based on LGPL code by John Burkardt.
# https://people.sc.fsu.edu/~jburkardt/
#
import numpy
import misc
def disk_quadrature_rule(nradial, nangles):
"""
:param nradial: number of radial samples
:param nangles: number of angle samples
:return: w: radial weights
:return: r: radial samples
:return: t: angular samples
"""
x, w = legendre_ek_compute(nradial)
x = (x + 1.0)/2.0
w /= 2.0
r = numpy.zeros(nradial)
t = numpy.zeros(nangles)
for it in numpy.arange(nangles):
t[it] = 2.0 * numpy.pi * it / nangles
w /= nangles
r = numpy.sqrt(x)
return w, r, t
# Reference:
#
# Sylvan Elhay, Jaroslav Kautsky,
# Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
# Interpolatory Quadrature,
# ACM Transactions on Mathematical Software,
# Volume 13, Number 4, December 1987, pages 399-415.
def legendre_ek_compute(n):
"""
Gauss-Legendre, Elhay-Kautsky method.
:param n: number of samples
:return: x, points
:return: w, weights
"""
pow2 = misc.power_function(2)
zemu = 2.0
bj = numpy.zeros(n)
for i in numpy.arange(n):
ip1 = i + 1.
bj[i] = numpy.sqrt(pow2(ip1) / (4. * pow2(ip1) - 1.))
x = numpy.zeros(n)
w = numpy.zeros(n)
w[0] = numpy.sqrt(zemu)
x_r, w_r = imtqlx(x, bj, w)
w_r = pow2(w_r)
return x_r, w_r
# Reference:
#
# Sylvan Elhay, Jaroslav Kautsky,
# Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
# Interpolatory Quadrature,
# ACM Transactions on Mathematical Software,
# Volume 13, Number 4, December 1987, pages 399-415.
#
# Roger Martin, James Wilkinson,
# The Implicit QL Algorithm,
# Numerische Mathematik,
# Volume 12, Number 5, December 1968, pages 377-383.
#
# This routine is a slightly modified version of the EISPACK routine to
# perform the implicit QL algorithm on a symmetric tridiagonal matrix.
#
# The authors thank the authors of EISPACK for permission to use this
# routine.
#
# It has been modified to produce the product Q' * Z, where Z is an input
# vector and Q is the orthogonal matrix diagonalizing the input matrix.
# The changes consist (essentially) of applying the orthogonal
# transformations directly to Z as they are generated.
# TODO: Fix fortran77-style indexes
def imtqlx(diagonal, subdiagonal, vec, abstol=1e-30, maxiterations = 30):
"""
Diagonalize symmetric tridiagonal matrix
:param diagonal: matrix diagonal, size N
:param subdiagonal: matrix subdiagonal, size N-1
:return: lam: diagonal entries of diagonalized matrix
:return: qtz: values of Q' * Z, where Q diagonalizes input matrix M
"""
n = diagonal.size
#assert subdiagonal.size == n-1
assert vec.size == n
pow2 = misc.power_function(2)
lam = numpy.zeros(n, dtype=numpy.complex128)
lam = diagonal
qtz = numpy.zeros(n, dtype=numpy.complex128)
qtz = vec
if n == 1:
return lam, qtz
subdiagonal[n-1] = 0
for l in numpy.arange(1, n + 1):
j = 0
while True:
for m in numpy.arange(l, n+1):
if m == n:
break
if numpy.abs(subdiagonal[m-1]) <= abstol * (numpy.abs(lam[m-1] + numpy.abs(lam[m]))):
break
p = lam[l-1]
if m == l:
break
if j >= maxiterations:
raise Exception("Failed to converge in {} iterations".format(maxiterations))
j += 1
g = (lam[l] - p) / (2.0 * subdiagonal[l - 1])
r = numpy.sqrt(pow2(g) + 1.0)
if g < 0:
t = g - r
else:
t = g + r
g = lam[m-1] - p + subdiagonal[l-1] / (g + t)
s = 1.0
c = 1.0
p = 0.0
mml = m - l
for ii in numpy.arange(1, mml + 1):
i = m - ii
f = s * subdiagonal[i - 1]
b = c * subdiagonal[i - 1]
if numpy.abs(g) <= numpy.abs(f):
c = g / f
r = numpy.sqrt(pow2(c) + 1.0)
subdiagonal[i] = f * r
s = 1.0 / r
c *= s
else:
s = f / g
r = numpy.sqrt(pow2(s) + 1.0)
subdiagonal[i] = g * r
c = 1.0 / r
s *= c
g = lam[i] - p
r = (lam[i - 1] - g) * s + 2.0 * c * b
p = s * r
lam[i] = g + p
g = c * r - b
f = qtz[i]
qtz[i] = s * qtz[i - 1] + c * f
qtz[i - 1] = c * qtz[i - 1] - s * f
lam[l - 1] -= p
subdiagonal[l - 1] = g
subdiagonal[m - 1] = 0.0
#Sort
for ii in numpy.arange(2, n + 1):
i = ii - 1
k = i
p = lam[i - 1]
for j in numpy.arange(ii, n + 1):
if lam[j - 1] < p:
k = j
p = lam[j - 1]
if k != i:
lam[k - 1] = lam[i-1]
lam[i-1] = p
p = qtz[i - 1]
qtz[i - 1] = qtz[k - 1]
qtz[k - 1] = p
return lam, qtz