test_uniform_on_sphere_distribution.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* test_uniform_on_sphere_distribution.cpp
  2. *
  3. * Copyright Steven Watanabe 2011
  4. * Distributed under the Boost Software License, Version 1.0. (See
  5. * accompanying file LICENSE_1_0.txt or copy at
  6. * http://www.boost.org/LICENSE_1_0.txt)
  7. *
  8. * $Id$
  9. *
  10. */
  11. #include <boost/random/uniform_on_sphere.hpp>
  12. #include <boost/assign/list_of.hpp>
  13. #include <limits>
  14. #define BOOST_RANDOM_DISTRIBUTION boost::random::uniform_on_sphere<>
  15. #define BOOST_RANDOM_ARG1 dim
  16. #define BOOST_RANDOM_ARG1_DEFAULT 2
  17. #define BOOST_RANDOM_ARG1_VALUE 3
  18. std::vector<double> min0 = boost::assign::list_of(-1.0)(0.0);
  19. std::vector<double> max0 = boost::assign::list_of(1.0)(0.0);
  20. std::vector<double> min1 = boost::assign::list_of(-1.0)(0.0)(0.0);
  21. std::vector<double> max1 = boost::assign::list_of(1.0)(0.0)(0.0);
  22. #define BOOST_RANDOM_DIST0_MIN min0
  23. #define BOOST_RANDOM_DIST0_MAX max0
  24. #define BOOST_RANDOM_DIST1_MIN min1
  25. #define BOOST_RANDOM_DIST1_MAX max1
  26. #define BOOST_RANDOM_TEST1_PARAMS (0)
  27. #define BOOST_RANDOM_TEST1_MIN std::vector<double>()
  28. #define BOOST_RANDOM_TEST1_MAX std::vector<double>()
  29. #define BOOST_RANDOM_TEST2_PARAMS
  30. #define BOOST_RANDOM_TEST2_MIN min0
  31. #define BOOST_RANDOM_TEST2_MAX max0
  32. #include <boost/test/test_tools.hpp>
  33. BOOST_TEST_DONT_PRINT_LOG_VALUE( std::vector<double> )
  34. #include "test_distribution.ipp"
  35. #include <boost/math/special_functions/fpclassify.hpp>
  36. struct generate_zeros {
  37. public:
  38. generate_zeros() : i(0) {}
  39. typedef unsigned result_type;
  40. static unsigned (min)() { return 0u; }
  41. static unsigned (max)() { return boost::random::minstd_rand0::max(); }
  42. unsigned operator()() {
  43. static unsigned data[] = { 0, 0, 0, 0, 0, 0 };
  44. if(i < 6) {
  45. return data[i++];
  46. } else {
  47. return gen();
  48. }
  49. }
  50. private:
  51. int i;
  52. boost::random::minstd_rand0 gen;
  53. };
  54. BOOST_AUTO_TEST_CASE(test_zeros) {
  55. generate_zeros gen;
  56. boost::random::uniform_on_sphere<> dist(2);
  57. std::vector<double> val = dist(gen);
  58. BOOST_CHECK(!(boost::math::isnan)(val[0]));
  59. }
  60. BOOST_AUTO_TEST_CASE(test_valid_output) {
  61. boost::random::minstd_rand0 gen;
  62. for(int n = 0; n < 10; ++n) {
  63. boost::random::uniform_on_sphere<> dist(n);
  64. std::vector<double> result = dist(gen);
  65. BOOST_TEST(result.size() == static_cast<std::size_t>(n));
  66. if(n > 0) {
  67. double sum_sq = 0;
  68. for(std::size_t j = 0; j < result.size(); ++j) {
  69. sum_sq += result[j] * result[j];
  70. }
  71. BOOST_CHECK_CLOSE_FRACTION(sum_sq, 1.0, 1e-5);
  72. }
  73. }
  74. }