StatMech
Loading...
Searching...
No Matches
maximum.c File Reference

Functions

double CDF (double x)
 
double inverse (double r)
 
int main (int argc, char **argv)
 

Function Documentation

◆ CDF()

double CDF ( double  x)
16 {
17 return sqrt(3)/(4*M_PI) *( log((x+1)*(x+1)/(x*x-x+1)) +2*sqrt(3) *(atan( (2*x-1)/sqrt(3.0) ) +M_PI/6.0) );
18}

◆ inverse()

double inverse ( double  r)
20 {
21 double min=0, max=0, mid;
22 double delta = 0.1;
23 double epsilon=1.0e-6;
24 while(CDF(max) < r) max += delta;
25 while( fabs(max-min) > epsilon ) {
26 mid = (max+min)/2.0;
27 if(CDF(mid) > r) max = mid;
28 else min = mid;
29 }
30 return mid;
31}
double CDF(double x)
Definition maximum.c:16

◆ main()

int main ( int  argc,
char **  argv 
)
33 {
34 if (argc < 3) {
35 printf("Usage: 1.This 2.n 3.ndata\n");
36 exit(EXIT_FAILURE);
37 }
38 Integer_t i, j, count;
39 Integer_t const n = atoi(argv[1]);
40 Integer_t const ndata = atoi(argv[2]);
41 Integer_t seed=0;
42 double max, maxData;
43 double rand, rand1, rand2;
44
45 Integer_t subMax, itemp;
46 Integer_t *histogram;
47 double mean, stddev;
48 double binwidth, binratio=0.01;
49 Integer_t const MAX=1000;
50
51 char str[100];
52 char *filename = str;
53
54 FILE *Infp;
55 FILE *Outfp;
56 sprintf(str, "Dist_maximum_%lld_raw.txt", n);
57 Outfp = fopen(filename, "w");
58 if (Outfp == NULL) {
59 printf("Can't open a file %s\n", filename);
60 exit(EXIT_FAILURE);
61 }
62 fprintf(Outfp, "#(n=%lld, seed=%lld) Distribution of maximum of |x| \n\n", n, seed);
63
64 init_genrand(seed);
65 maxData = NAN;
66 mean = 0;
67 stddev = 0;
68 for(i = 0;i < ndata; ++i) {
69 max = NAN;
70 for(j = 0;j < n; ++j) {
71 rand1 = genrand_real3();
72 rand = inverse(rand1);
73 max = fmax(max, rand);
74 // max = rand;
75 }
76 subMax = (Integer_t)(max/binwidth) +1;
77 if(subMax > MAX) {
78 i -=1;
79 continue;
80 }
81 maxData = fmax(maxData, max);
82 mean += max;
83 stddev += max*max;
84 fprintf(Outfp, "%lld %le\n", i, max);
85 }
86 mean /= (double)ndata;
87 stddev /= (double)ndata;
88 stddev = sqrt( stddev -mean*mean );
89 binwidth *= stddev;
90 // %binwidth = 1.0;
91 binwidth = 0.01;
92 subMax = (Integer_t)(maxData/binwidth) +1;
93 if(subMax > MAX) subMax = MAX;
94 subMax=MAX;
95 // printf("n=%lld, subMax=%lld, MAX=%lld, maxData=%le, binwidth=%lf\n", n, subMax, MAX, maxData, binwidth);
96 histogram = alloc_ivector(subMax); for(i = 0;i < subMax; ++i) histogram[i] = 0;
97
98 sprintf(str, "Dist_maximum_%lld_raw.txt", n);
99 Infp = fopen(filename, "r");
100 if (Infp == NULL) {
101 printf("Can't open a file %s\n", filename);
102 exit(EXIT_FAILURE);
103 }
104 skip_header(Infp);
105
106 count=0;
107 while ( fscanf(Infp, "%lld %le", &i, &max) != EOF ) {
108 itemp = (Integer_t)(max/binwidth);
109 if(itemp >= subMax) continue;
110 if(itemp < 0) continue;
111 histogram[itemp] += 1;
112 count += 1;
113 }
114
115 sprintf(str, "Dist_maximum_%lld.txt", n);
116 Outfp = fopen(filename, "w");
117 if (Outfp == NULL) {
118 printf("Can't open a file %s\n", filename);
119 exit(EXIT_FAILURE);
120 }
121 fprintf(Outfp, "# mean=%le, stddev=%le\n\n", mean, stddev);
122 fprintf(Outfp, "# 1.Maximum value 2.Probability density\n\n");
123 for(i = 0;i < subMax; ++i) {
124 fprintf(Outfp, "%le %le\n", binwidth*(i+0.5), histogram[i]/(binwidth*count));
125 }
126 printf("atan=%lf, %lf\n", atan( -1/sqrt(3.0) ), -M_PI/6);
127
128 return 0;
129}
double inverse(double r)
Definition maximum.c:20
MKL_INT Integer_t
Definition mytypes.hpp:359