test_legendre.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // Copyright John Maddock 2006.
  2. // Copyright Paul A. Bristow 2007, 2009
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifdef _MSC_VER
  7. # pragma warning (disable : 4756) // overflow in constant arithmetic
  8. #endif
  9. #include <boost/math/concepts/real_concept.hpp>
  10. #define BOOST_TEST_MAIN
  11. #include <boost/test/unit_test.hpp>
  12. #include <boost/test/tools/floating_point_comparison.hpp>
  13. #include <boost/math/special_functions/math_fwd.hpp>
  14. #include <boost/math/special_functions/legendre.hpp>
  15. #include <boost/math/constants/constants.hpp>
  16. #include <boost/multiprecision/cpp_bin_float.hpp>
  17. #include <boost/array.hpp>
  18. #include "functor.hpp"
  19. #include "handle_test_result.hpp"
  20. #include "table_type.hpp"
  21. #ifndef SC_
  22. #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
  23. #endif
  24. template <class Real, class T>
  25. void do_test_legendre_p(const T& data, const char* type_name, const char* test_name)
  26. {
  27. typedef Real value_type;
  28. typedef value_type (*pg)(int, value_type);
  29. pg funcp;
  30. #if !(defined(ERROR_REPORTING_MODE) && !defined(LEGENDRE_P_FUNCTION_TO_TEST))
  31. #ifdef LEGENDRE_P_FUNCTION_TO_TEST
  32. funcp = LEGENDRE_P_FUNCTION_TO_TEST;
  33. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  34. funcp = boost::math::legendre_p<value_type>;
  35. #else
  36. funcp = boost::math::legendre_p;
  37. #endif
  38. boost::math::tools::test_result<value_type> result;
  39. std::cout << "Testing " << test_name << " with type " << type_name
  40. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  41. //
  42. // test legendre_p against data:
  43. //
  44. result = boost::math::tools::test_hetero<Real>(
  45. data,
  46. bind_func_int1<Real>(funcp, 0, 1),
  47. extract_result<Real>(2));
  48. handle_test_result(result, data[result.worst()], result.worst(), type_name, "legendre_p", test_name);
  49. #endif
  50. typedef value_type (*pg2)(unsigned, value_type);
  51. #if !(defined(ERROR_REPORTING_MODE) && !defined(LEGENDRE_Q_FUNCTION_TO_TEST))
  52. #ifdef LEGENDRE_Q_FUNCTION_TO_TEST
  53. pg2 funcp2 = LEGENDRE_Q_FUNCTION_TO_TEST;
  54. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  55. pg2 funcp2 = boost::math::legendre_q<value_type>;
  56. #else
  57. pg2 funcp2 = boost::math::legendre_q;
  58. #endif
  59. //
  60. // test legendre_q against data:
  61. //
  62. result = boost::math::tools::test_hetero<Real>(
  63. data,
  64. bind_func_int1<Real>(funcp2, 0, 1),
  65. extract_result<Real>(3));
  66. handle_test_result(result, data[result.worst()], result.worst(), type_name, "legendre_q", test_name);
  67. std::cout << std::endl;
  68. #endif
  69. }
  70. template <class Real, class T>
  71. void do_test_assoc_legendre_p(const T& data, const char* type_name, const char* test_name)
  72. {
  73. #if !(defined(ERROR_REPORTING_MODE) && !defined(LEGENDRE_PA_FUNCTION_TO_TEST))
  74. typedef Real value_type;
  75. typedef value_type (*pg)(int, int, value_type);
  76. #ifdef LEGENDRE_PA_FUNCTION_TO_TEST
  77. pg funcp = LEGENDRE_PA_FUNCTION_TO_TEST;
  78. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  79. pg funcp = boost::math::legendre_p<value_type>;
  80. #else
  81. pg funcp = boost::math::legendre_p;
  82. #endif
  83. boost::math::tools::test_result<value_type> result;
  84. std::cout << "Testing " << test_name << " with type " << type_name
  85. << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  86. //
  87. // test legendre_p against data:
  88. //
  89. result = boost::math::tools::test_hetero<Real>(
  90. data,
  91. bind_func_int2<Real>(funcp, 0, 1, 2),
  92. extract_result<Real>(3));
  93. handle_test_result(result, data[result.worst()], result.worst(), type_name, "legendre_p (associated)", test_name);
  94. std::cout << std::endl;
  95. #endif
  96. }
  97. template <class T>
  98. void test_legendre_p(T, const char* name)
  99. {
  100. //
  101. // The actual test data is rather verbose, so it's in a separate file
  102. //
  103. // The contents are as follows, each row of data contains
  104. // three items, input value a, input value b and erf(a, b):
  105. //
  106. # include "legendre_p.ipp"
  107. do_test_legendre_p<T>(legendre_p, name, "Legendre Polynomials: Small Values");
  108. # include "legendre_p_large.ipp"
  109. do_test_legendre_p<T>(legendre_p_large, name, "Legendre Polynomials: Large Values");
  110. # include "assoc_legendre_p.ipp"
  111. do_test_assoc_legendre_p<T>(assoc_legendre_p, name, "Associated Legendre Polynomials: Small Values");
  112. }
  113. template <class T>
  114. void test_spots(T, const char* t)
  115. {
  116. std::cout << "Testing basic sanity checks for type " << t << std::endl;
  117. //
  118. // basic sanity checks, tolerance is 100 epsilon:
  119. //
  120. T tolerance = boost::math::tools::epsilon<T>() * 100;
  121. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(1, static_cast<T>(0.5L)), static_cast<T>(0.5L), tolerance);
  122. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-1, static_cast<T>(0.5L)), static_cast<T>(1L), tolerance);
  123. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(4, static_cast<T>(0.5L)), static_cast<T>(-0.2890625000000000000000000000000000000000L), tolerance);
  124. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-4, static_cast<T>(0.5L)), static_cast<T>(-0.4375000000000000000000000000000000000000L), tolerance);
  125. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(7, static_cast<T>(0.5L)), static_cast<T>(0.2231445312500000000000000000000000000000L), tolerance);
  126. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-7, static_cast<T>(0.5L)), static_cast<T>(0.3232421875000000000000000000000000000000L), tolerance);
  127. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(40, static_cast<T>(0.5L)), static_cast<T>(-0.09542943523261546936538467572384923220258L), tolerance);
  128. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-40, static_cast<T>(0.5L)), static_cast<T>(-0.1316993126940266257030910566308990611306L), tolerance);
  129. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(4, 2, static_cast<T>(0.5L)), static_cast<T>(4.218750000000000000000000000000000000000L), tolerance);
  130. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-4, 2, static_cast<T>(0.5L)), static_cast<T>(5.625000000000000000000000000000000000000L), tolerance);
  131. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(7, 5, static_cast<T>(0.5L)), static_cast<T>(-5696.789530152175143607977274672800795328L), tolerance);
  132. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-7, 4, static_cast<T>(0.5L)), static_cast<T>(465.1171875000000000000000000000000000000L), tolerance);
  133. if(std::numeric_limits<T>::max_exponent > std::numeric_limits<float>::max_exponent)
  134. {
  135. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(40, 30, static_cast<T>(0.5L)), static_cast<T>(-7.855722083232252643913331343916012143461e45L), tolerance);
  136. }
  137. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-40, 20, static_cast<T>(0.5L)), static_cast<T>(4.966634149702370788037088925152355134665e30L), tolerance);
  138. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(4, 2, static_cast<T>(-0.5L)), static_cast<T>(4.218750000000000000000000000000000000000L), tolerance);
  139. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-4, 2, static_cast<T>(-0.5L)), static_cast<T>(-5.625000000000000000000000000000000000000L), tolerance);
  140. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(7, 5, static_cast<T>(-0.5L)), static_cast<T>(-5696.789530152175143607977274672800795328L), tolerance);
  141. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-7, 4, static_cast<T>(-0.5L)), static_cast<T>(465.1171875000000000000000000000000000000L), tolerance);
  142. if(std::numeric_limits<T>::max_exponent > std::numeric_limits<float>::max_exponent)
  143. {
  144. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(40, 30, static_cast<T>(-0.5L)), static_cast<T>(-7.855722083232252643913331343916012143461e45L), tolerance);
  145. }
  146. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-40, 20, static_cast<T>(-0.5L)), static_cast<T>(-4.966634149702370788037088925152355134665e30L), tolerance);
  147. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(4, -2, static_cast<T>(0.5L)), static_cast<T>(0.01171875000000000000000000000000000000000L), tolerance);
  148. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-4, -2, static_cast<T>(0.5L)), static_cast<T>(0.04687500000000000000000000000000000000000L), tolerance);
  149. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(7, -5, static_cast<T>(0.5L)), static_cast<T>(0.00002378609812640364935569308025139290054701L), tolerance);
  150. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-7, -4, static_cast<T>(0.5L)), static_cast<T>(0.0002563476562500000000000000000000000000000L), tolerance);
  151. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(40, -30, static_cast<T>(0.5L)), static_cast<T>(-2.379819988646847616996471299410611801239e-48L), tolerance);
  152. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p(-40, -20, static_cast<T>(0.5L)), static_cast<T>(4.356454600748202401657099008867502679122e-33L), tolerance);
  153. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_q(1, static_cast<T>(0.5L)), static_cast<T>(-0.7253469278329725771511886907693685738381L), tolerance);
  154. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_q(4, static_cast<T>(0.5L)), static_cast<T>(0.4401745259867706044988642951843745400835L), tolerance);
  155. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_q(7, static_cast<T>(0.5L)), static_cast<T>(-0.3439152932669753451878700644212067616780L), tolerance);
  156. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_q(40, static_cast<T>(0.5L)), static_cast<T>(0.1493671665503550095010454949479907886011L), tolerance);
  157. }
  158. template <class T>
  159. void test_legendre_p_prime()
  160. {
  161. T tolerance = 100*boost::math::tools::epsilon<T>();
  162. T x = -1;
  163. while (x <= 1)
  164. {
  165. // P_0'(x) = 0
  166. BOOST_CHECK_SMALL(::boost::math::legendre_p_prime<T>(0, x), tolerance);
  167. // Reflection formula for P_{-1}(x) = P_{0}(x):
  168. BOOST_CHECK_SMALL(::boost::math::legendre_p_prime<T>(-1, x), tolerance);
  169. // P_1(x) = x, so P_1'(x) = 1:
  170. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(1, x), static_cast<T>(1), tolerance);
  171. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(-2, x), static_cast<T>(1), tolerance);
  172. // P_2(x) = 3x^2/2 + k => P_2'(x) = 3x
  173. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(2, x), 3*x, tolerance);
  174. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(-3, x), 3*x, tolerance);
  175. // P_3(x) = (5x^3 - 3x)/2 => P_3'(x) = (15x^2 - 3)/2:
  176. T xsq = x*x;
  177. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(3, x), (15*xsq - 3)/2, tolerance);
  178. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(-4, x), (15*xsq -3)/2, tolerance);
  179. // P_4(x) = (35x^4 - 30x^2 +3)/8 => P_4'(x) = (5x/2)*(7x^2 - 3)
  180. T expected = 5*x*(7*xsq - 3)/2;
  181. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(4, x), expected, tolerance);
  182. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(-5, x), expected, tolerance);
  183. // P_5(x) = (63x^5 - 70x^3 + 15x)/8 => P_5'(x) = (315*x^4 - 210*x^2 + 15)/8
  184. T x4 = xsq*xsq;
  185. expected = (315*x4 - 210*xsq + 15)/8;
  186. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(5, x), expected, tolerance);
  187. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(-6, x), expected, tolerance);
  188. // P_6(x) = (231x^6 -315*x^4 +105x^2 -5)/16 => P_6'(x) = (6*231*x^5 - 4*315*x^3 + 105x)/16
  189. expected = 21*x*(33*x4 - 30*xsq + 5)/8;
  190. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(6, x), expected, tolerance);
  191. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(-7, x), expected, tolerance);
  192. // Mathematica: D[LegendreP[7, x],x]
  193. T x6 = x4*xsq;
  194. expected = 7*(429*x6 -495*x4 + 135*xsq - 5)/16;
  195. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(7, x), expected, tolerance);
  196. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(-8, x), expected, tolerance);
  197. // Mathematica: D[LegendreP[8, x],x]
  198. // The naive polynomial evaluation algorithm is going to get worse from here out, so this will be enough.
  199. expected = 9*x*(715*x6 - 1001*x4 + 385*xsq - 35)/16;
  200. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(8, x), expected, tolerance);
  201. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(-9, x), expected, tolerance);
  202. x += static_cast<T>(1)/static_cast<T>(pow(T(2), T(4)));
  203. }
  204. int n = 0;
  205. while (n < 5000)
  206. {
  207. T expected = n*(n+1)*boost::math::constants::half<T>();
  208. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(n, (T) 1), expected, tolerance);
  209. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(-n - 1, (T) 1), expected, tolerance);
  210. if (n & 1)
  211. {
  212. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(n, (T) -1), expected, tolerance);
  213. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(-n - 1, (T) -1), expected, tolerance);
  214. }
  215. else
  216. {
  217. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(n, (T) -1), -expected, tolerance);
  218. BOOST_CHECK_CLOSE_FRACTION(::boost::math::legendre_p_prime<T>(-n - 1, (T) -1), -expected, tolerance);
  219. }
  220. ++n;
  221. }
  222. }
  223. template<class Real>
  224. void test_legendre_p_zeros()
  225. {
  226. std::cout << "Testing Legendre zeros on type " << boost::typeindex::type_id<Real>().pretty_name() << "\n";
  227. using std::sqrt;
  228. using std::abs;
  229. using boost::math::legendre_p_zeros;
  230. using boost::math::legendre_p;
  231. using boost::math::constants::third;
  232. Real tol = std::numeric_limits<Real>::epsilon();
  233. // Check the trivial cases:
  234. std::vector<Real> zeros = legendre_p_zeros<Real>(1);
  235. BOOST_ASSERT(zeros.size() == 1);
  236. BOOST_CHECK_SMALL(zeros[0], tol);
  237. zeros = legendre_p_zeros<Real>(2);
  238. BOOST_ASSERT(zeros.size() == 1);
  239. BOOST_CHECK_CLOSE_FRACTION(zeros[0], (Real) 1/ sqrt(static_cast<Real>(3)), tol);
  240. zeros = legendre_p_zeros<Real>(3);
  241. BOOST_ASSERT(zeros.size() == 2);
  242. BOOST_CHECK_SMALL(zeros[0], tol);
  243. BOOST_CHECK_CLOSE_FRACTION(zeros[1], sqrt(static_cast<Real>(3)/static_cast<Real>(5)), tol);
  244. zeros = legendre_p_zeros<Real>(4);
  245. BOOST_ASSERT(zeros.size() == 2);
  246. BOOST_CHECK_CLOSE_FRACTION(zeros[0], sqrt( (15-2*sqrt(static_cast<Real>(30)))/static_cast<Real>(35) ), tol);
  247. BOOST_CHECK_CLOSE_FRACTION(zeros[1], sqrt( (15+2*sqrt(static_cast<Real>(30)))/static_cast<Real>(35) ), tol);
  248. zeros = legendre_p_zeros<Real>(5);
  249. BOOST_ASSERT(zeros.size() == 3);
  250. BOOST_CHECK_SMALL(zeros[0], tol);
  251. BOOST_CHECK_CLOSE_FRACTION(zeros[1], third<Real>()*sqrt( (35 - 2*sqrt(static_cast<Real>(70)))/static_cast<Real>(7) ), 2*tol);
  252. BOOST_CHECK_CLOSE_FRACTION(zeros[2], third<Real>()*sqrt( (35 + 2*sqrt(static_cast<Real>(70)))/static_cast<Real>(7) ), 2*tol);
  253. // Don't take the tolerances too seriously.
  254. // The other test shows that the zeros are estimated more accurately than the function!
  255. for (unsigned n = 6; n < 130; ++n)
  256. {
  257. zeros = legendre_p_zeros<Real>(n);
  258. if (n & 1)
  259. {
  260. BOOST_CHECK(zeros.size() == (n-1)/2 +1);
  261. BOOST_CHECK_SMALL(zeros[0], tol);
  262. }
  263. else
  264. {
  265. // Zero is not a zero of the odd Legendre polynomials
  266. BOOST_CHECK(zeros.size() == n/2);
  267. BOOST_CHECK(zeros[0] > 0);
  268. BOOST_CHECK_SMALL(legendre_p(n, zeros[0]), 550*tol);
  269. }
  270. Real previous_zero = zeros[0];
  271. for (unsigned k = 1; k < zeros.size(); ++k)
  272. {
  273. Real next_zero = zeros[k];
  274. BOOST_CHECK(next_zero > previous_zero);
  275. std::string err = "Tolerance failed for (n, k) = (" + boost::lexical_cast<std::string>(n) + "," + boost::lexical_cast<std::string>(k) + ")\n";
  276. if (n < 40)
  277. {
  278. BOOST_CHECK_MESSAGE( abs(legendre_p(n, next_zero)) < 100*tol,
  279. err);
  280. }
  281. else
  282. {
  283. BOOST_CHECK_MESSAGE( abs(legendre_p(n, next_zero)) < 1000*tol,
  284. err);
  285. }
  286. previous_zero = next_zero;
  287. }
  288. // The zeros of orthogonal polynomials are contained strictly in (a, b).
  289. BOOST_CHECK(previous_zero < 1);
  290. }
  291. return;
  292. }
  293. int test_legendre_p_zeros_double_ulp(int min_x, int max_n)
  294. {
  295. std::cout << "Testing ULP distance for Legendre zeros.\n";
  296. using std::abs;
  297. using boost::math::legendre_p_zeros;
  298. using boost::math::float_distance;
  299. using boost::multiprecision::cpp_bin_float_quad;
  300. double max_float_distance = 0;
  301. for (int n = min_x; n < max_n; ++n)
  302. {
  303. std::vector<double> double_zeros = legendre_p_zeros<double>(n);
  304. std::vector<cpp_bin_float_quad> quad_zeros = legendre_p_zeros<cpp_bin_float_quad>(n);
  305. BOOST_ASSERT(quad_zeros.size() == double_zeros.size());
  306. for (int k = 0; k < (int)double_zeros.size(); ++k)
  307. {
  308. double d = abs(float_distance(double_zeros[k], quad_zeros[k].convert_to<double>()));
  309. if (d > max_float_distance)
  310. {
  311. max_float_distance = d;
  312. }
  313. }
  314. }
  315. return (int) max_float_distance;
  316. }