stats.cpp
1/*
2 $Id: stats.cpp 466447 2005-10-02 17:54:10Z zander $
3
4 KCalc, a scientific calculator for the X window system using the
5 TQt widget libraries, available at no cost at http://www.troll.no
6
7 Copyright (C) 1996 Bernd Johannes Wuebben
8 wuebben@math.cornell.edu
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
24
25*/
26
27
28#include "stats.h"
29#include <stdio.h>
30#include <stdlib.h>
31
32KStats::KStats(){
33
34 error_flag = false;
35 data.setAutoDelete(true);
36
37}
38
39KStats::~KStats(){
40
41 data.clear();
42
43}
44
45void KStats::clearAll(){
46
47 data.clear();
48
49}
50
51void KStats::enterData(CALCAMNT _data){
52
53 CALCAMNT *newdata;
54 newdata = new CALCAMNT;
55 *newdata = _data;
56 data.append(newdata);
57
58#ifdef DEBUG_STATS
59 printf("Added %Lg\n",*newdata);
60 printf("count %d\n",data.count());
61#endif
62
63}
64
65
66void KStats::clearLast(){
67
68
69 data.removeLast();
70#ifdef DEBUG_STATS
71printf("count %d\n",data.count());
72#endif
73
74
75}
76
77CALCAMNT KStats::sum(){
78
79 CALCAMNT result = 0.0;
80 CALCAMNT *dp;
81 for ( dp=data.first(); dp != 0; dp=data.next() ){
82
83 result += *dp;
84
85 }
86
87#ifdef DEBUG_STATS
88 printf("Sum %Lg\n",result);
89#endif
90
91 return result;
92}
93
94CALCAMNT KStats::min()
95{
96 printf("MIIINNNN\n");
97
98 if ( data.count() == 0 )
99 return 0.0;
100
101 printf("1\n");
102
103 CALCAMNT result = *(data.first());
104 printf("result=%f\n",result);
105
106 CALCAMNT *dp = data.next();
107 for ( ; dp != 0; dp=data.next() )
108 if ( *dp < result )
109 result = *dp;
110
111 printf("Return\n");
112
113 return result;
114}
115
116CALCAMNT KStats::max()
117{
118 if ( data.count() == 0 )
119 return 0.0;
120
121 CALCAMNT result = *(data.first());
122 CALCAMNT *dp = data.next();
123 for ( ; dp != 0; dp=data.next() )
124 if ( *dp > result )
125 result = *dp;
126
127 return result;
128}
129
130CALCAMNT KStats::mul(){
131
132 CALCAMNT result = 1.0;
133 CALCAMNT *dp;
134 for ( dp=data.first(); dp != 0; dp=data.next() ){
135
136 result *= *dp;
137
138 }
139
140 return result;
141}
142
143CALCAMNT KStats::median(){
144
145 int index;
146 CALCAMNT result;
147 CALCAMNT *dp;
148 int bound = 0;
149
150 MyList list;
151
152 for ( dp=data.first(); dp != 0; dp=data.next() ){
153 list.inSort(dp);
154 }
155
156#ifdef DEBUG_STATS
157 for(int l = 0; l < (int)list.count();l++){
158
159 printf("Sorted %Lg\n",*list.at(l));
160
161 }
162#endif
163
164 bound = list.count();
165
166 if (bound == 0){
167 error_flag = true;
168 return 0.0;
169 }
170
171 if ( bound == 1)
172 return *list.at(0);
173
174 if( bound % 2){ // odd
175
176 index = (bound - 1 ) / 2 + 1;
177 result = *list.at(index - 1 );
178 }
179 else { // even
180
181 index = bound / 2;
182 result = ((*list.at(index - 1)) + (*list.at(index)))/2;
183 }
184
185 return result;
186
187}
188
189
190
191CALCAMNT KStats::std_kernel(){
192
193 CALCAMNT result = 0.0;
194 CALCAMNT _mean;
195
196 _mean = mean();
197
198 CALCAMNT *dp;
199 for ( dp=data.first(); dp != 0; dp=data.next() ){
200
201 result += (*dp - _mean) * (*dp - _mean);
202
203 }
204
205#ifdef DEBUG_STATS
206 printf("std_kernel %Lg\n",result);
207#endif
208
209 return result;
210
211}
212
213
214CALCAMNT KStats::sum_of_squares(){
215
216 CALCAMNT result = 0.0;
217 CALCAMNT *dp;
218 for ( dp=data.first(); dp != 0; dp=data.next() ){
219
220 result += (*dp) * (*dp);
221
222 }
223
224#ifdef DEBUG_STATS
225 printf("Sum of Squares %Lg\n",result);
226#endif
227
228 return result;
229
230}
231
232CALCAMNT KStats::mean(){
233
234 CALCAMNT result = 0.0;
235
236 if(data.count() == 0){
237 error_flag = true;
238 return 0.0;
239 }
240
241 result = sum()/data.count();
242
243#ifdef DEBUG_STATS
244 printf("mean: %Lg\n",result);
245#endif
246
247 return result;
248
249}
250
251CALCAMNT KStats::std(){
252
253 CALCAMNT result = 0.0;
254
255 if(data.count() == 0){
256 error_flag = true;
257
258#ifdef DEBUG_STATS
259 printf("set stats error\n");
260#endif
261
262 return 0.0;
263 }
264
265 result = SQRT(std_kernel());
266
267#ifdef DEBUG_STATS
268 printf ("data.count %d\n",data.count());
269#endif
270
271 result = result/data.count();
272
273#ifdef DEBUG_STATS
274 printf("std: %Lg\n",result);
275#endif
276
277 return result;
278
279}
280
281CALCAMNT KStats::sample_std(){
282
283 CALCAMNT result = 0.0;
284
285 if(data.count() < 2 ){
286 error_flag = true;
287 return 0.0;
288 }
289
290 result = SQRT(std_kernel());
291 result = result/(data.count() - 1);
292#ifdef DEBUG_STATS
293 printf("sample std: %Lg\n",result);
294#endif
295 return result;
296
297}
298
299int KStats::count(){
300
301 return data.count();
302
303}
304
305bool KStats::error(){
306
307 bool value;
308 value = error_flag;
309 error_flag = false;
310
311 return value;
312
313}
314
315int MyList::compareItems(Item item_1, Item item_2){
316
317 CALCAMNT *item1;
318 CALCAMNT *item2;
319
320 item1 = (CALCAMNT*) item_1;
321 item2 = (CALCAMNT*) item_2;
322
323 if(*item1 > *item2)
324 return 1;
325
326 if(*item2 > *item1)
327 return -1;
328
329 return 0;
330
331}
332
333
334