-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgaussqr_prb.cpp
607 lines (496 loc) · 17.3 KB
/
gaussqr_prb.cpp
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
/*
* Copyright (c) 2005, Andrew Fernandes ([email protected]);
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the North Carolina State University nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
# include "gaussqr.h"
# include <algorithm>
using std::min;
using std::max;
# include <iostream>
using std::cout;
using std::endl;
# include <iomanip>
# include <cmath>
# include <iomanip>
using std::fabs;
#include <float.h>
using namespace std;
int main ( );
void test01 ( );
void test02 ( );
void test03 ( );
//****************************************************************************80
static real_t pow_ru ( real_t x , unsigned n )
//****************************************************************************80
{
real_t y = 1.0;
while ( n > 0 )
{
if ( n & 1 ) y *= x;
if ( n >>= 1 ) x *= x;
}
return ( y );
}
//****************************************************************************80
static real_t p0 ( const real_t &x )
//****************************************************************************80
{
return( (0.74e2 + (0.6e1 + (-0.92e2 + (0.75e2 + 0.23e2 * pow_ru(x,3)) * pow_ru(x,4)) * x) * x) * x * x ); } // degree 11
//****************************************************************************80
static real_t p1 ( const real_t &x )
//****************************************************************************80
{
return( (-0.8e1 + (-0.61e2 + (0.10e2 + (-0.23e2 + 0.98e2 * pow_ru(x,8)) * pow_ru(x,3)) * x) * pow_ru(x,3)) * pow_ru(x,6) ); } // degree 21
//****************************************************************************80
static real_t p2 ( const real_t &x )
//****************************************************************************80
{
return( (0.68e2 + (0.91e2 + (-0.81e2 + (0.40e2 - 0.47e2 * pow_ru(x,4)) * pow_ru(x,7)) * x) * pow_ru(x,4)) * pow_ru(x,3) ); } // degree 19
//****************************************************************************80
static real_t p3 ( const real_t &x )
//****************************************************************************80
{
return( (-0.15e2 + (-0.27e2 + (0.30e2 + (0.16e2 - 0.28e2 * x) * x * x) * pow_ru(x,5)) * pow_ru(x,7)) * pow_ru(x,5) ); } // degree 20
//****************************************************************************80
static real_t p4 ( const real_t &x )
//****************************************************************************80
{
return( (-0.91e2 + (0.92e2 + (0.43e2 + (-0.90e2 + 0.47e2 * pow_ru(x,3)) * x * x) * pow_ru(x,3)) * x) * pow_ru(x,9) ); } // degree 18
//****************************************************************************80
real_t absolute_error( const integer_t n , const real_t *estimated , const real_t *actual )
//****************************************************************************80
{
if ( n < 1 || estimated == 0 || actual == 0 )
return(FLT_MAX);
real_t err = 0.0;
for ( integer_t i = 0; i < n; i++ ) {
if ( actual[i] == 0.0 ) return(FLT_MAX);
err = max( err , std::fabs( (estimated[i]-actual[i])/actual[i] ) );
}
return(err);
}
//****************************************************************************80
static real_t abs_err( const real_t &approx , const real_t &exact )
//****************************************************************************80
{
return ( fabs((approx-exact)/exact) );
}
//****************************************************************************80
gaussqr_result test_distribution ( distribution_type distribution,
const real_t left, const real_t right, const domain_type domain,
integer_t n_rcoefs, const real_t *p )
//****************************************************************************80
{
gaussqr_result gqr = gaussqr_success;
const integer_t n = 1023;
real_t a0[n];
real_t a1[n];
real_t b0[n];
real_t b1[n];
real_t dx[n];
real_t z[n], q[n], x[n], f[n], w[n];
real_t err = FLT_MAX;
//
// Get the Fejer2 abscissas.
//
cout << " Get Fejer2 rule of order " << n << ".\n";
gqr = fejer2_abscissae ( n, z, q );
if ( gqr != gaussqr_success )
{
return gqr;
}
cout << " Map Fejer2 rule from [-1,+1] to this domain.\n";
gqr = map_fejer2_domain ( left, right, domain, n, z, x, dx );
if ( gqr != gaussqr_success )
{
return gqr;
}
cout << " Evaluate PDF.\n";
for ( integer_t i = 0; i < n; i++ )
{
gqr = standard_distribution_pdf(distribution,x[i],&f[i],p);
if ( gqr != gaussqr_success )
{
return gqr;
}
w[i] = f[i] * q[i] * dx[i];
}
//
// Calculate the recursion coefficients A0 and B0 from the quadrature scheme
//
cout << " Use Lanczos to calculate A0 and B0 from quadrature scheme.\n";
gqr = lanczos_tridiagonalize(n,x,w,a0,b0);
if ( gqr != gaussqr_success )
{
return gqr;
}
//
// calculate the recursion coefficients A1 and B1 from the known recurrences.
//
cout << " Look up recursion coefficients A1 and B1.\n";
gqr = standard_distribution_rcoeffs ( distribution, n, a1, b1, p );
if ( gqr != gaussqr_success )
{
return gqr;
}
//
// Calculate abscissa and weights
//
cout << " Get QR\n";
gqr = gaussqr_from_rcoeffs(n_rcoefs,a0,b0,x,w);
if ( gqr != gaussqr_success )
{
return gqr;
}
//
// Compare recurrence coefficients, theoretical vs derived
//
cout << " Compare.\n";
cout << "[i]\ta0\ta1\tb0\tb1" << endl << endl;
for ( integer_t i = 0; i < min(n,n_rcoefs); i++ )
{
cout << '[' << i << ']'
<< '\t' << a0[i]
<< '\t' << a1[i]
<< '\t' << b0[i]
<< '\t' << b1[i] << endl;
}
cout << endl;
err = absolute_error(n_rcoefs,b0,b1);
if ( gqr != gaussqr_success )
{
return gqr;
}
cout << "max(b_err) => " << err*100.0 << '%' << endl;
if ( err > 0.01 ) {
cout << "WARNING: large error detected; check the first few" << endl;
cout << " entries for convergence..." << endl;
}
cout << endl;
//
// print abscissa and weights
//
cout << "[i]\tabscissa\tweight" << endl;
cout << endl;
for ( integer_t i = 0; i < min(n,n_rcoefs); i++ )
{
cout << '[' << std::setw(2) << i << ']'
<< " " << std::setw(24) << std::setprecision(16) << x[i]
<< " " << std::setw(24) << std::setprecision(16) << w[i] << endl;
}
cout << endl;
return gqr;
}
//****************************************************************************80
int main ( int argc , char *argv[] )
//****************************************************************************80
{
gaussqr_result gqr = gaussqr_success;
real_t parameter[2] = { 0.0 , 0.0 };
cout << "\n";
cout << "GAUSSQR_PRB\n";
cout << " C++ version.\n";
cout << " Test the GAUSSQR library.\n";
test01 ( );
test02 ( );
test03 ( );
cout << "============= Gamma Coefficient Tests" << endl << endl;
parameter[0] = 1.0;
parameter[1] = 1.0;
gqr = test_distribution(distribution_gamma,0,FLT_MAX,domain_right_infinite,16,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 2.0;
parameter[1] = 2.0;
gqr = test_distribution(distribution_gamma,0,FLT_MAX,domain_right_infinite,16,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
cout << "============= Log-Normal Coefficient Tests" << endl << endl;
parameter[0] = 0.0;
parameter[1] = 1.0;
gqr = test_distribution(distribution_log_normal,0,FLT_MAX,domain_right_infinite,1,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
if ( false )
{
parameter[0] = 0.0;
parameter[1] = 1.0;
gqr = test_distribution(distribution_log_normal,0,FLT_MAX,domain_right_infinite,2,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
}
parameter[0] = 0.0;
parameter[1] = 1.0;
gqr = test_distribution(distribution_log_normal,0,FLT_MAX,domain_right_infinite,3,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 0.0;
parameter[1] = 0.5;
gqr = test_distribution(distribution_log_normal,0,FLT_MAX,domain_right_infinite,3,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 0.0;
parameter[1] = 1.0;
gqr = test_distribution(distribution_log_normal,0,FLT_MAX,domain_right_infinite,4,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 0.0;
parameter[1] = 1.0;
gqr = test_distribution(distribution_log_normal,0,FLT_MAX,domain_right_infinite,5,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 0.0;
parameter[1] = 1.0;
gqr = test_distribution(distribution_log_normal,0,FLT_MAX,domain_right_infinite,6,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 0.0;
parameter[1] = 1.0;
gqr = test_distribution(distribution_log_normal,0,FLT_MAX,domain_right_infinite,7,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 0.0;
parameter[1] = 1.0;
gqr = test_distribution(distribution_log_normal,0,FLT_MAX,domain_right_infinite,8,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 0.0;
parameter[1] = 1.0;
gqr = test_distribution(distribution_log_normal,0,FLT_MAX,domain_right_infinite,9,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
cout << "============= Student's T Coefficient Tests" << endl << endl;
parameter[0] = 18.0;
gqr = test_distribution(distribution_students_t,-FLT_MAX,FLT_MAX,domain_infinite,9,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 25.0;
gqr = test_distribution(distribution_students_t,-FLT_MAX,FLT_MAX,domain_infinite,13,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
cout << "============= Inverse-Gamma Coefficient Tests" << endl << endl;
parameter[0] = 12.0;
parameter[1] = 1.0;
gqr = test_distribution(distribution_inverse_gamma,0,FLT_MAX,domain_right_infinite,6,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 25.0;
parameter[1] = 2.0;
gqr = test_distribution(distribution_inverse_gamma,0,FLT_MAX,domain_right_infinite,12,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
cout << "============= Beta Coefficient Tests" << endl << endl;
parameter[0] = 1.0;
parameter[1] = 1.0;
gqr = test_distribution(distribution_beta,0,1,domain_finite,16,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 1.0;
parameter[1] = 2.25;
gqr = test_distribution(distribution_beta,0,1,domain_finite,16,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 2.25;
parameter[1] = 1.0;
gqr = test_distribution(distribution_beta,0,1,domain_finite,16,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 1.5;
parameter[1] = 3.5;
gqr = test_distribution(distribution_beta,0,1,domain_finite,16,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 2.0;
parameter[1] = 2.0;
gqr = test_distribution(distribution_beta,0,1,domain_finite,16,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
cout << "============= Fisher's F Coefficient Tests" << endl << endl;
parameter[0] = 37.0;
parameter[1] = 121.0;
gqr = test_distribution(distribution_fishers_f,0,FLT_MAX,domain_right_infinite,16,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
parameter[0] = 25.0;
parameter[1] = 32.0;
gqr = test_distribution(distribution_fishers_f,0,FLT_MAX,domain_right_infinite,8,parameter);
cout << endl << endl;
if ( gqr != gaussqr_success ) goto done;
done:
if ( gqr != gaussqr_success ) {
cout << "FAILED: code " << static_cast<int>(gqr) << endl;
}
return static_cast<int>(gqr);
}
//****************************************************************************80
void test01 ( )
//****************************************************************************80
//
// Purpose:
//
// TEST01 tests FEJER2_ABSCISSAE.
//
// Modified:
//
// 29 April 2013
//
// Author:
//
// John Burkardt
//
{
int i;
integer_t n;
real_t *q;
gaussqr_result result;
real_t *z;
cout << "\n";
cout << "TEST01\n";
cout << " FEJER2_ABSCISSAE computes points and weights for a\n";
cout << " Fejer Type 2 quadrature rule.\n";
for ( n = 1; n <= 10; n++ )
{
cout << "\n";
cout << " I X[i] W[i]\n";
cout << "\n";
z = new real_t[n];
q = new real_t[n];
result = fejer2_abscissae ( n, z, q );
for ( i = 0; i < n; i++ )
{
cout << " " << std::setw(2) << i
<< " " << std::setw(24) << std::setprecision(16) << z[i]
<< " " << std::setw(24) << std::setprecision(16) << q[i] << "\n";
}
delete [] z;
delete [] q;
}
return;
}
//****************************************************************************80
void test02 ( )
//****************************************************************************80
//
// Purpose:
//
// TEST02 tests FEJER2_ABSCISSAE.
//
// Modified:
//
// 16 May 2013
//
{
const integer_t np = 5;
gaussqr_result gqr;
real_t integral_p[np] = {
0.146e3/0.5e1,
-0.36e2/0.77e2,
-18.0,
-0.266e3/0.39e2,
0.4530e4/0.209e3 };
int m;
const integer_t max_fejer2_degree = 13;
const integer_t min_fejer2_degree = 5;
int n;
real_t (*p[np])( const real_t &x ) = { p0, p1 , p2 , p3 , p4 };
double sum;
cout << "\n";
cout << "TEST02\n";
cout << " Test Fejer2 quadrature.\n";
cout << "\n";
cout << " N poly approx exact %abs_err\n";
cout << "\n";
for ( m = 0; m < np; m++ )
{
for ( n = min_fejer2_degree; n < max_fejer2_degree; n++ )
{
real_t z[max_fejer2_degree];
real_t q[max_fejer2_degree];
real_t f[max_fejer2_degree];
gaussqr_result gqr = fejer2_abscissae ( n, z, q );
if ( gqr != gaussqr_success )
{
return;
}
for ( integer_t i = 0; i < n; i++ )
{
f[i] = p[m]( z[i] );
}
sum = 0.0;
for ( integer_t i = 0; i < n; i++ )
{
sum = sum + q[i] * f[i];
}
cout << " " << setw(2) << n
<< " " << setw(2) << m
<< " " << setw(14) << setprecision(6) << sum
<< " " << setw(14) << integral_p[m]
<< " " << setw(8) << setprecision(2) << abs_err ( sum, integral_p[m] ) * 100.0 << '%' << endl;
}
cout << "\n";
}
return;
}
//****************************************************************************80
void test03 ( )
//****************************************************************************80
//
// Purpose:
//
// TEST03 checks the computations for the Normal distribution.
//
// Modified:
//
// 18 May 2013
//
{
gaussqr_result gqr;
real_t parameter[2] = { 0.0 , 0.0 };
cout << "\n";
cout << "TEST03\n";
cout << " Compute quadrature rules for the normal distribution.\n";
parameter[0] = 0.0;
parameter[1] = 1.0;
cout << "\n";
cout << " Mean MU = " << parameter[0] << "\n";
cout << " Standard deviation SIGMA = " << parameter[1] << "\n";
gqr = test_distribution ( distribution_normal, -FLT_MAX, FLT_MAX,
domain_infinite, 16, parameter );
parameter[0] = 1.0;
parameter[1] = 0.5;
cout << "\n";
cout << " Mean MU = " << parameter[0] << "\n";
cout << " Standard deviation SIGMA = " << parameter[1] << "\n";
gqr = test_distribution ( distribution_normal, -FLT_MAX, FLT_MAX,
domain_infinite, 16, parameter );
return;
}