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.
piecewise_constant_distribution missing constructorSection: 29.5.9.6.2 [rand.dist.samp.pconst] 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.pconst].
View all issues with Resolved status.
Discussion:
piecewise_constant_distribution should have a constructor like:
template<class _Fn>
piecewise_constant_distribution(size_t _Count,
_Ty _Low, _Ty _High, _Fn& _Func);
(Makes it easier to fill a histogram with function values over a range.
The two (reference 793(i)) make a sensible replacement for
general_pdf_distribution.)
[ Sophia Antipolis: ]
Marc: uses variable width of bins and weight for each bin. This is not giving enough flexibility to control both variables.
Add a library issue to provide an constructor taking an
initializer_list<double>and_Fnforpiecewise_constant_distribution.Daniel to draft wording.
[ Pre San Francisco, Daniel provided wording. ]
The here proposed changes of the WP refer to the current state of N2691. For reasons explained in 793(i), the author decided to propose a function argument that is provided by value. The issue proposes a c'tor signature, that does not take advantage of the full flexibility of
piecewise_constant_distribution, because it restricts on a constant bin width, but the use-case seems to be popular enough to justify it's introduction.
Proposed resolution:
Non-concept version of the proposed resolution
In 29.5.9.6.2 [rand.dist.samp.pconst]/1, class piecewise_constant_distribution,
just before the member declaration
explicit piecewise_constant_distribution(const param_type& parm);
insert:
template<typename Func> piecewise_constant_distribution(size_t nf, RealType xmin, RealType xmax, Func fw);
Between p.4 and p.5 insert a new sequence of paragraphs nominated below as [p5_1], [p5_2], [p5_3], and [p5_4] as part of the new member description:
template<typename Func> piecewise_constant_distribution(size_t nf, RealType xmin, RealType xmax, Func fw);[p5_1] Complexity: Exactly
nfinvocations offw.[p5_2] Requires:
fwshall be callable with one argument of typeRealType, and shall return values of a type convertible to double;- For all sample values
xkdefined below, fw(xk) shall return a weight valuewkthat is non-negative, non-NaN, and non-infinity;- The following relations shall hold:
xmin<xmax, and 0 < S =w0+. . .+wn-1.[p5_3] Effects:
If nf == 0,
- sets deltax =
xmax-xmin, and- lets the sequence
whave lengthn = 1and consist of the single valuew0= 1, and- lets the sequence
bhave lengthn+1withb0=xminandb1=xmaxOtherwise,
- sets
n = nf,deltax =(xmax-xmin)/n,xcent=xmin+ 0.5 * deltax, andlets the sequences
for each k = 0, . . . ,n-1, calculates:wandbhave lengthnandn+1, resp. anddxk= k * deltaxbk=xmin+dxkxk=xcent+dxkwk= fw(xk),and
- sets
bn=xmaxConstructs a
piecewise_constant_distributionobject with the above computed sequencebas the interval boundaries and with the probability densities:
ρk=wk/(S * deltax) for k = 0, . . . , n-1.[p5_4] [Note: In this context, the subintervals [
bk,bk+1) are commonly known as the bins of a histogram. -- end note]
Concept version of the proposed resolution
In 29.5.9.6.2 [rand.dist.samp.pconst]/1, class piecewise_constant_distribution,
just before the member declaration
explicit piecewise_constant_distribution(const param_type& parm);
insert:
template<Callable<auto, RealType> Func> requires Convertible<Func::result_type, double> piecewise_constant_distribution(size_t nf, RealType xmin, RealType xmax, Func fw);
Between p.4 and p.5 insert a new sequence of paragraphs nominated below as [p5_1], [p5_2], [p5_3], and [p5_4] as part of the new member description:
template<Callable<auto, RealType> Func> requires Convertible<Func::result_type, double> piecewise_constant_distribution(size_t nf, RealType xmin, RealType xmax, Func fw);[p5_1] Complexity: Exactly
nfinvocations offw.[p5_2] Requires:
- For all sample values
xkdefined below, fw(xk) shall return a weight valuewkthat is non-negative, non-NaN, and non-infinity;- The following relations shall hold:
xmin<xmax, and 0 < S =w0+. . .+wn-1.[p5_3] Effects:
If nf == 0,
- sets deltax =
xmax-xmin, and- lets the sequence
whave lengthn = 1and consist of the single valuew0= 1, and- lets the sequence
bhave lengthn+1withb0=xminandb1=xmaxOtherwise,
- sets
n = nf,deltax =(xmax-xmin)/n,xcent=xmin+ 0.5 * deltax, andlets the sequences
for each k = 0, . . . ,n-1, calculates:wandbhave lengthnandn+1, resp. anddxk= k * deltaxbk=xmin+dxkxk=xcent+dxkwk= fw(xk),and
- sets
bn=xmaxConstructs a
piecewise_constant_distributionobject with the above computed sequencebas the interval boundaries and with the probability densities:
ρk=wk/(S * deltax) for k = 0, . . . , n-1.[p5_4] [Note: In this context, the subintervals [
bk,bk+1) are commonly known as the bins of a histogram. -- end note]
Rationale:
Addressed by N2836 "Wording Tweaks for Concept-enabled Random Number Generation in C++0X".