This repository has been archived by the owner on Aug 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_approximation.rkt
223 lines (210 loc) · 8.93 KB
/
test_approximation.rkt
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
#lang racket
(require rackunit)
(require math/matrix
math/array)
(require "approximation.rkt"
"fundamentals.rkt")
(define-simple-check (check-all-= actual-numbers expected-numbers epsilon)
(for ([actual actual-numbers]
[expected expected-numbers])
(check-= actual expected epsilon)))
(define-simple-check (check-matrix-= M N epsilon)
(check-all-= (matrix->list M) (matrix->list N) epsilon))
(define-simple-check (check-array-= A B epsilon)
(check-all-= (array->list A) (array->list B) epsilon))
(define (array-contiguous-slice arr start end)
(array-indexes-ref arr (for/array ([k (in-range start end)]) (vector k))))
(test-case
"Polyval"
(define p_0 (col-matrix [1.0]))
(define p_1 (col-matrix [0.0 2.0]))
(define p_11 (col-matrix [1.0 3.0]))
(define p_2 (col-matrix [0.0 0.0 1.0]))
(define p_22 (col-matrix [1.0 0.0 1.0]))
(define p_23 (col-matrix [2.0 1.0 3.0]))
(check-eqv? (polyval p_0 1.0) 1.0)
(check-eqv? (polyval p_0 (random 1 100)) 1.0)
(check-eqv? (polyval p_1 2.0) 4.0)
(check-eqv? (polyval p_11 2.0) (+ 1.0 (* 3.0 2.0)))
(check-eqv? (polyval p_2 2.0) 4.0)
(check-eqv? (polyval p_2 3.0) 9.0)
(check-eqv? (polyval p_22 2.0) (+ 1.0 (sqr 2.0)))
(check-eqv? (polyval p_23 3.0) (+ 2.0 3.0 (* 3.0 (sqr 3.0)))))
(test-case
"Climatology interpolation example"
(let ([xs '(-55 -25 5 35 65)]
[ys '(-3.25 -3.2 -3.02 -3.32 -3.1)])
(check-matrix-= (polyfit xs ys 4)
(col-matrix [-3.0132 3.7757e-4 -3.4684e-4 -4.5267e-7 8.2819e-8])
1e-3)))
(test-case
"Chebyshev nodes"
(let ([a -5]
[b 5]
[f (λ (x) (1 . / . (1 . + . (sqr x))))]
)
(define (err n)
(define xs (chebyshev-nodes a b n))
(define ys (map f xs))
(define c (polyfit xs ys n))
(define x1 (linspace -5 5 1000))
(define p (map (λ (x) (polyval c x)) x1))
(define f1 (map f x1))
(apply max (map (compose abs -) p f1)))
(check-= (err 5) 0.6386 1e-4)
(check-= (err 10) 0.1322 1e-4)
(check-= (err 20) 0.0177 1e-4)
;; this example outputs the following warning in octave 4.2.1:
;; warning: matrix singular to machine precision, rcond = 1.42019e-31
#;(check-= (err 40) 3.3996e-04 1e-4)))
(test-case
"Barycentric interpolation"
(define a -5)
(define b 5)
(define f (λ (x) (1 . / . (1 . + . (sqr x)))))
(let* ([xs (chebyshev-nodes a b 4)]
[ys (map f xs)])
(check-= (barycentric xs ys 4.9) 0.0088179 1e-7)
(check-= (barycentric xs ys -1.0) 0.89316 1e-5)
(check-= (barycentric xs ys 0.0) 1.0 1e-5)
(check-= (barycentric xs ys 227) 7.5591e+06 1e2))
(let* ([xs (chebyshev-nodes a b 12)]
[ys (map f xs)])
(check-= (barycentric xs ys 4.9) 0.035205 1e-6)
(check-= (barycentric xs ys -1.0) 0.55924 1e-5)
(check-= (barycentric xs ys 0.0) 1.0 1e-5)
;; Chosen nearer to b because further away the implementations diverge (?)
(check-= (barycentric xs ys 22) 3.2619e9 1e5))
(let* ([xs (chebyshev-nodes a b 128)]
[ys (map f xs)])
(check-= (barycentric xs ys 4.9) 0.039984 1e-6)
(check-= (barycentric xs ys -1.0) 1/2 1e-6)
(check-= (barycentric xs ys 0.0) 1.0 1e-9)
;; Notice how this has to be even nearer to b
(check-= (barycentric xs ys 5.1) 0.079201 1e-6)))
(test-case
"Trigonometric interpolation and FFT"
(check-array-= (dft (array #[1])) (array #[1]) 1e-9)
(check-array-= (dft (array #[1 1])) (array #[2 0]) 1e-9)
(let* ([n 9]
[xs (for/list ([k (in-range (+ n 1))]) (* 2 pi (1 . / . (+ n 1)) k))]
[ys (map (lambda (x) (* x (x . - . (* 2 pi)) (exp (* -1 x)))) xs)]
[Y (dft (list->array ys))]
[Y-ct (cooley-turkey-dft (list->array ys))])
(check-array-= Y-ct Y 1e-9))
(let* ([n 10]
[xs (for/list ([k (in-range (+ n 1))]) (* 2 pi (1 . / . (+ n 1)) k))]
[ys (map (lambda (x) (* x (x . - . (* 2 pi)) (exp (* -1 x)))) xs)]
[actual (idft (list->array ys))]
;; Numbers taken from Octave's ifft function
[expected (array #[-0.65749
-0.05236-0.42048i
0.12061-0.16318i
0.10256-0.06206i
0.08339-0.02501i
0.07455-0.00690i
0.07455+0.00690i
0.08339+0.02501i
0.10256+0.06206i
0.12061+0.16318i
-0.05236+0.42048i])])
(check-array-= actual expected 1e-5))
(let* ([n 10]
[xs (for/list ([k (in-range (+ n 1))]) (* 2 pi (1 . / . (+ n 1)) k))]
[ys (map (lambda (x) (* x (x . - . (* 2 pi)) (exp (* -1 x)))) xs)]
[actual (rader-dft (list->array ys))]
;; Numbers taken from Octave's fft function
[expected (array #[-7.23237
-0.57596+4.62532i
1.32667+1.79495i
1.12813+0.68268i
0.91729+0.27506i
0.82007+0.07585i
0.82007-0.07585i
0.91729-0.27506i
1.12813-0.68268i
1.32667-1.79495i
-0.57596-4.62532i])])
(check-array-= actual expected 1e-5))
;; Cooley-Turkey algorithm
(let* ([n 104]
[xs (for/list ([k (in-range (+ n 1))]) (* 2 pi (1 . / . (+ n 1)) k))]
[ys (map (lambda (x) (* x (x . - . (* 2 pi)) (exp (* -1 x)))) xs)]
[actual (dft (list->array ys))]
[expected (array #[-71.8046
-8.3086+44.2580i
9.7192+17.3651i
7.5798+6.9122i
5.1723+3.2650i
])])
(check-array-= (array-contiguous-slice actual 0 5) expected 1e-4))
;; Radix-2 DIT implemented in array-fft
(let* ([n 127]
[xs (for/list ([k (in-range (+ n 1))]) (* 2 pi (1 . / . (+ n 1)) k))]
[ys (map (lambda (x) (* x (x . - . (* 2 pi)) (exp (* -1 x)))) xs)]
[actual (dft (list->array ys))]
[expected (array #[-87.5457
-10.1411+53.9526i
11.8357+21.1689i
9.2275+8.4263i
6.2928+3.9803i])])
(check-array-= (array-contiguous-slice actual 0 5) expected 1e-4))
(let* ([n 127]
[xs (for/list ([k (in-range (+ n 1))]) (* 2 pi (1 . / . (+ n 1)) k))]
[ys (map (lambda (x) (* x (x . - . (* 2 pi)) (exp (* -1 x)))) xs)]
[actual (idft (list->array ys))]
[expected (array #[-0.68395
-0.07923-0.42150i
0.09247-0.16538i
0.07209-0.06583i
0.04916-0.03110i])])
(check-array-= (array-contiguous-slice actual 0 5) expected 1e-4))
;; Rader algorithm
(let* ([n 100]
[xs (for/list ([k (in-range (+ n 1))]) (* 2 pi (1 . / . (+ n 1)) k))]
[ys (map (lambda (x) (* x (x . - . (* 2 pi)) (exp (* -1 x)))) xs)]
[actual (dft (list->array ys))]
[expected (array #[-69.0667
-7.9897+42.5720i
9.3514+16.7036i
7.2935+6.6489i
4.9778+3.1406i])])
(check-array-= (array-contiguous-slice actual 0 5) expected 1e-4))
(let* ([n 100]
[xs (for/list ([k (in-range (+ n 1))]) (* 2 pi (1 . / . (+ n 1)) k))]
[ys (map (lambda (x) (* x (x . - . (* 2 pi)) (exp (* -1 x)))) xs)]
[actual (idft (list->array ys))]
[expected (array #[-0.68382
-0.07910-0.42150i
0.09259-0.16538i
0.07222-0.06583i
0.04929-0.03110i
])])
(check-array-= (array-contiguous-slice actual 0 5) expected 1e-4)))
(test-case
"fftshift"
(let ([x_even (array #[1 2 3 4 5 6])]
[expect (array #[4 5 6 1 2 3])])
(check-array-= (fftshift x_even) expect 1e-9))
(let ([x_odd (array #[1 2 3 4 5 6 7])]
[expect (array #[5 6 7 1 2 3 4])])
(check-array-= (fftshift x_odd) expect 1e-9)))
(test-case
"Interpolation in example 3.7"
(let* ([n 9]
[xs (for/list ([k (in-range (+ n 1))]) (* 2 pi (1 . / . (+ n 1)) k))]
[ys (for/array ([x xs]) (* x (x . - . (* 2 pi)) (exp (* -1 x))))]
[Y (dft ys)]
[C (array-map (λ (x) (x . / . (+ n 1))) (fftshift Y))]
[expect (array #[
0.08701
0.09258-0.02140i
0.10985-0.06008i
0.12681-0.16211i
-0.04673-0.42001i
-0.65203
-0.04673+0.42001i
0.12681+0.16211i
0.10985+0.06008i
0.09258+0.02140i])])
(check-array-= C expect 1e-5)))