-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsparse_grid_open_dataset.cpp
4576 lines (4311 loc) · 109 KB
/
sparse_grid_open_dataset.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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# include <cstdlib>
# include <cmath>
# include <iostream>
# include <iomanip>
# include <fstream>
# include <ctime>
# include <string>
using namespace std;
int main ( int argc, char *argv[] );
int *abscissa_level_open_nd ( int level_max, int dim_num, int test_num,
int test_val[] );
void comp_next ( int n, int k, int a[], bool *more, int *h, int *t );
double f2_abscissa ( int order, int i );
double *f2_weights ( int order );
double gp_abscissa ( int order, int i );
double *gp_weights ( int order );
int i4_choose ( int n, int k );
int i4_max ( int i1, int i2 );
int i4_min ( int i1, int i2 );
int i4_modp ( int i, int j );
int i4_power ( int i, int j );
string i4_to_string ( int i4, string format );
void i4mat_transpose_print_some ( int m, int n, int a[], int ilo, int jlo,
int ihi, int jhi, string title );
int i4vec_product ( int n, int a[] );
int index_to_level_open ( int dim_num, int t[], int order, int level_max );
void level_to_order_open ( int dim_num, int level[], int order[] );
int *multigrid_index1 ( int dim_num, int order_1d[], int order_nd );
void multigrid_scale_open ( int dim_num, int order_nd, int level_max,
int level_1d[], int grid_index[] );
double nco_abscissa ( int order, int i );
double *nco_weights ( int order );
double *product_weights_open ( int dim_num, int order_1d[], int order_nd,
int rule );
double r8_epsilon ( );
double r8_huge ( );
void r8mat_transpose_print_some ( int m, int n, double a[], int ilo, int jlo,
int ihi, int jhi, string title );
void r8mat_write ( string output_filename, int m, int n, double table[] );
void r8vec_copy ( int n, double a1[], double a2[] );
void r8vec_direct_product2 ( int factor_index, int factor_order,
double factor_value[], int factor_num, int point_num, double w[] );
void r8vec_print_some ( int n, double a[], int i_lo, int i_hi, string title );
double r8vec_sum ( int n, double a[] );
int s_len_trim ( string s );
int sparse_grid_ofn_size ( int dim_num, int level_max );
int *spgrid_open_index ( int dim_num, int level_max, int point_num );
double *spgrid_open_weights ( int dim_num, int level_max, int point_num,
int grid_index[], int rule );
void timestamp ( );
double ts_abscissa ( int order, int i );
double *ts_weights ( int order );
void vec_colex_next2 ( int dim_num, int base[], int a[], bool *more );
//****************************************************************************80
int main ( int argc, char *argv[] )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for SPARSE_GRID_OPEN_DATASET.
//
// Discussion:
//
// This program computes a quadrature rule and writes it to a file.
//
// The quadrature rule is associated with a sparse grid derived from
// a Smolyak construction using an open 1D quadrature rule.
//
// The user specifies:
// * the spatial dimension of the quadrature region,
// * the level that defines the Smolyak grid.
// * the open 1D quadrature rule.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 01 February 2009
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Fabio Nobile, Raul Tempone, Clayton Webster,
// A Sparse Grid Stochastic Collocation Method for Partial Differential
// Equations with Random Input Data,
// SIAM Journal on Numerical Analysis,
// Volume 46, Number 5, 2008, pages 2309-2345.
//
{
int dim;
int dim_num;
int *grid_index;
double *grid_point;
double *grid_region;
double *grid_weight;
double h;
int level_max;
int m;
int n;
int order_max;
int point;
int point_num;
string r_filename;
int rule;
string w_filename;
double weight_sum;
string x_filename;
timestamp ( );
cout << "\n";
cout << "SPARSE_GRID_OPEN_DATASET\n";
cout << " C++ version\n";
cout << "\n";
cout << " Compiled on " << __DATE__ << " at " << __TIME__ << ".\n";
cout << "\n";
cout << " Compute the abscissas and weights of a quadrature rule\n";
cout << " associated with a sparse grid derived from a Smolyak\n";
cout << " construction based on an open quadrature rule.\n";
cout << "\n";
cout << " Inputs to the program include:\n";
cout << "\n";
cout << " DIM_NUM, the spatial dimension.\n";
cout << " (typically in the range of 2 to 10)\n";
cout << "\n";
cout << " LEVEL_MAX, the \"level\" of the sparse grid.\n";
cout << " (typically in the range of 0, 1, 2, 3, ...\n";
cout << "\n";
cout << " RULE, the 1D quadrature rule\n";
cout << " 2: Fejer Type 2 (\"F2\").\n";
cout << " 3: Gauss-Patterson (\"GP\");\n";
cout << " 4: Newton-Cotes Open (\"NCO\").\n";
cout << " 5: Tanh-Sinh (\"TS\").\n";
cout << "\n";
cout << " Output from the program includes:\n";
cout << "\n";
cout << " A printed table of the abscissas and weights.\n";
cout << "\n";
cout << " A set of files defining the quadrature rules.\n";
cout << "\n";
cout << " \"***_d?_level?_x.txt\", a file of the abscissas;\n";
cout << " \"***_d?_level?_w.txt\", a file of the weights;\n";
cout << " \"***_d?_level?_r.txt\", a file of the ranges.\n";
//
// Get the spatial dimension:
//
if ( 1 < argc )
{
dim_num = atoi ( argv[1] );
}
else
{
cout << "\n";
cout << "SPARSE_GRID_OPEN_DATASET:\n";
cout << " Enter the value of DIM_NUM.\n";
cin >> dim_num;
}
cout << "\n";
cout << " Spatial dimension requested is = " << dim_num << "\n";
//
// Get the product file root name:
//
if ( 2 < argc )
{
level_max = atoi ( argv[2] );
}
else
{
cout << "\n";
cout << "SPARSE_GRID_OPEN_DATASET:\n";
cout << " Enter the value of LEVEL_MAX.\n";
cin >> level_max;
}
cout << "\n";
cout << " The sparse grid level is = " << level_max << "\n";
//
// Get the rule index:
//
if ( 3 < argc )
{
rule = atoi ( argv[3] );
}
else
{
cout << "\n";
cout << "SPARSE_GRID_OPEN_DATASET:\n";
cout << " Enter the value of RULE.\n";
cout << " 2 = F2 = Fejer Type 2 Rule,\n";
cout << " 3 = GP = Gauss-Patterson,\n";
cout << " 4 = NCO = Newton-Cotes Open,\n";
cout << " 5 = TS = Tanh-Sinh.\n";
cin >> rule;
}
cout << "\n";
cout << " The 1D quadrature rule index = " << rule << "\n";
if ( rule == 2 )
{
cout << " F2: Fejer Type 2 Rule.\n";
}
else if ( rule == 3 )
{
cout << " GP: Gauss-Patterson Rule.\n";
}
else if ( rule == 4 )
{
cout << " NCO: Newton-Cotes Open Rule.\n";
}
else if ( rule == 5 )
{
cout << " TS: Tanh-Sinh Rule.\n";
}
else
{
cout << "\n";
cout << "SPARSE_GRID_OPEN_DATASET - Fatal error!\n";
cout << " Illegal value of RULE.\n";
exit ( 1 );
}
//
// How many distinct points will there be?
//
point_num = sparse_grid_ofn_size ( dim_num, level_max );
cout << "\n";
cout << " The number of distinct abscissas in the\n";
cout << " quadrature rule is determined from the spatial\n";
cout << " dimension DIM_NUM and the level LEVEL_MAX.\n";
cout << " For the given input, this value will be = " << point_num << "\n";
grid_point = new double[dim_num*point_num];
//
// Determine the index vector, relative to the full product grid,
// that identifies the points in the sparse grid.
//
grid_index = spgrid_open_index ( dim_num, level_max, point_num );
i4mat_transpose_print_some ( dim_num, point_num, grid_index, 1, 1,
dim_num, 10, " First 10 entries of grid index:" );
//
// Compute the physical coordinates of the abscissas.
//
order_max = i4_power ( 2, level_max + 1 ) - 1;
if ( rule == 5 )
{
m = level_max - 3;
n = ( ( order_max + 1 ) / 2 ) - 1;
h = 4.0 / ( double ) ( order_max + 1 );
cout << " M = " << m
<< " ORDER_MAX = " << order_max
<< " N = " << n
<< " H = " << h << "\n";
}
if ( rule == 2 )
{
for ( point = 0; point < point_num; point++ )
{
for ( dim = 0; dim < dim_num; dim++ )
{
grid_point[dim+point*dim_num] =
f2_abscissa ( order_max, grid_index[dim+point*dim_num] );
}
}
}
else if ( rule == 3 )
{
for ( point = 0; point < point_num; point++ )
{
for ( dim = 0; dim < dim_num; dim++ )
{
grid_point[dim+point*dim_num] =
gp_abscissa ( order_max, grid_index[dim+point*dim_num] );
}
}
}
else if ( rule == 4 )
{
for ( point = 0; point < point_num; point++ )
{
for ( dim = 0; dim < dim_num; dim++ )
{
grid_point[dim+point*dim_num] =
nco_abscissa ( order_max, grid_index[dim+point*dim_num] );
}
}
}
else if ( rule == 5 )
{
for ( point = 0; point < point_num; point++ )
{
for ( dim = 0; dim < dim_num; dim++ )
{
grid_point[dim+point*dim_num] =
ts_abscissa ( order_max, grid_index[dim+point*dim_num] );
}
}
}
r8mat_transpose_print_some ( dim_num, point_num, grid_point, 1, 1,
dim_num, 10, " First 10 entries of grid point:" );
//
// Gather the weights.
//
grid_weight = spgrid_open_weights ( dim_num, level_max, point_num,
grid_index, rule );
r8vec_print_some ( point_num, grid_weight, 1, 10,
" First 10 grid weights:" );
weight_sum = r8vec_sum ( point_num, grid_weight );
cout << "\n";
cout << " Weights sum to "
<< setprecision(16) << setw(24) << weight_sum << "\n";
cout << " Correct value is "
<< setprecision(16) << setw(24) << pow ( 2.0, dim_num ) << "\n";
//
// Write the rule to files.
//
if ( rule == 2 )
{
r_filename = "f2_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_r.txt";
w_filename = "f2_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_w.txt";
x_filename = "f2_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_x.txt";
}
else if ( rule == 3 )
{
r_filename = "gp_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_r.txt";
w_filename = "gp_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_w.txt";
x_filename = "gp_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_x.txt";
}
else if ( rule == 4 )
{
r_filename = "nco_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_r.txt";
w_filename = "nco_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_w.txt";
x_filename = "nco_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_x.txt";
}
else if ( rule == 5 )
{
r_filename = "ts_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_r.txt";
w_filename = "ts_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_w.txt";
x_filename = "ts_d" + i4_to_string ( dim_num, "%d" )
+ "_level" + i4_to_string ( level_max, "%d" ) + "_x.txt";
}
cout << "\n";
cout << " Creating X file = \"" << x_filename << "\".\n";
r8mat_write ( x_filename, dim_num, point_num, grid_point );
cout << " Creating W file = \"" << w_filename << "\".\n";
r8mat_write ( w_filename, 1, point_num, grid_weight );
grid_region = new double[dim_num*2];
for ( dim = 0; dim < dim_num; dim++ )
{
grid_region[dim+0*dim_num] = -1.0;
grid_region[dim+1*dim_num] = +1.0;
}
cout << " Creating R file = \"" << r_filename << "\".\n";
r8mat_write ( r_filename, dim_num, 2, grid_region );
delete [] grid_index;
delete [] grid_point;
delete [] grid_region;
delete [] grid_weight;
cout << "\n";
cout << "SPARSE_GRID_OPEN_DATASET:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
int *abscissa_level_open_nd ( int level_max, int dim_num, int test_num,
int test_val[] )
//****************************************************************************80
//
// Purpose:
//
// ABSCISSA_LEVEL_OPEN_ND: first level at which given abscissa is generated.
//
// Discussion:
//
// We assume an underlying product grid. In each dimension, this product
// grid has order 2**(LEVEL_MAX+1) - 1.
//
// We will say a sparse grid has total level LEVEL if each point in the
// grid has a total level of LEVEL or less.
//
// The "level" of a point is determined as the sum of the levels of the
// point in each spatial dimension.
//
// The level of a point in a single spatial dimension I is determined as
// the level, between 0 and LEVEL_MAX, at which the point's I'th index
// would have been generated.
//
//
// This description is terse and perhaps unenlightening. Keep in mind
// that the product grid is the product of 1D grids,
// that the 1D grids are built up by levels, having
// orders (total number of points ) 1, 3, 7, 15, 31 and so on,
// and that these 1D grids are nested, so that each point in a 1D grid
// has a first level at which it appears.
//
// Our procedure for generating the points of a sparse grid, then, is
// to choose a value LEVEL_MAX, to generate the full product grid,
// but then only to keep those points on the full product grid whose
// LEVEL is less than or equal to LEVEL_MAX.
//
//
// Note that this routine is really just testing out the idea of
// determining the level. Our true desire is to be able to start
// with a value LEVEL, and determine, in a straightforward manner,
// all the points that are generated exactly at that level, or
// all the points that are generated up to and including that level.
//
// This allows us to generate the new points to be added to one sparse
// grid to get the next, or to generate a particular sparse grid at once.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 19 April 2007
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Fabio Nobile, Raul Tempone, Clayton Webster,
// A Sparse Grid Stochastic Collocation Method for Partial Differential
// Equations with Random Input Data,
// SIAM Journal on Numerical Analysis,
// Volume 46, Number 5, 2008, pages 2309-2345.
//
// Parameters:
//
// Input, int LEVEL_MAX, controls the size of the final sparse grid.
//
// Input, int DIM_NUM, the spatial dimension.
//
// Input, int TEST_NUM, the number of points to be tested.
//
// Input, int TEST_VAL[DIM_NUM*TEST_NUM], the indices of the points
// to be tested. Normally, each index would be between 0 and 2**LEVEL_MAX.
//
// Output, int ABSCISSA_OPEN_LEVEL_ND[TEST_NUM], the value of LEVEL at which the
// point would first be generated, assuming that a standard sequence of
// nested grids is used.
//
{
int dim;
int j;
int level;
int order;
int t;
int *test_level;
test_level = new int[test_num];
if ( level_max == 0 )
{
for ( j = 0; j < test_num; j++ )
{
test_level[j] = 0;
}
return test_level;
}
order = i4_power ( 2, level_max ) + 1;
for ( j = 0; j < test_num; j++ )
{
test_level[j] = index_to_level_open ( dim_num, test_val+j*dim_num,
order, level_max );
}
return test_level;
}
//****************************************************************************80
void comp_next ( int n, int k, int a[], bool *more, int *h, int *t )
//****************************************************************************80
//
// Purpose:
//
// COMP_NEXT computes the compositions of the integer N into K parts.
//
// Discussion:
//
// A composition of the integer N into K parts is an ordered sequence
// of K nonnegative integers which sum to N. The compositions (1,2,1)
// and (1,1,2) are considered to be distinct.
//
// The routine computes one composition on each call until there are no more.
// For instance, one composition of 6 into 3 parts is
// 3+2+1, another would be 6+0+0.
//
// On the first call to this routine, set MORE = FALSE. The routine
// will compute the first element in the sequence of compositions, and
// return it, as well as setting MORE = TRUE. If more compositions
// are desired, call again, and again. Each time, the routine will
// return with a new composition.
//
// However, when the LAST composition in the sequence is computed
// and returned, the routine will reset MORE to FALSE, signaling that
// the end of the sequence has been reached.
//
// This routine originally used a SAVE statement to maintain the
// variables H and T. I have decided that it is safer
// to pass these variables as arguments, even though the user should
// never alter them. This allows this routine to safely shuffle
// between several ongoing calculations.
//
//
// There are 28 compositions of 6 into three parts. This routine will
// produce those compositions in the following order:
//
// I A
// - ---------
// 1 6 0 0
// 2 5 1 0
// 3 4 2 0
// 4 3 3 0
// 5 2 4 0
// 6 1 5 0
// 7 0 6 0
// 8 5 0 1
// 9 4 1 1
// 10 3 2 1
// 11 2 3 1
// 12 1 4 1
// 13 0 5 1
// 14 4 0 2
// 15 3 1 2
// 16 2 2 2
// 17 1 3 2
// 18 0 4 2
// 19 3 0 3
// 20 2 1 3
// 21 1 2 3
// 22 0 3 3
// 23 2 0 4
// 24 1 1 4
// 25 0 2 4
// 26 1 0 5
// 27 0 1 5
// 28 0 0 6
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 02 July 2008
//
// Author:
//
// Original FORTRAN77 version by Albert Nijenhuis, Herbert Wilf.
// C++ version by John Burkardt.
//
// Reference:
//
// Albert Nijenhuis, Herbert Wilf,
// Combinatorial Algorithms for Computers and Calculators,
// Second Edition,
// Academic Press, 1978,
// ISBN: 0-12-519260-6,
// LC: QA164.N54.
//
// Parameters:
//
// Input, int N, the integer whose compositions are desired.
//
// Input, int K, the number of parts in the composition.
//
// Input/output, int A[K], the parts of the composition.
//
// Input/output, bool *MORE.
// Set MORE = FALSE on first call. It will be reset to TRUE on return
// with a new composition. Each new call returns another composition until
// MORE is set to FALSE when the last composition has been computed
// and returned.
//
// Input/output, int *H, *T, two internal parameters needed for the
// computation. The user should allocate space for these in the calling
// program, include them in the calling sequence, but never alter them!
//
{
int i;
if ( !( *more ) )
{
*t = n;
*h = 0;
a[0] = n;
for ( i = 1; i < k; i++ )
{
a[i] = 0;
}
}
else
{
if ( 1 < *t )
{
*h = 0;
}
*h = *h + 1;
*t = a[*h-1];
a[*h-1] = 0;
a[0] = *t - 1;
a[*h] = a[*h] + 1;
}
*more = ( a[k-1] != n );
return;
}
//****************************************************************************80
double f2_abscissa ( int order, int i )
//****************************************************************************80
//
// Purpose:
//
// F2_ABSCISSA returns the I-th abscissa for the Fejer type 2 rule.
//
// Discussion:
//
// Our convention is that the abscissas are numbered from left to
// right.
//
// This rule is defined on [-1,1].
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 11 April 2007
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int ORDER, the order of the Fejer type 2 rule.
// 1 <= ORDER.
//
// Input, int I, the index of the desired abscissa. 1 <= I <= ORDER.
//
// Output, double F2_ABSCISSA, the value of the I-th
// abscissa in the Fejer type 2 rule of order ORDER.
//
{
double pi = 3.141592653589793;
double value;
if ( order < 1 )
{
value = - r8_huge ( );
return value;
}
if ( i < 1 || order < i )
{
cout << "\n";
cout << "F2_ABSCISSA - Fatal error!\n";
cout << " 1 <= I <= ORDER is required.\n";
exit ( 1 );
}
if ( order == 1 )
{
value = 0.0;
return value;
}
value = cos ( ( double ) ( order + 1 - i ) * pi
/ ( double ) ( order + 1 ) );
return value;
}
//****************************************************************************80
double *f2_weights ( int order )
//****************************************************************************80
//
// Purpose:
//
// F2_WEIGHTS computes weights for a Fejer type 2 rule.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 28 May 2007
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Philip Davis, Philip Rabinowitz,
// Methods of Numerical Integration,
// Second Edition,
// Dover, 2007,
// ISBN: 0486453391,
// LC: QA299.3.D28.
//
// Walter Gautschi,
// Numerical Quadrature in the Presence of a Singularity,
// SIAM Journal on Numerical Analysis,
// Volume 4, Number 3, 1967, pages 357-362.
//
// Joerg Waldvogel,
// Fast Construction of the Fejer and Clenshaw-Curtis Quadrature Rules,
// BIT Numerical Mathematics,
// Volume 43, Number 1, 2003, pages 1-18.
//
// Parameters:
//
// Input, int ORDER, the order.
//
// Output, double F2_WEIGHTS[ORDER], the weights.
//
{
int i;
int j;
double p;
double pi = 3.141592653589793;
double *theta;
double *w;
if ( order < 1 )
{
cout << "\n";
cout << "F2_WEIGHTS - Fatal error!\n";
cout << " ORDER < 1.\n";
exit ( 1 );
}
w = new double[order];
if ( order == 1 )
{
w[0] = 2.0;
return w;
}
else if ( order == 2 )
{
w[0] = 1.0;
w[1] = 1.0;
return w;
}
theta = new double[order];
for ( i = 1; i <= order; i++ )
{
theta[i-1] = ( double ) ( order + 1 - i ) * pi
/ ( double ) ( order + 1 );
}
for ( i = 1; i <= order; i++ )
{
w[i-1] = 1.0;
for ( j = 1; j <= ( ( order - 1 ) / 2 ); j++ )
{
w[i-1] = w[i-1] - 2.0 * cos ( 2.0 * ( double ) ( j ) * theta[i-1] )
/ ( double ) ( 4 * j * j - 1 );
}
if ( 2 < order )
{
p = 2.0 * ( double ) ( ( ( order + 1 ) / 2 ) ) - 1.0;
w[i-1] = w[i-1] - cos ( ( p + 1.0 ) * theta[i-1] ) / p;
}
}
for ( i = 0; i < order; i++ )
{
w[i] = 2.0 * w[i] / ( double ) ( order + 1 );
}
delete [] theta;
return w;
}
//****************************************************************************80
double gp_abscissa ( int level, int index )
//****************************************************************************80
//
// Purpose:
//
// GP_ABSCISSA returns the I-th abscissa for a Gauss-Patterson rule.
//
// Discussion:
//
// The rule is specified by its level.
//
// The number of points in the rule, known as the order, is
// related to the level by the formula:
//
// ORDER = 2^(LEVEL+1)-1.
//
// Only rules of order 1, 3, 7, 15, 31, 63 and 127 are allowed.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 23 December 2009
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Prem Kythe, Michael Schaeferkotter,
// Handbook of Computational Methods for Integration,
// Chapman and Hall, 2004,
// ISBN: 1-58488-428-2,
// LC: QA299.3.K98.
//
// Thomas Patterson,
// The Optimal Addition of Points to Quadrature Formulae,
// Mathematics of Computation,
// Volume 22, Number 104, October 1968, pages 847-856.
//
// Parameters:
//
// Input, int LEVEL, the level of the rule.
// 0 <= LEVEL <= 6.
//
// Input, int INDEX, the index of the point in the rule.
//
// Output, double GP_ABSCISSA, the value of the INDEX-th
// abscissa in the rule of level LEVEL.
//
{
int order;
static double x_001[1] = {
0.0 };
static double x_003[3] = {
-0.77459666924148337704,
0.0,
0.77459666924148337704 };
static double x_007[7] = {
-0.96049126870802028342,
-0.77459666924148337704,
-0.43424374934680255800,
0.0,
0.43424374934680255800,
0.77459666924148337704,
0.96049126870802028342 };
static double x_015[15] = {
-0.99383196321275502221,
-0.96049126870802028342,
-0.88845923287225699889,
-0.77459666924148337704,
-0.62110294673722640294,
-0.43424374934680255800,
-0.22338668642896688163,
0.0,
0.22338668642896688163,
0.43424374934680255800,
0.62110294673722640294,
0.77459666924148337704,
0.88845923287225699889,
0.96049126870802028342,
0.99383196321275502221 };
static double x_031[31] = {
-0.99909812496766759766,
-0.99383196321275502221,
-0.98153114955374010687,
-0.96049126870802028342,
-0.92965485742974005667,
-0.88845923287225699889,
-0.83672593816886873550,
-0.77459666924148337704,
-0.70249620649152707861,
-0.62110294673722640294,
-0.53131974364437562397,
-0.43424374934680255800,
-0.33113539325797683309,
-0.22338668642896688163,
-0.11248894313318662575,
0.0,
0.11248894313318662575,
0.22338668642896688163,
0.33113539325797683309,
0.43424374934680255800,
0.53131974364437562397,
0.62110294673722640294,
0.70249620649152707861,
0.77459666924148337704,
0.83672593816886873550,
0.88845923287225699889,
0.92965485742974005667,
0.96049126870802028342,
0.98153114955374010687,
0.99383196321275502221,
0.99909812496766759766 };
static double x_063[63] = {
-0.99987288812035761194,
-0.99909812496766759766,
-0.99720625937222195908,
-0.99383196321275502221,
-0.98868475754742947994,
-0.98153114955374010687,
-0.97218287474858179658,
-0.96049126870802028342,
-0.94634285837340290515,
-0.92965485742974005667,
-0.91037115695700429250,
-0.88845923287225699889,
-0.86390793819369047715,
-0.83672593816886873550,
-0.80694053195021761186,
-0.77459666924148337704,
-0.73975604435269475868,
-0.70249620649152707861,
-0.66290966002478059546,
-0.62110294673722640294,
-0.57719571005204581484,
-0.53131974364437562397,
-0.48361802694584102756,
-0.43424374934680255800,
-0.38335932419873034692,
-0.33113539325797683309,
-0.27774982202182431507,
-0.22338668642896688163,
-0.16823525155220746498,
-0.11248894313318662575,
-0.056344313046592789972,
0.0,
0.056344313046592789972,
0.11248894313318662575,
0.16823525155220746498,
0.22338668642896688163,
0.27774982202182431507,
0.33113539325797683309,
0.38335932419873034692,
0.43424374934680255800,
0.48361802694584102756,
0.53131974364437562397,
0.57719571005204581484,
0.62110294673722640294,
0.66290966002478059546,