mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
added comments to the code
This commit is contained in:
parent
6da2b9645a
commit
b596b7e42d
@ -1,3 +1,11 @@
|
|||||||
|
/**
|
||||||
|
* \file
|
||||||
|
* \brief Compute statistics for data entered in rreal-time
|
||||||
|
*
|
||||||
|
* This algorithm is really beneficial to compute statistics on data read in
|
||||||
|
* realtime. For example, devices reading biometrics data. The algorithm is
|
||||||
|
* simple enough to be easily implemented in an embedded system.
|
||||||
|
*/
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
@ -8,6 +16,9 @@
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
class stats_computer1 {
|
class stats_computer1 {
|
||||||
public:
|
public:
|
||||||
|
/** Constructor
|
||||||
|
* \param[in] x new data sample
|
||||||
|
*/
|
||||||
void new_val(T x) {
|
void new_val(T x) {
|
||||||
if (n == 0) K = x;
|
if (n == 0) K = x;
|
||||||
n++;
|
n++;
|
||||||
@ -16,13 +27,18 @@ class stats_computer1 {
|
|||||||
Ex2 += tmp * tmp;
|
Ex2 += tmp * tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** return sample mean computed till last sample */
|
||||||
double mean() const { return K + Ex / n; }
|
double mean() const { return K + Ex / n; }
|
||||||
|
|
||||||
|
/** return data variance computed till last sample */
|
||||||
double variance() const { return (Ex2 - (Ex * Ex) / n) / (n - 1); }
|
double variance() const { return (Ex2 - (Ex * Ex) / n) / (n - 1); }
|
||||||
|
|
||||||
|
/** return sample standard deviation computed till last sample */
|
||||||
double std() const { return std::sqrt(this->variance()); }
|
double std() const { return std::sqrt(this->variance()); }
|
||||||
|
|
||||||
friend std::istream &operator>>(std::istream &input, stats_computer1 &stat) {
|
/** short-hand operator to read new sample from input stream */
|
||||||
|
friend std::istream &operator>>(std::istream &input,
|
||||||
|
stats_computer1 &stat) {
|
||||||
T val;
|
T val;
|
||||||
input >> val;
|
input >> val;
|
||||||
stat.new_val(val);
|
stat.new_val(val);
|
||||||
@ -42,6 +58,9 @@ class stats_computer1 {
|
|||||||
template <typename T>
|
template <typename T>
|
||||||
class stats_computer2 {
|
class stats_computer2 {
|
||||||
public:
|
public:
|
||||||
|
/** Constructor
|
||||||
|
* \param[in] x new data sample
|
||||||
|
*/
|
||||||
void new_val(T x) {
|
void new_val(T x) {
|
||||||
n++;
|
n++;
|
||||||
double delta = x - mu;
|
double delta = x - mu;
|
||||||
@ -50,13 +69,18 @@ class stats_computer2 {
|
|||||||
M += delta * delta2;
|
M += delta * delta2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** return sample mean computed till last sample */
|
||||||
double mean() const { return mu; }
|
double mean() const { return mu; }
|
||||||
|
|
||||||
|
/** return data variance computed till last sample */
|
||||||
double variance() const { return M / n; }
|
double variance() const { return M / n; }
|
||||||
|
|
||||||
|
/** return sample standard deviation computed till last sample */
|
||||||
double std() const { return std::sqrt(this->variance()); }
|
double std() const { return std::sqrt(this->variance()); }
|
||||||
|
|
||||||
friend std::istream &operator>>(std::istream &input, stats_computer2 &stat) {
|
/** short-hand operator to read new sample from input stream */
|
||||||
|
friend std::istream &operator>>(std::istream &input,
|
||||||
|
stats_computer2 &stat) {
|
||||||
T val;
|
T val;
|
||||||
input >> val;
|
input >> val;
|
||||||
stat.new_val(val);
|
stat.new_val(val);
|
||||||
@ -68,22 +92,27 @@ class stats_computer2 {
|
|||||||
double mu = 0, var = 0, M = 0;
|
double mu = 0, var = 0, M = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Main function */
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
std::cout << "Enter data. Any non-numeric data will terminate the data input."
|
std::cout
|
||||||
|
<< "Enter data. Any non-numeric data will terminate the data input."
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
stats_computer1<double> stats1;
|
stats_computer1<float> stats1;
|
||||||
stats_computer2<double> stats2;
|
stats_computer2<float> stats2;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
double val;
|
double val;
|
||||||
std::cout << "Enter number: ";
|
std::cout << "Enter number: ";
|
||||||
std::cin >> val;
|
std::cin >> val;
|
||||||
if (std::cin.fail()) // check for failure to read input. Happens for
|
|
||||||
|
// check for failure to read input. Happens for
|
||||||
// non-numeric data
|
// non-numeric data
|
||||||
break;
|
if (std::cin.fail()) break;
|
||||||
|
|
||||||
stats1.new_val(val);
|
stats1.new_val(val);
|
||||||
stats2.new_val(val);
|
stats2.new_val(val);
|
||||||
|
|
||||||
std::cout << "\tMethod 1:"
|
std::cout << "\tMethod 1:"
|
||||||
<< "\tMean: " << stats1.mean()
|
<< "\tMean: " << stats1.mean()
|
||||||
<< "\t Variance: " << stats1.variance()
|
<< "\t Variance: " << stats1.variance()
|
||||||
|
Loading…
Reference in New Issue
Block a user