File tree Expand file tree Collapse file tree 5 files changed +100
-0
lines changed
Expand file tree Collapse file tree 5 files changed +100
-0
lines changed Original file line number Diff line number Diff line change @@ -73,6 +73,8 @@ add_library(utility SHARED
7373 src/pflib/utility/str_to_int.cxx
7474 src/pflib/utility/median.cxx
7575 src/pflib/utility/efficiency.cxx
76+ src/pflib/utility/mean.cxx
77+ src/pflib/utility/stdev.cxx
7678)
7779target_include_directories (utility PUBLIC
7880 "$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR} /include>"
Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ #include < vector>
4+
5+ namespace pflib ::utility {
6+
7+ /* *
8+ * find the mean of the input vector of samples
9+ *
10+ * @param[in] samples list of samples to find mean of
11+ * @return mean of the samples
12+ */
13+ double mean (const std::vector<int >& samples);
14+
15+ /* *
16+ * find the mean of the input vector of samples
17+ *
18+ * @param[in] samples list of samples to find mean of
19+ * @return mean of the samples
20+ */
21+ double mean (const std::vector<double >& samples);
22+
23+ } // namespace pflib::utility
Original file line number Diff line number Diff line change 1+ #pragma once
2+
3+ #include < vector>
4+
5+ namespace pflib ::utility {
6+
7+ /* *
8+ * find the stdev of the input vector of samples
9+ *
10+ * @param[in] samples list of samples to find stdev of
11+ * @return stdev of the samples
12+ *
13+ * Method based on the method used by numpy.std. See numpy.std documentation.
14+ */
15+ double stdev (const std::vector<int >& samples);
16+
17+ /* *
18+ * find the stdev of the input vector of samples
19+ *
20+ * @param[in] samples list of samples to find stdev of
21+ * @return stdev of the samples
22+ *
23+ * Method based on the method used by numpy.std. See numpy.std documentation.
24+ */
25+ double stdev (const std::vector<double >& samples);
26+
27+ } // namespace pflib::utility
Original file line number Diff line number Diff line change 1+ #include " pflib/utility/mean.h"
2+
3+ #include < limits>
4+ #include < numeric> // std::accumulate
5+
6+ namespace pflib ::utility {
7+
8+ template <typename T>
9+ double mean_impl (const std::vector<T>& samples) {
10+ if (samples.size () == 0 ) {
11+ return std::numeric_limits<double >::quiet_NaN ();
12+ }
13+ return std::accumulate (samples.begin (), samples.end (), 0.0 ) / samples.size ();
14+ }
15+
16+ double mean (const std::vector<double >& samples) { return mean_impl (samples); }
17+
18+ double mean (const std::vector<int >& samples) { return mean_impl (samples); }
19+
20+ } // namespace pflib::utility
Original file line number Diff line number Diff line change 1+ #include " pflib/utility/stdev.h"
2+
3+ #include < cmath> // std::sqrt, std::pow, std::abs
4+ #include < limits>
5+ #include < numeric> // std::accumulate
6+
7+ #include " pflib/utility/mean.h"
8+
9+ namespace pflib ::utility {
10+
11+ template <typename T>
12+ double stdev_impl (const std::vector<T>& samples) {
13+ if (samples.size () == 0 ) {
14+ return std::numeric_limits<double >::quiet_NaN ();
15+ }
16+ double mean = pflib::utility::mean (samples);
17+ double sum = std::accumulate (samples.begin (), samples.end (), 0.0 ,
18+ [mean](double acc, double v) {
19+ return acc + std::pow (std::abs ((v - mean)), 2 );
20+ });
21+ return std::sqrt (sum / samples.size ());
22+ }
23+
24+ double stdev (const std::vector<double >& samples) { return stdev_impl (samples); }
25+
26+ double stdev (const std::vector<int >& samples) { return stdev_impl (samples); }
27+
28+ } // namespace pflib::utility
You can’t perform that action at this time.
0 commit comments