-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_gauss.t
392 lines (337 loc) · 11.1 KB
/
test_gauss.t
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
388
389
390
391
392
-- SPDX-FileCopyrightText: 2024 René Hiemstra <rrhiemstar@gmail.com>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <t.kessler@posteo.de>
--
-- SPDX-License-Identifier: MIT
import "terratest/terratest"
local Alloc = require('alloc')
local gauss = require("gauss")
local tmath = require("mathfuns")
local vector = require("dvector")
local poly = require("poly")
local rn = require("range")
local Allocator = Alloc.Allocator
local DefaultAllocator = Alloc.DefaultAllocator()
local dvec = vector.DynamicVector(double)
tmath.isapprox = terralib.overloadedfunction("isapprox")
tmath.isapprox:adddefinition(terra(a : double, b : double, atol : double)
return tmath.abs(b-a) < atol
end)
tmath.isapprox:adddefinition(terra(v : dvec, w : dvec, atol : double)
if v:size() == w:size() then
var s = 0.0
for i = 0, v:size() do
var e = v(i) - w(i)
s = s + e * e
end
return tmath.sqrt(s) < atol
end
return false
end)
testenv(skip) "gauss Legendre quadrature" do
terracode
var alloc : DefaultAllocator
end
for N=1, 50, 3 do
local D = 2*N-1
local polynomial = poly.Polynomial(double, D)
terracode
--create polynomial sum_{i=0}^{D} x^i dx
var p = polynomial{}
for k = 0, D do
p.coeffs(k) = 1.0
end
--exact value integral of int_[-1,1] p(x) dx
var S = 0.0
for j = 1, D+1, 2 do
var J : double = j
S = S + 2.0 / J
end
end
testset(N) "GL " do
terracode
var x, w = gauss.legendre(&alloc, N)
var s = 0.0
for i = 0, N do
s = s + w(i) * p(x(i))
end
end
test x:size() == N and w:size() == N
test tmath.isapprox(w:sum(), 2.0, 1e-13)
test tmath.isapprox(s, S, 1e-13)
end
end
end
testenv(skip) "gauss Chebyshev quadrature" do
local poly2 = poly.Polynomial(double, 3)
local poly3 = poly.Polynomial(double, 4)
local N = 10
terracode
var alloc : DefaultAllocator
var p2 = poly2.from(0.0,0.0,1.0)
var p3 = poly3.from(0.0,0.0,0.0,1.0)
end
for N=2, 50, 7 do
testset(N) "Chebyshev t" do
terracode
var x, w = gauss.chebyshev_t(&alloc, N)
var s2, s3 = 0.0, 0.0
for i = 0, N do
s2 = s2 + w(i) * p2(x(i))
s3 = s3 + w(i) * p3(x(i))
end
end
test x:size() == N and w:size() == N
test tmath.isapprox(s2, tmath.pi/2., 1e-14)
test tmath.isapprox(s3, 0., 1e-14)
end
testset(N) "Chebyshev u" do
terracode
var x, w = gauss.chebyshev_u(&alloc, N)
var s2, s3 = 0.0, 0.0
for i = 0, N do
s2 = s2 + w(i) * p2(x(i))
s3 = s3 + w(i) * p3(x(i))
end
end
test x:size() == N and w:size() == N
test tmath.isapprox(s2, tmath.pi/8., 1e-14)
test tmath.isapprox(s3, 0., 1e-14)
end
testset(N) "Chebyshev v" do
terracode
var x, w = gauss.chebyshev_v(&alloc, N)
var s2, s3 = 0.0, 0.0
for i = 0, N do
s2 = s2 + w(i) * p2(x(i))
s3 = s3 + w(i) * p3(x(i))
end
end
test x:size() == N and w:size() == N
test tmath.isapprox(s2, tmath.pi/2., 1e-14)
test tmath.isapprox(s3, 3.*tmath.pi/8., 1e-14)
end
testset(N) "Chebyshev w" do
terracode
var x, w = gauss.chebyshev_w(&alloc, N)
var s2, s3 = 0.0, 0.0
for i = 0, N do
s2 = s2 + w(i) * p2(x(i))
s3 = s3 + w(i) * p3(x(i))
end
end
test x:size() == N and w:size() == N
test tmath.isapprox(s2, tmath.pi/2., 1e-14)
test tmath.isapprox(s3, -3.*tmath.pi/8., 1e-14)
end
end
end
testenv(skip) "gauss Jacobi quadrature" do
terracode
var alloc : DefaultAllocator
end
for N=1, 50, 3 do
testset(N) "reproduce gauss-Legendre" do
terracode
var x, w = gauss.jacobi(&alloc, N, 0, 0)
var xref, wref = gauss.legendre(&alloc, N)
end
test x:size() == N and w:size() == N
test tmath.isapprox(xref, x, 1e-13)
test tmath.isapprox(wref, w, 1e-13)
end
testset(N) "reproduce gauss-Chebyshev of the 1st kind" do
terracode
var x, w = gauss.jacobi(&alloc, N, -0.5, -0.5)
var xref, wref = gauss.chebyshev_t(&alloc, N)
end
test x:size() == N and w:size() == N
test tmath.isapprox(xref, x, 1e-13)
test tmath.isapprox(wref, w, 1e-13)
end
testset(N) "reproduce gauss-Chebyshev of the 2st kind" do
terracode
var x, w = gauss.jacobi(&alloc, N, 0.5, 0.5)
var xref, wref = gauss.chebyshev_u(&alloc, N)
end
test x:size() == N and w:size() == N
test tmath.isapprox(xref, x, 1e-13)
test tmath.isapprox(wref, w, 1e-13)
end
testset(N) "reproduce gauss-Chebyshev of the 3rd kind" do
terracode
var x, w = gauss.jacobi(&alloc, N, -0.5, 0.5)
var xref, wref = gauss.chebyshev_v(&alloc, N)
end
test x:size() == N and w:size() == N
test tmath.isapprox(xref, x, 1e-13)
test tmath.isapprox(wref, w, 1e-13)
end
testset(N) "reproduce gauss-Chebyshev of the 4rd kind" do
terracode
var x, w = gauss.jacobi(&alloc, N, 0.5, -0.5)
var xref, wref = gauss.chebyshev_w(&alloc, N)
end
test x:size() == N and w:size() == N
test tmath.isapprox(xref, x, 1e-13)
test tmath.isapprox(wref, w, 1e-13)
end
end
testset "n = 1" do
terracode
var a, b = 1.0, 2.0
var x, w = gauss.jacobi(&alloc, 1, a, b)
end
test x:size() == 1 and w:size() == 1
test tmath.isapprox(x(0), (b - a) / (a + b + 2), 1e-13)
test tmath.isapprox(w(0), 1.3333333333333333, 1e-13)
end
testset "a specific n = 10" do
terracode
var x, w = gauss.jacobi(&alloc, 10, 0.2, -1./30.)
end
test x:size() == 10 and w:size() == 10
test tmath.isapprox(x(6), 0.41467011760532446, 1e-13)
test tmath.isapprox(w(2), 0.24824523988590236, 1e-13)
end
testset "a specific n = 42" do
terracode
var x, w = gauss.jacobi(&alloc, 42, -.1, .3)
end
test x:size() == 42 and w:size() == 42
test tmath.isapprox(x(36), 0.912883347814032, 1e-13)
test tmath.isapprox(w(36), 0.046661910947553, 1e-13)
end
end
testenv "gauss hermite quadrature" do
terracode
var alloc : DefaultAllocator
end
for N=1, 30, 3 do
local D = 2*N-1
local polynomial = poly.Polynomial(double, D)
local iexact = terra(K : int)
if K % 2 == 1 then return 0.0 end
var S = tmath.sqrt(tmath.pi)
for k = 2, K+1, 2 do
S = (k-1) * S / 2.0
end
return S
end
terracode
--create polynomial sum_{i=0}^{D} exp(-x^2) * x^i dx
var p = polynomial{}
for k = 0, D do
p.coeffs(k) = 1.0
end
var S = 0.0
for j = 0, D do
S = S + iexact(j)
end
end
testset(N) "hermite" do
terracode
var x, w = gauss.hermite(&alloc, N)
var s = 0.0
for t in rn.zip(&x, &w) do
var xx, ww = t
s = s + p(xx) * ww
end
end
test x:size() == N and w:size() == N
test x.data:owns_resource() and w.data:owns_resource()
test tmath.isapprox(s, S, S * 1e-12)
end
testset(N) "scaled hermite" do
terracode
var x, w = gauss.hermite(&alloc, N, {origin=1.0, scaling=0.5})
var s = 0.0
for t in rn.zip(&x, &w) do
var xx, ww = t
s = s + ww
end
end
test x:size() == N and w:size() == N
test tmath.isapprox(s, 0.5 * tmath.sqrt(tmath.pi), 1e-12)
end
end --N=1, 50, 3
end
local struct interval{
_0 : double
_1 : double
}
interval:setconvertible("tuple")
interval.metamethods.__entrymissing = macro(function(entryname, self)
if entryname=="a" then
return `self._0
end
if entryname=="b" then
return `self._1
end
end)
testenv(skip) "API" do
terracode
var alloc : DefaultAllocator
end
testset "legendre - with interval" do
terracode
var x,w = gauss.legendre(&alloc, 3, interval{1.0, 3.0})
end
test x:size() == 3 and w:size() == 3
test tmath.isapprox(w:sum(), 2.0, 1e-13)
end
N = 3
local D = 2*N-1
local polynomial = poly.Polynomial(double, D)
terracode
--create polynomial sum_{i=0}^{4} x^i dx
var p = polynomial{}
for k = 0, D do
p.coeffs(k) = 1.0
end
end
testset "affine scaling" do
terracode
--int_1^4 p(x) dx
var x,w = gauss.legendre(&alloc, N, interval{1.0, 4.0})
var s = 0.0
for qr in rn.zip(x,w) do
var xx, ww = qr
s = s + ww * p(xx)
end
end
test x:size() == N and w:size() == N
test tmath.isapprox(w:sum(), 3.0, 1e-13)
test tmath.isapprox(s, 5997.0 / 20.0, 1e-13)
end
terracode
var alloc : DefaultAllocator
var Q_1 = gauss.legendre(&alloc, 3, interval{0.0, 3.0})
var Q_2 = gauss.legendre(&alloc, 4, interval{1.0, 5.0})
var Q_3 = gauss.legendre(&alloc, 5, interval{2.0, 7.0})
end
test Q_1.x.data:owns_resource() and Q_1.w.data:owns_resource()
test Q_2.x.data:owns_resource() and Q_2.w.data:owns_resource()
test Q_3.x.data:owns_resource() and Q_3.w.data:owns_resource()
testset "2D tensor-product rules" do
terracode
var rule = gauss.productrule(Q_1, Q_2)
var s : double = 0.0
for qr in rn.zip(&rule.x, &rule.w) do
var x, w = qr
s = s + w
end
end
test tmath.isapprox(s, 12.0, 1e-14)
end
testset "3D tensor-product rules - pass by reference" do
terracode
var rule = gauss.productrule(&Q_1, &Q_2, Q_3)
var s : double = 0.0
for qr in rn.zip(&rule.x, &rule.w) do
var x, w = qr
s = s + w
end
end
test tmath.isapprox(s, 60.0, 1e-14)
end
end