/Src/Dependencies/Boost/libs/math/doc/sf_and_dist/relative_error.qbk

http://hadesmem.googlecode.com/ · text · 134 lines · 105 code · 29 blank · 0 comment · 0 complexity · 8f6a12a05369705fed741daf35d3dd17 MD5 · raw file

  1. [section:error_test Relative Error and Testing]
  2. [h4 Synopsis]
  3. ``
  4. #include <boost/math/tools/test.hpp>
  5. ``
  6. template <class T>
  7. T relative_error(T a, T b);
  8. template <class A, class F1, class F2>
  9. test_result<see-below> test(const A& a, F1 test_func, F2 expect_func);
  10. [h4 Description]
  11. template <class T>
  12. T relative_error(T a, T v);
  13. Returns the relative error between /a/ and /v/ using the usual formula:
  14. [equation error1]
  15. In addition the value returned is zero if:
  16. * Both /a/ and /v/ are infinite.
  17. * Both /a/ and /v/ are denormalised numbers or zero.
  18. Otherwise if only one of /a/ and /v/ is zero then the value returned is 1.
  19. template <class A, class F1, class F2>
  20. test_result<see-below> test(const A& a, F1 test_func, F2 expect_func);
  21. This function is used for testing a function against tabulated test data.
  22. The return type contains statistical data on the relative errors (max, mean,
  23. variance, and the number of test cases etc), as well as the row of test data that
  24. caused the largest relative error. Public members of type test_result are:
  25. [variablelist
  26. [[`unsigned worst()const;`][Returns the row at which the worst error occurred.]]
  27. [[`T min()const;`][Returns the smallest relative error found.]]
  28. [[`T max()const;`][Returns the largest relative error found.]]
  29. [[`T mean()const;`][Returns the mean error found.]]
  30. [[`boost::uintmax_t count()const;`][Returns the number of test cases.]]
  31. [[`T variance()const;`][Returns the variance of the errors found.]]
  32. [[`T variance1()const;`][Returns the unbiased variance of the errors found.]]
  33. [[`T rms()const`][Returns the Root Mean Square, or quadratic mean of the errors.]]
  34. [[`test_result& operator+=(const test_result& t)`][Combines two test_result's into
  35. a single result.]]
  36. ]
  37. The template parameter of test_result, is the same type as the values in the two
  38. dimensional array passed to function /test/, roughly that's
  39. `A::value_type::value_type`.
  40. Parameter /a/ is a matrix of test data: and must be a standard library Sequence type,
  41. that contains another Sequence type:
  42. typically it will be a two dimensional instance of
  43. [^boost::array]. Each row
  44. of /a/ should contain all the parameters that are passed to the function
  45. under test as well as the expected result.
  46. Parameter /test_func/ is the function under test, it is invoked with each row
  47. of test data in /a/. Typically type F1 is created with Boost.Lambda: see
  48. the example below.
  49. Parameter /expect_func/ is a functor that extracts the expected result
  50. from a row of test data in /a/. Typically type F2 is created with Boost.Lambda: see
  51. the example below.
  52. If the function under test returns a non-finite value when a finite result is
  53. expected, or if a gross error is found, then a message is sent to `std::cerr`,
  54. and a call to BOOST_ERROR() made (which means that including this header requires
  55. you use Boost.Test). This is mainly a debugging/development aid
  56. (and a good place for a breakpoint).
  57. [h4 Example]
  58. Suppose we want to test the tgamma and lgamma functions, we can create a
  59. two dimensional matrix of test data, each row is one test case, and contains
  60. three elements: the input value, and the expected results for the tgamma and
  61. lgamma functions respectively.
  62. static const boost::array<boost::array<TestType, 3>, NumberOfTests>
  63. factorials = {
  64. /* big array of test data goes here */
  65. };
  66. Now we can invoke the test function to test tgamma:
  67. using namespace boost::math::tools;
  68. using namespace boost::lambda;
  69. // get a pointer to the function under test:
  70. TestType (*funcp)(TestType) = boost::math::tgamma;
  71. // declare something to hold the result:
  72. test_result<TestType> result;
  73. //
  74. // and test tgamma against data:
  75. //
  76. result = test(
  77. factorials,
  78. bind(funcp, ret<TestType>(_1[0])), // calls tgamma with factorials[row][0]
  79. ret<TestType>(_1[1]) // extracts the expected result from factorials[row][1]
  80. );
  81. //
  82. // Print out some results:
  83. //
  84. std::cout << "The Mean was " << result.mean() << std::endl;
  85. std::cout << "The worst error was " << (result.max)() << std::endl;
  86. std::cout << "The worst error was at row " << result.worst_case() << std::endl;
  87. //
  88. // same again with lgamma this time:
  89. //
  90. funcp = boost::math::lgamma;
  91. result = test(
  92. factorials,
  93. bind(funcp, ret<TestType>(_1[0])), // calls tgamma with factorials[row][0]
  94. ret<TestType>(_1[2]) // extracts the expected result from factorials[row][2]
  95. );
  96. //
  97. // etc ...
  98. //
  99. [endsect][/section:error_test Relative Error and Testing]
  100. [/
  101. Copyright 2006 John Maddock and Paul A. Bristow.
  102. Distributed under the Boost Software License, Version 1.0.
  103. (See accompanying file LICENSE_1_0.txt or copy at
  104. http://www.boost.org/LICENSE_1_0.txt).
  105. ]