<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 793: discrete_distribution missing constructor</title>
<meta property="og:title" content="Issue 793: discrete_distribution missing constructor">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue793.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="793"><a href="lwg-defects.html#793">793</a>. <code>discrete_distribution</code> missing constructor</h3>
<p><b>Section:</b> 29.5.9.6.1 <a href="https://wg21.link/rand.dist.samp.discrete">[rand.dist.samp.discrete]</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.discrete">issues</a> in [rand.dist.samp.discrete].</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>discrete_distribution</code> should have a constructor like:
</p>

<blockquote><pre>
template&lt;class _Fn&gt;
  discrete_distribution(result_type _Count, double _Low, double _High,
                        _Fn&amp; _Func);
</pre></blockquote>

<p>
(Makes it easier to fill a histogram with function values over a range.)
</p>

<p><i>[
Bellevue:
]</i></p>


<blockquote><p>
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.
</p></blockquote>

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


<blockquote>
<p>
Bill is not requesting this.
</p>
<p>
Marc Paterno: <code>_Fn</code> cannot return negative values at the points where the
function is sampled. It is sampled in the middle of each bin. <code>_Fn</code> cannot
return 0 everywhere it is sampled.
</p>
<p>
Jens: lambda expressions are rvalues
</p>
<p>
Add a library issue to provide an
<code>initializer_list&lt;double&gt;</code> constructor for
<code>discrete_distribution</code>.
</p>
<p>
Marc Paterno: dislikes reference for <code>_Fn</code> parameter. Make it pass-by-value (to use lambda),
use <code>std::ref</code> to wrap giant-state function objects.
</p>
<p>
Daniel: See <code>random_shuffle</code>, pass-by-rvalue-reference.
</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>.
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
<code>general_pdf_distribution</code> 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 of <code>Func</code> by <code>Func&amp;&amp;</code> in this proposal as part
of an editorial process.
</p></blockquote>



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

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

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

<blockquote><pre>
explicit discrete_distribution(const param_type&amp; parm);
</pre></blockquote>

<p>
insert:
</p>


<blockquote><pre>
template&lt;typename Func&gt;
discrete_distribution(result_type nf, double xmin, double xmax, Func fw);
</pre></blockquote>
</li>

<li>
<p>
Between p.4 and p.5 insert a series of new paragraphs as part of the
new member description::
</p>
<blockquote><pre>
template&lt;typename Func&gt;
discrete_distribution(result_type nf, double xmin, double xmax, Func fw);
</pre>

<p>
<i>Complexity:</i> Exactly nf invocations of fw.
</p>
<p>
<i>Requires:</i>
</p>
<ol style="list-style-type:lower-alpha">
<li>
fw shall be callable with one argument of type double, and shall
return values of a type convertible to double;</li>

<li>If nf &gt; 0, the relation <code><i>x</i><sub><i>min</i></sub></code> &lt; <code><i>x</i><sub><i>max</i></sub></code> shall hold, and for all sample values
<code><i>x</i><sub><i>k</i></sub></code>, fw(<code><i>x</i><sub><i>k</i></sub></code>) shall return a weight value <code><i>w</i><sub><i>k</i></sub></code> that is non-negative, non-NaN,
and non-infinity;</li>

<li>The following relations shall hold: nf &ge; 0, and 0 &lt; S = <code><i>w</i><sub><i>0</i></sub></code>+. . .+<code><i>w<sub>n-1</sub></i></code>.</li>

</ol>

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

<li>
<p>Otherwise, sets n = nf, deltax = (<code><i>x</i><sub><i>max</i></sub></code> - <code><i>x</i><sub><i>min</i></sub></code>)/n 
and <code><i>x</i><sub><i>cent</i></sub></code> = <code><i>x</i><sub><i>min</i></sub></code> + 0.5 * deltax.</p>
<p>
For each k = 0, . . . ,n-1, calculates:</p>
<p>
<code><i>x</i><sub><i>k</i></sub></code> = <code><i>x</i><sub><i>cent</i></sub></code> + k * deltax
<code><i>w</i><sub><i>k</i></sub></code> = fw(<code><i>x</i><sub><i>k</i></sub></code>)
</p>
</li>
<li>
<p>Constructs a discrete_distribution object with probabilities:</p>
<blockquote><p>
<code><i>p</i><sub><i>k</i></sub></code> = <code><i>w</i><sub><i>k</i></sub></code>/S  for k = 0, . . . , n-1.
</p></blockquote>
</li>
</ol>
</blockquote>
</li>
</ol>

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


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

<blockquote><pre>
explicit discrete_distribution(const param_type&amp; parm);
</pre></blockquote>

<p>
insert:
</p>


<blockquote><pre>
template&lt;Callable&lt;auto, double&gt; Func&gt;
 requires Convertible&lt;Func::result_type, double&gt;
discrete_distribution(result_type nf, double xmin, double xmax, Func fw);
</pre></blockquote>
</li>

<li>
<p>
Between p.4 and p.5 insert a series of new paragraphs as part of the
new member description::
</p>
<blockquote><pre>
template&lt;Callable&lt;auto, double&gt; Func&gt;
 requires Convertible&lt;Func::result_type, double&gt;
discrete_distribution(result_type nf, double xmin, double xmax, Func fw);
</pre>

<p>
<i>Complexity:</i> Exactly nf invocations of fw.
</p>
<p>
<i>Requires:</i>
</p>
<ol style="list-style-type:lower-alpha">
<li>If nf &gt; 0, the relation <code><i>x</i><sub><i>min</i></sub></code> &lt; <code><i>x</i><sub><i>max</i></sub></code> shall hold, and for all sample values
<code><i>x</i><sub><i>k</i></sub></code>, fw(<code><i>x</i><sub><i>k</i></sub></code>) shall return a weight value <code><i>w</i><sub><i>k</i></sub></code> that is non-negative, non-NaN,
and non-infinity;</li>

<li>The following relations shall hold: nf &ge; 0, and 0 &lt; S = <code><i>w</i><sub><i>0</i></sub></code>+. . .+<code><i>w<sub>n-1</sub></i></code>.</li>

</ol>

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

<li>
<p>Otherwise, sets n = nf, deltax = (<code><i>x</i><sub><i>max</i></sub></code> - <code><i>x</i><sub><i>min</i></sub></code>)/n 
and <code><i>x</i><sub><i>cent</i></sub></code> = <code><i>x</i><sub><i>min</i></sub></code> + 0.5 * deltax.
<p/>
For each k = 0, . . . ,n-1, calculates:
<p/>
<code><i>x</i><sub><i>k</i></sub></code> = <code><i>x</i><sub><i>cent</i></sub></code> + k * deltax
<code><i>w</i><sub><i>k</i></sub></code> = fw(<code><i>x</i><sub><i>k</i></sub></code>)
</p>
</li>
<li>
<p>Constructs a discrete_distribution object with probabilities:</p>
<blockquote><p>
<code><i>p</i><sub><i>k</i></sub></code> = <code><i>w</i><sub><i>k</i></sub></code>/S  for k = 0, . . . , n-1.
</p></blockquote>
</li>
</ol>
</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>
