Statistics
| Revision:

root / tmp / org.txm.core / src / groovy / org / txm / functions / mesures / SyntheseBuilder.groovy @ 187

History | View | Annotate | Download (3.7 kB)

1
package org.txm.functions.mesures;
2

    
3
import java.util.Arrays;
4

    
5
import org.apache.tools.ant.types.resources.selectors.InstanceOf;
6

    
7
public class SyntheseBuilder {
8
        private Integer[] isrc;
9
        private Integer[] isorted;
10
        private Float[] fsrc;
11
        private Float[] fsorted;
12

    
13
        public SyntheseBuilder(Integer[] rez) {
14
                assert(rez.length > 0);
15
                isrc = rez;
16
                isorted = new Integer[rez.length];
17
                System.arraycopy(rez, 0, isorted, 0, rez.length);
18
                Arrays.sort(isorted);
19
        }
20

    
21
        public SyntheseBuilder(Float[] rez) {
22
                assert(rez.length > 0);
23
                fsrc = rez;
24
                fsorted = new Float[rez.length];
25
                System.arraycopy(rez, 0, fsorted, 0, rez.length);
26
                Arrays.sort(fsorted);
27
        }
28

    
29
        public Number doSynthese(Synthese synt) {
30
                if (isorted != null) {
31
                        return doSynthese(synt, isrc);
32
                } else {
33
                        return doSynthese(synt, fsrc);
34
                }
35
        }
36

    
37
        private Integer[] getSorted(Integer[] rez) {
38
                return isorted;
39
        }
40

    
41
        private Float[] getSorted(Float[] rez) {
42
                return fsorted;
43
        }
44

    
45
        private Number[] getSorted(Number[] rez) {
46
                if (isorted != null) return isorted;
47
                return fsorted;
48
        }
49

    
50
        public Number doSynthese(Synthese synt, Number[] rez) {
51
                if (rez instanceof Integer[])
52
                        return doISynthese(synt, rez);
53
                else if  (rez instanceof Float[])
54
                        return doFSynthese(synt, rez);
55

    
56
                println "Error: doSynthese unsuported array type: "+rez.getClass();
57
                return -1;
58
        }
59

    
60
        public Number doISynthese(Synthese synt, Integer[] rez) {
61
                switch (synt) {
62
                        case Synthese.MIN: return min(rez);
63
                        case Synthese.MAX: return max(rez);
64
                        case Synthese.MEAN: return mean(rez);
65
                        case Synthese.QUARTILE1: return quartile1(rez);
66
                        case Synthese.MEDIAN: return median(rez);
67
                        case Synthese.QUARTILE3: return quartile3(rez);
68
                        case Synthese.SUM: return sum(rez);
69
                        case Synthese.COUNTMATCHES: return rez.length;
70
                        case Synthese.FIRST:
71
                                if (rez.length > 0) return rez[0];
72
                                else return -1;
73
                        case Synthese.LAST:
74
                                if (rez.length > 0) return rez[(int)rez.length -1];
75
                                else return -1;
76
                        case Synthese.MIDDLE:
77
                                if (rez.length > 0) return rez[(int)rez.length/2];
78
                                else return -1;
79
                }
80
                return null;
81
        }
82

    
83
        public Number doFSynthese(Synthese synt, Float[] rez) {
84
                switch (synt) {
85
                        case Synthese.MIN: return min(rez);
86
                        case Synthese.MAX: return max(rez);
87
                        case Synthese.MEAN: return mean(rez);
88
                        case Synthese.QUARTILE1: return quartile1(rez);
89
                        case Synthese.MEDIAN: return median(rez);
90
                        case Synthese.QUARTILE3: return quartile3(rez);
91
                        case Synthese.SUM: return sum(rez);
92
                        case Synthese.COUNTMATCHES: return rez.length;
93
                        case Synthese.FIRST:
94
                                if (rez.length > 0) return rez[0];
95
                                else return -1;
96
                        case Synthese.LAST:
97
                                if (rez.length > 0) return rez[(int)rez.length -1];
98
                                else return -1;
99
                        case Synthese.MIDDLE:
100
                                if (rez.length > 0) return rez[(int)rez.length/2];
101
                                else return -1;
102
                }
103
                return null;
104
        }
105

    
106
        public Number min(Number[] obj) {
107
                obj = getSorted(obj);
108
                return obj[0];
109
        }
110

    
111
        public Number max(Number[] obj) {
112
                obj = getSorted(obj);
113
                return obj[obj.length - 1];
114
        }
115

    
116
        public Number sum(Float[] obj) {
117
                Float sum = 0.0f;
118
                for (int i = 0 ; i < obj.length ; i++)
119
                        sum += obj[i];
120
                return sum;
121
        }
122

    
123
        public Number sum(Integer[] obj) {
124
                Integer sum = 0;
125
                for (int i = 0 ; i < obj.length ; i++)
126
                        sum += obj[i];
127
                return sum;
128
        }
129

    
130
        public Number mean(Integer[] obj) {
131
                Integer i = (Integer) sum(obj);
132
                return (float) i / (float)obj.length;
133
        }
134

    
135
        public Number mean(Float[] obj) {
136
                Float f = (Float) sum(obj);
137
                return (float) f / obj.length;
138
        }
139

    
140
        public Number quartile1(Number[] obj) {
141
                obj = getSorted(obj);
142
                int len = obj.length;
143
                return obj[(int)len/4];
144
        }
145

    
146
        public Number quartile3(Number[] obj) {
147
                obj = getSorted(obj);
148
                int len = obj.length;
149
                return obj[(int)len/4 + len/2];
150
        }
151

    
152
        public Number median(Number[] obj) {
153
                obj = getSorted(obj);
154
                int len = obj.length;
155
                return obj[(int)len/2];
156
        }
157
}