-
Notifications
You must be signed in to change notification settings - Fork 196
/
polylib_test.c
498 lines (436 loc) · 13.1 KB
/
polylib_test.c
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#include <stdio.h>
#include <math.h>
#include "polylib.h"
/* --------------------------------------------------------------------
To compile:
cc -g -c polylib.c -I ./
cc -g -c polylib_test.c -I ./
cc -g -o polytest polylib_test.o polylib.o -lm
* --------------------------------------------------------------------*/
/* -------------------------------------------------------------------
This is a routine to test the integration, differentiation and
interpolation routines in the polylib.c.
First, it performs the integral
/1 alpha beta alpha,beta
| (1-x) (1+x) P (x) dx = 0
/-1 n
for all -0.5 <= alpha <= 5 (increments of 0.5)
-0.5 <= beta <= 5 (increments of 0.5)
using np points where
NPLOWER <= np <= NPUPPER
2 <= n <= 2*np - delta
delta = 1 (gauss), 2(radau), 3(lobatto).
The integral is evaluated and if it is larger that EPS then the
value of alpha,beta,np,n and the integral is printed to the screen.
After every alpha value the statement
"finished checking all beta values for alpha = #"
is printed
The routine then evaluates the derivate of
d n n-1
-- x = n x
dx
for all -0.5 <= alpha <= 5 (increments of 0.5)
-0.5 <= beta <= 5 (increments of 0.5)
using np points where
NPLOWER <= np <= NPUPPER
2 <= n <= np - 1
The error is check in a pointwise sense and if it is larger than
EPS then the value of alpha,beta,np,n and the error is printed to
the screen. After every alpha value the statement
"finished checking all beta values for alpha = #"
is printed
Finally the routine evaluates the interpolation of
n n
z to x
where z are the quadrature zeros and x are the equispaced points
2*i
x = ----- - 1.0 (0 <= i <= np-1)
i (np-1)
for all -0.5 <= alpha <= 5 (increments of 0.5)
-0.5 <= beta <= 5 (increments of 0.5)
using np points where
NPLOWER <= np <= NPUPPER
2 <= n <= np - 1
The error is check in a pointwise sense and if it is larger than
EPS then the value of alpha,beta,np,n and the error is printed to
the screen. After every alpha value the statement
"finished checking all beta values for alpha = #"
is printed
The above checks are performed for all the Gauss, Gauss-Radau and
Gauss-Lobatto points. If you want to disable any routine then set
GAUSS_INT, GAUSS_RADAU_INT, GAUSS_LOBATTO_INT = 0
for the integration rouintes
GAUSS_DIFF,GAUSS_RADAU_DIFF, GAUSS_LOBATTO_DIFF = 0
for the differentiation routines
GAUSS_INTERP,GAUSS_RADAU_INTERP, GAUSS_LOBATTO_INTERP = 0
for the interpolation routines.
------------------------------------------------------------------*/
#define NPLOWER 5
#define NPUPPER 15
#define EPS 1e-12
#define GAUSS_INT 1
#define GAUSS_RADAUM_INT 1
#define GAUSS_RADAUP_INT 1
#define GAUSS_LOBATTO_INT 1
#define GAUSS_DIFF 1
#define GAUSS_RADAUM_DIFF 1
#define GAUSS_RADAUP_DIFF 1
#define GAUSS_LOBATTO_DIFF 1
#define GAUSS_INTERP 1
#define GAUSS_RADAUM_INTERP 1
#define GAUSS_RADAUP_INTERP 1
#define GAUSS_LOBATTO_INTERP 1
/* local routines */
double ddot (int, double *, int, double *, int);
double *dvector (int, int);
main(){
int np,n,i;
double *z,*w,*p,sum=0,alpha,beta,*d,*dt;
z = dvector(0,NPUPPER-1);
w = dvector(0,NPUPPER-1);
p = dvector(0,NPUPPER-1);
d = dvector(0,NPUPPER*NPUPPER-1);
dt = dvector(0,NPUPPER*NPUPPER-1);
#if GAUSS_INT
/* Gauss Integration */
printf("Begin checking Gauss integration\n");
alpha = -0.5;
while(alpha <= 5.0){
beta = -0.5;
while(beta <= 5.0){
for(np = NPLOWER; np <= NPUPPER; ++np){
zwgj(z,w,np,alpha,beta);
for(n = 2; n < 2*np-1; ++n){
jacobfd(np,z,p,NULL,n,alpha,beta);
sum = ddot(np,w,1,p,1);
if(fabs(sum)>EPS)
printf("alpha = %lf, beta = %lf, np = %d, n = %d integal was %lg\n"
,alpha,beta,np,n,sum);
}
}
beta += 0.5;
}
printf("finished checking all beta values for alpha = %lf\n",alpha);
alpha += 0.5;
}
printf("Finished checking Gauss Integration\n");
#endif
#if GAUSS_RADAUM_INT
/* Gauss Radau Integration */
printf("Begin checking Gauss Radau Integration\n");
alpha = -0.5;
while(alpha <= 5.0){
beta = -0.5;
while(beta <= 5.0){
for(np = NPLOWER; np <= NPUPPER; ++np){
zwgrjm(z,w,np,alpha,beta);
for(n = 2; n < 2*np-2; ++n){
jacobfd(np,z,p,NULL,n,alpha,beta);
sum = ddot(np,w,1,p,1);
if(fabs(sum)>EPS)
printf("alpha = %lf, beta = %lf, np = %d, n = %d integal was %lg\n"
,alpha,beta,np,n,sum);
}
}
beta += 0.5;
}
printf("finished checking all beta values for alpha = %lf\n",alpha);
alpha += 0.5;
}
printf("Finished checking Gauss Radau (z=-1) Integration\n");
#endif
#if GAUSS_RADAUP_INT
/* Gauss Radau Integration */
printf("Begin checking Gauss Radau Integration\n");
alpha = -0.5;
while(alpha <= 5.0){
beta = -0.5;
while(beta <= 5.0){
for(np = NPLOWER; np <= NPUPPER; ++np){
zwgrjp(z,w,np,alpha,beta);
for(n = 2; n < 2*np-2; ++n){
jacobfd(np,z,p,NULL,n,alpha,beta);
sum = ddot(np,w,1,p,1);
if(fabs(sum)>EPS)
printf("alpha = %lf, beta = %lf, np = %d, n = %d integal was %lg\n"
,alpha,beta,np,n,sum);
}
}
beta += 0.5;
}
printf("finished checking all beta values for alpha = %lf\n",alpha);
alpha += 0.5;
}
printf("Finished checking Gauss Radau (z=1) Integration\n");
#endif
#if GAUSS_LOBATTO_INT
/* Gauss Lobatto Integration */
printf("Begin checking Gauss Lobatto integration\n");
alpha = -0.5;
while(alpha <= 5.0){
beta = -0.5;
while(beta <= 5.0){
for(np = NPLOWER; np <= NPUPPER; ++np){
zwglj(z,w,np,alpha,beta);
for(n = 2; n < 2*np-3; ++n){
jacobfd(np,z,p,NULL,n,alpha,beta);
sum = ddot(np,w,1,p,1);
if(fabs(sum)>EPS)
printf("alpha = %lf, beta = %lf, np = %d, n = %d integal was %lg\n"
,alpha,beta,np,n,sum);
}
}
beta += 0.5;
}
printf("finished checking all beta values for alpha = %lf\n",alpha);
alpha += 0.5;
}
printf("Finished checking Gauss Lobatto Integration\n");
#endif
#if GAUSS_DIFF
printf("Begin checking differentiation through Gauss points\n");
alpha = -0.5;
while(alpha <= 5.0){
beta = -0.5;
while(beta <= 5.0){
for(np = NPLOWER; np <= NPUPPER; ++np){
zwgj(z,w,np,alpha,beta);
for(n = 2; n < np-1; ++n){
Dgj(d,dt,z,np,alpha,beta);
for(i = 0; i < np; ++i) p[i] = pow(z[i],n);
sum = 0;
for(i = 0; i < np; ++i)
sum += fabs(ddot(np,d+i*np,1,p,1) - n*pow(z[i],n-1));
sum /= np;
if(fabs(sum)>EPS)
printf("alpha = %lf, beta = %lf, np = %d, n = %d difference %lg\n"
,alpha,beta,np,n,sum);
}
}
beta += 0.5;
}
printf("finished checking all beta values for alpha = %lf\n",alpha);
alpha += 0.5;
}
printf("Finished checking Gauss Jacobi differentiation\n");
#endif
#if GAUSS_RADAUM_DIFF
printf("Begin checking differentiation through Gauss Radau points\n");
alpha = -0.5;
while(alpha <= 5.0){
beta = -0.5;
while(beta <= 5.0){
for(np = NPLOWER; np <= NPUPPER; ++np){
zwgrjm(z,w,np,alpha,beta);
for(n = 2; n < np-1; ++n){
Dgrjm(d,dt,z,np,alpha,beta);
for(i = 0; i < np; ++i) p[i] = pow(z[i],n);
sum = 0;
for(i = 0; i < np; ++i)
sum += fabs(ddot(np,d+i*np,1,p,1) - n*pow(z[i],n-1));
sum /= np;
if(fabs(sum)>EPS)
printf("alpha = %lf, beta = %lf, np = %d, n = %d difference %lg\n"
,alpha,beta,np,n,sum);
}
}
beta += 0.5;
}
printf("finished checking all beta values for alpha = %lf\n",alpha);
alpha += 0.5;
}
printf("Finished checking Gauss Radau (z=-1) differentiation\n");
#endif
#if GAUSS_RADAUP_DIFF
printf("Begin checking differentiation through Gauss Radau (z=1) points\n");
alpha = -0.5;
while(alpha <= 5.0){
beta = -0.5;
while(beta <= 5.0){
for(np = NPLOWER; np <= NPUPPER; ++np){
zwgrjp(z,w,np,alpha,beta);
for(n = 2; n < np-1; ++n){
Dgrjp(d,dt,z,np,alpha,beta);
for(i = 0; i < np; ++i) p[i] = pow(z[i],n);
sum = 0;
for(i = 0; i < np; ++i)
sum += fabs(ddot(np,d+i*np,1,p,1) - n*pow(z[i],n-1));
sum /= np;
if(fabs(sum)>EPS)
printf("alpha = %lf, beta = %lf, np = %d, n = %d difference %lg\n"
,alpha,beta,np,n,sum);
}
}
beta += 0.5;
}
printf("finished checking all beta values for alpha = %lf\n",alpha);
alpha += 0.5;
}
printf("Finished checking Gauss Radau (z=1) differentiation\n");
#endif
#if GAUSS_LOBATTO_DIFF
printf("Begin checking differentiation through Gauss Lobatto points\n");
alpha = -0.5;
while(alpha <= 5.0){
beta = -0.5;
while(beta <= 5.0){
for(np = NPLOWER; np <= NPUPPER; ++np){
zwglj(z,w,np,alpha,beta);
for(n = 2; n < np-1; ++n){
Dglj(d,dt,z,np,alpha,beta);
for(i = 0; i < np; ++i) p[i] = pow(z[i],n);
sum = 0;
for(i = 0; i < np; ++i)
sum += fabs(ddot(np,d+i*np,1,p,1) - n*pow(z[i],n-1));
sum /= np;
if(fabs(sum)>EPS)
printf("alpha = %lf, beta = %lf, np = %d, n = %d difference %lg\n"
,alpha,beta,np,n,sum);
}
}
beta += 0.5;
}
printf("finished checking all beta values for alpha = %lf\n",alpha);
alpha += 0.5;
}
printf("Finished checking Gauss Lobatto differentiation\n");
#endif
/* check interpolation routines */
#if GAUSS_INTERP
printf("Begin checking interpolation through Gauss points\n");
alpha = -0.5;
while(alpha <= 5.0){
beta = -0.5;
while(beta <= 5.0){
for(np = NPLOWER; np <= NPUPPER; ++np){
zwgj(z,w,np,alpha,beta);
for(n = 2; n < np-1; ++n){
for(i = 0; i < np; ++i) {
w[i] = 2.0*i/(double)(np-1)-1.0;
p[i] = pow(z[i],n);
}
Imgj(d,z,w,np,np,alpha,beta);
sum = 0;
for(i = 0; i < np; ++i)
sum += fabs(ddot(np,d+i*np,1,p,1) - pow(w[i],n));
sum /= np;
if(fabs(sum)>EPS)
printf("alpha = %lf, beta = %lf, np = %d, n = %d difference %lg\n"
,alpha,beta,np,n,sum);
}
}
beta += 0.5;
}
printf("finished checking all beta values for alpha = %lf\n",alpha);
alpha += 0.5;
}
printf("Finished checking Gauss Jacobi interpolation\n");
#endif
#if GAUSS_RADAUM_INTERP
printf("Begin checking Interpolation through Gauss Radau (z=-1) points\n");
alpha = -0.5;
while(alpha <= 5.0){
beta = -0.5;
while(beta <= 5.0){
for(np = NPLOWER; np <= NPUPPER; ++np){
zwgrjm(z,w,np,alpha,beta);
for(n = 2; n < np-1; ++n){
for(i = 0; i < np; ++i) {
w[i] = 2.0*i/(double)(np-1)-1.0;
p[i] = pow(z[i],n);
}
Imgrjm(d,z,w,np,np,alpha,beta);
sum = 0;
for(i = 0; i < np; ++i)
sum += fabs(ddot(np,d+i*np,1,p,1) - pow(w[i],n));
sum /= np;
if(fabs(sum)>EPS)
printf("alpha = %lf, beta = %lf, np = %d, n = %d difference %lg\n"
,alpha,beta,np,n,sum);
}
}
beta += 0.5;
}
printf("finished checking all beta values for alpha = %lf\n",alpha);
alpha += 0.5;
}
printf("Finished checking Gauss Radua Jacobi (z=-1) interpolation\n");
#endif
#if GAUSS_RADAUP_INTERP
printf("Begin checking Interpolation through Gauss Radau (z=1) points\n");
alpha = -0.5;
while(alpha <= 5.0){
beta = -0.5;
while(beta <= 5.0){
for(np = NPLOWER; np <= NPUPPER; ++np){
zwgrjp(z,w,np,alpha,beta);
for(n = 2; n < np-1; ++n){
for(i = 0; i < np; ++i) {
w[i] = 2.0*i/(double)(np-1)-1.0;
p[i] = pow(z[i],n);
}
Imgrjp(d,z,w,np,np,alpha,beta);
sum = 0;
for(i = 0; i < np; ++i)
sum += fabs(ddot(np,d+i*np,1,p,1) - pow(w[i],n));
sum /= np;
if(fabs(sum)>EPS)
printf("alpha = %lf, beta = %lf, np = %d, n = %d difference %lg\n"
,alpha,beta,np,n,sum);
}
}
beta += 0.5;
}
printf("finished checking all beta values for alpha = %lf\n",alpha);
alpha += 0.5;
}
printf("Finished checking Gauss Radau (z=1) interpolation\n");
#endif
#if GAUSS_LOBATTO_INTERP
printf("Begin checking Interpolation through Gauss Lobatto points\n");
alpha = -0.5;
while(alpha <= 5.0){
beta = -0.5;
while(beta <= 5.0){
for(np = NPLOWER; np <= NPUPPER; ++np){
zwglj(z,w,np,alpha,beta);
for(n = 2; n < np-1; ++n){
for(i = 0; i < np; ++i) {
w[i] = 2.0*i/(double)(np-1)-1.0;
p[i] = pow(z[i],n);
}
Imglj(d,z,w,np,np,alpha,beta);
sum = 0;
for(i = 0; i < np; ++i)
sum += fabs(ddot(np,d+i*np,1,p,1) - pow(w[i],n));
sum /= np;
if(fabs(sum)>EPS)
printf("alpha = %lf, beta = %lf, np = %d, n = %d difference %lg\n"
,alpha,beta,np,n,sum);
}
}
beta += 0.5;
}
printf("finished checking all beta values for alpha = %lf\n",alpha);
alpha += 0.5;
}
printf("Finished checking Gauss Lobatto interploation\n");
#endif
free(z); free(w); free(p); free(d); free(dt);
}
double ddot (int n, double *x, int incx, double *y, int incy)
{
register double sum = 0.;
while (n--) {
sum += (*x) * (*y);
x += incx;
y += incy;
}
return sum;
}
double *dvector(int nl,int nh)
{
double *v;
v = (double *)malloc((unsigned) (nh-nl+1)*sizeof(double));
return v-nl;
}