add Outputable instance for OccIfaceEq
[ghc-hetmet.git] / utils / hp2ps / Deviation.c
1 #include "Main.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <math.h>
6 #include "Defines.h"
7 #include "Error.h"
8 #include "HpFile.h"
9 #include "Utilities.h"
10
11 /* own stuff */
12 #include "Deviation.h"
13
14 /*
15  *      Reorder the identifiers in the identifier table so that the
16  *      ones whose data points exhibit the mininal standard deviation
17  *      come first.     
18  */
19
20 void
21 Deviation()
22 {
23     intish i;
24     intish j;
25     floatish dev;
26     struct chunk* ch;
27     int min;
28     floatish t;
29     struct entry* e;
30     floatish *averages; 
31     floatish *deviations;
32
33     averages   = (floatish*) xmalloc(nidents * sizeof(floatish));
34     deviations = (floatish*) xmalloc(nidents * sizeof(floatish));
35
36     /* find averages */
37
38     for (i = 0; i < nidents; i++) {
39         averages[i] = 0.0;
40     }
41  
42     for (i = 0; i < nidents; i++) {
43         for (ch = identtable[i]->chk; ch; ch = ch->next) {
44             for (j = 0; j < ch->nd; j++) {
45                 averages[i] += ch->d[j].value; 
46             }
47         }
48     }    
49
50     for (i = 0; i < nidents; i++) {
51         averages[i] /= (floatish) nsamples;
52     }
53
54     /* calculate standard deviation */
55
56     for (i = 0; i < nidents; i++) {
57         deviations[i] = 0.0;
58     }
59  
60     for (i = 0; i < nidents; i++) {
61         for (ch = identtable[i]->chk; ch; ch = ch->next) {
62             for (j = 0; j < ch->nd; j++) {
63                 dev = ch->d[j].value - averages[i];
64                 deviations[i] += dev * dev; 
65             }
66         }
67     }
68
69     for (i = 0; i < nidents; i++) {
70         deviations[i] = (floatish) sqrt ((doublish) (deviations[i] / 
71                                          (floatish) (nsamples - 1)));
72     }
73
74
75     /* sort on basis of standard deviation */
76
77     for (i = 0; i < nidents-1; i++) {
78         min = i; 
79         for (j = i+1; j < nidents; j++) {
80             if (deviations[ j ] < deviations[min]) {
81                 min = j;
82             }
83         }
84
85         t = deviations[min]; 
86         deviations[min] = deviations[i];        
87         deviations[i] = t;
88
89         e = identtable[min];
90         identtable[min] = identtable[i];
91         identtable[i] = e;
92     }   
93
94     free(averages);
95     free(deviations);
96 }
97
98 void
99 Identorder(iflag)
100     int iflag;  /* a funny three-way flag ? WDP 95/03 */
101 {
102     int i;
103     int j;
104     int min;
105     struct entry* e;
106
107     /* sort on basis of ident string */
108     if (iflag > 0) {
109         /* greatest at top i.e. smallest at start */
110
111         for (i = 0; i < nidents-1; i++) {
112             min = i; 
113             for (j = i+1; j < nidents; j++) {
114                 if (strcmp(identtable[j]->name, identtable[min]->name) < 0) {
115                     min = j;
116                 }
117             }
118
119             e = identtable[min];
120             identtable[min] = identtable[i];
121             identtable[i] = e;
122         }       
123     } else {
124         /* smallest at top i.e. greatest at start */
125
126         for (i = 0; i < nidents-1; i++) {
127             min = i; 
128             for (j = i+1; j < nidents; j++) {
129                 if (strcmp(identtable[j]->name, identtable[min]->name) > 0) {
130                     min = j;
131                 }
132             }
133
134             e = identtable[min];
135             identtable[min] = identtable[i];
136             identtable[i] = e;
137         }       
138     }   
139 }