-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathnormal_dataset.cpp
1242 lines (1140 loc) · 26 KB
/
normal_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 <ctime>
# include <iostream>
# include <iomanip>
# include <fstream>
# include <sstream>
# include <string>
using namespace std;
int main ( int argc, char *argv[] );
int i4_max ( int i1, int i2 );
int i4_min ( int i1, int i2 );
double *multinormal_sample ( int m, int n, double a[], double mu[], int *seed );
double r8_uniform_01 ( int *seed );
void r8mat_print ( int m, int n, double a[], string title );
void r8mat_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[] );
double *r8po_fa ( int n, double a[] );
double *r8vec_normal_01_new ( int n, int *seed );
void r8vec_print ( int n, double a[], string title );
double *r8vec_uniform_01_new ( int n, int *seed );
void timestamp ( );
//****************************************************************************80
int main ( int argc, char *argv[] )
//****************************************************************************80
//
// Purpose:
//
// MAIN is the main program for NORMAL_DATASET.
//
// Discussion:
//
// NORMAL_DATASET generates a dataset of multivariate normal random values,
// and writes it to a file.
//
// Usage:
//
// normal_dataset m n seed mu a
//
// where
//
// * M, the spatial dimension,
// * N, the number of points to generate,
// * SEED, the seed, a positive integer.
// * MU, the mean vector.
// * A, the MxM variance-covariance matrix.
//
// creates an M by N multivariate normal random dataset and writes it
// to the file "normal_M_N.txt"./
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 09 December 2009
//
// Author:
//
// John Burkardt
//
{
double *a;
string output_filename;
int i;
int j;
int k;
int m;
double *mu;
ostringstream m_ostring;
int n;
ostringstream n_ostring;
double *x;
int seed;
timestamp ( );
cout << "\n";
cout << "NORMAL_DATASET\n";
cout << " C++ version\n";
cout << "\n";
cout << " Compiled on " << __DATE__ << " at " << __TIME__ << ".\n";
cout << "\n";
cout << " Generate a multivariate normal random dataset.\n";
cout << "\n";
cout << " The program requests input values from the user:\n";
cout << "\n";
cout << " * M, the spatial dimension,\n";
cout << " * N, the number of points to generate,\n";
cout << " * SEED, a positive integer.\n";
cout << " * MU, the mean vector of length M.\n";
cout << " * A, the MxM variance-covariance matrix.\n";
cout << "\n";
cout << " The program generates the data, writes it to the file\n";
cout << "\n";
cout << " normal_M_N.txt\n";
cout << "\n";
cout << " where ""M"" and ""N"" are the numeric values specified\n";
cout << " by the user.\n";
//
// Get the spatial dimension.
//
if ( 1 < argc )
{
m = atoi ( argv[1] );
}
else
{
cout << "\n";
cout << " Enter the value of M\n";
cin >> m;
}
cout << "\n";
cout << " Spatial dimension M = " << m << "\n";
//
// Get the number of points.
//
if ( 2 < argc )
{
n = atoi ( argv[2] );
}
else
{
cout << "\n";
cout << " Enter the number of points N\n";
cin >> n;
}
cout << "\n";
cout << " Number of points N = " << n << "\n";
//
// Get the seed.
//
if ( 3 < argc )
{
seed = atoi ( argv[3] );
}
else
{
cout << "\n";
cout << " Enter the value of SEED\n";
cin >> seed;
}
cout << "\n";
cout << " The seed is = " << seed << "\n";
//
// Get the mean.
//
mu = new double[m];
k = 4;
if ( 4 < argc )
{
for ( i = 0; i < m; i++ )
{
mu[i] = atof ( argv[k] );
k = k + 1;
}
}
else
{
cout << "\n";
cout << " Enter MU:\n";
for ( i = 0; i < m; i++ )
{
cin >> mu[i];
}
}
r8vec_print ( m, mu, " The mean vector M:" );
//
// Get the variance-covariance matrix.
//
a = new double[m*m];
if ( 5 < argc )
{
for ( i = 0; i < m; i++ )
{
for ( j = 0; j < m; j++ )
{
a[i+j*m] = atof ( argv[k] );
k = k + 1;
}
}
}
else
{
cout << "\n";
cout << " Enter A:\n";
for ( i = 0; i < m; i++ )
{
for ( j = 0; j < m; j++ )
{
cin >> a[i+j*m];
}
}
}
r8mat_print ( m, m, a, " The variance-covariance matrix A:" );
//
// Compute the data.
//
x = multinormal_sample ( m, n, a, mu, &seed );
//
// Write it to a file.
//
m_ostring << m;
n_ostring << n;
output_filename = "normal_" + m_ostring.str ( ) + "_"
+ n_ostring.str ( ) + ".txt";
r8mat_write ( output_filename, m, n, x );
cout << "\n";
cout << " The data was written to the file \""
<< output_filename << "\".\n";
//
// Terminate.
//
delete [] a;
delete [] mu;
delete [] x;
cout << "\n";
cout << "NORMAL_DATASET:\n";
cout << " Normal end of execution.\n";
cout << "\n";
timestamp ( );
return 0;
}
//****************************************************************************80
int i4_max ( int i1, int i2 )
//****************************************************************************80
//
// Purpose:
//
// I4_MAX returns the maximum of two I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 October 1998
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I1, I2, are two integers to be compared.
//
// Output, int I4_MAX, the larger of I1 and I2.
//
{
int value;
if ( i2 < i1 )
{
value = i1;
}
else
{
value = i2;
}
return value;
}
//****************************************************************************80
int i4_min ( int i1, int i2 )
//****************************************************************************80
//
// Purpose:
//
// I4_MIN returns the minimum of two I4's.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 13 October 1998
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int I1, I2, two integers to be compared.
//
// Output, int I4_MIN, the smaller of I1 and I2.
//
{
int value;
if ( i1 < i2 )
{
value = i1;
}
else
{
value = i2;
}
return value;
}
//****************************************************************************80
double *multinormal_sample ( int m, int n, double a[], double mu[], int *seed )
//****************************************************************************80
//
// Purpose:
//
// MULTINORMAL_SAMPLE samples a multivariate normal distribution.
//
// Discussion:
//
// The multivariate normal distribution for the M dimensional vector X
// has the form:
//
// pdf(X) = (2*pi*det(A))**(-M/2) * exp(-0.5*(X-MU)'*inverse(A)*(X-MU))
//
// where MU is the mean vector, and A is a positive definite symmetric
// matrix called the variance-covariance matrix.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 09 December 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, the dimension of the space.
//
// Input, int N, the number of points.
//
// Input, double A[M*M], the variance-covariance
// matrix. A must be positive definite symmetric.
//
// Input, double MU[M], the mean vector.
//
// Input/output, int *SEED, the random number seed.
//
// Output, double MULTINORMAL_SAMPLE[M], the points.
//
{
int i;
int j;
int k;
double *r;
double *x;
double *y;
//
// Compute the upper triangular Cholesky factor R of the variance-covariance
// matrix.
//
r = r8po_fa ( m, a );
if ( !r )
{
cout << "\n";
cout << "MULTINORMAL_SAMPLE - Fatal error!\n";
cout << " The variance-covariance matrix is not positive definite symmetric.\n";
exit ( 1 );
}
//
// Y = MxN matrix of samples of the 1D normal distribution with mean 0
// and variance 1.
//
y = r8vec_normal_01_new ( m*n, seed );
//
// Compute X = MU + R' * Y.
//
x = new double[m*n];
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < m; i++ )
{
x[i+j*m] = mu[i];
for ( k = 0; k < m; k++ )
{
x[i+j*m] = x[i+j*m] + r[k+i*m] * y[k+j*m];
}
}
}
delete [] r;
delete [] y;
return x;
}
//****************************************************************************80
double r8_uniform_01 ( int *seed )
//****************************************************************************80
//
// Purpose:
//
// R8_UNIFORM_01 returns a unit pseudorandom R8.
//
// Discussion:
//
// This routine implements the recursion
//
// seed = ( 16807 * seed ) mod ( 2^31 - 1 )
// u = seed / ( 2^31 - 1 )
//
// The integer arithmetic never requires more than 32 bits,
// including a sign bit.
//
// If the initial seed is 12345, then the first three computations are
//
// Input Output R8_UNIFORM_01
// SEED SEED
//
// 12345 207482415 0.096616
// 207482415 1790989824 0.833995
// 1790989824 2035175616 0.947702
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 11 August 2004
//
// Author:
//
// John Burkardt
//
// Reference:
//
// Paul Bratley, Bennett Fox, Linus Schrage,
// A Guide to Simulation,
// Second Edition,
// Springer, 1987,
// ISBN: 0387964673,
// LC: QA76.9.C65.B73.
//
// Bennett Fox,
// Algorithm 647:
// Implementation and Relative Efficiency of Quasirandom
// Sequence Generators,
// ACM Transactions on Mathematical Software,
// Volume 12, Number 4, December 1986, pages 362-376.
//
// Pierre L'Ecuyer,
// Random Number Generation,
// in Handbook of Simulation,
// edited by Jerry Banks,
// Wiley, 1998,
// ISBN: 0471134031,
// LC: T57.62.H37.
//
// Peter Lewis, Allen Goodman, James Miller,
// A Pseudo-Random Number Generator for the System/360,
// IBM Systems Journal,
// Volume 8, Number 2, 1969, pages 136-143.
//
// Parameters:
//
// Input/output, int *SEED, the "seed" value. Normally, this
// value should not be 0. On output, SEED has been updated.
//
// Output, double R8_UNIFORM_01, a new pseudorandom variate,
// strictly between 0 and 1.
//
{
int i4_huge = 2147483647;
int k;
double r;
if ( *seed == 0 )
{
cerr << "\n";
cerr << "R8_UNIFORM_01 - Fatal error!\n";
cerr << " Input value of SEED = 0.\n";
exit ( 1 );
}
k = *seed / 127773;
*seed = 16807 * ( *seed - k * 127773 ) - k * 2836;
if ( *seed < 0 )
{
*seed = *seed + i4_huge;
}
//
// Although SEED can be represented exactly as a 32 bit integer,
// it generally cannot be represented exactly as a 32 bit real number.
//
r = ( double ) ( *seed ) * 4.656612875E-10;
return r;
}
//****************************************************************************80
void r8mat_print ( int m, int n, double a[], string title )
//****************************************************************************80
//
// Purpose:
//
// R8MAT_PRINT prints an R8MAT.
//
// Discussion:
//
// An R8MAT is a doubly dimensioned array of R8 values, stored as a vector
// in column-major order.
//
// Entry A(I,J) is stored as A[I+J*M]
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 10 September 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int M, the number of rows in A.
//
// Input, int N, the number of columns in A.
//
// Input, double A[M*N], the M by N matrix.
//
// Input, string TITLE, a title.
//
{
r8mat_print_some ( m, n, a, 1, 1, m, n, title );
return;
}
//****************************************************************************80
void r8mat_print_some ( int m, int n, double a[], int ilo, int jlo, int ihi,
int jhi, string title )
//****************************************************************************80
//
// Purpose:
//
// R8MAT_PRINT_SOME prints some of an R8MAT.
//
// 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, the number of rows of the matrix.
// M must be positive.
//
// Input, int N, the number of columns of the matrix.
// N must be positive.
//
// Input, double A[M*N], the matrix.
//
// Input, int ILO, JLO, IHI, JHI, designate the first row and
// column, and the last row and column to be printed.
//
// Input, string TITLE, a title.
//
{
# define INCX 5
int i;
int i2hi;
int i2lo;
int j;
int j2hi;
int j2lo;
cout << "\n";
cout << title << "\n";
//
// Print the columns of the matrix, in strips of 5.
//
for ( j2lo = jlo; j2lo <= jhi; j2lo = j2lo + INCX )
{
j2hi = j2lo + INCX - 1;
j2hi = i4_min ( j2hi, n );
j2hi = i4_min ( j2hi, jhi );
cout << "\n";
//
// For each column J in the current range...
//
// Write the header.
//
cout << " Col: ";
for ( j = j2lo; j <= j2hi; j++ )
{
cout << setw(7) << j << " ";
}
cout << "\n";
cout << " Row\n";
cout << "\n";
//
// Determine the range of the rows in this strip.
//
i2lo = i4_max ( ilo, 1 );
i2hi = i4_min ( ihi, m );
for ( i = i2lo; i <= i2hi; i++ )
{
//
// Print out (up to) 5 entries in row I, that lie in the current strip.
//
cout << setw(5) << i << " ";
for ( j = j2lo; j <= j2hi; j++ )
{
cout << setw(12) << a[i-1+(j-1)*m] << " ";
}
cout << "\n";
}
}
return;
# undef INCX
}
//****************************************************************************80
void r8mat_write ( string output_filename, int m, int n, double table[] )
//****************************************************************************80
//
// Purpose:
//
// R8MAT_WRITE writes an R8MAT file with no header.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 29 June 2009
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, string OUTPUT_FILENAME, the output filename.
//
// Input, int M, the spatial dimension.
//
// Input, int N, the number of points.
//
// Input, double TABLE[M*N], the table data.
//
{
int i;
int j;
ofstream output;
//
// Open the file.
//
output.open ( output_filename.c_str ( ) );
if ( !output )
{
cerr << "\n";
cerr << "R8MAT_WRITE - Fatal error!\n";
cerr << " Could not open the output file.\n";
return;
}
//
// Write the data.
//
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < m; i++ )
{
output << " " << setw(24) << setprecision(16) << table[i+j*m];
}
output << "\n";
}
//
// Close the file.
//
output.close ( );
return;
}
//****************************************************************************80
double *r8po_fa ( int n, double a[] )
//****************************************************************************80
//
// Purpose:
//
// R8PO_FA factors a R8PO matrix.
//
// Discussion:
//
// The R8PO storage format is appropriate for a symmetric positive definite
// matrix and its inverse. (The Cholesky factor of a R8PO matrix is an
// upper triangular matrix, so it will be in R8GE storage format.)
//
// Only the diagonal and upper triangle of the square array are used.
// This same storage format is used when the matrix is factored by
// R8PO_FA, or inverted by R8PO_INVERSE. For clarity, the lower triangle
// is set to zero.
//
// The positive definite symmetric matrix A has a Cholesky factorization
// of the form:
//
// A = R' * R
//
// where R is an upper triangular matrix with positive elements on
// its diagonal.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 04 February 2004
//
// Author:
//
// Original FORTRAN77 version by Dongarra, Bunch, Moler, Stewart.
// C++ version by John Burkardt.
//
// Reference:
//
// Jack Dongarra, Jim Bunch, Cleve Moler, Pete Stewart,
// LINPACK User's Guide,
// SIAM, 1979,
// ISBN13: 978-0-898711-72-1,
// LC: QA214.L56.
//
// Parameters:
//
// Input, int N, the order of the matrix.
//
// Input, double A[N*N], the matrix in R8PO storage.
//
// Output, double R8PO_FA[N*N], the Cholesky factor in SGE
// storage, or NULL if there was an error.
//
{
double *b;
int i;
int j;
int k;
double s;
b = new double[n*n];
for ( j = 0; j < n; j++ )
{
for ( i = 0; i < n; i++ )
{
b[i+j*n] = a[i+j*n];
}
}
for ( j = 0; j < n; j++ )
{
for ( k = 0; k <= j-1; k++ )
{
for ( i = 0; i <= k-1; i++ )
{
b[k+j*n] = b[k+j*n] - b[i+k*n] * b[i+j*n];
}
b[k+j*n] = b[k+j*n] / b[k+k*n];
}
s = b[j+j*n];
for ( i = 0; i <= j-1; i++ )
{
s = s - b[i+j*n] * b[i+j*n];
}
if ( s <= 0.0 )
{
delete [] b;
return NULL;
}
b[j+j*n] = sqrt ( s );
}
//
// Since the Cholesky factor is in R8GE format, zero out the lower triangle.
//
for ( i = 0; i < n; i++ )
{
for ( j = 0; j < i; j++ )
{
b[i+j*n] = 0.0;
}
}
return b;
}
//****************************************************************************80
double *r8vec_normal_01_new ( int n, int *seed )
//****************************************************************************80
//
// Purpose:
//
// R8VEC_NORMAL_01_NEW returns a unit pseudonormal R8VEC.
//
// Discussion:
//
// The standard normal probability distribution function (PDF) has
// mean 0 and standard deviation 1.
//
// This routine can generate a vector of values on one call. It
// has the feature that it should provide the same results
// in the same order no matter how we break up the task.
//
// Before calling this routine, the user may call RANDOM_SEED
// in order to set the seed of the random number generator.
//
// The Box-Muller method is used, which is efficient, but
// generates an even number of values each time. On any call
// to this routine, an even number of new values are generated.
// Depending on the situation, one value may be left over.
// In that case, it is saved for the next call.
//
// Licensing:
//
// This code is distributed under the GNU LGPL license.
//
// Modified:
//
// 18 October 2004
//
// Author:
//
// John Burkardt
//
// Parameters:
//
// Input, int N, the number of values desired. If N is negative,
// then the code will flush its internal memory; in particular,
// if there is a saved value to be used on the next call, it is
// instead discarded. This is useful if the user has reset the
// random number seed, for instance.
//
// Input/output, int *SEED, a seed for the random number generator.
//
// Output, double R8VEC_NORMAL_01_NEW[N], a sample of the standard normal PDF.
//
// Local parameters:
//
// Local, int MADE, records the number of values that have
// been computed. On input with negative N, this value overwrites
// the return value of N, so the user can get an accounting of
// how much work has been done.
//
// Local, real R(N+1), is used to store some uniform random values.
// Its dimension is N+1, but really it is only needed to be the
// smallest even number greater than or equal to N.
//
// Local, int SAVED, is 0 or 1 depending on whether there is a
// single saved value left over from the previous call.
//
// Local, int X_LO, X_HI, records the range of entries of
// X that we need to compute. This starts off as 1:N, but is adjusted
// if we have a saved value that can be immediately stored in X(1),
// and so on.
//
// Local, real Y, the value saved from the previous call, if
// SAVED is 1.
//
{
# define PI 3.141592653589793
int i;
int m;
static int made = 0;
double *r;
static int saved = 0;
double *x;
int x_hi;
int x_lo;
static double y = 0.0;
x = new double[n];
//
// I'd like to allow the user to reset the internal data.
// But this won't work properly if we have a saved value Y.
// I'm making a crock option that allows the user to signal
// explicitly that any internal memory should be flushed,
// by passing in a negative value for N.
//
if ( n < 0 )
{
made = 0;
saved = 0;
y = 0.0;
return NULL;
}
else if ( n == 0 )
{
return NULL;
}
//
// Record the range of X we need to fill in.
//
x_lo = 1;
x_hi = n;
//
// Use up the old value, if we have it.
//
if ( saved == 1 )
{
x[0] = y;
saved = 0;
x_lo = 2;
}
//
// Maybe we don't need any more values.
//
if ( x_hi - x_lo + 1 == 0 )
{
}
//
// If we need just one new value, do that here to avoid null arrays.
//
else if ( x_hi - x_lo + 1 == 1 )
{
r = r8vec_uniform_01_new ( 2, seed );
x[x_hi-1] = sqrt ( - 2.0 * log ( r[0] ) ) * cos ( 2.0 * PI * r[1] );
y = sqrt ( - 2.0 * log ( r[0] ) ) * sin ( 2.0 * PI * r[1] );
saved = 1;
made = made + 2;
delete [] r;
}
//
// If we require an even number of values, that's easy.
//
else if ( ( x_hi - x_lo + 1 ) % 2 == 0 )
{
m = ( x_hi - x_lo + 1 ) / 2;