examples.htm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/html4/loose.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="Content-Language" content="en-us">
  6. <meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
  7. <link rel="stylesheet" type="text/css" href="../../../../boost.css">
  8. <title>Tests and Examples</title>
  9. </head>
  10. <body lang="en">
  11. <h1>Tests and Examples</h1>
  12. <h2>A first example</h2>
  13. <p>This example shows how to design a function which takes a polynomial and
  14. a value and returns the sign of this polynomial at this point. This
  15. function is a filter: if the answer is not guaranteed, the functions says
  16. so. The reason of using a filter rather than a simple evaluation function
  17. is: computations with floating-point numbers will incur approximations and
  18. it can be enough to change the sign of the polynomial. So, in order to
  19. validate the result, the function will use interval arithmetic.</p>
  20. <p>The first step is the inclusion of the appropriate headers. Because the
  21. function will handle floating-point bounds, the easiest solution is:</p>
  22. <pre>
  23. #include &lt;boost/numeric/interval.hpp&gt;
  24. </pre>
  25. <p>Now, let's begin the function. The polynomial is given by the array of
  26. its coefficients and its size (strictly greater to its degree). In order to
  27. simplify the code, two namespaces of the library are included.</p>
  28. <pre>
  29. int sign_polynomial(double x, double P[], int sz) {
  30. using namespace boost::numeric;
  31. using namespace interval_lib;
  32. </pre>
  33. <p>Then we can define the interval type. Since no special behavior is
  34. required, the default policies are enough:</p>
  35. <pre>
  36. typedef interval&lt;double&gt; I;
  37. </pre>
  38. <p>For the evaluation, let's just use the Horner scheme with interval
  39. arithmetic. The library overloads all the arithmetic operators and provides
  40. mixed operations, so the only difference between the code with and without
  41. interval arithmetic lies in the type of the iterated value
  42. <code>y</code>:</p>
  43. <pre>
  44. I y = P[sz - 1];
  45. for(int i = sz - 2; i &gt;= 0; i--)
  46. y = y * x + P[i];
  47. </pre>
  48. <p>The last step is the computation of the sign of <code>y</code>. It is
  49. done by choosing an appropriate comparison scheme and then doing the
  50. comparison with the usual operators:</p>
  51. <pre>
  52. using namespace compare::certain;
  53. if (y &gt; 0.) return 1;
  54. if (y &lt; 0.) return -1;
  55. return 0;
  56. }
  57. </pre>
  58. <p>The answer <code>0</code> does not mean the polynomial is zero at this
  59. point. It only means the answer is not known since <code>y</code> contains
  60. zero and thus does not have a precise sign.</p>
  61. <p>Now we have the expected function. However, due to the poor
  62. implementations of floating-point rounding in most of the processors, it
  63. can be useful to say to optimize the code; or rather, to let the library
  64. optimize it. The main condition for this optimization is that the interval
  65. code should not be mixed with floating-point code. In this example, it is
  66. the case, since all the operations done in the functions involve the
  67. library. So the code can be rewritten:</p>
  68. <pre>
  69. int sign_polynomial(double x, double P[], int sz) {
  70. using namespace boost::numeric;
  71. using namespace interval_lib;
  72. typedef interval&lt;double&gt; I_aux;
  73. I_aux::traits_type::rounding rnd;
  74. typedef unprotect&lt;I_aux&gt;::type I;
  75. I y = P[sz - 1];
  76. for(int i = sz - 2; i &gt;= 0; i--)
  77. y = y * x + P[i];
  78. using namespace compare::certain;
  79. if (y &gt; 0.) return 1;
  80. if (y &lt; 0.) return -1;
  81. return 0;
  82. }
  83. </pre>
  84. <p>The difference between this code and the previous is the use of another
  85. interval type. This new type <code>I</code> indicates to the library that
  86. all the computations can be done without caring for the rounding mode. And
  87. because of that, it is up to the function to care about it: a rounding
  88. object need to be alive whenever the optimized type is used.</p>
  89. <h2>Other tests and examples</h2>
  90. <p>In <code>libs/numeric/interval/test/</code> and
  91. <code>libs/numeric/interval/examples/</code> are some test and example
  92. programs.. The examples illustrate a few uses of intervals. For a general
  93. description and considerations on using this library, and some potential
  94. domains of application, please read this <a href=
  95. "guide.htm">mini-guide</a>.</p>
  96. <h3>Tests</h3>
  97. <p>The test programs are as follows. Please note that they require the use
  98. of the Boost.test library and can be automatically tested by using
  99. <code>bjam</code> (except for interval_test.cpp).</p>
  100. <p><b>add.cpp</b> tests if the additive and subtractive operators and the
  101. respective _std and _opp rounding functions are correctly implemented. It
  102. is done by using symbolic expressions as a base type.</p>
  103. <p><b>cmp.cpp</b>, <b>cmp_lex.cpp</b>, <b>cmp_set.cpp</b>, and
  104. <b>cmp_tribool.cpp</b> test if the operators <code>&lt;</code>
  105. <code>&gt;</code> <code>&lt;=</code> <code>&gt;=</code> <code>==</code>
  106. <code>!=</code> behave correctly for the default, lexicographic, set, and
  107. tristate comparisons. <b>cmp_exp.cpp</b> tests the explicit comparison
  108. functions <code>cer..</code> and <code>pos..</code> behave correctly.
  109. <b>cmp_exn.cpp</b> tests if the various policies correctly detect
  110. exceptional cases. All these tests use some simple intervals ([1,2] and
  111. [3,4], [1,3] and [2,4], [1,2] and [2,3], etc).</p>
  112. <p><b>det.cpp</b> tests if the <code>_std</code> and <code>_opp</code>
  113. versions in protected and unprotected mode produce the same result when
  114. Gauss scheme is used on an unstable matrix (in order to exercise rounding).
  115. The tests are done for <code>interval&lt;float&gt;</code> and
  116. <code>interval&lt;double&gt;</code>.</p>
  117. <p><b>fmod.cpp</b> defines a minimalistic version of
  118. <code>interval&lt;int&gt;</code> and uses it in order to test
  119. <code>fmod</code> on some specific interval values.</p>
  120. <p><b>mul.cpp</b> exercises the multiplication, the finite division, the
  121. square and the square root with some integer intervals leading to exact
  122. results.</p>
  123. <p><b>pi.cpp</b> tests if the interval value of &pi; (for <code>int</code>,
  124. <code>float</code> and <code>double</code> base types) contains the number
  125. &pi; (defined with 21 decimal digits) and if it is a subset of
  126. [&pi;&plusmn;1ulp] (in order to ensure some precision).</p>
  127. <p><b>pow.cpp</b> tests if the <code>pow</code> function behaves correctly
  128. on some simple test cases.</p>
  129. <p><b>test_float.cpp</b> exercises the arithmetic operations of the library
  130. for floating point base types.</p>
  131. <p><b>interval_test.cpp</b> tests if the interval library respects the
  132. inclusion property of interval arithmetic by computing some functions and
  133. operations for both <code>double</code> and
  134. <code>interval&lt;double&gt;</code>.</p>
  135. <h2>Examples</h2>
  136. <p><b>filter.cpp</b> contains filters for computational geometry able to
  137. find the sign of a determinant. This example is inspired by the article
  138. <em>Interval arithmetic yields efficient dynamic filters for computational
  139. geometry</em> by Br&ouml;nnimann, Burnikel and Pion, 2001.</p>
  140. <p><b>findroot_demo.cpp</b> finds zeros of some functions by using
  141. dichotomy and even produces gnuplot data for one of them. The processor has
  142. to correctly handle elementary functions for this example to properly
  143. work.</p>
  144. <p><b>horner.cpp</b> is a really basic example of unprotecting the interval
  145. operations for a whole function (which computes the value of a polynomial
  146. by using Horner scheme).</p>
  147. <p><b>io.cpp</b> shows some stream input and output operators for intervals
  148. .The wide variety of possibilities explains why the library do not
  149. implement i/o operators and they are left to the user.</p>
  150. <p><b>newton-raphson.cpp</b> is an implementation of a specialized version
  151. of Newton-Raphson algorithm for finding the zeros of a function knowing its
  152. derivative. It exercises unprotecting, full division, some set operations
  153. and empty intervals.</p>
  154. <p><b>transc.cpp</b> implements the transcendental part of the rounding
  155. policy for <code>double</code> by using an external library (the MPFR
  156. subset of GMP in this case).</p>
  157. <hr>
  158. <p><a href="http://validator.w3.org/check?uri=referer"><img border="0" src=
  159. "../../../../doc/images/valid-html401.png" alt="Valid HTML 4.01 Transitional"
  160. height="31" width="88"></a></p>
  161. <p>Revised
  162. <!--webbot bot="Timestamp" s-type="EDITED" s-format="%Y-%m-%d" startspan -->2006-12-24<!--webbot bot="Timestamp" endspan i-checksum="12172" --></p>
  163. <p><i>Copyright &copy; 2002 Guillaume Melquiond, Sylvain Pion, Herv&eacute;
  164. Br&ouml;nnimann, Polytechnic University<br>
  165. Copyright &copy; 2003 Guillaume Melquiond</i></p>
  166. <p><i>Distributed under the Boost Software License, Version 1.0. (See
  167. accompanying file <a href="../../../../LICENSE_1_0.txt">LICENSE_1_0.txt</a>
  168. or copy at <a href=
  169. "http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</i></p>
  170. </body>
  171. </html>