representing-dimensions.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <!-- Copyright Aleksey Gurtovoy 2006. Distributed under the Boost -->
  5. <!-- Software License, Version 1.0. (See accompanying -->
  6. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  7. <head>
  8. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  9. <meta name="generator" content="Docutils 0.3.6: http://docutils.sourceforge.net/" />
  10. <title>THE BOOST MPL LIBRARY: Representing Dimensions</title>
  11. <link rel="stylesheet" href="../style.css" type="text/css" />
  12. </head>
  13. <body class="docframe">
  14. <table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./dimensional-analysis.html" class="navigation-link">Prev</a>&nbsp;<a href="./representing-quantities.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group">Back&nbsp;<a href="./representing-quantities.html" class="navigation-link">Along</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./dimensional-analysis.html" class="navigation-link">Up</a>&nbsp;<a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td>
  15. <td class="header-group page-location"><a href="../index.html" class="navigation-link">Front Page</a> / <a href="./tutorial-metafunctions.html" class="navigation-link">Tutorial: Metafunctions and Higher-Order Metaprogramming</a> / <a href="./dimensional-analysis.html" class="navigation-link">Dimensional Analysis</a> / <a href="./representing-dimensions.html" class="navigation-link">Representing Dimensions</a></td>
  16. </tr></table><div class="header-separator"></div>
  17. <div class="section" id="representing-dimensions">
  18. <h1><a class="toc-backref" href="./dimensional-analysis.html#id42" name="representing-dimensions">Representing Dimensions</a></h1>
  19. <p>An international standard called <em>Système
  20. International d'Unites</em> (SI), breaks every quantity down into a
  21. combination of the dimensions <em>mass</em>, <em>length</em> (or <em>position</em>),
  22. <em>time</em>, <em>charge</em>, <em>temperature</em>, <em>intensity</em>, and <em>angle</em>. To be
  23. reasonably general, our system would have to be able to
  24. represent seven or more fundamental dimensions. It also needs
  25. the ability to represent composite dimensions that, like <em>force</em>,
  26. are built through multiplication or division of the fundamental
  27. ones.</p>
  28. <p>In general, a composite dimension is the product of powers of
  29. fundamental dimensions. <a class="footnote-reference" href="#divisor" id="id6" name="id6">[1]</a> If we were going to represent
  30. these powers for manipulation at runtime, we could use an array of
  31. seven <tt class="literal"><span class="pre">int</span></tt>s, with each position in the array holding the power
  32. of a different fundamental dimension:</p>
  33. <pre class="literal-block">
  34. typedef int dimension[7]; // m l t ...
  35. dimension const mass = {1, 0, 0, 0, 0, 0, 0};
  36. dimension const length = {0, 1, 0, 0, 0, 0, 0};
  37. dimension const time = {0, 0, 1, 0, 0, 0, 0};
  38. ...
  39. </pre>
  40. <table class="footnote" frame="void" id="divisor" rules="none">
  41. <colgroup><col class="label" /><col /></colgroup>
  42. <tbody valign="top">
  43. <tr><td class="label"><a class="fn-backref" href="#id6" name="divisor">[1]</a></td><td>Divisors just contribute negative exponents, since
  44. 1/<em>x</em> = <em>x</em><sup>-1</sup>.</td></tr>
  45. </tbody>
  46. </table>
  47. <p>In that representation, force would be:</p>
  48. <pre class="literal-block">
  49. dimension const force = {1, 1, -2, 0, 0, 0, 0};
  50. </pre>
  51. <!-- @compile(2) -->
  52. <!-- @litre_translator.line_offset -= 7 -->
  53. <p>that is, <em>mlt</em><sup>-2</sup>. However, if we want to get dimensions into the
  54. type system, these arrays won't do the trick: they're all
  55. the same type! Instead, we need types that <em>themselves</em> represent
  56. sequences of numbers, so that two masses have the same type and a
  57. mass is a different type from a length.</p>
  58. <p>Fortunately, the MPL provides us with a collection of <strong>type
  59. sequences</strong>. For example, we can build a sequence of the built-in
  60. signed integral types this way:</p>
  61. <pre class="literal-block">
  62. #include &lt;boost/mpl/vector.hpp&gt;
  63. typedef boost::mpl::vector&lt;
  64. signed char, short, int, long&gt; signed_types;
  65. </pre>
  66. <p>How can we use a type sequence to represent numbers? Just as
  67. numerical metafunctions pass and return wrapper <em>types</em> having a
  68. nested <tt class="literal"><span class="pre">::value</span></tt>, so numerical sequences are really sequences of
  69. wrapper types (another example of polymorphism). To make this sort
  70. of thing easier, MPL supplies the <tt class="literal"><span class="pre">int_&lt;N&gt;</span></tt> class template, which
  71. presents its integral argument as a nested <tt class="literal"><span class="pre">::value</span></tt>:</p>
  72. <pre class="literal-block">
  73. #include &lt;boost/mpl/int.hpp&gt;
  74. namespace mpl = boost::mpl; // namespace alias
  75. static int const five = mpl::int_&lt;5&gt;::value;
  76. </pre>
  77. <div class="sidebar">
  78. <p class="sidebar-title first">Namespace Aliases</p>
  79. <div class="line-block">
  80. <div class="line"><tt class="literal"><span class="pre">namespace</span></tt> <em>alias</em> <tt class="literal"><span class="pre">=</span></tt> <em>namespace-name</em><tt class="literal"><span class="pre">;</span></tt></div>
  81. </div>
  82. <p>declares <em>alias</em> to be a synonym for <em>namespace-name</em>. Many
  83. examples in this book will use <tt class="literal"><span class="pre">mpl::</span></tt> to indicate
  84. <tt class="literal"><span class="pre">boost::mpl::</span></tt>, but will omit the alias that makes it legal
  85. C++.</p>
  86. </div>
  87. <!-- @ignore() # nonsense isn't worth testing
  88. prefix +=['''
  89. #include <boost/mpl/int.hpp>
  90. #include <boost/mpl/vector.hpp>
  91. '''] -->
  92. <p>In fact, the library contains a whole suite of integral constant
  93. wrappers such as <tt class="literal"><span class="pre">long_</span></tt> and <tt class="literal"><span class="pre">bool_</span></tt>, each one wrapping a
  94. different type of integral constant within a class template.</p>
  95. <p>Now we can build our fundamental dimensions:</p>
  96. <pre class="literal-block">
  97. typedef mpl::vector&lt;
  98. mpl::int_&lt;1&gt;, mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;
  99. , mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;
  100. &gt; mass;
  101. typedef mpl::vector&lt;
  102. mpl::int_&lt;0&gt;, mpl::int_&lt;1&gt;, mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;
  103. , mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;, mpl::int_&lt;0&gt;
  104. &gt; length;
  105. ...
  106. </pre>
  107. <!-- @ # We explained about the implicit namespace alias above
  108. prefix.append("""
  109. namespace boost{namespace mpl {}}
  110. namespace mpl = boost::mpl;
  111. """)
  112. compile('all') -->
  113. <p>Whew! That's going to get tiring pretty quickly. Worse, it's hard
  114. to read and verify: The essential information, the powers of each
  115. fundamental dimension, is buried in repetitive syntactic &quot;noise.&quot;
  116. Accordingly, MPL supplies <strong>integral sequence wrappers</strong> that allow
  117. us to write:</p>
  118. <pre class="literal-block">
  119. #include &lt;boost/mpl/vector_c.hpp&gt;
  120. typedef mpl::vector_c&lt;int,1,0,0,0,0,0,0&gt; mass;
  121. typedef mpl::vector_c&lt;int,0,1,0,0,0,0,0&gt; length; // or position
  122. typedef mpl::vector_c&lt;int,0,0,1,0,0,0,0&gt; time;
  123. typedef mpl::vector_c&lt;int,0,0,0,1,0,0,0&gt; charge;
  124. typedef mpl::vector_c&lt;int,0,0,0,0,1,0,0&gt; temperature;
  125. typedef mpl::vector_c&lt;int,0,0,0,0,0,1,0&gt; intensity;
  126. typedef mpl::vector_c&lt;int,0,0,0,0,0,0,1&gt; angle;
  127. </pre>
  128. <p>Even though they have different types, you can think of these
  129. <tt class="literal"><span class="pre">mpl::vector_c</span></tt> specializations as being equivalent to the more
  130. verbose versions above that use <tt class="literal"><span class="pre">mpl::vector</span></tt>.</p>
  131. <p>If we want, we can also define a few composite dimensions:</p>
  132. <pre class="literal-block">
  133. // base dimension: m l t ...
  134. typedef mpl::vector_c&lt;int,0,1,-1,0,0,0,0&gt; velocity; // l/t
  135. typedef mpl::vector_c&lt;int,0,1,-2,0,0,0,0&gt; acceleration; // l/(t<sup>2</sup>)
  136. typedef mpl::vector_c&lt;int,1,1,-1,0,0,0,0&gt; momentum; // ml/t
  137. typedef mpl::vector_c&lt;int,1,1,-2,0,0,0,0&gt; force; // ml/(t<sup>2</sup>)
  138. </pre>
  139. <p>And, incidentally, the dimensions of scalars (like pi) can be
  140. described as:</p>
  141. <pre class="literal-block">
  142. typedef mpl::vector_c&lt;int,0,0,0,0,0,0,0&gt; scalar;
  143. </pre>
  144. <!-- @stack[0].replace('hpp>', 'hpp>\nnamespace {')
  145. stack[0].append('}')
  146. compile('all', pop = None) -->
  147. </div>
  148. <div class="footer-separator"></div>
  149. <table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./dimensional-analysis.html" class="navigation-link">Prev</a>&nbsp;<a href="./representing-quantities.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group">Back&nbsp;<a href="./representing-quantities.html" class="navigation-link">Along</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./dimensional-analysis.html" class="navigation-link">Up</a>&nbsp;<a href="../index.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./tutorial_toc.html" class="navigation-link">Full TOC</a></span></td>
  150. </tr></table></body>
  151. </html>