-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpyramid_integrals.cpp
735 lines (666 loc) · 13.5 KB
/
pyramid_integrals.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
# include <cmath>
# include <cstdlib>
# include <iostream>
# include <iomanip>
# include <ctime>
# include <cstring>
using namespace std;
# include "pyramid_integrals.hpp"
//****************************************************************************80
double *monomial_value ( int m, int n, int e[], double x[] )
//****************************************************************************80
//
// Purpose:
//
// MONOMIAL_VALUE evaluates a monomial.
//
// Discussion:
//
// This routine evaluates a monomial of the form
//
// product ( 1 <= i <= m ) x(i)^e(i)
//
// where the exponents are nonnegative integers. Note that
// if the combination 0^0 is encountered, it should be treated
// as 1.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 08 May 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, the spatial dimension.
//
// Input, int N, the number of points at which the
// monomial is to be evaluated.
//
// Input, int E[M], the exponents.
//
// Input, double X[M*N], the point coordinates.
//
// Output, double MONOMIAL_VALUE[N], the value of the monomial.
//
{
int i;
int j;
double *v;
v = new double[n];
for ( j = 0; j < n; j++ )
{
v[j] = 1.0;
}
for ( i = 0; i < m; i++ )
{
if ( 0 != e[i] )
{
for ( j = 0; j < n; j++ )
{
v[j] = v[j] * pow ( x[i+j*m], e[i] );
}
}
}
return v;
}
//****************************************************************************80
double pyramid01_monomial_integral ( int expon[3] )
//****************************************************************************80
//
// Purpose:
//
// PYRAMID01_MONOMIAL_INTEGRAL: monomial integral in a unit pyramid.
//
// Discussion:
//
// This function returns the value of the integral of X^ALPHA Y^BETA Z^GAMMA
// over the unit pyramid.
//
// The integration region is:
//
// - ( 1 - Z ) <= X <= 1 - Z
// - ( 1 - Z ) <= Y <= 1 - Z
// 0 <= Z <= 1.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 22 June 2015
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Arthur Stroud,
// Approximate Calculation of Multiple Integrals,
// Prentice Hall, 1971,
// ISBN: 0130438936,
// LC: QA311.S85.
//
// Parameters:
//
// Input, int EXPON[3], the exponents.
//
// Output, double PYRAMID01_MONOMIAL_INTEGRAL, the integral of the monomial
// over the pyramid.
//
{
int i;
int i_hi;
double value;
value = 0.0;
if ( ( expon[0] % 2 ) == 0 && ( expon[1] % 2 ) == 0 )
{
i_hi = 2 + expon[0] + expon[1];
for ( i = 0; i <= i_hi; i++ )
{
value = value + r8_mop ( i ) * r8_choose ( i_hi, i )
/ ( double ) ( i + expon[2] + 1 );
}
value = value
* 2.0 / ( double ) ( expon[0] + 1 )
* 2.0 / ( double ) ( expon[1] + 1 );
}
return value;
}
//****************************************************************************80
double *pyramid01_sample ( int n, int &seed )
//****************************************************************************80
//
// Purpose:
//
// PYRAMID01_SAMPLE: sample the unit pyramid.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 14 April 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int N, the number of samples desired.
//
// Input/output, int SEED, a seed for the random
// number generator.
//
// Output, double PYRAMID01_SAMPLE[3*N], the sample values.
//
{
int j;
static int m = 3;
static double one_third = 1.0 / 3.0;
double *x;
x = r8mat_uniform_01_new ( m, n, seed );
for ( j = 0; j < n; j++ )
{
x[2+j*3] = 1.0 - pow ( x[2+j*3], one_third );
x[1+j*3] = ( 1.0 - x[2+j*3] ) * ( 2.0 * x[1+j*3] - 1.0 );
x[0+j*3] = ( 1.0 - x[2+j*3] ) * ( 2.0 * x[0+j*3] - 1.0 );
}
return x;
}
//****************************************************************************80
double pyramid01_volume ( )
//****************************************************************************80
//
// Purpose:
//
// PYRAMID01_VOLUME: volume of a unit pyramid with square base.
//
// Discussion:
//
// The volume of this unit pyramid is 4/3.
//
// The integration region is:
//
// - ( 1 - Z ) <= X <= 1 - Z
// - ( 1 - Z ) <= Y <= 1 - Z
// 0 <= Z <= 1.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 14 April 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Output, double PYRAMID01_VOLUME, the volume of the pyramid.
//
{
double volume;
volume = 4.0 / 3.0;
return volume;
}
//****************************************************************************80
double r8_choose ( int n, int k )
//****************************************************************************80
//
// Purpose:
//
// R8_CHOOSE computes the binomial coefficient C(N,K) as an R8.
//
// Discussion:
//
// The value is calculated in such a way as to avoid overflow and
// roundoff. The calculation is done in R8 arithmetic.
//
// The formula used is:
//
// C(N,K) = N! / ( K! * (N-K)! )
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 09 June 2013
//
// Author:
//
// John Burkardt
//
// Reference:
//
// ML Wolfson, HV Wright,
// Algorithm 160:
// Combinatorial of M Things Taken N at a Time,
// Communications of the ACM,
// Volume 6, Number 4, April 1963, page 161.
//
// Parameters:
//
// Input, int N, K, the values of N and K.
//
// Output, double R8_CHOOSE, the number of combinations of N
// things taken K at a time.
//
{
int i;
int mn;
int mx;
double value;
if ( k < n - k )
{
mn = k;
mx = n - k;
}
else
{
mn = n - k;
mx = k;
}
if ( mn < 0 )
{
value = 0.0;
}
else if ( mn == 0 )
{
value = 1.0;
}
else
{
value = ( double ) ( mx + 1 );
for ( i = 2; i <= mn; i++ )
{
value = ( value * ( double ) ( mx + i ) ) / ( double ) i;
}
}
return value;
}
//****************************************************************************80
double r8_mop ( int i )
//****************************************************************************80
//
// Purpose:
//
// R8_MOP returns the I-th power of -1 as an R8 value.
//
// Discussion:
//
// An R8 is an double value.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 16 November 2007
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I, the power of -1.
//
// Output, double R8_MOP, the I-th power of -1.
//
{
double value;
if ( ( i % 2 ) == 0 )
{
value = 1.0;
}
else
{
value = -1.0;
}
return value;
}
//****************************************************************************80
void r8mat_transpose_print ( int m, int n, double a[], string title )
//****************************************************************************80
//
// Purpose:
//
// R8MAT_TRANSPOSE_PRINT prints an R8MAT, transposed.
//
// Discussion:
//
// An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
// in column-major order.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 10 September 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, N, the number of rows and columns.
//
// Input, double A[M*N], an M by N matrix to be printed.
//
// Input, string TITLE, a title.
//
{
r8mat_transpose_print_some ( m, n, a, 1, 1, m, n, title );
return;
}
//****************************************************************************80
void r8mat_transpose_print_some ( int m, int n, double a[], int ilo, int jlo,
int ihi, int jhi, string title )
//****************************************************************************80
//
// Purpose:
//
// R8MAT_TRANSPOSE_PRINT_SOME prints some of an R8MAT, transposed.
//
// Discussion:
//
// An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
// in column-major order.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 07 April 2014
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, N, the number of rows and columns.
//
// Input, double A[M*N], an M by N matrix to be printed.
//
// Input, int ILO, JLO, the first row and column to print.
//
// Input, int IHI, JHI, the last row and column to print.
//
// Input, string TITLE, a title.
//
{
# define INCX 5
int i;
int i2;
int i2hi;
int i2lo;
int i2lo_hi;
int i2lo_lo;
int inc;
int j;
int j2hi;
int j2lo;
cout << "\n";
cout << title << "\n";
if ( m <= 0 || n <= 0 )
{
cout << "\n";
cout << " (None)\n";
return;
}
if ( ilo < 1 )
{
i2lo_lo = 1;
}
else
{
i2lo_lo = ilo;
}
if ( ihi < m )
{
i2lo_hi = m;
}
else
{
i2lo_hi = ihi;
}
for ( i2lo = i2lo_lo; i2lo <= i2lo_hi; i2lo = i2lo + INCX )
{
i2hi = i2lo + INCX - 1;
if ( m < i2hi )
{
i2hi = m;
}
if ( ihi < i2hi )
{
i2hi = ihi;
}
inc = i2hi + 1 - i2lo;
cout << "\n";
cout << " Row: ";
for ( i = i2lo; i <= i2hi; i++ )
{
cout << setw(7) << i - 1 << " ";
}
cout << "\n";
cout << " Col\n";
cout << "\n";
if ( jlo < 1 )
{
j2lo = 1;
}
else
{
j2lo = jlo;
}
if ( n < jhi )
{
j2hi = n;
}
else
{
j2hi = jhi;
}
for ( j = j2lo; j <= j2hi; j++ )
{
cout << setw(5) << j - 1 << ":";
for ( i2 = 1; i2 <= inc; i2++ )
{
i = i2lo - 1 + i2;
cout << setw(14) << a[(i-1)+(j-1)*m];
}
cout << "\n";
}
}
return;
# undef INCX
}
//****************************************************************************80
double *r8mat_uniform_01_new ( int m, int n, int &seed )
//****************************************************************************80
//
// Purpose:
//
// R8MAT_UNIFORM_01_NEW returns a unit pseudorandom R8MAT.
//
// Discussion:
//
// An R8MAT is a doubly dimensioned array of R8's, stored as a vector
// in column-major order.
//
// This routine implements the recursion
//
// seed = 16807 * seed mod ( 2^31 - 1 )
// unif = seed / ( 2^31 - 1 )
//
// The integer arithmetic never requires more than 32 bits,
// including a sign bit.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 03 October 2005
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Paul Bratley, Bennett Fox, Linus Schrage,
// A Guide to Simulation,
// Springer Verlag, pages 201-202, 1983.
//
// Bennett Fox,
// Algorithm 647:
// Implementation and Relative Efficiency of Quasirandom
// Sequence Generators,
// ACM Transactions on Mathematical Software,
// Volume 12, Number 4, pages 362-376, 1986.
//
// Philip Lewis, Allen Goodman, James Miller,
// A Pseudo-Random Number Generator for the System/360,
// IBM Systems Journal,
// Volume 8, pages 136-143, 1969.
//
// Parameters:
//
// Input, int M, N, the number of rows and columns.
//
// Input/output, int &SEED, the "seed" value. Normally, this
// value should not be 0, otherwise the output value of SEED
// will still be 0, and R8_UNIFORM will be 0. On output, SEED has
// been updated.
//
// Output, double R8MAT_UNIFORM_01_NEW[M*N], a matrix of pseudorandom values.
//
{
int i;
int j;
int k;
double *r;
r = new double[m*n];
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < m; i++ )
{
k = seed / 127773;
seed = 16807 * ( seed - k * 127773 ) - k * 2836;
if ( seed < 0 )
{
seed = seed + 2147483647;
}
r[i+j*m] = ( double ) ( seed ) * 4.656612875E-10;
}
}
return r;
}
//****************************************************************************80
double r8vec_sum ( int n, double a[] )
//****************************************************************************80
//
// Purpose:
//
// R8VEC_SUM returns the sum of an R8VEC.
//
// Discussion:
//
// An R8VEC is a vector of R8's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 15 October 2004
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int N, the number of entries in the vector.
//
// Input, double A[N], the vector.
//
// Output, double R8VEC_SUM, the sum of the vector.
//
{
int i;
double value;
value = 0.0;
for ( i = 0; i < n; i++ )
{
value = value + a[i];
}
return value;
}
//****************************************************************************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
}