• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeutils
  • Sitemap
  • Contact Us
 

kcalc

stats.cpp

Go to the documentation of this file.
00001 /*
00002     $Id: stats.cpp 493364 2006-01-02 10:30:13Z kniederk $
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00023 
00024 */
00025 
00026 
00027 #include "stats.h"
00028 #ifdef DEBUG_STATS
00029     #include <stdio.h>
00030 #endif
00031 
00032 KStats::KStats() {
00033     error_flag = false;
00034 }
00035 
00036 KStats::~KStats() {
00037 }
00038 
00039 void KStats::clearAll() {
00040     mData.clear();
00041 }
00042  
00043 void KStats::enterData(KNumber const & _data) {
00044 
00045     mData.push_back(_data);
00046 #ifdef DEBUG_STATS
00047     printf("Added %Lg\n", _data);
00048     printf("count %d\n", mData.size());
00049 #endif
00050 
00051 }
00052 
00053 
00054 void KStats::clearLast(void) {
00055 
00056     mData.pop_back();
00057 #ifdef DEBUG_STATS
00058     printf("count %d\n",mData.size());
00059 #endif   
00060 
00061 
00062 }
00063 
00064 KNumber KStats::sum(void) {
00065 
00066     KNumber result = 0;
00067     QVector<KNumber>::iterator p;
00068 
00069     for(p = mData.begin(); p != mData.end(); ++p) {
00070         result += *p;
00071     }
00072     
00073 #ifdef DEBUG_STATS
00074     printf("Sum %Lg\n", result);
00075 #endif
00076 
00077     return result;
00078 }
00079 
00080 KNumber KStats::median(void) {
00081 
00082     KNumber result = 0;
00083     unsigned int bound;
00084     size_t index;
00085 
00086     bound = count();
00087 
00088     if (bound == 0){
00089         error_flag = true;
00090         return 0;
00091     }
00092 
00093     if (bound == 1)
00094         return mData.at(0);
00095 
00096     // need to copy mData-list, because sorting afterwards
00097     QVector<KNumber> tmp_mData(mData);
00098     qSort(tmp_mData);
00099 
00100     if( bound & 1) {  // odd
00101         index = (bound - 1 ) / 2 + 1;
00102         result =  tmp_mData.at(index - 1);
00103     } else { // even
00104       index = bound / 2;
00105       result = ((tmp_mData.at(index - 1))  + (tmp_mData.at(index))) / KNumber(2);
00106     }
00107 
00108     return result;
00109 }
00110 
00111 
00112 KNumber KStats::std_kernel(void)
00113 {
00114     KNumber result = KNumber::Zero;
00115     KNumber _mean;
00116     QVector<KNumber>::iterator p;
00117 
00118     _mean = mean();
00119 
00120     for(p = mData.begin(); p != mData.end(); ++p) {
00121         result += (*p - _mean) * (*p - _mean);
00122     }
00123 
00124     return result;
00125 }
00126 
00127 
00128 KNumber KStats::sum_of_squares() {
00129 
00130     KNumber result = 0;
00131     QVector<KNumber>::iterator p;
00132   
00133     for(p = mData.begin(); p != mData.end(); ++p) {
00134         result += ((*p) * (*p));
00135     }
00136 
00137     return result;
00138 }
00139 
00140 
00141 KNumber KStats::mean(void)
00142 {
00143     if(count() == 0){
00144         error_flag = true;
00145         return 0;
00146     }
00147 
00148     return (sum() / KNumber(count()));
00149 }
00150 
00151 
00152 KNumber KStats::std(void)
00153 {
00154     if(count() == 0){
00155         error_flag = true;
00156         return KNumber::Zero;
00157     }
00158 
00159     return (std_kernel() / KNumber(count())).sqrt();
00160 }
00161 
00162 
00163 KNumber KStats::sample_std(void) {
00164     KNumber result = 0;
00165 
00166     if(count() < 2 ){
00167         error_flag = true;
00168         return KNumber::Zero;
00169     }
00170 
00171     result = (std_kernel() / KNumber(count() - 1)).sqrt();
00172     
00173     //  result = result/(count() - 1);
00174 #ifdef DEBUG_STATS
00175     printf("sample std: %Lg\n",result);
00176 #endif
00177 
00178     return result;
00179 }
00180 
00181 
00182 int KStats::count(void) const
00183 {
00184   return static_cast<int>(mData.size());
00185 }
00186 
00187 
00188 bool KStats::error() {
00189 
00190   bool value = error_flag;
00191   error_flag = false;
00192   return value;
00193 }
00194 
00195 
00196 

kcalc

Skip menu "kcalc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members

kdeutils

Skip menu "kdeutils"
  • ark
  • kcalc
  • kcharselect
  • kdelirc
  • kdessh
  • kdf
  • kfloppy
  • kgpg
  • kjots
  • klaptopdaemon
  • kmilo
  • ksim
  • ktimer
  • kwallet
  • superkaramba
Generated for kdeutils by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal