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