mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-31 19:04:27 +02:00
added STL random number distributions wrappers
This commit is contained in:
committed by
Mateusz Pusz
parent
5dd9eaac87
commit
1ce7949cf5
453
src/include/units/random.h
Normal file
453
src/include/units/random.h
Normal file
@@ -0,0 +1,453 @@
|
||||
#pragma once
|
||||
|
||||
#include <random>
|
||||
#include <functional>
|
||||
|
||||
namespace units {
|
||||
|
||||
template<typename Quantity>
|
||||
struct uniform_int_distribution : public std::uniform_int_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::uniform_int_distribution<Rep>;
|
||||
|
||||
uniform_int_distribution() : Base() {}
|
||||
uniform_int_distribution(Quantity a, Quantity b) : Base(a.count(), b.count()) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity a() const { return Quantity(Base::a()); }
|
||||
Quantity b() const { return Quantity(Base::b()); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct uniform_real_distribution : public std::uniform_real_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::uniform_real_distribution<Rep>;
|
||||
|
||||
uniform_real_distribution() : Base() {}
|
||||
uniform_real_distribution(Quantity a, Quantity b) : Base(a.count(), b.count()) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity a() const { return Quantity(Base::a()); }
|
||||
Quantity b() const { return Quantity(Base::b()); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct binomial_distribution : public std::binomial_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::binomial_distribution<Rep>;
|
||||
|
||||
binomial_distribution() : Base() {}
|
||||
binomial_distribution(Quantity t, double p) : Base(t.count(), p) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity t() const { return Quantity(Base::t()); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct negative_binomial_distribution : public std::negative_binomial_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::negative_binomial_distribution<Rep>;
|
||||
|
||||
negative_binomial_distribution() : Base() {}
|
||||
negative_binomial_distribution(Quantity k, double p) : Base(k.count(), p) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity k() const { return Quantity(Base::k()); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct geometric_distribution : public std::geometric_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::geometric_distribution<Rep>;
|
||||
|
||||
geometric_distribution() : Base() {}
|
||||
geometric_distribution(double p) : Base(p) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct poisson_distribution : public std::poisson_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::poisson_distribution<Rep>;
|
||||
|
||||
poisson_distribution() : Base() {}
|
||||
poisson_distribution(double p) : Base(p) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct exponential_distribution : public std::exponential_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::exponential_distribution<Rep>;
|
||||
|
||||
exponential_distribution() : Base() {}
|
||||
exponential_distribution(Rep lambda) : Base(lambda) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct gamma_distribution : public std::gamma_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::gamma_distribution<Rep>;
|
||||
|
||||
gamma_distribution() : Base() {}
|
||||
gamma_distribution(Rep alpha, Rep beta) : Base(alpha, beta) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct weibull_distribution : public std::weibull_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::weibull_distribution<Rep>;
|
||||
|
||||
weibull_distribution() : Base() {}
|
||||
weibull_distribution(Rep a, Rep b) : Base(a, b) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct extreme_value_distribution : public std::extreme_value_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::extreme_value_distribution<Rep>;
|
||||
|
||||
extreme_value_distribution() : Base() {}
|
||||
extreme_value_distribution(Quantity a, Rep b) : Base(a.count(), b) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity a() const { return Quantity(Base::a()); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct normal_distribution : public std::normal_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::normal_distribution<Rep>;
|
||||
|
||||
normal_distribution() : Base() {}
|
||||
normal_distribution(Quantity mean, Quantity stddev) : Base(mean.count(), stddev.count()) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity mean() const { return Quantity(Base::mean()); }
|
||||
Quantity stddev() const { return Quantity(Base::stddev()); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct lognormal_distribution : public std::lognormal_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::lognormal_distribution<Rep>;
|
||||
|
||||
lognormal_distribution() : Base() {}
|
||||
lognormal_distribution(Quantity m, Quantity s) : Base(m.count(), s.count()) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity m() const { return Quantity(Base::m()); }
|
||||
Quantity s() const { return Quantity(Base::s()); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct chi_squared_distribution : public std::chi_squared_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::chi_squared_distribution<Rep>;
|
||||
|
||||
chi_squared_distribution() : Base() {}
|
||||
chi_squared_distribution(Rep n) : Base(n) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct cauchy_distribution : public std::cauchy_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::cauchy_distribution<Rep>;
|
||||
|
||||
cauchy_distribution() : Base() {}
|
||||
cauchy_distribution(Quantity a, Quantity b) : Base(a.count(), b.count()) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity a() const { return Quantity(Base::a()); }
|
||||
Quantity b() const { return Quantity(Base::b()); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct fisher_f_distribution : public std::fisher_f_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::fisher_f_distribution<Rep>;
|
||||
|
||||
fisher_f_distribution() : Base() {}
|
||||
fisher_f_distribution(Rep m, Rep n) : Base(m, n) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct student_t_distribution : public std::student_t_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::student_t_distribution<Rep>;
|
||||
|
||||
student_t_distribution() : Base() {}
|
||||
student_t_distribution(Rep n) : Base(n) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
struct discrete_distribution : public std::discrete_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::discrete_distribution<Rep>;
|
||||
|
||||
discrete_distribution() : Base() {}
|
||||
|
||||
template <typename InputIt>
|
||||
discrete_distribution(InputIt first, InputIt last) : Base(first, last) {}
|
||||
|
||||
discrete_distribution(std::initializer_list<double> weights) : Base(weights) {}
|
||||
|
||||
template <typename UnaryOperation>
|
||||
discrete_distribution(std::size_t count, double xmin, double xmax, UnaryOperation unary_op) :
|
||||
Base(count, xmin, xmax, unary_op) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
class piecewise_constant_distribution : public std::piecewise_constant_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::piecewise_constant_distribution<Rep>;
|
||||
|
||||
template <typename InputIt>
|
||||
inline static std::vector<Rep> i_qty_to_rep(InputIt first, InputIt last)
|
||||
{
|
||||
std::vector<Rep> intervals_rep;
|
||||
intervals_rep.reserve(static_cast<size_t>(std::distance(first, last)));
|
||||
for (auto itr = first; itr != last; ++itr) { intervals_rep.push_back(itr->count()); }
|
||||
return intervals_rep;
|
||||
}
|
||||
|
||||
inline static std::vector<Rep> bl_qty_to_rep(std::initializer_list<Quantity>& bl)
|
||||
{
|
||||
std::vector<Rep> bl_rep;
|
||||
bl_rep.reserve(bl.size());
|
||||
for (const Quantity& qty : bl) { bl_rep.push_back(qty.count()); }
|
||||
return bl_rep;
|
||||
}
|
||||
|
||||
template <typename UnaryOperation>
|
||||
inline static std::vector<Rep> fw_bl(std::initializer_list<Quantity>& bl, UnaryOperation fw)
|
||||
{
|
||||
std::vector<Rep> w_bl;
|
||||
w_bl.reserve(bl.size());
|
||||
for (const Quantity& qty : bl) { w_bl.push_back(fw(qty)); }
|
||||
std::vector<Rep> weights;
|
||||
weights.reserve(bl.size());
|
||||
for (size_t i = 0; i < bl.size() - 1; ++i) { weights.push_back(w_bl[i] + w_bl[i + 1]); }
|
||||
weights.push_back(0);
|
||||
return weights;
|
||||
}
|
||||
|
||||
template <typename InputIt>
|
||||
piecewise_constant_distribution(const std::vector<Rep>& i, InputIt first_w) :
|
||||
Base(i.cbegin(), i.cend(), first_w) {}
|
||||
|
||||
piecewise_constant_distribution(const std::vector<Rep>& bl, const std::vector<Rep>& weights) :
|
||||
Base(bl.cbegin(), bl.cend(), weights.cbegin()) {}
|
||||
|
||||
public:
|
||||
piecewise_constant_distribution() : Base() {}
|
||||
|
||||
template <typename InputIt1, typename InputIt2>
|
||||
piecewise_constant_distribution(InputIt1 first_i, InputIt1 last_i, InputIt2 first_w) :
|
||||
piecewise_constant_distribution(i_qty_to_rep(first_i, last_i), first_w) {}
|
||||
|
||||
template <typename UnaryOperation>
|
||||
piecewise_constant_distribution(std::initializer_list<Quantity> bl, UnaryOperation fw) :
|
||||
piecewise_constant_distribution(bl_qty_to_rep(bl), fw_bl(bl, fw)) {}
|
||||
|
||||
template <typename UnaryOperation>
|
||||
piecewise_constant_distribution(std::size_t nw, Quantity xmin, Quantity xmax, UnaryOperation fw) :
|
||||
Base(nw, xmin.count(), xmax.count(), [fw](Rep val) { return fw(Quantity(val)); }) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
std::vector<Quantity> intervals() const
|
||||
{
|
||||
std::vector<Rep> intervals_rep = Base::intervals();
|
||||
std::vector<Quantity> intervals_qty;
|
||||
intervals_qty.reserve(intervals_rep.size());
|
||||
for (const Rep& val : intervals_rep) { intervals_qty.push_back(Quantity(val)); }
|
||||
return intervals_qty;
|
||||
}
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
template<typename Quantity>
|
||||
class piecewise_linear_distribution : public std::piecewise_linear_distribution<typename Quantity::rep>
|
||||
{
|
||||
using Rep = Quantity::rep;
|
||||
using Base = std::piecewise_linear_distribution<Rep>;
|
||||
|
||||
template <typename InputIt>
|
||||
inline static std::vector<Rep> i_qty_to_rep(InputIt first, InputIt last)
|
||||
{
|
||||
std::vector<Rep> intervals_rep;
|
||||
intervals_rep.reserve(static_cast<size_t>(std::distance(first, last)));
|
||||
for (auto itr = first; itr != last; ++itr) { intervals_rep.push_back(itr->count()); }
|
||||
return intervals_rep;
|
||||
}
|
||||
|
||||
inline static std::vector<Rep> bl_qty_to_rep(std::initializer_list<Quantity>& bl)
|
||||
{
|
||||
std::vector<Rep> bl_rep;
|
||||
bl_rep.reserve(bl.size());
|
||||
for (const Quantity& qty : bl) { bl_rep.push_back(qty.count()); }
|
||||
return bl_rep;
|
||||
}
|
||||
|
||||
template <typename UnaryOperation>
|
||||
inline static std::vector<Rep> fw_bl(std::initializer_list<Quantity>& bl, UnaryOperation fw)
|
||||
{
|
||||
std::vector<Rep> weights;
|
||||
weights.reserve(bl.size());
|
||||
for (const Quantity& qty : bl) { weights.push_back(fw(qty)); }
|
||||
return weights;
|
||||
}
|
||||
|
||||
template <typename InputIt>
|
||||
piecewise_linear_distribution(const std::vector<Rep>& i, InputIt first_w) :
|
||||
Base(i.cbegin(), i.cend(), first_w) {}
|
||||
|
||||
piecewise_linear_distribution(const std::vector<Rep>& bl, const std::vector<Rep>& weights) :
|
||||
Base(bl.cbegin(), bl.cend(), weights.cbegin()) {}
|
||||
|
||||
public:
|
||||
piecewise_linear_distribution() : Base() {}
|
||||
|
||||
template <typename InputIt1, typename InputIt2>
|
||||
piecewise_linear_distribution(InputIt1 first_i, InputIt1 last_i, InputIt2 first_w) :
|
||||
piecewise_linear_distribution(i_qty_to_rep(first_i, last_i), first_w) {}
|
||||
|
||||
template <typename UnaryOperation>
|
||||
piecewise_linear_distribution(std::initializer_list<Quantity> bl, UnaryOperation fw) :
|
||||
piecewise_linear_distribution(bl_qty_to_rep(bl), fw_bl(bl, fw)) {}
|
||||
|
||||
template <typename UnaryOperation>
|
||||
piecewise_linear_distribution(std::size_t nw, Quantity xmin, Quantity xmax, UnaryOperation fw) :
|
||||
Base(nw, xmin.count(), xmax.count(), [fw](Rep val) { return fw(Quantity(val)); }) {}
|
||||
|
||||
template<typename Generator>
|
||||
Quantity operator()(Generator& g) { return Quantity(Base::operator()(g)); }
|
||||
|
||||
std::vector<Quantity> intervals() const
|
||||
{
|
||||
std::vector<Rep> intervals_rep = Base::intervals();
|
||||
std::vector<Quantity> intervals_qty;
|
||||
intervals_qty.reserve(intervals_rep.size());
|
||||
for (const Rep& val : intervals_rep) { intervals_qty.push_back(Quantity(val)); }
|
||||
return intervals_qty;
|
||||
}
|
||||
|
||||
Quantity min() const { return Quantity(Base::min()); }
|
||||
Quantity max() const { return Quantity(Base::max()); }
|
||||
};
|
||||
|
||||
} // namespace units
|
@@ -29,6 +29,7 @@ add_executable(unit_tests_runtime
|
||||
math_test.cpp
|
||||
fmt_test.cpp
|
||||
fmt_units_test.cpp
|
||||
distribution_test.cpp
|
||||
)
|
||||
target_link_libraries(unit_tests_runtime
|
||||
PRIVATE
|
||||
|
587
test/unit_test/runtime/distribution_test.cpp
Normal file
587
test/unit_test/runtime/distribution_test.cpp
Normal file
@@ -0,0 +1,587 @@
|
||||
#include <numeric>
|
||||
#include <units/random.h>
|
||||
#include <units/physical/si/length.h>
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
using namespace units;
|
||||
using namespace units::physical::si;
|
||||
|
||||
TEST_CASE("uniform_int_distribution")
|
||||
{
|
||||
using Rep = std::int64_t;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::uniform_int_distribution<Qty>();
|
||||
|
||||
CHECK(dist.a() == Qty::zero());
|
||||
CHECK(dist.b() == Qty::max());
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr Rep a = 5;
|
||||
constexpr Rep b = 2;
|
||||
|
||||
auto stl_dist = std::uniform_int_distribution(a, b);
|
||||
auto units_dist = units::uniform_int_distribution(Qty(a), Qty(b));
|
||||
|
||||
CHECK(units_dist.a() == Qty(stl_dist.a()));
|
||||
CHECK(units_dist.b() == Qty(stl_dist.b()));
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("uniform_real_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::uniform_real_distribution<Qty>();
|
||||
|
||||
CHECK(dist.a() == Qty::zero());
|
||||
CHECK(dist.b() == Qty::one());
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr Rep a = 5.0;
|
||||
constexpr Rep b = 2.0;
|
||||
|
||||
auto stl_dist = std::uniform_real_distribution(a, b);
|
||||
auto units_dist = units::uniform_real_distribution(Qty(a), Qty(b));
|
||||
|
||||
CHECK(units_dist.a() == Qty(stl_dist.a()));
|
||||
CHECK(units_dist.b() == Qty(stl_dist.b()));
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("binomial_distribution")
|
||||
{
|
||||
using Rep = std::int64_t;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::binomial_distribution<Qty>();
|
||||
|
||||
CHECK(dist.p() == 0.5);
|
||||
CHECK(dist.t() == Qty::one());
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr Rep t = 5;
|
||||
constexpr double p = 0.25;
|
||||
|
||||
auto stl_dist = std::binomial_distribution(t, p);
|
||||
auto units_dist = units::binomial_distribution(Qty(t), p);
|
||||
|
||||
CHECK(units_dist.p() == stl_dist.p());
|
||||
CHECK(units_dist.t() == Qty(stl_dist.t()));
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("negative_binomial_distribution")
|
||||
{
|
||||
using Rep = std::int64_t;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::negative_binomial_distribution<Qty>();
|
||||
|
||||
CHECK(dist.p() == 0.5);
|
||||
CHECK(dist.k() == Qty::one());
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr Rep k = 5;
|
||||
constexpr double p = 0.25;
|
||||
|
||||
auto stl_dist = std::negative_binomial_distribution(k, p);
|
||||
auto units_dist = units::negative_binomial_distribution(Qty(k), p);
|
||||
|
||||
CHECK(units_dist.p() == stl_dist.p());
|
||||
CHECK(units_dist.k() == Qty(stl_dist.k()));
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("geometric_distribution")
|
||||
{
|
||||
using Rep = std::int64_t;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::geometric_distribution<Qty>();
|
||||
|
||||
CHECK(dist.p() == 0.5);
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr double p = 0.25;
|
||||
|
||||
auto stl_dist = std::geometric_distribution<Rep>(p);
|
||||
auto units_dist = units::geometric_distribution<Qty>(p);
|
||||
|
||||
CHECK(units_dist.p() == stl_dist.p());
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("poisson_distribution")
|
||||
{
|
||||
using Rep = std::int64_t;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::poisson_distribution<Qty>();
|
||||
|
||||
CHECK(dist.mean() == 1.0);
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr double mean = 5.0;
|
||||
|
||||
auto stl_dist = std::poisson_distribution<Rep>(mean);
|
||||
auto units_dist = units::poisson_distribution<Qty>(mean);
|
||||
|
||||
CHECK(units_dist.mean() == stl_dist.mean());
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("exponential_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::exponential_distribution<Qty>();
|
||||
|
||||
CHECK(dist.lambda() == 1.0);
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr double lambda = 2.0;
|
||||
|
||||
auto stl_dist = std::exponential_distribution<Rep>(lambda);
|
||||
auto units_dist = units::exponential_distribution<Qty>(lambda);
|
||||
|
||||
CHECK(units_dist.lambda() == stl_dist.lambda());
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("gamma_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::gamma_distribution<Qty>();
|
||||
|
||||
CHECK(dist.alpha() == 1.0);
|
||||
CHECK(dist.beta() == 1.0);
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr double alpha = 5.0;
|
||||
constexpr double beta = 2.0;
|
||||
|
||||
auto stl_dist = std::gamma_distribution<Rep>(alpha, beta);
|
||||
auto units_dist = units::gamma_distribution<Qty>(alpha, beta);
|
||||
|
||||
CHECK(units_dist.alpha() == stl_dist.alpha());
|
||||
CHECK(units_dist.beta() == stl_dist.beta());
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("weibull_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::weibull_distribution<Qty>();
|
||||
|
||||
CHECK(dist.a() == 1.0);
|
||||
CHECK(dist.b() == 1.0);
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr Rep a = 5.0;
|
||||
constexpr Rep b = 2.0;
|
||||
|
||||
auto stl_dist = std::weibull_distribution(a, b);
|
||||
auto units_dist = units::weibull_distribution<Qty>(a, b);
|
||||
|
||||
CHECK(units_dist.a() == stl_dist.a());
|
||||
CHECK(units_dist.b() == stl_dist.b());
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("extreme_value_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::extreme_value_distribution<Qty>();
|
||||
|
||||
CHECK(dist.a() == Qty::zero());
|
||||
CHECK(dist.b() == 1.0);
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr Rep a = 5.0;
|
||||
constexpr Rep b = 2.0;
|
||||
|
||||
auto stl_dist = std::extreme_value_distribution(a, b);
|
||||
auto units_dist = units::extreme_value_distribution<Qty>(Qty(a), b);
|
||||
|
||||
CHECK(units_dist.a() == Qty(stl_dist.a()));
|
||||
CHECK(units_dist.b() == stl_dist.b());
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("normal_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::normal_distribution<Qty>();
|
||||
|
||||
CHECK(dist.mean() == Qty::zero());
|
||||
CHECK(dist.stddev() == Qty::one());
|
||||
}
|
||||
|
||||
SECTION("parametrized")
|
||||
{
|
||||
constexpr Rep mean = 5.0;
|
||||
constexpr Rep stddev = 2.0;
|
||||
|
||||
auto stl_dist = std::normal_distribution(mean, stddev);
|
||||
auto units_dist = units::normal_distribution(Qty(mean), Qty(stddev));
|
||||
|
||||
CHECK(units_dist.mean() == Qty(stl_dist.mean()));
|
||||
CHECK(units_dist.stddev() == Qty(stl_dist.stddev()));
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("lognormal_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::lognormal_distribution<Qty>();
|
||||
|
||||
CHECK(dist.m() == Qty::zero());
|
||||
CHECK(dist.s() == Qty::one());
|
||||
}
|
||||
|
||||
SECTION("parametrized")
|
||||
{
|
||||
constexpr Rep m = 5.0;
|
||||
constexpr Rep s = 2.0;
|
||||
|
||||
auto stl_dist = std::lognormal_distribution(m, s);
|
||||
auto units_dist = units::lognormal_distribution(Qty(m), Qty(s));
|
||||
|
||||
CHECK(units_dist.m() == Qty(stl_dist.m()));
|
||||
CHECK(units_dist.s() == Qty(stl_dist.s()));
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("chi_squared_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::chi_squared_distribution<Qty>();
|
||||
|
||||
CHECK(dist.n() == 1.0);
|
||||
}
|
||||
|
||||
SECTION("parametrized")
|
||||
{
|
||||
constexpr Rep n = 5.0;
|
||||
|
||||
auto stl_dist = std::chi_squared_distribution(n);
|
||||
auto units_dist = units::chi_squared_distribution<Qty>(n);
|
||||
|
||||
CHECK(units_dist.n() == stl_dist.n());
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("cauchy_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::cauchy_distribution<Qty>();
|
||||
|
||||
CHECK(dist.a() == Qty::zero());
|
||||
CHECK(dist.b() == Qty::one());
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr Rep a = 5.0;
|
||||
constexpr Rep b = 2.0;
|
||||
|
||||
auto stl_dist = std::cauchy_distribution(a, b);
|
||||
auto units_dist = units::cauchy_distribution(Qty(a), Qty(b));
|
||||
|
||||
CHECK(units_dist.a() == Qty(stl_dist.a()));
|
||||
CHECK(units_dist.b() == Qty(stl_dist.b()));
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("fisher_f_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::fisher_f_distribution<Qty>();
|
||||
|
||||
CHECK(dist.m() == 1.0);
|
||||
CHECK(dist.n() == 1.0);
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr Rep m = 5.0;
|
||||
constexpr Rep n = 2.0;
|
||||
|
||||
auto stl_dist = std::fisher_f_distribution<Rep>(m, n);
|
||||
auto units_dist = units::fisher_f_distribution<Qty>(m, n);
|
||||
|
||||
CHECK(units_dist.m() == stl_dist.m());
|
||||
CHECK(units_dist.n() == stl_dist.n());
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("student_t_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto dist = units::student_t_distribution<Qty>();
|
||||
|
||||
CHECK(dist.n() == 1.0);
|
||||
}
|
||||
|
||||
SECTION ("parametrized") {
|
||||
constexpr Rep n = 2.0;
|
||||
|
||||
auto stl_dist = std::student_t_distribution<Rep>(n);
|
||||
auto units_dist = units::student_t_distribution<Qty>(n);
|
||||
|
||||
CHECK(units_dist.n() == stl_dist.n());
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("discrete_distribution")
|
||||
{
|
||||
using Rep = std::int64_t;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto stl_dist = std::discrete_distribution<Rep>();
|
||||
auto units_dist = units::discrete_distribution<Qty>();
|
||||
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
CHECK(units_dist.probabilities() == stl_dist.probabilities());
|
||||
}
|
||||
|
||||
SECTION ("parametrized_input_it") {
|
||||
constexpr std::array<double, 3> weights = {1.0, 2.0, 3.0};
|
||||
|
||||
auto stl_dist = std::discrete_distribution<Rep>(weights.cbegin(), weights.cend());
|
||||
auto units_dist = units::discrete_distribution<Qty>(weights.cbegin(), weights.cend());
|
||||
|
||||
CHECK(units_dist.probabilities() == stl_dist.probabilities());
|
||||
}
|
||||
|
||||
SECTION ("parametrized_initializer_list") {
|
||||
std::initializer_list<double> weights = {1.0, 2.0, 3.0};
|
||||
|
||||
auto stl_dist = std::discrete_distribution<Rep>(weights);
|
||||
auto units_dist = units::discrete_distribution<Qty>(weights);
|
||||
|
||||
CHECK(units_dist.probabilities() == stl_dist.probabilities());
|
||||
}
|
||||
|
||||
SECTION ("parametrized_range") {
|
||||
constexpr std::size_t count = 3;
|
||||
constexpr double xmin = 1, xmax = 3;
|
||||
|
||||
auto stl_dist = std::discrete_distribution<Rep>(count, xmin, xmax, [](double val) { return val; });
|
||||
auto units_dist = units::discrete_distribution<Qty>(count, xmin, xmax, [](double val) { return val; });
|
||||
|
||||
CHECK(units_dist.probabilities() == stl_dist.probabilities());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("piecewise_constant_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
std::vector<Rep> intervals_rep_vec = {1.0, 2.0, 3.0};
|
||||
std::vector<Qty> intervals_qty_vec = {1.0q_m, 2.0q_m, 3.0q_m};
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto stl_dist = std::piecewise_constant_distribution<Rep>();
|
||||
auto units_dist = units::piecewise_constant_distribution<Qty>();
|
||||
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
CHECK(stl_dist.intervals().size() == 2);
|
||||
CHECK(units_dist.intervals().size() == 2);
|
||||
CHECK(stl_dist.densities().size() == 1);
|
||||
CHECK(units_dist.densities().size() == 1);
|
||||
}
|
||||
|
||||
SECTION ("parametrized_input_it") {
|
||||
constexpr std::array<Rep, 3> intervals_rep = {1.0, 2.0, 3.0};
|
||||
constexpr std::array<Qty, 3> intervals_qty = {1.0q_m, 2.0q_m, 3.0q_m};
|
||||
constexpr std::array<Rep, 3> weights = {1.0, 2.0, 3.0};
|
||||
|
||||
auto stl_dist = std::piecewise_constant_distribution<Rep>(intervals_rep.cbegin(), intervals_rep.cend(), weights.cbegin());
|
||||
auto units_dist = units::piecewise_constant_distribution<Qty>(intervals_qty.cbegin(), intervals_qty.cend(), weights.cbegin());
|
||||
|
||||
CHECK(stl_dist.intervals() == intervals_rep_vec);
|
||||
CHECK(units_dist.intervals() == intervals_qty_vec);
|
||||
CHECK(units_dist.densities() == stl_dist.densities());
|
||||
}
|
||||
|
||||
SECTION ("parametrized_initializer_list") {
|
||||
std::initializer_list<Rep> intervals_rep = {1.0, 2.0, 3.0};
|
||||
std::initializer_list<Qty> intervals_qty = {1.0q_m, 2.0q_m, 3.0q_m};
|
||||
|
||||
auto stl_dist = std::piecewise_constant_distribution<Rep>(intervals_rep, [](Rep val) { return val; });
|
||||
auto units_dist = units::piecewise_constant_distribution<Qty>(intervals_qty, [](Qty qty) { return qty.count(); });
|
||||
|
||||
CHECK(units_dist.intervals() == intervals_qty_vec);
|
||||
CHECK(units_dist.densities() == stl_dist.densities());
|
||||
}
|
||||
|
||||
SECTION ("parametrized_range") {
|
||||
constexpr std::size_t nw = 2;
|
||||
constexpr Rep xmin_rep = 1.0, xmax_rep = 3.0;
|
||||
constexpr Qty xmin_qty = 1.0q_m, xmax_qty = 3.0q_m;
|
||||
|
||||
auto stl_dist = std::piecewise_constant_distribution<Rep>(nw, xmin_rep, xmax_rep, [](Rep val) { return val; });
|
||||
auto units_dist = units::piecewise_constant_distribution<Qty>(nw, xmin_qty, xmax_qty, [](Qty qty) { return qty.count(); });
|
||||
|
||||
CHECK(units_dist.intervals() == intervals_qty_vec);
|
||||
CHECK(units_dist.densities() == stl_dist.densities());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("piecewise_linear_distribution")
|
||||
{
|
||||
using Rep = long double;
|
||||
using Qty = length<metre, Rep>;
|
||||
|
||||
std::vector<Rep> intervals_rep_vec = {1.0, 2.0, 3.0};
|
||||
std::vector<Qty> intervals_qty_vec = {1.0q_m, 2.0q_m, 3.0q_m};
|
||||
|
||||
SECTION("default")
|
||||
{
|
||||
auto stl_dist = std::piecewise_linear_distribution<Rep>();
|
||||
auto units_dist = units::piecewise_linear_distribution<Qty>();
|
||||
|
||||
CHECK(units_dist.min() == Qty(stl_dist.min()));
|
||||
CHECK(units_dist.max() == Qty(stl_dist.max()));
|
||||
CHECK(stl_dist.intervals().size() == 2);
|
||||
CHECK(units_dist.intervals().size() == 2);
|
||||
CHECK(stl_dist.densities().size() == 2);
|
||||
CHECK(units_dist.densities().size() == 2);
|
||||
}
|
||||
|
||||
SECTION ("parametrized_input_it") {
|
||||
constexpr std::array<Rep, 3> intervals_rep = {1.0, 2.0, 3.0};
|
||||
constexpr std::array<Qty, 3> intervals_qty = {1.0q_m, 2.0q_m, 3.0q_m};
|
||||
constexpr std::array<Rep, 3> weights = {1.0, 2.0, 3.0};
|
||||
|
||||
auto stl_dist = std::piecewise_linear_distribution<Rep>(intervals_rep.cbegin(), intervals_rep.cend(), weights.cbegin());
|
||||
auto units_dist = units::piecewise_linear_distribution<Qty>(intervals_qty.cbegin(), intervals_qty.cend(), weights.cbegin());
|
||||
|
||||
CHECK(stl_dist.intervals() == intervals_rep_vec);
|
||||
CHECK(units_dist.intervals() == intervals_qty_vec);
|
||||
CHECK(units_dist.densities() == stl_dist.densities());
|
||||
}
|
||||
|
||||
SECTION ("parametrized_initializer_list") {
|
||||
std::initializer_list<Rep> intervals_rep = {1.0, 2.0, 3.0};
|
||||
std::initializer_list<Qty> intervals_qty = {1.0q_m, 2.0q_m, 3.0q_m};
|
||||
|
||||
auto stl_dist = std::piecewise_linear_distribution<Rep>(intervals_rep, [](Rep val) { return val; });
|
||||
auto units_dist = units::piecewise_linear_distribution<Qty>(intervals_qty, [](Qty qty) { return qty.count(); });
|
||||
|
||||
CHECK(units_dist.intervals() == intervals_qty_vec);
|
||||
CHECK(units_dist.densities() == stl_dist.densities());
|
||||
}
|
||||
|
||||
SECTION ("parametrized_range") {
|
||||
constexpr std::size_t nw = 2;
|
||||
constexpr Rep xmin_rep = 1.0, xmax_rep = 3.0;
|
||||
constexpr Qty xmin_qty = 1.0q_m, xmax_qty = 3.0q_m;
|
||||
|
||||
auto stl_dist = std::piecewise_linear_distribution<Rep>(nw, xmin_rep, xmax_rep, [](Rep val) { return val; });
|
||||
auto units_dist = units::piecewise_linear_distribution<Qty>(nw, xmin_qty, xmax_qty, [](Qty qty) { return qty.count(); });
|
||||
|
||||
CHECK(units_dist.intervals() == intervals_qty_vec);
|
||||
CHECK(units_dist.densities() == stl_dist.densities());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user