-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflt.pyx
373 lines (277 loc) · 9.65 KB
/
flt.pyx
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
# flt: fast Legendre transform
#
# author: Nicolas Tessore <[email protected]>
# license: MIT
#
# cython: language_level=3, boundscheck=False, embedsignature=True
#
'''
Discrete Legendre Transform (:mod:`flt`)
========================================
This is a minimal Python package for fast discrete Legendre transforms (DLTs).
The implementation uses a recursive version of the matrix relations by Alpert &
Rokhlin (1991) to compute the DLT via a discrete cosine transform (DCT).
The package can be installed using pip::
pip install flt
Current functionality covers the absolutely minimal use case. Please open an
issue on GitHub if you would like to see anything added.
Reference/API
-------------
.. currentmodule:: flt
.. autosummary::
:toctree: api
:nosignatures:
dlt
idlt
dltmtx
idltmtx
theta
'''
__all__ = [
'dlt',
'idlt',
'dltmtx',
'idltmtx',
'theta',
]
import numpy as np
from scipy.fft import dct, idct
cdef extern from "dctdlt.c":
void dctdlt(unsigned int, unsigned int, const double*,
unsigned int, double*)
void dltdct(unsigned int, unsigned int, const double*,
unsigned int, double*)
def dlt(a, closed=False):
r'''discrete Legendre transform
Takes a function in :math:`\mathtt{n}` points and returns its discrete
Legendre transform coefficients from :math:`0` to :math:`\mathtt{lmax}
= \mathtt{n}-1`.
The function must be given at the points :math:`\cos(\theta)` returned by
:func:`flt.theta`. These can be distributed either over the open interval
:math:`\theta \in (0, \pi)`, or over the closed interval :math:`\theta \in
[0, \pi]`, in which case :math:`\theta_0 = 0` and :math:`\theta_{n-1} =
\pi`.
Parameters
----------
a : (n,) array_like
Function values.
closed : bool, optional
Compute DLT over open (``closed=False``) or closed (``closed=True``)
interval.
Returns
-------
b : (n,) array_like
Legendre coefficients :math:`0` to :math:`\mathtt{lmax}`.
See Also
--------
flt.idlt : the inverse operation
flt.theta : compute the angles at which the function is evaluated
Notes
-----
The discrete Legendre transform takes a function :math:`f(\cos\theta)` over
the domain :math:`\theta \in [0, \pi]` and returns the coefficients of the
series
.. math::
f(\cos\theta) = \sum_{l=0}^{l_{\max}} c_l \, P_l(\cos\theta) \;,
where :math:`P_l(\cos\theta)` is a Legendre polynomial.
The computation is done in two steps:
First, the function is transformed to the coefficients of a discrete cosine
transform (DCT) using an inverse DCT-III for the open interval, or an
inverse DCT-I for the closed interval.
Second, the DCT coefficients are transformed to the DLT coefficients using
a recursive version of the matrix relation given by [1]_.
References
----------
.. [1] Alpert, B. K., & Rokhlin, V. (1991). A fast algorithm for the
evaluation of Legendre expansions. SIAM Journal on Scientific and
Statistical Computing, 12(1), 158-179.
'''
# length n of the transform
if np.ndim(a) != 1:
raise TypeError('array must be 1d')
n = np.shape(a)[-1]
# type of the DCT depends on open or closed interval
if closed:
dcttype = 1
else:
dcttype = 3
# compute the DCT coefficients
b = idct(a, type=dcttype, axis=-1, norm='backward')
# fix last coefficient for DCT-I
if closed:
b[-1] /= 2
# memview for C interop
cdef double[::1] b_ = b
# transform DCT coefficients to DLT coefficients using C function
dctdlt(n, 1, &b_[0], 1, &b_[0])
# done
return b
def idlt(b, closed=False):
r'''inverse discrete Legendre transform
Takes the :math:`\mathtt{n} = \mathtt{lmax}+1` coefficients of a DLT and
returns the corresponding function in :math:`\mathtt{n}` points.
The function will be given at the points :math:`\cos(\theta)` returned by
:func:`flt.theta`. These can be distributed either over the open interval
:math:`\theta \in (0, \pi)`, or over the closed interval :math:`\theta \in
[0, \pi]`, in which case :math:`\theta_0 = 0` and :math:`\theta_{n-1} =
\pi`.
Parameters
----------
b : (n,) array_like
DLT coefficients from :math:`0` to :math:`\mathtt{lmax}`.
closed : bool, optional
Compute function over open (``closed=False``) or closed
(``closed=True``) interval.
Returns
-------
a : (n,) array_like
Function values.
See Also
--------
flt.dlt : the forward operation
flt.theta : compute the angles at which the function is evaluated
Notes
-----
The inverse discrete Legendre transform returns a function
:math:`f(\cos\theta)` over the domain :math:`\theta \in [0, \pi]` given the
coefficients of the series
.. math::
f(\cos\theta) = \sum_{l=0}^{l_{\max}} c_l \, P_l(\cos\theta) \;,
where :math:`P_l(\cos\theta)` is a Legendre polynomial.
The computation is done in two steps:
First, the DLT coefficients are transformed to the coefficients of a
discrete cosine transform (DCT) using a recursive version of the matrix
relation given by [1]_.
Second, the function values are computed using a DCT-III for the open
interval, or a DCT-I for the closed interval.
References
----------
.. [1] Alpert, B. K., & Rokhlin, V. (1991). A fast algorithm for the
evaluation of Legendre expansions. SIAM Journal on Scientific and
Statistical Computing, 12(1), 158-179.
'''
# length n of the transform
if np.ndim(b) != 1:
raise TypeError('array must be 1d')
n = np.shape(b)[-1]
# type of the DCT depends on open or closed interval
if closed:
dcttype = 1
else:
dcttype = 3
# this holds the DCT coefficients
a = np.empty(n, dtype=float)
# memviews for C interop
cdef double[::1] a_ = a
cdef double[::1] b_ = b
# transform DLT coefficients to DCT coefficients using C function
dltdct(n, 1, &b_[0], 1, &a_[0])
# fix last coefficient for DCT-I
if closed:
a[-1] *= 2
# perform the DCT
return dct(a, type=dcttype, axis=-1, norm='backward', overwrite_x=True)
def dltmtx(n, closed=False):
r'''discrete Legendre transform matrix
Computes a matrix that performs the discrete Legendre transform
:func:`flt.dlt` when multiplied by a vector of function values.
Parameters
----------
n : int
Length of the transform.
closed : bool, optional
Compute DLT over open (``closed=False``) or closed (``closed=True``)
interval.
Returns
-------
m : (n, n) array_like
Discrete Legendre transform matrix.
See Also
--------
flt.dlt : the equivalent operation
flt.theta : compute the angles at which the function is evaluated
Notes
-----
The discrete Legendre transform :func:`flt.dlt` performs the transformation
in place and does not compute the matrix :func:`flt.dltmtx`.
'''
# type of the DCT depends on open or closed interval
if closed:
dcttype = 1
else:
dcttype = 3
# compute the DCT matrix
a = idct(np.eye(n), type=dcttype, axis=0, norm=None, overwrite_x=True)
# memview for C interop
cdef double[:, ::1] a_ = a
# transform DCT column to DLT column using C function
for i in range(n):
dctdlt(n, n, &a_[0, i], n, &a_[0, i])
# done
return a
def idltmtx(n, closed=False):
r'''inverse discrete Legendre transform matrix
Computes a matrix that performs the inverse discrete Legendre transform
:func:`flt.idlt` when multiplied by a vector of coefficients.
Parameters
----------
n : int
Length of the transform.
closed : bool, optional
Compute inverse DLT over open (``closed=False``) or closed
(``closed=True``) interval.
Returns
-------
m : (n, n) array_like
Inverse discrete Legendre transform matrix.
See Also
--------
flt.idlt : the equivalent operation
flt.theta : compute the angles at which the function is evaluated
Notes
-----
The inverse discrete Legendre transform :func:`flt.idlt` performs the
transformation in place and does not compute the matrix
:func:`flt.idltmtx`.
'''
# type of the DCT depends on open or closed interval
if closed:
dcttype = 1
else:
dcttype = 3
# this is the input matrix
a = np.eye(n, dtype=float)
# memview for C interop
cdef double[:, ::1] a_ = a
# transform DLT unit column to DCT column using C function
for i in range(n):
dltdct(n, n, &a_[0, i], n, &a_[0, i])
# multiply by DCT matrix
return dct(a, type=dcttype, axis=0, norm=None, overwrite_x=True)
def theta(n, closed=False):
r'''compute angles for DLT function values
Returns :math:`n` angles :math:`\theta_0, \ldots, \theta_{n-1}` at which
the function :math:`f(\cos\theta)` is evaluated in :func:`flt.dlt` or
:func:`flt.idlt`.
The returned angles can be distributed either over the open interval
:math:`(0, \theta)`, or over the closed interval :math:`[0, \pi]`, in which
case :math:`\theta_0 = 0, \theta_{n-1} = \pi`.
Parameters
----------
n : int
Number of nodes.
Returns
-------
theta : array_like (n,)
Angles in radians.
closed : bool, optional
Compute angles over open (``closed=False``) or closed (``closed=True``)
interval.
'''
if closed:
t = np.linspace(0, np.pi, n, dtype=float)
else:
t = np.arange(n, dtype=float)
t += 0.5
t *= np.pi/n
return t