<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 794: piecewise_constant_distribution missing constructor</title>
<meta property="og:title" content="Issue 794: piecewise_constant_distribution missing constructor">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue794.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#Resolved">Resolved</a> status.</em></p>
<h3 id="794"><a href="lwg-defects.html#794">794</a>. <code>piecewise_constant_distribution</code> missing constructor</h3>
<p><b>Section:</b> 29.5.9.6.2 <a href="https://wg21.link/rand.dist.samp.pconst">[rand.dist.samp.pconst]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> P.J. Plauger <b>Opened:</b> 2008-02-09 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#rand.dist.samp.pconst">issues</a> in [rand.dist.samp.pconst].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>piecewise_constant_distribution</code> should have a constructor like:
</p>

<blockquote><pre>
template&lt;class _Fn&gt;
   piecewise_constant_distribution(size_t _Count,
            _Ty _Low, _Ty _High, _Fn&amp; _Func);
</pre></blockquote>

<p>
(Makes it easier to fill a histogram with function values over a range.
The two (reference <a href="lwg-defects.html#793" title="discrete_distribution missing constructor (Status: Resolved)">793</a><sup><a href="https://cplusplus.github.io/LWG/issue793" title="Latest snapshot">(i)</a></sup>) make a sensible replacement for
<code>general_pdf_distribution</code>.)
</p>

<p><i>[
Sophia Antipolis:
]</i></p>


<blockquote>
<p>
Marc: uses variable width of bins and weight for each bin. This is not
giving enough flexibility to control both variables.
</p>
<p>
Add a library issue to provide an constructor taking an
<code>initializer_list&lt;double&gt;</code> and <code>_Fn</code> for <code>piecewise_constant_distribution</code>.
</p>
<p>
Daniel to draft wording.
</p>
</blockquote>

<p><i>[
Pre San Francisco, Daniel provided wording.
]</i></p>


<blockquote><p>
The here proposed changes of the WP refer to the current state of
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2691.pdf">N2691</a>.
For reasons explained in <a href="lwg-defects.html#793" title="discrete_distribution missing constructor (Status: Resolved)">793</a><sup><a href="https://cplusplus.github.io/LWG/issue793" title="Latest snapshot">(i)</a></sup>, 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
<code>piecewise_constant_distribution</code>,
because it restricts on a constant bin width, but the use-case seems to
be popular enough to justify it's introduction.
</p></blockquote>



<p id="res-794"><b>Proposed resolution:</b></p>

<p><b>Non-concept version of the proposed resolution</b></p>

<ol>
<li>
<p>
In 29.5.9.6.2 <a href="https://wg21.link/rand.dist.samp.pconst">[rand.dist.samp.pconst]</a>/1, class <code>piecewise_constant_distribution</code>,
just <em>before</em> the member declaration
</p>

<blockquote><pre>
explicit piecewise_constant_distribution(const param_type&amp; parm);
</pre></blockquote>
<p>
insert:
</p>
<blockquote><pre>
template&lt;typename Func&gt;
piecewise_constant_distribution(size_t nf, RealType xmin, RealType xmax, Func fw);
</pre></blockquote>
</li>

<li>
<p>
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:
</p>

<blockquote><pre>
template&lt;typename Func&gt;
piecewise_constant_distribution(size_t nf, RealType xmin, RealType xmax, Func fw);
</pre>
<blockquote>
<p>
[p5_1] <i>Complexity:</i> Exactly <code>nf</code> invocations of <code>fw</code>.
</p>
<p>
[p5_2] <i>Requires:</i>
</p>
<ol style="list-style-type:lower-alpha">
<li><code>fw</code> shall be callable with one argument of type <code>RealType</code>, and shall
return values of a type convertible to double;
</li>
<li>
For all sample values <code><i>x<sub>k</sub></i></code> defined below, fw(<code><i>x<sub>k</sub></i></code>) shall return a weight
value <code><i>w<sub>k</sub></i></code> that is non-negative, non-NaN, and non-infinity;
</li>
<li>
The following relations shall hold: <code><i>x<sub>min</sub></i></code> &lt; <code><i>x<sub>max</sub></i></code>, and
0 &lt; S = <code><i>w<sub>0</sub></i></code>+. . .+<code><i>w<sub>n-1</sub></i></code>.
</li>
</ol>
<p>
[p5_3] <i>Effects:</i>
</p>
<ol style="list-style-type:lower-alpha">
<li>
<p>If nf == 0,</p>
 <ol style="list-style-type:lower-alpha">
 <li>
