kspread Library API Documentation

stats.cpp

00001 /*
00002     $Id: stats.cpp 214601 2003-03-17 20:10:20Z mueller $
00003 
00004     KCalc, a scientific calculator for the X window system using the
00005     Qt widget libraries, available at no cost at http://www.troll.no
00006 
00007     Copyright (C) 1996 Bernd Johannes Wuebben
00008                        wuebben@math.cornell.edu
00009 
00010     This program is free software; you can redistribute it and/or modify
00011     it under the terms of the GNU General Public License as published by
00012     the Free Software Foundation; either version 2 of the License, or
00013     (at your option) any later version.
00014 
00015     This program is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU General Public License for more details.
00019 
00020     You should have received a copy of the GNU General Public License
00021     along with this program; if not, write to the Free Software
00022     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00023 
00024 */
00025 
00026 
00027 #include "stats.h"
00028 #include <stdio.h>
00029 #include <stdlib.h>
00030 
00031 KStats::KStats(){
00032 
00033   error_flag = FALSE;
00034   data.setAutoDelete(TRUE);
00035 
00036 }
00037 
00038 KStats::~KStats(){
00039 
00040   data.clear();
00041 
00042 }
00043 
00044 void KStats::clearAll(){
00045 
00046   data.clear();
00047 
00048 }
00049 
00050 void KStats::enterData(CALCAMNT _data){
00051 
00052   CALCAMNT *newdata;
00053   newdata = new CALCAMNT;
00054   *newdata = _data;
00055   data.append(newdata);
00056 
00057 #ifdef DEBUG_STATS
00058   printf("Added %Lg\n",*newdata);
00059   printf("count %d\n",data.count());
00060 #endif
00061 
00062 }
00063 
00064 
00065 void KStats::clearLast(){
00066 
00067 
00068  data.removeLast();
00069 #ifdef DEBUG_STATS
00070 printf("count %d\n",data.count());
00071 #endif
00072 
00073 
00074 }
00075 
00076 CALCAMNT KStats::sum(){
00077 
00078   CALCAMNT result = 0.0;
00079   CALCAMNT *dp;
00080   for ( dp=data.first(); dp != 0; dp=data.next() ){
00081 
00082     result += *dp;
00083 
00084   }
00085 
00086 #ifdef DEBUG_STATS
00087   printf("Sum %Lg\n",result);
00088 #endif
00089 
00090   return result;
00091 }
00092 
00093 CALCAMNT KStats::min()
00094 {
00095   printf("MIIINNNN\n");
00096 
00097   if ( data.count() == 0 )
00098     return 0.0;
00099 
00100   printf("1\n");
00101 
00102   CALCAMNT result = *(data.first());
00103   printf("result=%f\n",result);
00104 
00105   CALCAMNT *dp = data.next();
00106   for ( ; dp != 0; dp=data.next() )
00107     if ( *dp < result )
00108       result = *dp;
00109 
00110   printf("Return\n");
00111 
00112   return result;
00113 }
00114 
00115 CALCAMNT KStats::max()
00116 {
00117   if ( data.count() == 0 )
00118     return 0.0;
00119 
00120   CALCAMNT result = *(data.first());
00121   CALCAMNT *dp = data.next();
00122   for ( ; dp != 0; dp=data.next() )
00123     if ( *dp > result )
00124       result = *dp;
00125 
00126   return result;
00127 }
00128 
00129 CALCAMNT KStats::mul(){
00130 
00131   CALCAMNT result = 1.0;
00132   CALCAMNT *dp;
00133   for ( dp=data.first(); dp != 0; dp=data.next() ){
00134 
00135     result *= *dp;
00136 
00137   }
00138 
00139   return result;
00140 }
00141 
00142 CALCAMNT KStats::median(){
00143 
00144   int index;
00145   CALCAMNT result;
00146   CALCAMNT *dp;
00147   int bound = 0;
00148 
00149   MyList list;
00150 
00151   for ( dp=data.first(); dp != 0; dp=data.next() ){
00152     list.inSort(dp);
00153   }
00154 
00155 #ifdef DEBUG_STATS
00156   for(int l = 0; l < (int)list.count();l++){
00157 
00158     printf("Sorted %Lg\n",*list.at(l));
00159 
00160   }
00161 #endif
00162 
00163   bound = list.count();
00164 
00165   if (bound == 0){
00166     error_flag = TRUE;
00167     return 0.0;
00168   }
00169 
00170   if ( bound == 1)
00171     return *list.at(0);
00172 
00173   if( bound % 2){  // odd
00174 
00175     index = (bound - 1 ) / 2 + 1;
00176     result =  *list.at(index - 1 );
00177   }
00178   else { // even
00179 
00180     index = bound / 2;
00181     result = ((*list.at(index - 1))  + (*list.at(index)))/2;
00182  }
00183 
00184   return result;
00185 
00186 }
00187 
00188 
00189 
00190 CALCAMNT KStats::std_kernel(){
00191 
00192   CALCAMNT result = 0.0;
00193   CALCAMNT _mean;
00194 
00195   _mean = mean();
00196 
00197   CALCAMNT *dp;
00198   for ( dp=data.first(); dp != 0; dp=data.next() ){
00199 
00200     result += (*dp - _mean) * (*dp - _mean);
00201 
00202   }
00203 
00204 #ifdef DEBUG_STATS
00205   printf("std_kernel %Lg\n",result);
00206 #endif
00207 
00208   return result;
00209 
00210 }
00211 
00212 
00213 CALCAMNT KStats::sum_of_squares(){
00214 
00215   CALCAMNT result = 0.0;
00216   CALCAMNT *dp;
00217   for ( dp=data.first(); dp != 0; dp=data.next() ){
00218 
00219     result += (*dp) * (*dp);
00220 
00221   }
00222 
00223 #ifdef DEBUG_STATS
00224   printf("Sum of Squares %Lg\n",result);
00225 #endif
00226 
00227   return result;
00228 
00229 }
00230 
00231 CALCAMNT KStats::mean(){
00232 
00233   CALCAMNT result = 0.0;
00234 
00235   if(data.count() == 0){
00236     error_flag = TRUE;
00237     return 0.0;
00238   }
00239 
00240   result = sum()/data.count();
00241 
00242 #ifdef DEBUG_STATS
00243   printf("mean: %Lg\n",result);
00244 #endif
00245 
00246   return result;
00247 
00248 }
00249 
00250 CALCAMNT KStats::std(){
00251 
00252   CALCAMNT result = 0.0;
00253 
00254   if(data.count() == 0){
00255     error_flag = TRUE;
00256 
00257 #ifdef DEBUG_STATS
00258     printf("set stats error\n");
00259 #endif
00260 
00261     return 0.0;
00262   }
00263 
00264   result = SQRT(std_kernel());
00265 
00266 #ifdef DEBUG_STATS
00267   printf ("data.count %d\n",data.count());
00268 #endif
00269 
00270   result = result/data.count();
00271 
00272 #ifdef DEBUG_STATS
00273   printf("std: %Lg\n",result);
00274 #endif
00275 
00276   return result;
00277 
00278 }
00279 
00280 CALCAMNT KStats::sample_std(){
00281 
00282   CALCAMNT result = 0.0;
00283 
00284   if(data.count() < 2 ){
00285     error_flag = TRUE;
00286     return 0.0;
00287   }
00288 
00289   result = SQRT(std_kernel());
00290   result = result/(data.count() - 1);
00291 #ifdef DEBUG_STATS
00292   printf("sample std: %Lg\n",result);
00293 #endif
00294   return result;
00295 
00296 }
00297 
00298 int  KStats::count(){
00299 
00300   return data.count();
00301 
00302 }
00303 
00304 bool KStats::error(){
00305 
00306   bool value;
00307   value = error_flag;
00308   error_flag = FALSE;
00309 
00310   return value;
00311 
00312 }
00313 
00314 int MyList::compareItems(Item item_1, Item item_2){
00315 
00316   CALCAMNT *item1;
00317   CALCAMNT *item2;
00318 
00319   item1 = (CALCAMNT*) item_1;
00320   item2 = (CALCAMNT*) item_2;
00321 
00322   if(*item1 > *item2)
00323     return 1;
00324 
00325   if(*item2 > *item1)
00326     return -1;
00327 
00328   return 0;
00329 
00330 }
00331 
00332 
00333 
KDE Logo
This file is part of the documentation for kspread Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:43:38 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003