Skip to content

Commit

Permalink
Add class template argument deduction guides for distributions (#756)
Browse files Browse the repository at this point in the history
* Demonstrate deduction guides for normal_distribution.

* Add missing test case.

* add class template argument deduction guides for distributions templated on real type - issue #754

* Remove no-arg tests in test_dist_deduction_guides.cpp - issue #754

GCC-8 and clang 6-8 were unhappy with the no-arg cases, which use the
default template arg, not the deduction guide, anyway.

* remove unused deduction guide for fisher_f - issue #754

Co-authored-by: jzmaddock <john@johnmaddock.co.uk>
  • Loading branch information
jamesfolberth and jzmaddock authored Feb 12, 2022
1 parent a6ee67e commit 01a938c
Show file tree
Hide file tree
Showing 34 changed files with 354 additions and 1 deletion.
6 changes: 6 additions & 0 deletions include/boost/math/distributions/arcsine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ namespace boost
// Convenient typedef to construct double version.
typedef arcsine_distribution<double> arcsine;

#ifdef __cpp_deduction_guides
template <class RealType>
arcsine_distribution(RealType)->arcsine_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
arcsine_distribution(RealType, RealType)->arcsine_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const arcsine_distribution<RealType, Policy>& dist)
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/bernoulli.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ namespace boost

typedef bernoulli_distribution<double> bernoulli;

#ifdef __cpp_deduction_guides
template <class RealType>
bernoulli_distribution(RealType)->bernoulli_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const bernoulli_distribution<RealType, Policy>& /* dist */)
{ // Range of permissible values for random variable k = {0, 1}.
Expand Down
7 changes: 7 additions & 0 deletions include/boost/math/distributions/beta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ namespace boost
RealType m_beta;
}; // template <class RealType, class Policy> class beta_distribution