sets deltax = <code><i>x<sub>max</sub></i></code> - <code><i>x<sub>min</sub></i></code>, and</li>
<li> lets the sequence <code>w</code> have length <code>n = 1</code> and consist of the single
    value <code><i>w<sub>0</sub></i></code> = 1, and</li>
<li> lets the sequence <code>b</code> have length <code>n+1</code> with <code><i>b<sub>0</sub></i></code> = <code><i>x<sub>min</sub></i></code> and 
              <code><i>b<sub>1</sub></i></code> = <code><i>x<sub>max</sub></i></code>
</li>
</ol>
</li>
<li>
<p>Otherwise,</p>
<ol style="list-style-type:lower-alpha">
<li> sets <code>n = nf</code>, <code>deltax = </code>(<code><i>x<sub>max</sub></i></code> - <code><i>x<sub>min</sub></i></code>)/n,
                 <code><i>x<sub>cent</sub></i></code> = <code><i>x<sub>min</sub></i></code> + 0.5 * deltax, and
</li>
<li><p>lets the sequences <code>w</code> and <code>b</code> have length <code>n</code> and <code>n+1</code>, resp. and
<p/>
for each k = 0, . . . ,n-1, calculates:
<p/>
  <code><i>dx<sub>k</sub></i></code> = k * deltax
  <code><i>b<sub>k</sub></i></code> = <code><i>x<sub>min</sub></i></code> + <code><i>dx<sub>k</sub></i></code>
  <code><i>x<sub>k</sub></i></code> = <code><i>x<sub>cent</sub></i></code> + <code><i>dx<sub>k</sub></i></code>
  <code><i>w<sub>k</sub></i></code> = fw(<code><i>x<sub>k</sub></i></code>),
</p> 
<p> and</p>
</li>
<li> sets <code><i>b<sub>n</sub></i></code> = <code><i>x<sub>max</sub></i></code></li>
</ol>
</li>
<li>
<p>
Constructs a <code>piecewise_constant_distribution</code> object with
the above computed sequence <code>b</code> as the interval boundaries
and with the probability densities:
</p>
<blockquote><p>
<code><i>&rho;<sub>k</sub></i></code> = <code><i>w<sub>k</sub></i></code>/(S * deltax)  for k = 0, . . . , n-1.
</p></blockquote> 
</li>
</ol>
<p>
[p5_4] [<i>Note:</i> In this context, the subintervals [<code><i>b<sub>k</sub></i></code>, <code><i>b<sub>k+1</sub></i></code>) are commonly
 known as the <i>bins</i> of a histogram. <i>-- end note</i>]
 </p>
</blockquote>
</blockquote>
</li>
</ol>

<p><b>Concept version of the proposed resolution</b></p>

<ol>
<li>
<p>
In 29.5.9.6.2 <a href="https://wg21.link/rand.dist.samp.pconst">[rand.dist.samp.pconst]</a>/1, class <code>piecewise_constant_distribution</code>,
just <em>before</em> the member declaration
</p>

<blockquote><pre>
explicit piecewise_constant_distribution(const param_type&amp; parm);
</pre></blockquote>
<p>
insert:
</p>
<blockquote><pre>
template&lt;Callable&lt;auto, RealType&gt; Func&gt;
 requires Convertible&lt;Func::result_type, double&gt;
piecewise_constant_distribution(size_t nf, RealType xmin, RealType xmax, Func fw);
</pre></blockquote>
</li>

<li>
<p>
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:
</p>

<blockquote><pre>
template&lt;Callable&lt;auto, RealType&gt; Func&gt;
 requires Convertible&lt;Func::result_type, double&gt;
