Ran `indent -linux` on src/global_operations/*.c.
[parallel_computing.git] / src / monte_carlo / bin_distribution.c
1
2              /* bin_distribution.c                              */
3              /* find # of random numbers in equally spaced grid */
4              /* bin the grid counter                            */
5
6 /* Syntax: */
7
8 /* random_demo_2_series 1 20000 | bin_distribution > data_file */
9
10 /* random_demo_2_series 2 20000 | bin_distribution > data_file */
11
12                                                        /* Michel Vallieres */
13 #include <stdio.h>
14 #include <stdlib.h>
15
16 #define NMAX 100000
17 #define N_MAX_GRID 25
18
19 int main ( int argc, char *argv[] )
20 {
21   int    N;
22   double x[NMAX], y[NMAX];
23   int    counter[N_MAX_GRID][N_MAX_GRID];
24   int    ix_scan, iy_scan, ic, max_bin, ibin, *bin;
25   double dx, dy;
26
27                                                  /* read in the random points coordinates */
28   N = 0;
29   while ( scanf( "%lf %lf", &x[N], &y[N] ) != EOF )
30       N++;
31   fprintf( stderr, "\n  Number of random numbers %d \n", N );
32                                                  /* set counter to zero */
33   for ( ix_scan=0 ; ix_scan<N_MAX_GRID ; ix_scan++ )
34     {
35       for ( iy_scan=0 ; iy_scan<N_MAX_GRID ; iy_scan++ )
36         {
37           counter[ix_scan][iy_scan] = 0;
38         }
39     }
40                                                  /* grid of squares dx & dy */
41   dx = 1.0/N_MAX_GRID;
42   dy = 1.0/N_MAX_GRID;
43                                                  /* scan grid */
44   for ( ix_scan=0 ; ix_scan<N_MAX_GRID ; ix_scan++ )
45     {
46       for ( iy_scan=0 ; iy_scan<N_MAX_GRID ; iy_scan++ )
47         {
48                                                  /* scan over random numbers */
49           for ( ic=0 ; ic<N ; ic++ )
50             {
51               if ( ( ix_scan*dx <= x[ic] && x[ic] < (ix_scan+1)*dx )
52                 && (  iy_scan*dy <= y[ic] && y[ic] < (iy_scan+1)*dy ) )
53                    counter[ix_scan][iy_scan]++;
54             }
55         }
56     }
57                                                  /* find max of counter value */
58                                                  /* & print counter */
59   max_bin = 0;
60   fprintf( stderr, "\n distribution \n" );
61   for ( ix_scan=0 ; ix_scan<N_MAX_GRID ; ix_scan++ )
62     {
63       printf( "\n" );
64       for ( iy_scan=0 ; iy_scan<N_MAX_GRID ; iy_scan++ )
65         {
66           if ( counter[ix_scan][iy_scan] > max_bin )
67             max_bin++;
68           fprintf( stderr, " %2d", counter[ix_scan][iy_scan] );
69         }
70     }
71   fprintf( stderr,  "\n" );
72                                                  /* bin the counter */
73   bin = (int *)malloc( (max_bin+1)*sizeof(int) );
74   for ( ix_scan=0 ; ix_scan<N_MAX_GRID ; ix_scan++ )
75     {
76       for ( iy_scan=0 ; iy_scan<N_MAX_GRID ; iy_scan++ )
77         {
78           bin[ counter[ix_scan][iy_scan] ]++;
79         }
80     }
81                                                  /* output the bin */
82   fprintf( stderr, "\n binning \n" );
83   for ( ibin=0 ; ibin<max_bin ; ibin++ )
84      if ( bin[ibin] != 0 )
85         printf( " %d %d \n", ibin, bin[ibin] );
86
87 }
88
89
90
91
92
93
94
95