generated from cpp-best-practices/gui_starter_template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added first version of central_moment, variance and standard_deviation * Bumped visual studio version for tests --------- Co-authored-by: Florian Weik <[email protected]>
- Loading branch information
Showing
5 changed files
with
136 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#ifndef NRNG_CENTRAL_MOMENT_HPP | ||
#define NRNG_CENTRAL_MOMENT_HPP | ||
|
||
#include <nrng/moment.hpp> | ||
#include <nrng/type_traits.hpp> | ||
|
||
#include <cmath> | ||
|
||
namespace nrng { | ||
/** | ||
* @brief N-th central moment of range. | ||
*/ | ||
template <unsigned Exponent, std::input_iterator I, std::sentinel_for<I> S> | ||
constexpr auto central_moment(I first, S last) { | ||
auto const mean = ::nrng::mean(first, last); | ||
|
||
return ::nrng::transform_reduce( | ||
first, last, std::iter_value_t<I>{}, std::plus<>{}, | ||
[mean](auto v) { return power<Exponent>(v - mean); }) / | ||
std::ranges::distance(first, last); | ||
} | ||
|
||
/** | ||
* @brief N-th central moment of range. | ||
*/ | ||
template <unsigned Exponent, std::ranges::input_range Rng> | ||
constexpr auto central_moment(Rng values) { | ||
return central_moment<Exponent>(std::ranges::begin(values), | ||
std::ranges::end(values)); | ||
} | ||
|
||
template <std::ranges::input_range Rng> constexpr auto variance(Rng values) { | ||
return central_moment<2>(values); | ||
} | ||
|
||
template <std::input_iterator I, std::sentinel_for<I> S> | ||
constexpr auto variance(I first, S last) { | ||
return central_moment<2>(first, last); | ||
} | ||
|
||
struct sqrt { | ||
template <class T> | ||
requires std::is_arithmetic_v<T> | ||
auto operator()(T const x) const { | ||
return std::sqrt(x); | ||
} | ||
}; | ||
|
||
template <std::input_iterator I, std::sentinel_for<I> S> | ||
requires unary_closed_under<std::iter_value_t<I>, sqrt> | ||
constexpr auto standard_deviation(I first, S last) { | ||
return sqrt{}(variance(first, last)); | ||
} | ||
|
||
template <std::ranges::input_range Rng> | ||
requires unary_closed_under<std::ranges::range_value_t<Rng>, | ||
sqrt> | ||
constexpr auto standard_deviation(Rng values) { | ||
return standard_deviation(std::ranges::begin(values), | ||
std::ranges::end(values)); | ||
} | ||
} // namespace nrng | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <doctest/doctest.h> | ||
|
||
#include <nrng/central_moment.hpp> | ||
|
||
#include <array> | ||
|
||
using nrng::moment; | ||
using nrng::central_moment; | ||
using nrng::mean; | ||
using nrng::variance; | ||
using nrng::standard_deviation; | ||
|
||
TEST_CASE("first central moment") { | ||
auto const values = std::array{1, 2, 3, 4, 5}; | ||
|
||
CHECK(central_moment<1>(values) == 0); | ||
CHECK(central_moment<1>(values.begin(), values.end()) == central_moment<1>(values)); | ||
} | ||
|
||
TEST_CASE("second central moment") { | ||
auto const values = std::array{1, 2, 3, 4, 5}; | ||
auto shift_by_mean = [mu = mean(values)](auto v) { return v - mu; }; | ||
|
||
CHECK(central_moment<2>(values) == moment<2>(values | std::views::transform(shift_by_mean))); | ||
} | ||
|
||
TEST_CASE("variance") { | ||
auto const values = std::array{1, 2, 3, 4, 5}; | ||
|
||
CHECK(variance(values) == central_moment<2>(values)); | ||
} | ||
|
||
TEST_CASE("standard deviation") { | ||
auto const values = std::array{1, 2, 3, 4, 5}; | ||
|
||
CHECK(standard_deviation(values) == std::sqrt(variance(values))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters