-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdisk_rule.cpp
964 lines (899 loc) · 18.8 KB
/
disk_rule.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
# include <cmath>
# include <cstdlib>
# include <cstring>
# include <ctime>
# include <iomanip>
# include <iostream>
using namespace std;
# include "disk01_rule.hpp"
//****************************************************************************80
void disk_rule ( int nr, int nt, double xc, double yc, double rc, double w[],
double x[], double y[] )
//****************************************************************************80
//
// Purpose:
//
// DISK_RULE computes a quadrature rule for the general disk.
//
// Discussion:
//
// The general disk is the region:
//
// ( x - xc ) ^ 2 + ( y - yc ) ^ 2 <= rc ^ 2.
//
// The integral I(f) is then approximated by
//
// S(f) = sum ( 1 <= i <= NT * NR ) W(i) * F ( X(i), Y(i) ).
//
// Area = pi * RC ^ 2
//
// Q(f) = Area * S(f)
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 19 March 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int NR, the number of points in the radial rule.
//
// Input, int NT, the number of angles to use.
//
// Input, double XC, YC, the center of the disk.
//
// Input, double RC, the radius of the disk.
//
// Output, double W[NR*NT], the weights for the rule.
//
// Output, double X[NR*NT], Y[NR*NT], the points for the rule.
//
{
int i;
int j;
double *r01;
double *t01;
double *w01;
w01 = new double[nr];
r01 = new double[nr];
t01 = new double[nt];
disk01_rule ( nr, nt, w01, r01, t01 );
//
// Recompute the rule for the general circle in terms of X, Y.
//
for ( j = 0; j < nt; j++ )
{
for ( i = 0; i < nr; i++ )
{
w[i+j*nr] = w01[i];
x[i+j*nr] = xc + rc * r01[i] * cos ( t01[j] );
y[i+j*nr] = yc + rc * r01[i] * sin ( t01[j] );
}
}
//
// Free memory.
//
delete [] r01;
delete [] t01;
delete [] w01;
return;
}
//****************************************************************************80
double disk01_monomial_integral ( int e[2] )
//****************************************************************************80
//
// Purpose:
//
// DISK01_MONOMIAL_INTEGRAL returns monomial integrals in the unit disk in 2D.
//
// Discussion:
//
// The integration region is
//
// X^2 + Y^2 <= 1.
//
// The monomial is F(X,Y) = X^E(1) * Y^E(2).
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 03 January 2014
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Philip Davis, Philip Rabinowitz,
// Methods of Numerical Integration,
// Second Edition,
// Academic Press, 1984, page 263.
//
// Parameters:
//
// Input, int E[2], the exponents of X and Y in the
// monomial. Each exponent must be nonnegative.
//
// Output, double DISK01_MONOMIAL_INTEGRAL, the integral.
//
{
double arg;
int i;
double integral;
const double r = 1.0;
const double r8_pi = 3.141592653589793;
double s;
if ( e[0] < 0 || e[1] < 0 )
{
cerr << "\n";
cerr << "DISK01_MONOMIAL_INTEGRAL - Fatal error!\n";
cerr << " All exponents must be nonnegative.\n";
cerr << " E[0] = " << e[0] << "\n";
cerr << " E[1] = " << e[1] << "\n";
exit ( 1 );
}
if ( ( e[0] % 2 ) == 1 || ( e[1] % 2 ) == 1 )
{
integral = 0.0;
}
else
{
integral = 2.0;
for ( i = 0; i < 2; i++ )
{
arg = 0.5 * ( double ) ( e[i] + 1 );
integral = integral * r8_gamma ( arg );
}
arg = 0.5 * ( double ) ( e[0] + e[1] + 2 );
integral = integral / r8_gamma ( arg );
}
//
// Adjust the surface integral to get the volume integral.
//
s = e[0] + e[1] + 2;
integral = integral * pow ( r, s ) / ( double ) ( s );
return integral;
}
//****************************************************************************80
void disk01_rule ( int nr, int nt, double w[], double r[], double t[] )
//****************************************************************************80
//
// Purpose:
//
// DISK01_RULE computes a quadrature rule for the unit disk.
//
// Discussion:
//
// The unit disk is the region:
//
// x * x + y * y <= 1.
//
// The integral I(f) is then approximated by
//
// Q(f) = pi * sum ( 1 <= j <= NT ) sum ( 1 <= i <= NR )
// W(i) * F ( R(i) * cos(T(j)), R(i) * sin(T(j)) ).
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 17 March 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int NR, the number of points in the radial rule.
//
// Input, int NT, the number of angles to use.
//
// Output, double W[NR], the weights for the disk rule.
//
// Output, double R[NR], T[NT], the (R,Theta) points for the rule.
//
{
int ir;
int it;
const double r8_pi = 3.141592653589793;
double *wr;
double *xr;
//
// Request a Legendre rule for [-1,+1].
//
xr = new double[nr];
wr = new double[nr];
legendre_ek_compute ( nr, xr, wr );
//
// Shift the rule to [0,1].
//
for ( ir = 0; ir < nr; ir++ )
{
xr[ir] = ( xr[ir] + 1.0 ) / 2.0;
wr[ir] = wr[ir] / 2.0;
}
//
// Compute the disk rule.
//
for ( it = 0; it < nt; it++ )
{
t[it] = 2.0 * r8_pi * ( double ) ( it ) / ( double ) ( nt );
}
for ( ir = 0; ir < nr; ir++ )
{
w[ir] = wr[ir] / ( double ) ( nt );
r[ir] = sqrt ( xr[ir] );
}
delete [] wr;
delete [] xr;
return;
}
//****************************************************************************80
void imtqlx ( int n, double d[], double e[], double z[] )
//****************************************************************************80
//
// Purpose:
//
// IMTQLX diagonalizes a symmetric tridiagonal matrix.
//
// Discussion:
//
// This routine is a slightly modified version of the EISPACK routine to
// perform the implicit QL algorithm on a symmetric tridiagonal matrix.
//
// The authors thank the authors of EISPACK for permission to use this
// routine.
//
// It has been modified to produce the product Q' * Z, where Z is an input
// vector and Q is the orthogonal matrix diagonalizing the input matrix.
// The changes consist (essentially) of applying the orthogonal transformations
// directly to Z as they are generated.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 08 January 2010
//
// Author:
//
// Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky.
// C++ version by John Burkardt.
//
// Reference:
//
// Sylvan Elhay, Jaroslav Kautsky,
// Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
// Interpolatory Quadrature,
// ACM Transactions on Mathematical Software,
// Volume 13, Number 4, December 1987, pages 399-415.
//
// Roger Martin, James Wilkinson,
// The Implicit QL Algorithm,
// Numerische Mathematik,
// Volume 12, Number 5, December 1968, pages 377-383.
//
// Parameters:
//
// Input, int N, the order of the matrix.
//
// Input/output, double D(N), the diagonal entries of the matrix.
// On output, the information in D has been overwritten.
//
// Input/output, double E(N), the subdiagonal entries of the
// matrix, in entries E(1) through E(N-1). On output, the information in
// E has been overwritten.
//
// Input/output, double Z(N). On input, a vector. On output,
// the value of Q' * Z, where Q is the matrix that diagonalizes the
// input symmetric tridiagonal matrix.
//
{
double b;
double c;
double f;
double g;
int i;
int ii;
int itn = 30;
int j;
int k;
int l;
int m;
int mml;
double p;
double prec;
double r;
double s;
prec = r8_epsilon ( );
if ( n == 1 )
{
return;
}
e[n-1] = 0.0;
for ( l = 1; l <= n; l++ )
{
j = 0;
for ( ; ; )
{
for ( m = l; m <= n; m++ )
{
if ( m == n )
{
break;
}
if ( fabs ( e[m-1] ) <= prec * ( fabs ( d[m-1] ) + fabs ( d[m] ) ) )
{
break;
}
}
p = d[l-1];
if ( m == l )
{
break;
}
if ( itn <= j )
{
cerr << "\n";
cerr << "IMTQLX - Fatal error!\n";
cerr << " Iteration limit exceeded\n";
exit ( 1 );
}
j = j + 1;
g = ( d[l] - p ) / ( 2.0 * e[l-1] );
r = sqrt ( g * g + 1.0 );
g = d[m-1] - p + e[l-1] / ( g + fabs ( r ) * r8_sign ( g ) );
s = 1.0;
c = 1.0;
p = 0.0;
mml = m - l;
for ( ii = 1; ii <= mml; ii++ )
{
i = m - ii;
f = s * e[i-1];
b = c * e[i-1];
if ( fabs ( g ) <= fabs ( f ) )
{
c = g / f;
r = sqrt ( c * c + 1.0 );
e[i] = f * r;
s = 1.0 / r;
c = c * s;
}
else
{
s = f / g;
r = sqrt ( s * s + 1.0 );
e[i] = g * r;
c = 1.0 / r;
s = s * c;
}
g = d[i] - p;
r = ( d[i-1] - g ) * s + 2.0 * c * b;
p = s * r;
d[i] = g + p;
g = c * r - b;
f = z[i];
z[i] = s * z[i-1] + c * f;
z[i-1] = c * z[i-1] - s * f;
}
d[l-1] = d[l-1] - p;
e[l-1] = g;
e[m-1] = 0.0;
}
}
//
// Sorting.
//
for ( ii = 2; ii <= m; ii++ )
{
i = ii - 1;
k = i;
p = d[i-1];
for ( j = ii; j <= n; j++ )
{
if ( d[j-1] < p )
{
k = j;
p = d[j-1];
}
}
if ( k != i )
{
d[k-1] = d[i-1];
d[i-1] = p;
p = z[i-1];
z[i-1] = z[k-1];
z[k-1] = p;
}
}
return;
}
//****************************************************************************80
void legendre_ek_compute ( int n, double x[], double w[] )
//****************************************************************************80
//
// Purpose:
//
// LEGENDRE_EK_COMPUTE: Legendre quadrature rule by the Elhay-Kautsky method.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 19 April 2011
//
// Author:
//
// Original FORTRAN77 version by Sylvan Elhay, Jaroslav Kautsky.
// C++ version by John Burkardt.
//
// Reference:
//
// Sylvan Elhay, Jaroslav Kautsky,
// Algorithm 655: IQPACK, FORTRAN Subroutines for the Weights of
// Interpolatory Quadrature,
// ACM Transactions on Mathematical Software,
// Volume 13, Number 4, December 1987, pages 399-415.
//
// Parameters:
//
// Input, int N, the order.
//
// Output, double X[N], the abscissas.
//
// Output, double W[N], the weights.
//
{
double *bj;
int i;
double zemu;
//
// Define the zero-th moment.
//
zemu = 2.0;
//
// Define the Jacobi matrix.
//
bj = new double[n];
for ( i = 0; i < n; i++ )
{
bj[i] = ( double ) ( ( i + 1 ) * ( i + 1 ) )
/ ( double ) ( 4 * ( i + 1 ) * ( i + 1 ) - 1 );
bj[i] = sqrt ( bj[i] );
}
for ( i = 0; i < n; i++ )
{
x[i] = 0.0;
}
w[0] = sqrt ( zemu );
for ( i = 1; i < n; i++ )
{
w[i] = 0.0;
}
//
// Diagonalize the Jacobi matrix.
//
imtqlx ( n, x, bj, w );
for ( i = 0; i < n; i++ )
{
w[i] = w[i] * w[i];
}
delete [] bj;
return;
}
//****************************************************************************80
double r8_epsilon ( )
//****************************************************************************80
//
// Purpose:
//
// R8_EPSILON returns the R8 roundoff unit.
//
// Discussion:
//
// The roundoff unit is a number R which is a power of 2 with the
// property that, to the precision of the computer's arithmetic,
// 1 < 1 + R
// but
// 1 = ( 1 + R / 2 )
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 01 September 2012
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Output, double R8_EPSILON, the R8 round-off unit.
//
{
const double value = 2.220446049250313E-016;
return value;
}
//****************************************************************************80
double r8_gamma ( double x )
//****************************************************************************80
//
// Purpose:
//
// R8_GAMMA evaluates Gamma(X) for an R8.
//
// Discussion:
//
// The C MATH library includes a function GAMMA ( X ) which should be
// invoked instead of this function.
//
// This routine calculates the gamma function for a real argument X.
//
// Computation is based on an algorithm outlined in reference 1.
// The program uses rational functions that approximate the gamma
// function to at least 20 significant decimal digits. Coefficients
// for the approximation over the interval (1,2) are unpublished.
// Those for the approximation for 12 <= X are from reference 2.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 January 2008
//
// Author:
//
// Original FORTRAN77 version by William Cody, Laura Stoltz.
// C++ version by John Burkardt.
//
// Reference:
//
// William Cody,
// An Overview of Software Development for Special Functions,
// in Numerical Analysis Dundee, 1975,
// edited by GA Watson,
// Lecture Notes in Mathematics 506,
// Springer, 1976.
//
// John Hart, Ward Cheney, Charles Lawson, Hans Maehly,
// Charles Mesztenyi, John Rice, Henry Thatcher,
// Christoph Witzgall,
// Computer Approximations,
// Wiley, 1968,
// LC: QA297.C64.
//
// Parameters:
//
// Input, double X, the argument of the function.
//
// Output, double R8_GAMMA, the value of the function.
//
{
double c[7] = {
-1.910444077728E-03,
8.4171387781295E-04,
-5.952379913043012E-04,
7.93650793500350248E-04,
-2.777777777777681622553E-03,
8.333333333333333331554247E-02,
5.7083835261E-03 };
double eps = 2.22E-16;
double fact;
int i;
int n;
double p[8] = {
-1.71618513886549492533811E+00,
2.47656508055759199108314E+01,
-3.79804256470945635097577E+02,
6.29331155312818442661052E+02,
8.66966202790413211295064E+02,
-3.14512729688483675254357E+04,
-3.61444134186911729807069E+04,
6.64561438202405440627855E+04 };
bool parity;
const double pi = 3.1415926535897932384626434;
double q[8] = {
-3.08402300119738975254353E+01,
3.15350626979604161529144E+02,
-1.01515636749021914166146E+03,
-3.10777167157231109440444E+03,
2.25381184209801510330112E+04,
4.75584627752788110767815E+03,
-1.34659959864969306392456E+05,
-1.15132259675553483497211E+05 };
double res;
const double sqrtpi = 0.9189385332046727417803297;
double sum;
double value;
double xbig = 171.624;
double xden;
double xinf = 1.79E+308;
double xminin = 2.23E-308;
double xnum;
double y;
double y1;
double ysq;
double z;
parity = false;
fact = 1.0;
n = 0;
y = x;
//
// Argument is negative.
//
if ( y <= 0.0 )
{
y = - x;
y1 = ( double ) ( int ) ( y );
res = y - y1;
if ( res != 0.0 )
{
if ( y1 != ( double ) ( int ) ( y1 * 0.5 ) * 2.0 )
{
parity = true;
}
fact = - pi / sin ( pi * res );
y = y + 1.0;
}
else
{
res = xinf;
value = res;
return value;
}
}
//
// Argument is positive.
//
if ( y < eps )
{
//
// Argument < EPS.
//
if ( xminin <= y )
{
res = 1.0 / y;
}
else
{
res = xinf;
value = res;
return value;
}
}
else if ( y < 12.0 )
{
y1 = y;
//
// 0.0 < argument < 1.0.
//
if ( y < 1.0 )
{
z = y;
y = y + 1.0;
}
//
// 1.0 < argument < 12.0.
// Reduce argument if necessary.
//
else
{
n = ( int ) ( y ) - 1;
y = y - ( double ) ( n );
z = y - 1.0;
}
//
// Evaluate approximation for 1.0 < argument < 2.0.
//
xnum = 0.0;
xden = 1.0;
for ( i = 0; i < 8; i++ )
{
xnum = ( xnum + p[i] ) * z;
xden = xden * z + q[i];
}
res = xnum / xden + 1.0;
//
// Adjust result for case 0.0 < argument < 1.0.
//
if ( y1 < y )
{
res = res / y1;
}
//
// Adjust result for case 2.0 < argument < 12.0.
//
else if ( y < y1 )
{
for ( i = 1; i <= n; i++ )
{
res = res * y;
y = y + 1.0;
}
}
}
else
{
//
// Evaluate for 12.0 <= argument.
//
if ( y <= xbig )
{
ysq = y * y;
sum = c[6];
for ( i = 0; i < 6; i++ )
{
sum = sum / ysq + c[i];
}
sum = sum / y - y + sqrtpi;
sum = sum + ( y - 0.5 ) * log ( y );
res = exp ( sum );
}
else
{
res = xinf;
value = res;
return value;
}
}
//
// Final adjustments and return.
//
if ( parity )
{
res = - res;
}
if ( fact != 1.0 )
{
res = fact / res;
}
value = res;
return value;
}
//****************************************************************************80
double r8_sign ( double x )
//****************************************************************************80
//
// Purpose:
//
// R8_SIGN returns the sign of an R8.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 October 2004
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, double X, the number whose sign is desired.
//
// Output, double R8_SIGN, the sign of X.
//
{
double value;
if ( x < 0.0 )
{
value = -1.0;
}
else
{
value = 1.0;
}
return value;
}
//****************************************************************************80
void r8vec_print ( int n, double a[], string title )
//****************************************************************************80
//
// Purpose:
//
// R8VEC_PRINT prints an R8VEC.
//
// Discussion:
//
// An R8VEC is a vector of R8's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 16 August 2004
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int N, the number of components of the vector.
//
// Input, double A[N], the vector to be printed.
//
// Input, string TITLE, a title.
//
{
int i;
cout << "\n";
cout << title << "\n";
cout << "\n";
for ( i = 0; i < n; i++ )
{
cout << " " << setw(8) << i
<< ": " << setw(14) << a[i] << "\n";
}
return;
}
//****************************************************************************80
void timestamp ( )
//****************************************************************************80
//
// Purpose:
//
// TIMESTAMP prints the current YMDHMS date as a time stamp.
//
// Example:
//
// 31 May 2001 09:45:54 AM
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 08 July 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// None
//
{
# define TIME_SIZE 40
static char time_buffer[TIME_SIZE];
const struct std::tm *tm_ptr;
size_t len;
std::time_t now;
now = std::time ( NULL );
tm_ptr = std::localtime ( &now );
len = std::strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm_ptr );
std::cout << time_buffer << "\n";
return;
# undef TIME_SIZE
}