summaryrefslogtreecommitdiff
path: root/ghc/utils/hp2ps/Deviation.c
blob: ecf7faba165b566953806864d78b0c2fbc3d1de5 (plain)
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
#include "Main.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "Defines.h"
#include "Error.h"
#include "HpFile.h"
#include "Utilities.h"

/* own stuff */
#include "Deviation.h"

/*
 *	Reorder the identifiers in the identifier table so that the
 *	ones whose data points exhibit the mininal standard deviation
 *	come first.	
 */

void
Deviation()
{
    intish i;
    intish j;
    floatish dev;
    struct chunk* ch;
    int min;
    floatish t;
    struct entry* e;
    floatish *averages; 
    floatish *deviations;

    averages   = (floatish*) xmalloc(nidents * sizeof(floatish));
    deviations = (floatish*) xmalloc(nidents * sizeof(floatish));

    /* find averages */

    for (i = 0; i < nidents; i++) {
	averages[i] = 0.0;
    }
 
    for (i = 0; i < nidents; i++) {
        for (ch = identtable[i]->chk; ch; ch = ch->next) {
	    for (j = 0; j < ch->nd; j++) {
	        averages[i] += ch->d[j].value; 
	    }
        }
    }    

    for (i = 0; i < nidents; i++) {
        averages[i] /= (floatish) nsamples;
    }

    /* calculate standard deviation */

    for (i = 0; i < nidents; i++) {
	deviations[i] = 0.0;
    }
 
    for (i = 0; i < nidents; i++) {
        for (ch = identtable[i]->chk; ch; ch = ch->next) {
	    for (j = 0; j < ch->nd; j++) {
		dev = ch->d[j].value - averages[i];
	        deviations[i] += dev * dev; 
	    }
        }
    }

    for (i = 0; i < nidents; i++) {
        deviations[i] = (floatish) sqrt ((doublish) (deviations[i] / 
					 (floatish) (nsamples - 1)));
    }


    /* sort on basis of standard deviation */

    for (i = 0; i < nidents-1; i++) {
	min = i; 
	for (j = i+1; j < nidents; j++) {
	    if (deviations[ j ] < deviations[min]) {
		min = j;
	    }
	}

        t = deviations[min]; 
	deviations[min] = deviations[i];	
	deviations[i] = t;

        e = identtable[min];
	identtable[min] = identtable[i];
	identtable[i] = e;
    } 	

    free(averages);
    free(deviations);
}

void
Identorder(iflag)
    int iflag;	/* a funny three-way flag ? WDP 95/03 */
{
    int i;
    int j;
    int min;
    struct entry* e;

    /* sort on basis of ident string */
    if (iflag > 0) {
	/* greatest at top i.e. smallest at start */

	for (i = 0; i < nidents-1; i++) {
	    min = i; 
	    for (j = i+1; j < nidents; j++) {
		if (strcmp(identtable[j]->name, identtable[min]->name) < 0) {
		    min = j;
		}
	    }

	    e = identtable[min];
	    identtable[min] = identtable[i];
	    identtable[i] = e;
	} 	
    } else {
	/* smallest at top i.e. greatest at start */

	for (i = 0; i < nidents-1; i++) {
	    min = i; 
	    for (j = i+1; j < nidents; j++) {
		if (strcmp(identtable[j]->name, identtable[min]->name) > 0) {
		    min = j;
		}
	    }

	    e = identtable[min];
	    identtable[min] = identtable[i];
	    identtable[i] = e;
	} 	
    }	
}