X-Git-Url: http://git.kpe.io/?p=ctsim.git;a=blobdiff_plain;f=libctsupport%2Fmathfuncs.cpp;h=c6dd98dafc0c3188eccda386b43e5eb7a81a3f03;hp=c9b18ac3da75ca61cf6c10e82bfe5689a4bca0a7;hb=c00c639073653fac7463a88f2b000f263236550d;hpb=23b7ef994fc5d95fcca6d4ae69abbd5971101262 diff --git a/libctsupport/mathfuncs.cpp b/libctsupport/mathfuncs.cpp index c9b18ac..c6dd98d 100644 --- a/libctsupport/mathfuncs.cpp +++ b/libctsupport/mathfuncs.cpp @@ -2,7 +2,7 @@ ** This is part of the CTSim program ** Copyright (C) 1983-2000 Kevin Rosenberg ** -** $Id: mathfuncs.cpp,v 1.6 2001/01/02 05:34:57 kevin Exp $ +** $Id: mathfuncs.cpp,v 1.7 2001/01/02 16:02:13 kevin Exp $ ** ** This program is free software; you can redistribute it and/or modify ** it under the terms of the GNU General Public License (version 2) as @@ -81,57 +81,57 @@ normalizeAngle (double theta) return (theta); } - - -void -vectorNumericStatistics (std::vector vec, const int nPoints, double& min, double& max, double& mean, double& mode, double& median, double& stddev) -{ - if (nPoints <= 0) - return; - - mean = 0; - min = vec[0]; - max = vec[0]; - int i; - for (i = 0; i < nPoints; i++) { - double v = vec[i]; - if (v > max) - max = v; - if (v < min) - min = v; - mean += v; - } - mean /= nPoints; - - static const int nbin = 1024; - int hist[ nbin ] = {0}; - double spread = max - min; - mode = 0; - stddev = 0; - for (i = 0; i < nPoints; i++) { - double v = vec[i]; - int b = static_cast((((v - min) / spread) * (nbin - 1)) + 0.5); - hist[b]++; - double diff = (v - mean); - stddev += diff * diff; - } - stddev = sqrt (stddev / nPoints); - - int max_binindex = 0; - int max_bin = -1; - for (int ibin = 0; ibin < nbin; ibin++) { - if (hist[ibin] > max_bin) { - max_bin = hist[ibin]; - max_binindex = ibin; - } - } - - mode = (max_binindex * spread / (nbin - 1)) + min; - - std::sort(vec.begin(), vec.end()); - - if (nPoints % 2) // Odd - median = vec[((nPoints - 1) / 2)]; - else // Even - median = (vec[ (nPoints / 2) - 1 ] + vec[ nPoints / 2 ]) / 2; -} + + +void +vectorNumericStatistics (std::vector vec, const int nPoints, double& min, double& max, double& mean, double& mode, double& median, double& stddev) +{ + if (nPoints <= 0) + return; + + mean = 0; + min = vec[0]; + max = vec[0]; + int i; + for (i = 0; i < nPoints; i++) { + double v = vec[i]; + if (v > max) + max = v; + if (v < min) + min = v; + mean += v; + } + mean /= nPoints; + + static const int nbin = 1024; + int hist[ nbin ] = {0}; + double spread = max - min; + mode = 0; + stddev = 0; + for (i = 0; i < nPoints; i++) { + double v = vec[i]; + int b = static_cast((((v - min) / spread) * (nbin - 1)) + 0.5); + hist[b]++; + double diff = (v - mean); + stddev += diff * diff; + } + stddev = sqrt (stddev / nPoints); + + int max_binindex = 0; + int max_bin = -1; + for (int ibin = 0; ibin < nbin; ibin++) { + if (hist[ibin] > max_bin) { + max_bin = hist[ibin]; + max_binindex = ibin; + } + } + + mode = (max_binindex * spread / (nbin - 1)) + min; + + std::sort(vec.begin(), vec.end()); + + if (nPoints % 2) // Odd + median = vec[((nPoints - 1) / 2)]; + else // Even + median = (vec[ (nPoints / 2) - 1 ] + vec[ nPoints / 2 ]) / 2; +}