piecewise_constant_distribution(size_t nf, RealType xmin, RealType xmax, Func fw);
</pre>
<blockquote>
<p>
[p5_1] <i>Complexity:</i> Exactly <code>nf</code> invocations of <code>fw</code>.
</p>
<p>
[p5_2] <i>Requires:</i>
</p>
<ol style="list-style-type:lower-alpha">
<li>
For all sample values <code><i>x<sub>k</sub></i></code> defined below, fw(<code><i>x<sub>k</sub></i></code>) shall return a weight
value <code><i>w<sub>k</sub></i></code> that is non-negative, non-NaN, and non-infinity;
</li>
<li>
The following relations shall hold: <code><i>x<sub>min</sub></i></code> &lt; <code><i>x<sub>max</sub></i></code>, and
0 &lt; S = <code><i>w<sub>0</sub></i></code>+. . .+<code><i>w<sub>n-1</sub></i></code>.
</li>
</ol>
<p>
[p5_3] <i>Effects:</i>
</p>
<ol style="list-style-type:lower-alpha">
<li>
<p>If nf == 0,</p>
 <ol style="list-style-type:lower-alpha">
 <li>
sets deltax = <code><i>x<sub>max</sub></i></code> - <code><i>x<sub>min</sub></i></code>, and</li>
<li> lets the sequence <code>w</code> have length <code>n = 1</code> and consist of the single
    value <code><i>w<sub>0</sub></i></code> = 1, and</li>
<li> lets the sequence <code>b</code> have length <code>n+1</code> with <code><i>b<sub>0</sub></i></code> = <code><i>x<sub>min</sub></i></code> and 
              <code><i>b<sub>1</sub></i></code> = <code><i>x<sub>max</sub></i></code>
</li>
</ol>
</li>
<li>
<p>Otherwise,</p>
<ol style="list-style-type:lower-alpha">
<li> sets <code>n = nf</code>, <code>deltax = </code>(<code><i>x<sub>max</sub></i></code> - <code><i>x<sub>min</sub></i></code>)/n,
                 <code><i>x<sub>cent</sub></i></code> = <code><i>x<sub>min</sub></i></code> + 0.5 * deltax, and
</li>
<li><p>lets the sequences <code>w</code> and <code>b</code> have length <code>n</code> and <code>n+1</code>, resp. and
<p/>
for each k = 0, . . . ,n-1, calculates:
  <code><i>dx<sub>k</sub></i></code> = k * deltax
  <code><i>b<sub>k</sub></i></code> = <code><i>x<sub>min</sub></i></code> + <code><i>dx<sub>k</sub></i></code>
  <code><i>x<sub>k</sub></i></code> = <code><i>x<sub>cent</sub></i></code> + <code><i>dx<sub>k</sub></i></code>
  <code><i>w<sub>k</sub></i></code> = fw(<code><i>x<sub>k</sub></i></code>),
</p> 
<p> and</p>
</li>
<li> sets <code><i>b<sub>n</sub></i></code> = <code><i>x<sub>max</sub></i></code></li>
</ol>
</li>
<li>
<p>
Constructs a <code>piecewise_constant_distribution</code> object with
the above computed sequence <code>b</code> as the interval boundaries
and with the probability densities:
</p>
<blockquote><p>
<code><i>&rho;<sub>k</sub></i></code> = <code><i>w<sub>k</sub></i></code>/(S * deltax)  for k = 0, . . . , n-1.
</p></blockquote> 
</li>
</ol>
<p>
[p5_4] [<i>Note:</i> In this context, the subintervals [<code><i>b<sub>k</sub></i></code>, <code><i>b<sub>k+1</sub></i></code>) are commonly
 known as the <i>bins</i> of a histogram. <i>-- end note</i>]
 </p>
</blockquote>
</blockquote>
</li>
</ol>



<p><b>Rationale:</b></p>
<p>
Addressed by
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2009/n2836.pdf">N2836</a> 
"Wording Tweaks for Concept-enabled Random Number Generation in C++0X".
</p>





</body>
</html>
