hyp_1f1_big_data.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #define BOOST_MATH_MAX_SERIES_ITERATION_POLICY 10000000
  6. #include <boost/math/special_functions/hypergeometric_1f1.hpp>
  7. #include <boost/math/constants/constants.hpp>
  8. #include <boost/lexical_cast.hpp>
  9. #include <fstream>
  10. #include <map>
  11. #include <boost/math/tools/test_data.hpp>
  12. #include <boost/random.hpp>
  13. #define BOOST_MATH_USE_MPFR
  14. #include "mp_t.hpp"
  15. #include <boost/multiprecision/mpfi.hpp>
  16. using namespace boost::math::tools;
  17. using namespace boost::math;
  18. using namespace std;
  19. using namespace boost::multiprecision;
  20. typedef mpfi_float_1000 mpfi_type;
  21. mp_t hypergeometric_1f1_generic_series(mp_t a_, mp_t b_, mp_t z_)
  22. {
  23. using namespace boost::math::tools;
  24. using namespace boost::math;
  25. using namespace std;
  26. using namespace boost::multiprecision;
  27. mpfi_type a(a_), b(b_), z(z_), sum(0), term(1), diff, term0(0);
  28. unsigned n = 0;
  29. bool cont = true;
  30. unsigned max_n;
  31. if (b < 0)
  32. max_n = itrunc(-b) + 10000;
  33. else
  34. max_n = 10000000;
  35. mpfi_type overflow_limit("1.189731495357231765e+4900"); // a bit less than LDBL_MAX for extended long doubles.
  36. do
  37. {
  38. sum += term;
  39. term *= (((a + n) / ((b + n) * (n + 1))) * z);
  40. ++n;
  41. diff = fabs(term / sum);
  42. if (n > max_n)
  43. {
  44. std::cout << "Aborting series evaluation due to too many iterations...\n";
  45. throw evaluation_error("");
  46. }
  47. if (fabs(upper(sum)) > overflow_limit)
  48. {
  49. std::cout << "Aborting series evaluation due to over large sum...\n";
  50. throw evaluation_error("");
  51. }
  52. cont = (fabs(upper(diff)) > 1e-40) || (b + n < 0) || (fabs(term0) < fabs(term));
  53. term0 = term;
  54. //std::cout << upper(term) << " " << upper(sum) << " " << upper(diff) << " " << cont << std::endl;
  55. } while (cont);
  56. mp_t r = mp_t(width(sum) / median(sum));
  57. if (fabs(r) > 1e-40)
  58. {
  59. std::cout << "Aborting to to error in result of " << r << std::endl;
  60. throw evaluation_error("");
  61. }
  62. std::cout << "Found error in sum was " << r << std::endl;
  63. return mp_t(median(sum));
  64. }
  65. struct hypergeometric_1f1_gen
  66. {
  67. mp_t operator()(mp_t a1, mp_t a2, mp_t z)
  68. {
  69. mp_t result;
  70. try {
  71. result = hypergeometric_1f1_generic_series(a1, a2, z);
  72. std::cout << a1 << " " << a2 << " " << z << " " << result << std::endl;
  73. }
  74. catch (...)
  75. {
  76. throw std::domain_error("");
  77. }
  78. if (fabs(result) > (std::numeric_limits<double>::max)())
  79. {
  80. std::cout << "Rejecting over large value\n";
  81. throw std::domain_error("");
  82. }
  83. return result;
  84. }
  85. };
  86. int main(int, char* [])
  87. {
  88. parameter_info<mp_t> arg1, arg2, arg3;
  89. test_data<mp_t> data;
  90. std::cout << "Welcome.\n"
  91. "This program will generate spot tests for 1F1 (Yeh!!):\n";
  92. std::string line;
  93. //bool cont;
  94. std::vector<mp_t> v;
  95. random_ns::mt19937 rnd;
  96. random_ns::uniform_real_distribution<float> ur_a(0, 1);
  97. mp_t p = ur_a(rnd);
  98. p *= 1e6;
  99. v.push_back(p);
  100. v.push_back(-p);
  101. p = ur_a(rnd);
  102. p *= 1e5;
  103. v.push_back(p);
  104. v.push_back(-p);
  105. p = ur_a(rnd);
  106. p *= 1e4;
  107. v.push_back(p);
  108. v.push_back(-p);
  109. p = ur_a(rnd);
  110. p *= 1e3;
  111. v.push_back(p);
  112. v.push_back(-p);
  113. p = ur_a(rnd);
  114. p *= 1e2;
  115. v.push_back(p);
  116. v.push_back(-p);
  117. p = ur_a(rnd);
  118. p *= 1e-5;
  119. v.push_back(p);
  120. v.push_back(-p);
  121. p = ur_a(rnd);
  122. p *= 1e-12;
  123. v.push_back(p);
  124. v.push_back(-p);
  125. p = ur_a(rnd);
  126. p *= 1e-30;
  127. v.push_back(p);
  128. v.push_back(-p);
  129. for (unsigned i = 0; i < v.size(); ++i)
  130. {
  131. for (unsigned j = 0; j < v.size(); ++j)
  132. {
  133. for (unsigned k = 0; k < v.size(); ++k)
  134. {
  135. std::cout << i << " " << j << " " << k << std::endl;
  136. std::cout << v[i] << " " << (v[j] * 3) / 2 << " " << (v[j] * 5) / 4 << std::endl;
  137. arg1 = make_single_param(v[i]);
  138. arg2 = make_single_param(mp_t((v[j] * 3) / 2));
  139. arg3 = make_single_param(mp_t((v[k] * 5) / 4));
  140. data.insert(hypergeometric_1f1_gen(), arg1, arg2, arg3);
  141. }
  142. }
  143. }
  144. std::cout << "Enter name of test data file [default=hypergeometric_1f1.ipp]";
  145. std::getline(std::cin, line);
  146. boost::algorithm::trim(line);
  147. if(line == "")
  148. line = "hypergeometric_1f1.ipp";
  149. std::ofstream ofs(line.c_str());
  150. ofs << std::scientific << std::setprecision(40);
  151. write_code(ofs, data, line.c_str());
  152. return 0;
  153. }