archetypes.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>Conceptual Archetypes for Reals and Distributions</title>
  5. <link rel="stylesheet" href="../math.css" type="text/css">
  6. <meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
  7. <link rel="home" href="../index.html" title="Math Toolkit 2.11.0">
  8. <link rel="up" href="../using_udt.html" title="Chapter&#160;19.&#160;Use with User-Defined Floating-Point Types - Boost.Multiprecision and others">
  9. <link rel="prev" href="dist_concept.html" title="Conceptual Requirements for Distribution Types">
  10. <link rel="next" href="../policy.html" title="Chapter&#160;20.&#160;Policies: Controlling Precision, Error Handling etc">
  11. </head>
  12. <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
  13. <table cellpadding="2" width="100%"><tr>
  14. <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../boost.png"></td>
  15. <td align="center"><a href="../../../../../index.html">Home</a></td>
  16. <td align="center"><a href="../../../../../libs/libraries.htm">Libraries</a></td>
  17. <td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
  18. <td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
  19. <td align="center"><a href="../../../../../more/index.htm">More</a></td>
  20. </tr></table>
  21. <hr>
  22. <div class="spirit-nav">
  23. <a accesskey="p" href="dist_concept.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_udt.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../policy.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  24. </div>
  25. <div class="section">
  26. <div class="titlepage"><div><div><h2 class="title" style="clear: both">
  27. <a name="math_toolkit.archetypes"></a><a class="link" href="archetypes.html" title="Conceptual Archetypes for Reals and Distributions">Conceptual Archetypes for Reals
  28. and Distributions</a>
  29. </h2></div></div></div>
  30. <p>
  31. There are a few concept archetypes available:
  32. </p>
  33. <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
  34. <li class="listitem">
  35. Real concept for floating-point types.
  36. </li>
  37. <li class="listitem">
  38. Distribution concept for statistical distributions.
  39. </li>
  40. </ul></div>
  41. <h6>
  42. <a name="math_toolkit.archetypes.h0"></a>
  43. <span class="phrase"><a name="math_toolkit.archetypes.real_concept"></a></span><a class="link" href="archetypes.html#math_toolkit.archetypes.real_concept">Real
  44. concept</a>
  45. </h6>
  46. <p>
  47. <code class="computeroutput"><span class="identifier">std_real_concept</span></code> is an archetype
  48. for theReal types, including the built-in float, double, long double.
  49. </p>
  50. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">concepts</span><span class="special">/</span><span class="identifier">std_real_concept</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></pre>
  51. <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span>
  52. <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span>
  53. <span class="keyword">namespace</span> <span class="identifier">concepts</span>
  54. <span class="special">{</span>
  55. <span class="keyword">class</span> <span class="identifier">std_real_concept</span><span class="special">;</span>
  56. <span class="special">}</span>
  57. <span class="special">}}</span> <span class="comment">// namespaces</span>
  58. </pre>
  59. <p>
  60. The main purpose in providing this type is to verify that standard library
  61. functions are found via a using declaration - bringing those functions into
  62. the current scope - and not just because they happen to be in global scope.
  63. </p>
  64. <p>
  65. In order to ensure that a call to say <code class="computeroutput"><span class="identifier">pow</span></code>
  66. can be found either via argument dependent lookup, or failing that then in
  67. the std namespace: all calls to standard library functions are unqualified,
  68. with the std:: versions found via a <code class="computeroutput"><span class="keyword">using</span></code>
  69. declaration to make them visible in the current scope. Unfortunately it's all
  70. to easy to forget the <code class="computeroutput"><span class="keyword">using</span></code> declaration,
  71. and call the double version of the function that happens to be in the global
  72. scope by mistake.
  73. </p>
  74. <p>
  75. For example if the code calls ::pow rather than std::pow, the code will cleanly
  76. compile, but truncation of long doubles to double will cause a significant
  77. loss of precision. In contrast a template instantiated with std_real_concept
  78. will <span class="bold"><strong>only</strong></span> compile if the all the standard
  79. library functions used have been brought into the current scope with a using
  80. declaration.
  81. </p>
  82. <h3>
  83. <a name="math_toolkit.archetypes.h1"></a>
  84. <span class="phrase"><a name="math_toolkit.archetypes.testing_the_real_concept"></a></span><a class="link" href="archetypes.html#math_toolkit.archetypes.testing_the_real_concept">Testing
  85. the real concept</a>
  86. </h3>
  87. <p>
  88. There is a test program <a href="../../../test/std_real_concept_check.cpp" target="_top">libs/math/test/std_real_concept_check.cpp</a>
  89. that instantiates every template in this library with type <code class="computeroutput"><span class="identifier">std_real_concept</span></code>
  90. to verify its usage of standard library functions.
  91. </p>
  92. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">concepts</span><span class="special">/</span><span class="identifier">real_concept</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></pre>
  93. <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span>
  94. <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span>
  95. <span class="keyword">namespace</span> <span class="identifier">concepts</span><span class="special">{</span>
  96. <span class="keyword">class</span> <span class="identifier">real_concept</span><span class="special">;</span>
  97. <span class="special">}}}</span> <span class="comment">// namespaces</span>
  98. </pre>
  99. <p>
  100. <code class="computeroutput"><span class="identifier">real_concept</span></code> is an archetype
  101. for <a class="link" href="real_concepts.html" title="Conceptual Requirements for Real Number Types">user defined real types</a>,
  102. it declares its standard library functions in its own namespace: these will
  103. only be found if they are called unqualified allowing argument dependent lookup
  104. to locate them. In addition this type is useable at runtime: this allows code
  105. that would not otherwise be exercised by the built-in floating point types
  106. to be tested. There is no std::numeric_limits&lt;&gt; support for this type,
  107. since numeric_limits is not a conceptual requirement for <a class="link" href="real_concepts.html" title="Conceptual Requirements for Real Number Types">RealType</a>s.
  108. </p>
  109. <p>
  110. NTL RR is an example of a type meeting the requirements that this type models,
  111. but note that use of a thin wrapper class is required: refer to <a class="link" href="high_precision/use_ntl.html" title="Using NTL Library">"Using
  112. With NTL - a High-Precision Floating-Point Library"</a>.
  113. </p>
  114. <p>
  115. There is no specific test case for type <code class="computeroutput"><span class="identifier">real_concept</span></code>,
  116. instead, since this type is usable at runtime, each individual test case as
  117. well as testing <code class="computeroutput"><span class="keyword">float</span></code>, <code class="computeroutput"><span class="keyword">double</span></code> and <code class="computeroutput"><span class="keyword">long</span>
  118. <span class="keyword">double</span></code>, also tests <code class="computeroutput"><span class="identifier">real_concept</span></code>.
  119. </p>
  120. <h3>
  121. <a name="math_toolkit.archetypes.h2"></a>
  122. <span class="phrase"><a name="math_toolkit.archetypes.distribution_concept"></a></span><a class="link" href="archetypes.html#math_toolkit.archetypes.distribution_concept">Distribution
  123. Concept</a>
  124. </h3>
  125. <p>
  126. Distribution Concept models statistical distributions.
  127. </p>
  128. <pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">math</span><span class="special">/</span><span class="identifier">concepts</span><span class="special">/</span><span class="identifier">distribution</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></pre>
  129. <pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">{</span>
  130. <span class="keyword">namespace</span> <span class="identifier">math</span><span class="special">{</span>
  131. <span class="keyword">namespace</span> <span class="identifier">concepts</span>
  132. <span class="special">{</span>
  133. <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">RealType</span><span class="special">&gt;</span>
  134. <span class="keyword">class</span> <span class="identifier">distribution_archetype</span><span class="special">;</span>
  135. <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Distribution</span><span class="special">&gt;</span>
  136. <span class="keyword">struct</span> <span class="identifier">DistributionConcept</span><span class="special">;</span>
  137. <span class="special">}}}</span> <span class="comment">// namespaces</span>
  138. </pre>
  139. <p>
  140. The class template <code class="computeroutput"><span class="identifier">distribution_archetype</span></code>
  141. is a model of the <a class="link" href="dist_concept.html" title="Conceptual Requirements for Distribution Types">Distribution concept</a>.
  142. </p>
  143. <p>
  144. The class template <code class="computeroutput"><span class="identifier">DistributionConcept</span></code>
  145. is a <a href="../../../../../libs/concept_check/index.html" target="_top">concept checking
  146. class</a> for distribution types.
  147. </p>
  148. <h3>
  149. <a name="math_toolkit.archetypes.h3"></a>
  150. <span class="phrase"><a name="math_toolkit.archetypes.testing_the_distribution_concept"></a></span><a class="link" href="archetypes.html#math_toolkit.archetypes.testing_the_distribution_concept">Testing
  151. the distribution concept</a>
  152. </h3>
  153. <p>
  154. The test program <a href="../../../test/compile_test/distribution_concept_check.cpp" target="_top">distribution_concept_check.cpp</a>
  155. is responsible for using <code class="computeroutput"><span class="identifier">DistributionConcept</span></code>
  156. to verify that all the distributions in this library conform to the <a class="link" href="dist_concept.html" title="Conceptual Requirements for Distribution Types">Distribution
  157. concept</a>.
  158. </p>
  159. <p>
  160. The class template <code class="computeroutput"><span class="identifier">DistributionConcept</span></code>
  161. verifies the existence (but not proper function) of the non-member accessors
  162. required by the <a class="link" href="dist_concept.html" title="Conceptual Requirements for Distribution Types">Distribution concept</a>.
  163. These are checked by calls like
  164. </p>
  165. <pre class="programlisting"><span class="identifier">v</span> <span class="special">=</span> <span class="identifier">pdf</span><span class="special">(</span><span class="identifier">dist</span><span class="special">,</span> <span class="identifier">x</span><span class="special">);</span> <span class="comment">// (Result v is ignored).</span>
  166. </pre>
  167. <p>
  168. And in addition, those that accept two arguments do the right thing when the
  169. arguments are of different types (the result type is always the same as the
  170. distribution's value_type). (This is implemented by some additional forwarding-functions
  171. in derived_accessors.hpp, so that there is no need for any code changes. Likewise
  172. boilerplate versions of the hazard/chf/coefficient_of_variation functions are
  173. implemented in there too.)
  174. </p>
  175. </div>
  176. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  177. <td align="left"></td>
  178. <td align="right"><div class="copyright-footer">Copyright &#169; 2006-2019 Nikhar
  179. Agrawal, Anton Bikineev, Paul A. Bristow, Marco Guazzone, Christopher Kormanyos,
  180. Hubert Holin, Bruno Lalande, John Maddock, Jeremy Murphy, Matthew Pulver, Johan
  181. R&#229;de, Gautam Sewani, Benjamin Sobotta, Nicholas Thompson, Thijs van den Berg,
  182. Daryle Walker and Xiaogang Zhang<p>
  183. Distributed under the Boost Software License, Version 1.0. (See accompanying
  184. file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
  185. </p>
  186. </div></td>
  187. </tr></table>
  188. <hr>
  189. <div class="spirit-nav">
  190. <a accesskey="p" href="dist_concept.html"><img src="../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_udt.html"><img src="../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="../policy.html"><img src="../../../../../doc/src/images/next.png" alt="Next"></a>
  191. </div>
  192. </body>
  193. </html>