<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 875: Missing initializer_list constructor for piecewise_constant_distribution</title>
<meta property="og:title" content="Issue 875: Missing initializer_list constructor for piecewise_constant_distribution">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue875.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="875"><a href="lwg-defects.html#875">875</a>. Missing <code>initializer_list</code> constructor for <code>piecewise_constant_distribution</code></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> Daniel Kr&uuml;gler <b>Opened:</b> 2008-08-22 <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>
During the Sophia Antipolis meeting it was decided to separate from
<a href="lwg-defects.html#794" title="piecewise_constant_distribution missing constructor (Status: Resolved)">794</a><sup><a href="https://cplusplus.github.io/LWG/issue794" title="Latest snapshot">(i)</a></sup> a subrequest that adds initializer list support to
<code>piecewise_constant_distribution</code>, specifically, the issue proposed
to add a c'tor taking a <code>initializer_list&lt;double&gt;</code> and a <code>Callable</code> to evaluate
weight values. For consistency with the remainder of this class and
the remainder of the <code>initializer_list</code>-aware library the author decided to
change the list argument type to the template parameter <code>RealType</code>
instead. For the reasoning to use <code>Func</code> instead of <code>Func&amp;&amp;</code> as c'tor
function argument see issue <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>.
</p>


<p id="res-875"><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(initializer_list&lt;RealType&gt; bl, Func fw);
</pre></blockquote>
</li>

<li>
<p>
Between p.4 and p.5 of the same section insert a series of
new paragraphs nominated below as [p5_1], [p5_2], and [p5_3]
as part of the new member description:
</p>

<blockquote><pre>
template&lt;typename Func&gt;
piecewise_constant_distribution(initializer_list&lt;RealType&gt; bl, Func fw);
</pre>

<blockquote>

<p>
[p5_1] <i>Complexity:</i> Exactly <code>nf = max(bl.size(), 1) - 1</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 <code>double</code>;
</li>
<li>
The relation <code>0 &lt; S = w<sub>0</sub>+. . .+w<sub>n-1</sub></code> shall hold. 
For all sampled values <code><i>x<sub>k</sub></i></code> defined below, <code>fw(<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>
If <code>nf &gt; 0</code> let <code>b<sub><i>k</i></sub> = *(bl.begin() + k), k = 0, . . . , bl.size()-1</code> and the
following relations shall hold for <code>k = 0, . . . , nf-1: b<sub><i>k</i></sub> &lt; b<sub><i>k+1</i></sub></code>.
</li>
</ol>

<p>
[p5_3] <i>Effects:</i>
</p>

<ol style="list-style-type:lower-alpha">
<li>
<p>If <code>nf == 0</code>,</p>
<ol style="list-style-type:lower-alpha">
<li>
lets the sequence <code>w</code> have length <code>n = 1</code> and consist of the single
     value <code>w<sub>0</sub> = 1</code>, and
</li>
<li>
lets the sequence <code>b</code> have length <code>n+1</code> with <code>b<sub>0</sub> = 0</code> and <code>b<sub>1</sub> = 1</code>.
</li>
</ol>
</li>

<li>
<p>Otherwise,</p>
<ol style="list-style-type:lower-alpha">
<li>
sets <code>n = nf</code>, and <code>[bl.begin(), bl.end())</code> shall form the sequence <code>b</code> of
length <code>n+1</code>, and
</li>
<li>
<p>lets the sequences <code>w</code> have length <code>n</code> and for each <code>k = 0, . . . ,n-1</code>,
     calculates:</p>
<blockquote><p style="white-space: pre;">
x<sub><i>k</i></sub> = 0.5*(b<sub><i>k+1</i></sub> + b<sub><i>k</i></sub>)
w<sub><i>k</i></sub> = fw(x<sub><i>k</i></sub>)
</p></blockquote>
</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 style="white-space: pre;">
&rho;<sub><i>k</i></sub> = w<sub><i>k</i></sub>/(S * (b<sub><i>k+1</i></sub> - b<sub><i>k</i></sub>)) for k = 0, . . . , n-1.
</p></blockquote>

</li>
</ol>

</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(initializer_list&lt;RealType&gt; bl, Func fw);
</pre></blockquote>
</li>

<li>
<p>
Between p.4 and p.5 of the same section insert a series of
new paragraphs nominated below as [p5_1], [p5_2], and [p5_3]
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(initializer_list&lt;RealType&gt; bl, Func fw);
</pre>

<blockquote>

<p>
[p5_1] <i>Complexity:</i> Exactly <code>nf = max(bl.size(), 1) - 1</code> invocations of <code>fw</code>.
</p>

<p>
[p5_2] <i>Requires:</i>
</p>

<ol style="list-style-type:lower-alpha">
<li>
The relation <code>0 &lt; S = w<sub>0</sub>+. . .+w<sub>n-1</sub></code> shall hold. 
For all sampled values <code><i>x<sub>k</sub></i></code> defined below, <code>fw(<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>
If <code>nf &gt; 0</code> let <code>b<sub><i>k</i></sub> = *(bl.begin() + k), k = 0, . . . , bl.size()-1</code> and the
following relations shall hold for <code>k = 0, . . . , nf-1: b<sub><i>k</i></sub> &lt; b<sub><i>k+1</i></sub></code>.
</li>
</ol>

<p>
[p5_3] <i>Effects:</i>
</p>

<ol style="list-style-type:lower-alpha">
<li>
<p>If <code>nf == 0</code>,</p>
<ol style="list-style-type:lower-alpha">
<li>
lets the sequence <code>w</code> have length <code>n = 1</code> and consist of the single
     value <code>w<sub>0</sub> = 1</code>, and
</li>
<li>
lets the sequence <code>b</code> have length <code>n+1</code> with <code>b<sub>0</sub> = 0</code> and <code>b<sub>1</sub> = 1</code>.
</li>
</ol>
</li>

<li>
<p>Otherwise,</p>
<ol style="list-style-type:lower-alpha">
<li>
sets <code>n = nf</code>, and <code>[bl.begin(), bl.end())</code> shall form the sequence <code>b</code> of
length <code>n+1</code>, and
</li>
<li>
<p>lets the sequences <code>w</code> have length <code>n</code> and for each <code>k = 0, . . . ,n-1</code>,
     calculates:</p>
<blockquote><p style="white-space: pre;">
x<sub><i>k</i></sub> = 0.5*(b<sub><i>k+1</i></sub> + b<sub><i>k</i></sub>)
w<sub><i>k</i></sub> = fw(x<sub><i>k</i></sub>)
</p></blockquote>
</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 style="white-space: pre;">
&rho;<sub><i>k</i></sub> = w<sub><i>k</i></sub>/(S * (b<sub><i>k+1</i></sub> - b<sub><i>k</i></sub>)) for k = 0, . . . , n-1.
</p></blockquote>

</li>
</ol>

</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>