#ifdef __cpp_deduction_guides
template <class RealType>
beta_distribution(RealType)->beta_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
beta_distribution(RealType, RealType)->beta_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const beta_distribution<RealType, Policy>& /* dist */)
{ // Range of permissible values for random variable x.
Expand Down
7 changes: 7 additions & 0 deletions include/boost/math/distributions/binomial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ namespace boost
// IS now included since no longer a name clash with function binomial.
//typedef binomial_distribution<double> binomial; // Reserved name of type double.

#ifdef __cpp_deduction_guides
template <class RealType>
binomial_distribution(RealType)->binomial_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
binomial_distribution(RealType,RealType)->binomial_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
const std::pair<RealType, RealType> range(const binomial_distribution<RealType, Policy>& dist)
{ // Range of permissible values for random variable k.
Expand Down
7 changes: 7 additions & 0 deletions include/boost/math/distributions/cauchy.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ class cauchy_distribution

typedef cauchy_distribution<double> cauchy;

#ifdef __cpp_deduction_guides
template <class RealType>
cauchy_distribution(RealType)->cauchy_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
cauchy_distribution(RealType,RealType)->cauchy_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const cauchy_distribution<RealType, Policy>&)
{ // Range of permissible values for random variable x.
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/chi_squared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class chi_squared_distribution

typedef chi_squared_distribution<double> chi_squared;

#ifdef __cpp_deduction_guides
template <class RealType>
chi_squared_distribution(RealType)->chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4127)
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/exponential.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class exponential_distribution

typedef exponential_distribution<double> exponential;

#ifdef __cpp_deduction_guides
template <class RealType>
exponential_distribution(RealType)->exponential_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const exponential_distribution<RealType, Policy>& /*dist*/)
{ // Range of permissible values for random variable x.
Expand Down
7 changes: 7 additions & 0 deletions include/boost/math/distributions/extreme_value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ class extreme_value_distribution

typedef extreme_value_distribution<double> extreme_value;

#ifdef __cpp_deduction_guides
template <class RealType>
extreme_value_distribution(RealType)->extreme_value_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
extreme_value_distribution(RealType,RealType)->extreme_value_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const extreme_value_distribution<RealType, Policy>& /*dist*/)
{ // Range of permissible values for random variable x.
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/fisher_f.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class fisher_f_distribution

typedef fisher_f_distribution<double> fisher_f;

#ifdef __cpp_deduction_guides
template <class RealType>
fisher_f_distribution(RealType,RealType)->fisher_f_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const fisher_f_distribution<RealType, Policy>& /*dist*/)
{ // Range of permissible values for random variable x.
Expand Down
7 changes: 7 additions & 0 deletions include/boost/math/distributions/gamma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ class gamma_distribution

// NO typedef because of clash with name of gamma function.

#ifdef __cpp_deduction_guides
template <class RealType>
gamma_distribution(RealType)->gamma_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
gamma_distribution(RealType,RealType)->gamma_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const gamma_distribution<RealType, Policy>& /* dist */)
{ // Range of permissible values for random variable x.
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/geometric.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,11 @@ namespace boost

typedef geometric_distribution<double> geometric; // Reserved name of type double.

#ifdef __cpp_deduction_guides
template <class RealType>
geometric_distribution(RealType)->geometric_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const geometric_distribution<RealType, Policy>& /* dist */)
{ // Range of permissible values for random variable k.
Expand Down
7 changes: 7 additions & 0 deletions include/boost/math/distributions/inverse_chi_squared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ class inverse_chi_squared_distribution

typedef inverse_chi_squared_distribution<double> inverse_chi_squared;

#ifdef __cpp_deduction_guides
template <class RealType>
inverse_chi_squared_distribution(RealType)->inverse_chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
inverse_chi_squared_distribution(RealType,RealType)->inverse_chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const inverse_chi_squared_distribution<RealType, Policy>& /*dist*/)
{ // Range of permissible values for random variable x.
Expand Down
7 changes: 7 additions & 0 deletions include/boost/math/distributions/inverse_gamma.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ typedef inverse_gamma_distribution<double> inverse_gamma;
// but there is a typedef for gamma
// typedef boost::math::gamma_distribution<Type, Policy> gamma;

#ifdef __cpp_deduction_guides
template <class RealType>
inverse_gamma_distribution(RealType)->inverse_gamma_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
inverse_gamma_distribution(RealType,RealType)->inverse_gamma_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

// Allow random variable x to be zero, treated as a special case (unlike some definitions).

template <class RealType, class Policy>
Expand Down
7 changes: 7 additions & 0 deletions include/boost/math/distributions/inverse_gaussian.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ class inverse_gaussian_distribution

typedef inverse_gaussian_distribution<double> inverse_gaussian;

#ifdef __cpp_deduction_guides
template <class RealType>
inverse_gaussian_distribution(RealType)->inverse_gaussian_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
inverse_gaussian_distribution(RealType,RealType)->inverse_gaussian_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const inverse_gaussian_distribution<RealType, Policy>& /*dist*/)
{ // Range of permissible values for random variable x, zero to max.
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/kolmogorov_smirnov.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ template <class RealType = double, class Policy = policies::policy<> >

typedef kolmogorov_smirnov_distribution<double> kolmogorov_k; // Convenience typedef for double version.

#ifdef __cpp_deduction_guides
template <class RealType>
kolmogorov_smirnov_distribution(RealType)->kolmogorov_smirnov_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

namespace detail {
template <class RealType, class Policy>
struct kolmogorov_smirnov_quantile_functor
Expand Down
7 changes: 7 additions & 0 deletions include/boost/math/distributions/laplace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ class laplace_distribution
// Convenient type synonym for double.
typedef laplace_distribution<double> laplace;

#ifdef __cpp_deduction_guides
template <class RealType>
laplace_distribution(RealType)->laplace_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
laplace_distribution(RealType,RealType)->laplace_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

//
// Non-member functions.
template <class RealType, class Policy>
Expand Down
9 changes: 8 additions & 1 deletion include/boost/math/distributions/logistic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ namespace boost { namespace math {


typedef logistic_distribution<double> logistic;


#ifdef __cpp_deduction_guides
template <class RealType>
logistic_distribution(RealType)->logistic_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
logistic_distribution(RealType,RealType)->logistic_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const logistic_distribution<RealType, Policy>& /* dist */)
{ // Range of permissible values for random variable x.
Expand Down
7 changes: 7 additions & 0 deletions include/boost/math/distributions/lognormal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ class lognormal_distribution

typedef lognormal_distribution<double> lognormal;

#ifdef __cpp_deduction_guides
template <class RealType>
lognormal_distribution(RealType)->lognormal_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
lognormal_distribution(RealType,RealType)->lognormal_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const lognormal_distribution<RealType, Policy>& /*dist*/)
{ // Range of permissible values for random variable x is >0 to +infinity.
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/negative_binomial.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ namespace boost

typedef negative_binomial_distribution<double> negative_binomial; // Reserved name of type double.

#ifdef __cpp_deduction_guides
template <class RealType>
negative_binomial_distribution(RealType,RealType)->negative_binomial_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const negative_binomial_distribution<RealType, Policy>& /* dist */)
{ // Range of permissible values for random variable k.
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/non_central_beta.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,11 @@ namespace boost

typedef non_central_beta_distribution<double> non_central_beta; // Reserved name of type double.

#ifdef __cpp_deduction_guides
template <class RealType>
non_central_beta_distribution(RealType,RealType,RealType)->non_central_beta_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

// Non-member functions to give properties of the distribution.

template <class RealType, class Policy>
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/non_central_chi_squared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,11 @@ namespace boost

typedef non_central_chi_squared_distribution<double> non_central_chi_squared; // Reserved name of type double.

#ifdef __cpp_deduction_guides
template <class RealType>
non_central_chi_squared_distribution(RealType,RealType)->non_central_chi_squared_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

// Non-member functions to give properties of the distribution.

template <class RealType, class Policy>
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/non_central_f.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ namespace boost

typedef non_central_f_distribution<double> non_central_f; // Reserved name of type double.

#ifdef __cpp_deduction_guides
template <class RealType>
non_central_f_distribution(RealType,RealType,RealType)->non_central_f_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

// Non-member functions to give properties of the distribution.

template <class RealType, class Policy>
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/non_central_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,11 @@ namespace boost

typedef non_central_t_distribution<double> non_central_t; // Reserved name of type double.

#ifdef __cpp_deduction_guides
template <class RealType>
non_central_t_distribution(RealType,RealType)->non_central_t_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

// Non-member functions to give properties of the distribution.

template <class RealType, class Policy>
Expand Down
14 changes: 14 additions & 0 deletions include/boost/math/distributions/normal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ class normal_distribution

typedef normal_distribution<double> normal;

//
// Deduction guides, note we don't check the
// value of __cpp_deduction_guides, just assume
// they work as advertised, even if this is pre-final C++17.
//
#ifdef __cpp_deduction_guides

template <class RealType>
normal_distribution(RealType, RealType)->normal_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
normal_distribution(RealType)->normal_distribution<typename boost::math::tools::promote_args<RealType>::type>;

#endif

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4127)
Expand Down
8 changes: 8 additions & 0 deletions include/boost/math/distributions/pareto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ namespace boost

typedef pareto_distribution<double> pareto; // Convenience to allow pareto(2., 3.);

#ifdef __cpp_deduction_guides
template <class RealType>
pareto_distribution(RealType)->pareto_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
pareto_distribution(RealType,RealType)->pareto_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif


template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const pareto_distribution<RealType, Policy>& /*dist*/)
{ // Range of permissible values for random variable x.
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/poisson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,11 @@ namespace boost

typedef poisson_distribution<double> poisson; // Reserved name of type double.

#ifdef __cpp_deduction_guides
template <class RealType>
poisson_distribution(RealType)->poisson_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

// Non-member functions to give properties of the distribution.

template <class RealType, class Policy>
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/rayleigh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,11 @@ class rayleigh_distribution

typedef rayleigh_distribution<double> rayleigh;

#ifdef __cpp_deduction_guides
template <class RealType>
rayleigh_distribution(RealType)->rayleigh_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const rayleigh_distribution<RealType, Policy>& /*dist*/)
{ // Range of permissible values for random variable x.
Expand Down
9 changes: 9 additions & 0 deletions include/boost/math/distributions/skew_normal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ namespace boost{ namespace math{

typedef skew_normal_distribution<double> skew_normal;

#ifdef __cpp_deduction_guides
template <class RealType>
skew_normal_distribution(RealType)->skew_normal_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
skew_normal_distribution(RealType,RealType)->skew_normal_distribution<typename boost::math::tools::promote_args<RealType>::type>;
template <class RealType>
skew_normal_distribution(RealType,RealType,RealType)->skew_normal_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const skew_normal_distribution<RealType, Policy>& /*dist*/)
{ // Range of permissible values for random variable x.
Expand Down
5 changes: 5 additions & 0 deletions include/boost/math/distributions/students_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class students_t_distribution

typedef students_t_distribution<double> students_t; // Convenience typedef for double version.

#ifdef __cpp_deduction_guides
template <class RealType>
students_t_distribution(RealType)->students_t_distribution<typename boost::math::tools::promote_args<RealType>::type>;
#endif

template <class RealType, class Policy>
inline const std::pair<RealType, RealType> range(const students_t_distribution<RealType, Policy>& /*dist*/)
{ // Range of permissible values for random variable x.
Expand Down
Loading

0 comments on commit 01a938c

Please sign in to comment.