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

kcalc

  • sources
  • kde-4.14
  • kdeutils
  • kcalc
stats.cpp
Go to the documentation of this file.
1 /*
2 Copyright (C) 2001 - 2013 Evan Teran
3  evan.teran@gmail.com
4 
5 Copyright (C) 1996 - 2000 Bernd Johannes Wuebben
6  wuebben@kde.org
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
22 #include "stats.h"
23 
24 //------------------------------------------------------------------------------
25 // Name: KStats
26 // Desc: constructor
27 //------------------------------------------------------------------------------
28 KStats::KStats() : error_flag_(false) {
29 }
30 
31 //------------------------------------------------------------------------------
32 // Name: ~KStats
33 // Desc: destructor
34 //------------------------------------------------------------------------------
35 KStats::~KStats() {
36 }
37 
38 //------------------------------------------------------------------------------
39 // Name: clearAll
40 // Desc: empties the data set
41 //------------------------------------------------------------------------------
42 void KStats::clearAll() {
43  data_.clear();
44 }
45 
46 //------------------------------------------------------------------------------
47 // Name: enterData
48 // Desc: adds an item to the data set
49 //------------------------------------------------------------------------------
50 void KStats::enterData(const KNumber &data) {
51  data_.push_back(data);
52 }
53 
54 //------------------------------------------------------------------------------
55 // Name: clearLast
56 // Desc: remoaves the last item from the data set
57 //------------------------------------------------------------------------------
58 void KStats::clearLast() {
59 
60  if(!data_.isEmpty()) {
61  data_.pop_back();
62  }
63 }
64 
65 //------------------------------------------------------------------------------
66 // Name: sum
67 // Desc: calculates the SUM of all values in the data set
68 //------------------------------------------------------------------------------
69 KNumber KStats::sum() const {
70 
71  KNumber result = KNumber::Zero;
72 
73  Q_FOREACH(const KNumber &x, data_) {
74  result += x;
75  }
76 
77  return result;
78 }
79 
80 //------------------------------------------------------------------------------
81 // Name: median
82 // Desc: calculates the MEDIAN of all values in the data set
83 //------------------------------------------------------------------------------
84 KNumber KStats::median() {
85 
86  KNumber result = KNumber::Zero;
87  size_t index;
88 
89  unsigned int bound = count();
90 
91  if (bound == 0) {
92  error_flag_ = true;
93  return KNumber::Zero;
94  }
95 
96  if (bound == 1)
97  return data_.at(0);
98 
99  // need to copy data_-list, because sorting afterwards
100  QVector<KNumber> tmp_data(data_);
101  qSort(tmp_data);
102 
103  if (bound & 1) { // odd
104  index = (bound - 1) / 2 + 1;
105  result = tmp_data.at(index - 1);
106  } else { // even
107  index = bound / 2;
108  result = ((tmp_data.at(index - 1)) + (tmp_data.at(index))) / KNumber(2);
109  }
110 
111  return result;
112 }
113 
114 //------------------------------------------------------------------------------
115 // Name: std_kernel
116 // Desc: calculates the STD Kernel of all values in the data set
117 //------------------------------------------------------------------------------
118 KNumber KStats::std_kernel() {
119  KNumber result = KNumber::Zero;
120  const KNumber mean_value = mean();
121 
122  if(mean_value.type() != KNumber::TYPE_ERROR) {
123  Q_FOREACH(const KNumber &x, data_) {
124  result += (x - mean_value) * (x - mean_value);
125  }
126  }
127 
128  return result;
129 }
130 
131 //------------------------------------------------------------------------------
132 // Name: sum_of_squares
133 // Desc: calculates the SUM of all values in the data set (each squared)
134 //------------------------------------------------------------------------------
135 KNumber KStats::sum_of_squares() const {
136 
137  KNumber result = KNumber::Zero;
138 
139  Q_FOREACH(const KNumber &x, data_) {
140  result += (x * x);
141  }
142 
143  return result;
144 }
145 
146 //------------------------------------------------------------------------------
147 // Name: mean
148 // Desc: calculates the MEAN of all values in the data set
149 //------------------------------------------------------------------------------
150 KNumber KStats::mean() {
151 
152  if (data_.isEmpty()) {
153  error_flag_ = true;
154  return KNumber::Zero;
155  }
156 
157  return (sum() / KNumber(count()));
158 }
159 
160 //------------------------------------------------------------------------------
161 // Name: std
162 // Desc: calculates the STANDARD DEVIATION of all values in the data set
163 //------------------------------------------------------------------------------
164 KNumber KStats::std() {
165 
166  if (data_.isEmpty()) {
167  error_flag_ = true;
168  return KNumber::Zero;
169  }
170 
171  return (std_kernel() / KNumber(count())).sqrt();
172 }
173 
174 //------------------------------------------------------------------------------
175 // Name: sample_std
176 // Desc: calculates the SAMPLE STANDARD DEVIATION of all values in the data set
177 //------------------------------------------------------------------------------
178 KNumber KStats::sample_std() {
179 
180  KNumber result = KNumber::Zero;
181 
182  if (count() < 2) {
183  error_flag_ = true;
184  return KNumber::Zero;
185  }
186 
187  result = (std_kernel() / KNumber(count() - 1)).sqrt();
188 
189  return result;
190 }
191 
192 //------------------------------------------------------------------------------
193 // Name: count
194 // Desc: returns the amount of values in the data set
195 //------------------------------------------------------------------------------
196 int KStats::count() const {
197 
198  return data_.size();
199 }
200 
201 //------------------------------------------------------------------------------
202 // Name: error
203 // Desc: returns the error state AND clears it
204 //------------------------------------------------------------------------------
205 bool KStats::error() {
206 
207  bool value = error_flag_;
208  error_flag_ = false;
209  return value;
210 }
211 
212 
213 
KStats::median
KNumber median()
Definition: stats.cpp:84
KStats::~KStats
~KStats()
Definition: stats.cpp:35
KNumber::Zero
static const KNumber Zero
Definition: knumber.h:49
KStats::count
int count() const
Definition: stats.cpp:196
KStats::error
bool error()
Definition: stats.cpp:205
KStats::sample_std
KNumber sample_std()
Definition: stats.cpp:178
stats.h
QVector::clear
void clear()
KStats::enterData
void enterData(const KNumber &data)
Definition: stats.cpp:50
sqrt
KNumber sqrt(const KNumber &x)
Definition: knumber_operators.cpp:132
KNumber::KNumber
KNumber()
Definition: knumber.cpp:270
KNumber::type
Type type() const
Definition: knumber.cpp:400
KStats::sum
KNumber sum() const
Definition: stats.cpp:69
QVector::pop_back
void pop_back()
KNumber
Definition: knumber.h:30
KStats::clearLast
void clearLast()
Definition: stats.cpp:58
KStats::KStats
KStats()
Definition: stats.cpp:28
QVector::at
const T & at(int i) const
QVector< KNumber >
QVector::isEmpty
bool isEmpty() const
KStats::std_kernel
KNumber std_kernel()
Definition: stats.cpp:118
QVector::push_back
void push_back(const T &value)
QVector::size
int size() const
KStats::sum_of_squares
KNumber sum_of_squares() const
Definition: stats.cpp:135
KNumber::TYPE_ERROR
Definition: knumber.h:41
KStats::mean
KNumber mean()
Definition: stats.cpp:150
KStats::std
KNumber std()
Definition: stats.cpp:164
KStats::clearAll
void clearAll()
Definition: stats.cpp:42
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:42:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kcalc

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

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • ktimer
  • kwallet
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal