ibeta_inv_data.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // (C) Copyright John Maddock 2006.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <boost/math/special_functions/beta.hpp>
  6. #include <boost/math/constants/constants.hpp>
  7. #include <boost/lexical_cast.hpp>
  8. #include <fstream>
  9. #include <boost/math/tools/test_data.hpp>
  10. #include <boost/random.hpp>
  11. #include "mp_t.hpp"
  12. using namespace boost::math::tools;
  13. //
  14. // Force trunctation to float precision of input values:
  15. // we must ensure that the input values are exactly representable
  16. // in whatever type we are testing, or the output values will all
  17. // be thrown off:
  18. //
  19. float external_f;
  20. float force_truncate(const float* f)
  21. {
  22. external_f = *f;
  23. return external_f;
  24. }
  25. float truncate_to_float(mp_t r)
  26. {
  27. float f = boost::math::tools::real_cast<float>(r);
  28. return force_truncate(&f);
  29. }
  30. boost::mt19937 rnd;
  31. boost::uniform_real<float> ur_a(1.0F, 5.0F);
  32. boost::variate_generator<boost::mt19937, boost::uniform_real<float> > gen(rnd, ur_a);
  33. boost::uniform_real<float> ur_a2(0.0F, 100.0F);
  34. boost::variate_generator<boost::mt19937, boost::uniform_real<float> > gen2(rnd, ur_a2);
  35. struct ibeta_inv_data_generator
  36. {
  37. boost::math::tuple<mp_t, mp_t, mp_t, mp_t, mp_t> operator()(mp_t ap, mp_t bp, mp_t x_)
  38. {
  39. float a = truncate_to_float(real_cast<float>(gen() * pow(mp_t(10), ap)));
  40. float b = truncate_to_float(real_cast<float>(gen() * pow(mp_t(10), bp)));
  41. float x = truncate_to_float(real_cast<float>(x_));
  42. std::cout << a << " " << b << " " << x << std::flush;
  43. mp_t inv = boost::math::ibeta_inv(mp_t(a), mp_t(b), mp_t(x));
  44. std::cout << " " << inv << std::flush;
  45. mp_t invc = boost::math::ibetac_inv(mp_t(a), mp_t(b), mp_t(x));
  46. std::cout << " " << invc << std::endl;
  47. return boost::math::make_tuple(a, b, x, inv, invc);
  48. }
  49. };
  50. int main(int argc, char*argv [])
  51. {
  52. bool cont;
  53. std::string line;
  54. parameter_info<mp_t> arg1, arg2, arg3;
  55. test_data<mp_t> data;
  56. std::cout << "Welcome.\n"
  57. "This program will generate spot tests for the inverse incomplete beta function:\n"
  58. " ibeta_inv(a, p) and ibetac_inv(a, q)\n\n";
  59. arg1 = make_periodic_param(mp_t(-5), mp_t(6), 11);
  60. arg2 = make_periodic_param(mp_t(-5), mp_t(6), 11);
  61. arg3 = make_random_param(mp_t(0.0001), mp_t(1), 10);
  62. arg1.type |= dummy_param;
  63. arg2.type |= dummy_param;
  64. arg3.type |= dummy_param;
  65. data.insert(ibeta_inv_data_generator(), arg1, arg2, arg3);
  66. line = "ibeta_inv_data.ipp";
  67. std::ofstream ofs(line.c_str());
  68. ofs << std::scientific << std::setprecision(40);
  69. write_code(ofs, data, "ibeta_inv_data");
  70. return 0;
  71. }