-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.py
228 lines (177 loc) · 6.88 KB
/
helpers.py
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
import numpy as np
import sympy
def full_like(x, val):
if isinstance(x, np.ndarray):
return np.full_like(x, val)
# assume x is just a float or int or sympy.Poly
return x * 0 + val
class Eval1D:
def __init__(self, x, rc):
self.rc = rc
self.x = x
self.k = 0
self.last = [None, None]
def __iter__(self):
return self
def __next__(self):
if self.k == 0:
out = full_like(self.x, self.rc.p0)
else:
a, b, c = self.rc[self.k - 1]
out = self.last[0] * (self.x * a - b)
if self.k > 1:
out -= self.last[1] * c
self.last[1] = self.last[0]
self.last[0] = out
self.k += 1
return out
class ProductEvalWithDegrees:
"""Evaluates the entire tree of orthogonal polynomials for an n-dimensional product
domain.
The computation is organized such that tree returns a list of arrays, L={0, ...,
dim}, where each level corresponds to the polynomial degree L. Further, each level
is organized like a discrete (dim-1)-dimensional simplex. Let's demonstrate this for
3D:
L = 1:
(0, 0, 0)
L = 2:
(1, 0, 0)
(0, 1, 0) (0, 0, 1)
L = 3:
(2, 0, 0)
(1, 1, 0) (1, 0, 1)
(0, 2, 0) (0, 1, 1) (0, 0, 2)
L = 4:
(3, 0, 0)
(2, 1, 0) (2, 0, 1)
(1, 2, 0) (1, 1, 1) (1, 0, 2)
(0, 3, 0) (0, 2, 1) (0, 1, 2) (0, 0, 3)
The main insight here that makes computation for n dimensions easy is that the next
level is composed by:
* Taking the whole previous level and adding +1 to the first entry.
* Taking the last row of the previous level and adding +1 to the second entry.
* Taking the last entry of the last row of the previous and adding +1 to the
third entry.
In the same manner this can be repeated for `dim` dimensions.
"""
def __init__(self, rc, int_1, X):
self.rc = rc
self.a = None
self.b = None
self.c = None
X = np.asarray(X)
self.dim = X.shape[0]
self.p0n = rc.p0 ** self.dim
self.int_p0 = self.p0n * int_1 ** self.dim
self.L = 0
self.X = X
self.last_values = [None, None]
self.last_degrees = [None, None]
def __iter__(self):
return self
def __next__(self):
X = self.X
dim = X.shape[0]
if self.L == 0:
values = np.array([X[0] * 0 + self.p0n])
degrees = np.array([np.zeros(dim, dtype=int)])
else:
aa, bb, cc = self.rc[self.L - 1]
# cannot just np.append here since numpy will convert to float64
# https://github.com/numpy/numpy/issues/18189
self.a = np.array([aa]) if self.a is None else np.append(self.a, aa)
self.b = np.array([bb]) if self.b is None else np.append(self.b, bb)
self.c = np.array([cc]) if self.c is None else np.append(self.c, cc)
a = self.a
b = self.b
c = self.c
values = []
degrees = []
mask0 = np.ones(len(self.last_degrees[0]), dtype=bool)
if self.L > 1:
mask1 = np.ones(len(self.last_degrees[1]), dtype=bool)
for i in range(dim):
lv0 = self.last_values[0][mask0]
idx0 = self.last_degrees[0][mask0][:, i]
val = lv0 * (np.multiply.outer(a[idx0], X[i]).T - b[idx0]).T
if self.L > 1:
lv1 = self.last_values[1][mask1]
idx1 = self.last_degrees[1][mask1][:, i]
yy = idx1 + 1 > 0
val[: len(idx1)][yy] -= (lv1[yy].T * c[idx1[yy] + 1]).T
values.append(val)
deg = self.last_degrees[0][mask0]
deg[:, i] += 1
degrees.append(deg)
# mask is True for all entries where the first `i` degrees are 0
mask0 &= self.last_degrees[0][:, i] == 0
if self.L > 1:
mask1 &= self.last_degrees[1][:, i] == 0
values = np.concatenate(values)
degrees = np.concatenate(degrees)
self.last_values[1] = self.last_values[0]
self.last_values[0] = values
self.last_degrees[1] = self.last_degrees[0]
self.last_degrees[0] = degrees
self.L += 1
return values, degrees
class ProductEval(ProductEvalWithDegrees):
"""Same as ProductEvalWithDegrees, but next() only returns the values."""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def __next__(self):
vals, _ = super().__next__()
return vals
class Eval135:
"""Evaluates a 1-3-5-tree as seen with associated Legendre polynomials and spherical
harmonics.
There are many recurrence relations that can be used to construct the associated
Legendre polynomials. However, only few are numerically stable. Many implementations
(including this one) use the classical Legendre recurrence relation with increasing
L.
The return value is a list of arrays, where `values[k]` hosts the `2*k+1` values of
the `k`th level of the tree
(0, 0)
(-1, 1) (0, 1) (1, 1)
(-2, 2) (-1, 2) (0, 2) (1, 2) (2, 2)
... ... ... ... ...
"""
def __init__(self, rc, x, xi=None, symbolic=False):
self.rc = rc
self.k = 0
self.x = x
# xi[0] == sqrt(1 - x**2) / exp(i*phi)
# xi[1] == sqrt(1 - x**2) * exp(i*phi)
if xi is None:
sqrt = np.vectorize(sympy.sqrt) if symbolic else np.sqrt
# Such functions aren't always polynomials, see, e.g.,
# <https://en.wikipedia.org/wiki/Associated_Legendre_polynomials>:
#
# > In general, when l and m are integers, the regular solutions are
# > sometimes called "associated Legendre polynomials", even though they are
# > not polynomials when m is odd.
a = sqrt(1 - x ** 2)
self.xi = [a, a]
else:
self.xi = xi
self.last = [None, None]
def __iter__(self):
return self
def __next__(self):
if self.k == 0:
out = np.array([full_like(self.x, self.rc.p0)])
else:
z0, z1, c0, c1 = self.rc[self.k]
out = np.concatenate(
[
[self.last[0][0] * (self.xi[0] * z0)],
self.last[0] * np.multiply.outer(c0, self.x),
[self.last[0][-1] * (self.xi[1] * z1)],
]
)
if self.k > 1:
out[2:-2] -= (self.last[1].T * c1).T
self.last[1] = self.last[0]
self.last[0] = out
self.k += 1
return out