test_carlson.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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. #include <boost/math/concepts/real_concept.hpp>
  7. #define BOOST_TEST_MAIN
  8. #include <boost/test/unit_test.hpp>
  9. #include <boost/test/tools/floating_point_comparison.hpp>
  10. #include <boost/math/special_functions/math_fwd.hpp>
  11. #include <boost/math/constants/constants.hpp>
  12. #include <boost/array.hpp>
  13. #include <boost/random.hpp>
  14. #include "functor.hpp"
  15. #include "handle_test_result.hpp"
  16. #include "table_type.hpp"
  17. #ifndef SC_
  18. #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
  19. #endif
  20. template <class Real, typename T>
  21. void do_test_ellint_rf(T& data, const char* type_name, const char* test)
  22. {
  23. #if !(defined(ERROR_REPORTING_MODE) && !defined(ELLINT_RF_FUNCTION_TO_TEST))
  24. typedef Real value_type;
  25. std::cout << "Testing: " << test << std::endl;
  26. #ifdef ELLINT_RF_FUNCTION_TO_TEST
  27. value_type(*fp)(value_type, value_type, value_type) = ELLINT_RF_FUNCTION_TO_TEST;
  28. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  29. value_type (*fp)(value_type, value_type, value_type) = boost::math::ellint_rf<value_type, value_type, value_type>;
  30. #else
  31. value_type (*fp)(value_type, value_type, value_type) = boost::math::ellint_rf;
  32. #endif
  33. boost::math::tools::test_result<value_type> result;
  34. result = boost::math::tools::test_hetero<Real>(
  35. data,
  36. bind_func<Real>(fp, 0, 1, 2),
  37. extract_result<Real>(3));
  38. handle_test_result(result, data[result.worst()], result.worst(),
  39. type_name, "ellint_rf", test);
  40. std::cout << std::endl;
  41. #endif
  42. }
  43. template <class Real, typename T>
  44. void do_test_ellint_rc(T& data, const char* type_name, const char* test)
  45. {
  46. #if !(defined(ERROR_REPORTING_MODE) && !defined(ELLINT_RC_FUNCTION_TO_TEST))
  47. typedef Real value_type;
  48. std::cout << "Testing: " << test << std::endl;
  49. #ifdef ELLINT_RC_FUNCTION_TO_TEST
  50. value_type(*fp)(value_type, value_type) = ELLINT_RC_FUNCTION_TO_TEST;
  51. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  52. value_type (*fp)(value_type, value_type) = boost::math::ellint_rc<value_type, value_type>;
  53. #else
  54. value_type (*fp)(value_type, value_type) = boost::math::ellint_rc;
  55. #endif
  56. boost::math::tools::test_result<value_type> result;
  57. result = boost::math::tools::test_hetero<Real>(
  58. data,
  59. bind_func<Real>(fp, 0, 1),
  60. extract_result<Real>(2));
  61. handle_test_result(result, data[result.worst()], result.worst(),
  62. type_name, "ellint_rc", test);
  63. std::cout << std::endl;
  64. #endif
  65. }
  66. template <class Real, typename T>
  67. void do_test_ellint_rj(T& data, const char* type_name, const char* test)
  68. {
  69. #if !(defined(ERROR_REPORTING_MODE) && !defined(ELLINT_RJ_FUNCTION_TO_TEST))
  70. typedef Real value_type;
  71. std::cout << "Testing: " << test << std::endl;
  72. #ifdef ELLINT_RJ_FUNCTION_TO_TEST
  73. value_type(*fp)(value_type, value_type, value_type, value_type) = ELLINT_RJ_FUNCTION_TO_TEST;
  74. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  75. value_type (*fp)(value_type, value_type, value_type, value_type) = boost::math::ellint_rj<value_type, value_type, value_type, value_type>;
  76. #else
  77. value_type (*fp)(value_type, value_type, value_type, value_type) = boost::math::ellint_rj;
  78. #endif
  79. boost::math::tools::test_result<value_type> result;
  80. result = boost::math::tools::test_hetero<Real>(
  81. data,
  82. bind_func<Real>(fp, 0, 1, 2, 3),
  83. extract_result<Real>(4));
  84. handle_test_result(result, data[result.worst()], result.worst(),
  85. type_name, "ellint_rj", test);
  86. std::cout << std::endl;
  87. #endif
  88. }
  89. template <class Real, typename T>
  90. void do_test_ellint_rd(T& data, const char* type_name, const char* test)
  91. {
  92. #if !(defined(ERROR_REPORTING_MODE) && !defined(ELLINT_RD_FUNCTION_TO_TEST))
  93. typedef Real value_type;
  94. std::cout << "Testing: " << test << std::endl;
  95. #ifdef ELLINT_RD_FUNCTION_TO_TEST
  96. value_type(*fp)(value_type, value_type, value_type) = ELLINT_RD_FUNCTION_TO_TEST;
  97. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  98. value_type (*fp)(value_type, value_type, value_type) = boost::math::ellint_rd<value_type, value_type, value_type>;
  99. #else
  100. value_type (*fp)(value_type, value_type, value_type) = boost::math::ellint_rd;
  101. #endif
  102. boost::math::tools::test_result<value_type> result;
  103. result = boost::math::tools::test_hetero<Real>(
  104. data,
  105. bind_func<Real>(fp, 0, 1, 2),
  106. extract_result<Real>(3));
  107. handle_test_result(result, data[result.worst()], result.worst(),
  108. type_name, "ellint_rd", test);
  109. std::cout << std::endl;
  110. #endif
  111. }
  112. template <class Real, typename T>
  113. void do_test_ellint_rg(T& data, const char* type_name, const char* test)
  114. {
  115. #if !(defined(ERROR_REPORTING_MODE) && !defined(ELLINT_RD_FUNCTION_TO_TEST))
  116. typedef Real value_type;
  117. std::cout << "Testing: " << test << std::endl;
  118. #ifdef ELLINT_RG_FUNCTION_TO_TEST
  119. value_type(*fp)(value_type, value_type, value_type) = ELLINT_RG_FUNCTION_TO_TEST;
  120. #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
  121. value_type(*fp)(value_type, value_type, value_type) = boost::math::ellint_rg<value_type, value_type, value_type>;
  122. #else
  123. value_type(*fp)(value_type, value_type, value_type) = boost::math::ellint_rg;
  124. #endif
  125. boost::math::tools::test_result<value_type> result;
  126. result = boost::math::tools::test_hetero<Real>(
  127. data,
  128. bind_func<Real>(fp, 0, 1, 2),
  129. extract_result<Real>(3));
  130. handle_test_result(result, data[result.worst()], result.worst(),
  131. type_name, "ellint_rg", test);
  132. std::cout << std::endl;
  133. #endif
  134. }
  135. #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3) && !defined(TEST4)
  136. #define TEST1
  137. #define TEST2
  138. #define TEST3
  139. #define TEST4
  140. #endif
  141. #ifdef TEST1
  142. template <typename T>
  143. void t1(T, const char* type_name)
  144. {
  145. #include "ellint_rf_data.ipp"
  146. do_test_ellint_rf<T>(ellint_rf_data, type_name, "RF: Random data");
  147. }
  148. template <typename T>
  149. void t2(T, const char* type_name)
  150. {
  151. #include "ellint_rf_xxx.ipp"
  152. do_test_ellint_rf<T>(ellint_rf_xxx, type_name, "RF: x = y = z");
  153. }
  154. template <typename T>
  155. void t3(T, const char* type_name)
  156. {
  157. #include "ellint_rf_xyy.ipp"
  158. do_test_ellint_rf<T>(ellint_rf_xyy, type_name, "RF: x = y or y = z or x = z");
  159. }
  160. template <typename T>
  161. void t4(T, const char* type_name)
  162. {
  163. #include "ellint_rf_0yy.ipp"
  164. do_test_ellint_rf<T>(ellint_rf_0yy, type_name, "RF: x = 0, y = z");
  165. }
  166. template <typename T>
  167. void t5(T, const char* type_name)
  168. {
  169. #include "ellint_rf_xy0.ipp"
  170. do_test_ellint_rf<T>(ellint_rf_xy0, type_name, "RF: z = 0");
  171. }
  172. #endif
  173. #ifdef TEST2
  174. template <typename T>
  175. void t6(T, const char* type_name)
  176. {
  177. #include "ellint_rc_data.ipp"
  178. do_test_ellint_rc<T>(ellint_rc_data, type_name, "RC: Random data");
  179. }
  180. template <typename T>
  181. void t7(T, const char* type_name)
  182. {
  183. #include "ellint_rj_data.ipp"
  184. do_test_ellint_rj<T>(ellint_rj_data, type_name, "RJ: Random data");
  185. }
  186. template <typename T>
  187. void t8(T, const char* type_name)
  188. {
  189. #include "ellint_rj_e4.ipp"
  190. do_test_ellint_rj<T>(ellint_rj_e4, type_name, "RJ: 4 Equal Values");
  191. }
  192. template <typename T>
  193. void t9(T, const char* type_name)
  194. {
  195. #include "ellint_rj_e3.ipp"
  196. do_test_ellint_rj<T>(ellint_rj_e3, type_name, "RJ: 3 Equal Values");
  197. }
  198. template <typename T>
  199. void t10(T, const char* type_name)
  200. {
  201. #include "ellint_rj_e2.ipp"
  202. do_test_ellint_rj<T>(ellint_rj_e2, type_name, "RJ: 2 Equal Values");
  203. }
  204. template <typename T>
  205. void t11(T, const char* type_name)
  206. {
  207. #include "ellint_rj_zp.ipp"
  208. do_test_ellint_rj<T>(ellint_rj_zp, type_name, "RJ: Equal z and p");
  209. }
  210. #endif
  211. #ifdef TEST3
  212. template <typename T>
  213. void t12(T, const char* type_name)
  214. {
  215. #include "ellint_rd_data.ipp"
  216. do_test_ellint_rd<T>(ellint_rd_data, type_name, "RD: Random data");
  217. }
  218. template <typename T>
  219. void t13(T, const char* type_name)
  220. {
  221. #include "ellint_rd_xyy.ipp"
  222. do_test_ellint_rd<T>(ellint_rd_xyy, type_name, "RD: y = z");
  223. }
  224. template <typename T>
  225. void t14(T, const char* type_name)
  226. {
  227. #include "ellint_rd_xxz.ipp"
  228. do_test_ellint_rd<T>(ellint_rd_xxz, type_name, "RD: x = y");
  229. }
  230. template <typename T>
  231. void t15(T, const char* type_name)
  232. {
  233. #include "ellint_rd_0yy.ipp"
  234. do_test_ellint_rd<T>(ellint_rd_0yy, type_name, "RD: x = 0, y = z");
  235. }
  236. template <typename T>
  237. void t16(T, const char* type_name)
  238. {
  239. #include "ellint_rd_xxx.ipp"
  240. do_test_ellint_rd<T>(ellint_rd_xxx, type_name, "RD: x = y = z");
  241. }
  242. template <typename T>
  243. void t17(T, const char* type_name)
  244. {
  245. #include "ellint_rd_0xy.ipp"
  246. do_test_ellint_rd<T>(ellint_rd_0xy, type_name, "RD: x = 0");
  247. }
  248. #endif
  249. #ifdef TEST4
  250. template <typename T>
  251. void t18(T, const char* type_name)
  252. {
  253. #include "ellint_rg.ipp"
  254. do_test_ellint_rg<T>(ellint_rg, type_name, "RG: Random Data");
  255. }
  256. template <typename T>
  257. void t19(T, const char* type_name)
  258. {
  259. #include "ellint_rg_00x.ipp"
  260. do_test_ellint_rg<T>(ellint_rg_00x, type_name, "RG: two values 0");
  261. }
  262. template <typename T>
  263. void t20(T, const char* type_name)
  264. {
  265. #include "ellint_rg_xxx.ipp"
  266. do_test_ellint_rg<T>(ellint_rg_xxx, type_name, "RG: All values the same or zero");
  267. }
  268. template <typename T>
  269. void t21(T, const char* type_name)
  270. {
  271. #include "ellint_rg_xyy.ipp"
  272. do_test_ellint_rg<T>(ellint_rg_xyy, type_name, "RG: two values the same");
  273. }
  274. template <typename T>
  275. void t22(T, const char* type_name)
  276. {
  277. #include "ellint_rg_xy0.ipp"
  278. do_test_ellint_rg<T>(ellint_rg_xy0, type_name, "RG: one value zero");
  279. }
  280. #endif
  281. template <typename T>
  282. void test_spots(T val, const char* type_name)
  283. {
  284. #ifndef TEST_UDT
  285. using namespace boost::math;
  286. using namespace std;
  287. // Spot values from Numerical Computation of Real or Complex
  288. // Elliptic Integrals, B. C. Carlson: http://arxiv.org/abs/math.CA/9409227
  289. // RF:
  290. T tolerance = (std::max)(T(1e-13f), tools::epsilon<T>() * 5) * 100; // Note 5eps expressed as a persentage!!!
  291. T eps2 = 5 * tools::epsilon<T>();
  292. BOOST_CHECK_CLOSE(ellint_rf(T(1), T(2), T(0)), T(1.3110287771461), tolerance);
  293. BOOST_CHECK_CLOSE(ellint_rf(T(0.5), T(1), T(0)), T(1.8540746773014), tolerance);
  294. BOOST_CHECK_CLOSE(ellint_rf(T(2), T(3), T(4)), T(0.58408284167715), tolerance);
  295. // RC:
  296. BOOST_CHECK_CLOSE_FRACTION(ellint_rc(T(0), T(1)/4), boost::math::constants::pi<T>(), eps2);
  297. BOOST_CHECK_CLOSE_FRACTION(ellint_rc(T(9)/4, T(2)), boost::math::constants::ln_two<T>(), eps2);
  298. BOOST_CHECK_CLOSE_FRACTION(ellint_rc(T(1) / 4, T(-2)), boost::math::constants::ln_two<T>() / 3, eps2);
  299. // RJ:
  300. BOOST_CHECK_CLOSE(ellint_rj(T(0), T(1), T(2), T(3)), T(0.77688623778582), tolerance);
  301. BOOST_CHECK_CLOSE(ellint_rj(T(2), T(3), T(4), T(5)), T(0.14297579667157), tolerance);
  302. BOOST_CHECK_CLOSE(ellint_rj(T(2), T(3), T(4), T(-0.5)), T(0.24723819703052), tolerance);
  303. BOOST_CHECK_CLOSE(ellint_rj(T(2), T(3), T(4), T(-5)), T(-0.12711230042964), tolerance);
  304. // RD:
  305. BOOST_CHECK_CLOSE(ellint_rd(T(0), T(2), T(1)), T(1.7972103521034), tolerance);
  306. BOOST_CHECK_CLOSE(ellint_rd(T(2), T(3), T(4)), T(0.16510527294261), tolerance);
  307. // Sanity/consistency checks from Numerical Computation of Real or Complex
  308. // Elliptic Integrals, B. C. Carlson: http://arxiv.org/abs/math.CA/9409227
  309. boost::mt19937 ran;
  310. boost::uniform_real<float> ur(0, 1000);
  311. T eps40 = 40 * tools::epsilon<T>();
  312. for(unsigned i = 0; i < 1000; ++i)
  313. {
  314. T x = ur(ran);
  315. T y = ur(ran);
  316. T z = ur(ran);
  317. T lambda = ur(ran);
  318. T mu = x * y / lambda;
  319. // RF, eq 49:
  320. T s1 = ellint_rf(x+lambda, y+lambda, lambda) +
  321. ellint_rf(x + mu, y + mu, mu);
  322. T s2 = ellint_rf(x, y, T(0));
  323. BOOST_CHECK_CLOSE_FRACTION(s1, s2, eps40);
  324. // RC is degenerate case of RF:
  325. s1 = ellint_rc(x, y);
  326. s2 = ellint_rf(x, y, y);
  327. BOOST_CHECK_CLOSE_FRACTION(s1, s2, eps40);
  328. // RC, eq 50 (Note have to assume y = x):
  329. T mu2 = x * x / lambda;
  330. s1 = ellint_rc(lambda, x+lambda)
  331. + ellint_rc(mu2, x + mu2);
  332. s2 = ellint_rc(T(0), x);
  333. BOOST_CHECK_CLOSE_FRACTION(s1, s2, eps40);
  334. /*
  335. T p = ????; // no closed form for a, b and p???
  336. s1 = ellint_rj(x+lambda, y+lambda, lambda, p+lambda)
  337. + ellint_rj(x+mu, y+mu, mu, p+mu);
  338. s2 = ellint_rj(x, y, T(0), p)
  339. - 3 * ellint_rc(a, b);
  340. */
  341. // RD, eq 53:
  342. s1 = ellint_rd(lambda, x+lambda, y+lambda)
  343. + ellint_rd(mu, x+mu, y+mu);
  344. s2 = ellint_rd(T(0), x, y)
  345. - 3 / (y * sqrt(x+y+lambda+mu));
  346. BOOST_CHECK_CLOSE_FRACTION(s1, s2, eps40);
  347. // RD is degenerate case of RJ:
  348. s1 = ellint_rd(x, y, z);
  349. s2 = ellint_rj(x, y, z, z);
  350. BOOST_CHECK_CLOSE_FRACTION(s1, s2, eps40);
  351. }
  352. #endif
  353. //
  354. // Now random spot values:
  355. //
  356. #ifdef TEST1
  357. t1(val, type_name);
  358. t2(val, type_name);
  359. t3(val, type_name);
  360. t4(val, type_name);
  361. t5(val, type_name);
  362. #endif
  363. #ifdef TEST2
  364. t6(val, type_name);
  365. t7(val, type_name);
  366. t8(val, type_name);
  367. t9(val, type_name);
  368. t10(val, type_name);
  369. t11(val, type_name);
  370. #endif
  371. #ifdef TEST3
  372. t12(val, type_name);
  373. t13(val, type_name);
  374. t14(val, type_name);
  375. t15(val, type_name);
  376. t16(val, type_name);
  377. t17(val, type_name);
  378. #endif
  379. #ifdef TEST4
  380. t18(val, type_name);
  381. t19(val, type_name);
  382. t20(val, type_name);
  383. t21(val, type_name);
  384. t22(val, type_name);
  385. #endif
  386. }