-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChebDiff.lhs
387 lines (292 loc) · 10.4 KB
/
ChebDiff.lhs
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
% Chebyshev Approximations
% Dominic Steinitz
% 13th November 2014
---
bibliography: Legendre.bib
---
Introduction
============
It is well known that one can represent a sufficiently well-behaved
function on the interval $[-\pi, \pi]$ by its Fourier expansion. In
the case of a symmetric function, $f(-x) = f(x)$, this becomes
$$
f(\theta) = \frac{a_0}{2} + \sum_{n=1}^\infty a_n \cos{n\theta}
$$
If we make a change of variable, $x = \cos{\theta}$ we obtain
$$
g(x) = \frac{a_0}{2} + \sum_{n=1}^\infty a_n T_n(x)
$$
where $T_n(x) = \cos{n\theta}$ is the $n$-th Chebyshev polynomial and
$g$ is an aribtrary (sufficiently well-behaved) function on $[-1, 1]$.
Here are a few examples of Chebyshev polynomials.
```{.dia width='800'}
import ChebDiff
import ChebDiag
import Data.Vector ( toList )
dia = diag (\i x -> toList $ chebPoly i x)
````
We would like to approximate functions by a truncated Chebyshev
expansion, for example, in order to solve differential
equations. However, the coefficients are given by
$$
\begin{aligned}
a_n &= \frac{1}{\pi}\int_{-\pi}^{\pi} f(\theta)\cos{(n\theta)}\,\mathrm{d}\theta \\
&= \frac{1}{\pi}\int_{-\pi}^{\pi} g(cos{(\theta)})\cos{(n\theta)}\,\mathrm{d}\theta \\
&= \frac{1}{\pi}\int_{-1}^{1} \frac{g(x)T_n(x)}{\sqrt{1 - x^2}}\,\mathrm{d}x
\end{aligned}
$$
So it seems if we have to evaluate (potentially many) integrals in
order to derive the approximation then we have not gained very much.
Fortunately it turns out by using some of the many properties of
Chebyshev polynomials and the fast Fourier transform (FFT) we can
avoid integration and calcuate the approximation very efficiently.
Haskell Preamble
----------------
> {-# OPTIONS_GHC -Wall #-}
> {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
> {-# OPTIONS_GHC -fno-warn-type-defaults #-}
> {-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
> {-# OPTIONS_GHC -fno-warn-missing-methods #-}
> {-# OPTIONS_GHC -fno-warn-orphans #-}
> module ChebDiff where
> import Prelude hiding ( length, sum, zipWith, zipWith3,
> map, (++), reverse, drop, replicate )
> -- import qualified Prelude as P
> import Data.Complex
> import Data.Vector hiding ( tail )
> import qualified Data.Vector as V
> import Numeric.FFT
> -- import Text.Printf
Chebyshev Polynomial Properties
===============================
Whole books have been published on these, e.g., [@mason2002chebyshev],
but let us collect together the few we need in order to accomplish our
goal.
Chebyshev Polynomials are Polynomials
-------------------------------------
We have $T_0(x) = 1$, $T_1(x) = x$ and
$$
\begin{aligned}
T_m(x)
&= \cos m\theta \\
&= \cos m\theta + \cos (m - 2)\theta - \cos (m - 2)\theta \\
&= \cos{((m - 1)\theta + \theta)} + \cos{((m - 1)\theta - \theta)} - \cos (m - 2)\theta \\
&= 2\cos\theta\cos{((m - 1)\theta)} - \cos (m - 2)\theta \\
&= 2xT_{m-1}(x) - T_{m-2}(x)
\end{aligned}
$$
We could implement this directly
> chebPoly' :: Int -> Double -> Double
> chebPoly' 0 _ = 1
> chebPoly' 1 x = x
> chebPoly' n x = 2 * x * chebPoly' (n - 1) x - chebPoly' (n - 2) x
but of course this is highly inefficient and it is much better to use
an unfold (and return all the intermediate Chebyshev polynomials
evaluated at the desired point).
> chebPoly :: Int -> Double -> Vector Double
> chebPoly n x = unfoldrN (n + 1) chebPolyAux (1, x)
> where
> chebPolyAux (a, b) = Just (a, (b, 2 * x * b - a))
[ghci]
V.last $ chebPoly 1001 0.0
Discrete Orthogonality
----------------------
The Chebyshev polynomials satisify a discrete orthogonality condition
$$
\langle T_j, T_k\rangle \triangleq
\sum_{l=0}^n \cos j\hat{x}_l \cos k\hat{x}_l =
\begin{cases}
0 & \text{if } j \neq k \\
\frac{n}{2} & \text{if } j = k \neq 0 \\
n & \text{if } j = k = 0
\end{cases}
$$
where
$$
\hat{x}_l = \frac{\pi(l + 1/2)}{n+1}
$$
are the Chebyshev zeros of $T_{n+1}$ (they have other names but this seems safest).
The case $j = k = 0$ is obvious.
In the case $j \neq k$ and writing
$$
\cos(j\hat{x}_i) = \frac{1}{2}\big(z^{j\hat{x}_i} + z^{-j\hat{x}_i}\big) \\
$$
we have
$$
\begin{aligned}
\sum_{l=0}^n \big(z^{j\hat{x}_l} + z^{-j\hat{x}_l}\big)\big(z^{k\hat{x}_l} + z^{-k\hat{x}_l}\big) & =
\sum_{l=0}^n z^{(j+k)\hat{x}_l} + z^{-(j+k)\hat{x}_l} + z^{(j-k)\hat{x}_l} + z^{-(j-k)\hat{x}_l} \\
& = \sum_{l=0}^n z^{(j+k)(hl + h/2)} + z^{-(j+k)(hl + h/2)} + z^{(j-k)(hl + h/2)} + z^{-(j-k)(hl + h/2)} \\
& = z^{(j+k)h/2}\frac{z^{(j+k)(n+1)h} - 1}{z^{(j+k)h} - 1} + z^{-(j+k)h/2}\frac{z^{-(j+k)(n+1)h} - 1}{z^{-(j+k)h} - 1} \\
& \quad + z^{(j+k)h/2}\frac{z^{(j+k)(n+1)h} - 1}{z^{(j+k)h} - 1} + z^{-(j+k)h/2}\frac{z^{-(j+k)(n+1)h} - 1}{z^{-(j+k)h} - 1} \\
& = \frac{z^{(j+k)\pi} - 1}{z^{(j+k)h/2} - z^{-(j+k)h/2}} + \frac{z^{-(j+k)\pi} - 1}{z^{-(j+k)h/2} - z^{(j+k)h/2}} \\
& \quad + \frac{z^{(j+k)\pi} - 1}{z^{(j+k)h/2} - z^{-(j+k)h/2}} + \frac{z^{-(j+k)\pi} - 1}{z^{-(j+k)h/2} - z^{(j+k)h/2}}
\end{aligned}
$$
All the denominators on the RHS are imaginary and all the numerators
are real thus the RHS is imaginary. Since the LHS is real, both must
be 0.
A similar argument applies when $j = k \neq 0$ except that the last
two terms in the RHS of the first line are 1. Summing these and
equating real parts gives $\frac{2n}{4} = \frac{n}{2}$ as required.
The Chebyshev polynomials satisify another discrete orthogonality condition
$$
\langle T_j, T_k\rangle \triangleq
\sum_{l=0}^n \cos j\check{x}_l \cos k\check{x}_l =
\begin{cases}
0 & \text{if } j \neq k \\
\frac{n}{2} & \text{if } j = k \neq 0 \\
n & \text{if } j = k = 0
\end{cases}
$$
where
$$
\check{x}_l = \frac{\pi l}{n}
$$
are the Chebyshev extrema of $T_{n+1}$ (again they have other names but this seems safest).
Clenshaw
--------
We can approximate any function by its expansion in Chebyshev polynomials
$$
f(x) \approx \sum_0^n c_i T_i(x)
$$
We can substitute in a Chebyshev zero of $T_{n+1}$, $\hat{x}_k$
$$
\sum_{k=0}^{n} f(\hat{x}_k) T_j(\hat{x}_k) =
\sum_{i=0}^n c_i \sum_{k=0}^{n} \Bigg[T_i(\hat{x}_k)T_j(\hat{x}_k)\Bigg]
$$
and then apply the discrete orthogonality condition to obtain
$$
\begin{aligned}
c_0 &= \frac{1}{n}\sum_{k=0}^{n} f(\hat{x}_k) \\
c_i &= \frac{2}{n}\sum_{k=0}^{n} f(\hat{x}_k)T_i(\hat{x}_k)
\end{aligned}
$$
Now we can write the approximation as
$$
f(x) \approx \underbrace{\frac{1}{n}\sum_{k=0}^{n} f(\hat{x}_k)}_{c_0} +
\sum_{i=0}^n\underbrace{\frac{2}{n}\Bigg(\sum_{k=0}^{n}T_i(\hat{x}_k)f(\hat{x}_k)\Bigg)}_{c_i}T_i(x)
$$
> chebPolFit :: Vector Double -> (Double -> Double) -> Vector Double
> chebPolFit zs f = (sum ys / n') `cons` (unfoldrN (n - 1) chebUnfoldAux (replicate n 1, zs))
> where
> n = length zs
> n' = fromIntegral n
> ys = map f zs
> chebUnfoldAux (t1, t2) =
> Just ((sum (t2 .* ys)) * 2 / n',
> (t2, zipWith3 (\x a b -> 2 * x * b - a) zs t1 t2))
It is tempting to calculate the Chebyshev extrema directly.
> chebPoints' :: Floating a => Int -> Vector a
> chebPoints' n = map f (enumFromN 0 (n + 1))
> where
> n' = fromIntegral n
> f k = cos (pi * k' / n')
> where
> k' = fromIntegral k
But this has some unfortunate consequences
[ghci]
chebPoints' 2
It is better to use a method which produces symmetric values.
> chebPoints :: Floating a => Int -> Vector a
> chebPoints n = generate (n + 1) f
> where
> f i = sin (pi * fromIntegral (2 * i - n) / fromIntegral (2 * n))
[ghci]
chebPoints 2
> bigN :: Int
> bigN = 10
> x :: Vector (Complex Double)
> x = map realToFrac $ chebPoints bigN
> x' :: Vector (Complex Double)
> x' = generate
> (bigN + 1)
> (\i -> realToFrac $ sin (pi * fromIntegral (2 * i - bigN) / fromIntegral (2 * bigN)))
> v :: Vector (Complex Double)
> v = map (\x -> exp x * sin ( 5 * x)) x
> bigV :: Vector (Complex Double)
> bigV = v ++ (reverse $ slice 1 (bigN - 1) v)
> ii, jj, kk :: Vector (Complex Double)
> ii = generate bigN fromIntegral
> jj = cons 0 (generate (bigN - 1) (\i -> fromIntegral (i + 1 - bigN)))
> kk = ii ++ jj
> i1 :: Complex Double
> i1 = 0.0 :+ 1.0
> test :: IO ()
> test = do
> bigU <- fft bigV
> let bigU' = map (\z -> (realPart z :+ 0)) bigU
> preInv = zipWith (*) (replicate (2 * bigN) i1) (zipWith (*) kk bigU')
> bigW <- ifft preInv
> let bigW' = map (\z -> (realPart z :+ 0)) bigW
> -- putStrLn $ show $ bigU'
> -- putStrLn $ show preInv
> putStrLn $ show bigW'
> chebPolVal :: Vector Double -> Vector Double -> Vector Double
> chebPolVal cs xs = u .- xs .* ujp1
> where
> (u, ujp1) = us!(n-3)
> us = unfoldr chebPolValAux (initU, initUjp1, ds)
>
> initUjp1 = replicate m (cs!(n-1))
> initU = map ((cs!(n-2))+) $ map (2*cs!(n-1)*) xs
> ds = V.init (V.init cs)
>
> n = length cs
> m = length xs
>
> chebPolValAux (_, _, c)
> | V.null c = Nothing
> chebPolValAux (ujp1, ujp2, c)
> | otherwise = Just ((u, ujp1), (u, ujp1, V.init c))
> where
> u = map ((V.last c)+) $
> zipWith (-) (map (2*) $ zipWith (*) xs ujp1) ujp2
> witchOfAgnesi :: Double -> Double
> witchOfAgnesi x = 1 / (1+25 * x^2)
> chebZeros :: Int -> Vector Double
> chebZeros n = map f (enumFromN 0 n)
> where
> n' = fromIntegral (n - 1)
> f k = cos (pi * (2 * k' + 1) / (2 * n' + 2))
> where
> k' = fromIntegral k
> chebFft :: Vector a -> (a -> Complex Double) -> IO (Vector Double)
> chebFft ps f = do
> fcs <- fft $ map f ps'
> return $ map realPart $ zipWith (*) ns (V.take l fcs)
> where
> ns = recip2l `cons` (replicate (l - 2) (1 / l')) `snoc` recip2l
> recip2l = 1 / (2 * l')
> l = length ps
> l' = fromIntegral l - 1
> ps' = ps ++ reverse (slice 1 (l - 2) ps)
> testZeros :: Int -> Vector Double
> testZeros n = map (\z -> (chebPoly n z)!(n - 1)) (chebZeros (n - 1))
[ghci]
testZeros 5
```{.dia width='800'}
import ChebDiff
import ChebDiag
import Data.Vector ( toList )
dia = diagChebFit (toList x1) (toList y) witchOfAgnesi
````
> infixr 8 ^*
> (^*) :: Num a => a -> Vector a -> Vector a
> s ^* v = map (* s) v
> infixl 7 .*
> (.*) :: Num a => Vector a -> Vector a -> Vector a
> (.*) = zipWith (*)
> infixl 6 .-
> (.-) :: Num a => Vector a -> Vector a -> Vector a
> (.-) = zipWith (-)
To Be Moved
===========
> x1, c, y :: Vector Double
> x1 = generate 201 (\n -> (fromIntegral n - 100) / 100)
> c = chebPolFit (chebZeros 11) witchOfAgnesi
> y = chebPolVal c x1
https://hackage.haskell.org/package/polynomial
http://www.chebfun.org/docs/guide/guide08.html
Bibliography
============