This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of Resolved status.
discrete_distribution
missing constructorSection: 29.5.9.6.1 [rand.dist.samp.discrete] Status: Resolved Submitter: P.J. Plauger Opened: 2008-02-09 Last modified: 2016-01-28
Priority: Not Prioritized
View all other issues in [rand.dist.samp.discrete].
View all issues with Resolved status.
Discussion:
discrete_distribution
should have a constructor like:
template<class _Fn> discrete_distribution(result_type _Count, double _Low, double _High, _Fn& _Func);
(Makes it easier to fill a histogram with function values over a range.)
[ Bellevue: ]
How do you specify the function so that it does not return negative values? If you do it is a bad construction. This requirement is already there. Where in each bin does one evaluate the function? In the middle. Need to revisit tomorrow.
[ Sophia Antipolis: ]
Bill is not requesting this.
Marc Paterno:
_Fn
cannot return negative values at the points where the function is sampled. It is sampled in the middle of each bin._Fn
cannot return 0 everywhere it is sampled.Jens: lambda expressions are rvalues
Add a library issue to provide an
initializer_list<double>
constructor fordiscrete_distribution
.Marc Paterno: dislikes reference for
_Fn
parameter. Make it pass-by-value (to use lambda), usestd::ref
to wrap giant-state function objects.Daniel: See
random_shuffle
, pass-by-rvalue-reference.Daniel to draft wording.
[ Pre San Francisco, Daniel provided wording: ]
The here proposed changes of the WP refer to the current state of N2691. During the Sophia Antipolis meeting two different proposals came up regarding the functor argument type, either by value or by rvalue-reference. For consistence with existing conventions (state-free algorithms and the
general_pdf_distribution
c'tor signature) the author decided to propose a function argument that is provided by value. If severe concerns exists that stateful functions would be of dominant relevance, it should be possible to replace the two occurrences ofFunc
byFunc&&
in this proposal as part of an editorial process.
Proposed resolution:
Non-concept version of the proposed resolution
In 29.5.9.6.1 [rand.dist.samp.discrete]/1, class discrete_distribution
, just
before the member declaration
explicit discrete_distribution(const param_type& parm);
insert:
template<typename Func> discrete_distribution(result_type nf, double xmin, double xmax, Func fw);
Between p.4 and p.5 insert a series of new paragraphs as part of the new member description::
template<typename Func> discrete_distribution(result_type nf, double xmin, double xmax, Func fw);Complexity: Exactly nf invocations of fw.
Requires:
- fw shall be callable with one argument of type double, and shall return values of a type convertible to double;
- If nf > 0, the relation
xmin
<xmax
shall hold, and for all sample valuesxk
, fw(xk
) shall return a weight valuewk
that is non-negative, non-NaN, and non-infinity;- The following relations shall hold: nf ≥ 0, and 0 < S =
w0
+. . .+wn-1
.Effects:
- If nf == 0, sets n = 1 and lets the sequence w have length n = 1 and consist of the single value
w0
= 1.Otherwise, sets n = nf, deltax = (
xmax
-xmin
)/n andxcent
=xmin
+ 0.5 * deltax.For each k = 0, . . . ,n-1, calculates:
xk
=xcent
+ k * deltaxwk
= fw(xk
)Constructs a discrete_distribution object with probabilities:
pk
=wk
/S for k = 0, . . . , n-1.
Concept version of the proposed resolution
In 29.5.9.6.1 [rand.dist.samp.discrete]/1, class discrete_distribution
, just
before the member declaration
explicit discrete_distribution(const param_type& parm);
insert:
template<Callable<auto, double> Func> requires Convertible<Func::result_type, double> discrete_distribution(result_type nf, double xmin, double xmax, Func fw);
Between p.4 and p.5 insert a series of new paragraphs as part of the new member description::
template<Callable<auto, double> Func> requires Convertible<Func::result_type, double> discrete_distribution(result_type nf, double xmin, double xmax, Func fw);Complexity: Exactly nf invocations of fw.
Requires:
- If nf > 0, the relation
xmin
<xmax
shall hold, and for all sample valuesxk
, fw(xk
) shall return a weight valuewk
that is non-negative, non-NaN, and non-infinity;- The following relations shall hold: nf ≥ 0, and 0 < S =
w0
+. . .+wn-1
.Effects:
- If nf == 0, sets n = 1 and lets the sequence w have length n = 1 and consist of the single value
w0
= 1.Otherwise, sets n = nf, deltax = (
For each k = 0, . . . ,n-1, calculates:xmax
-xmin
)/n andxcent
=xmin
+ 0.5 * deltax.xk
=xcent
+ k * deltaxwk
= fw(xk
)Constructs a discrete_distribution object with probabilities:
pk
=wk
/S for k = 0, . . . , n-1.
Rationale:
Addressed by N2836 "Wording Tweaks for Concept-enabled Random Number Generation in C++0X".