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)
7 #include <boost/math/concepts/real_concept.hpp>
8 #define BOOST_TEST_MAIN
9 #include <boost/test/unit_test.hpp>
10 #include <boost/test/tools/floating_point_comparison.hpp>
11 #include <boost/math/special_functions/math_fwd.hpp>
12 #include <boost/math/constants/constants.hpp>
13 #include <boost/array.hpp>
14 #include <boost/random.hpp>
15 #include "functor.hpp"
17 #include "handle_test_result.hpp"
18 #include "table_type.hpp"
21 #define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
24 template <class Real, typename T>
25 void do_test_ellint_rf(T& data, const char* type_name, const char* test)
27 #if !(defined(ERROR_REPORTING_MODE) && !defined(ELLINT_RF_FUNCTION_TO_TEST))
28 typedef Real value_type;
30 std::cout << "Testing: " << test << std::endl;
32 #ifdef ELLINT_RF_FUNCTION_TO_TEST
33 value_type(*fp)(value_type, value_type, value_type) = ELLINT_RF_FUNCTION_TO_TEST;
34 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
35 value_type (*fp)(value_type, value_type, value_type) = boost::math::ellint_rf<value_type, value_type, value_type>;
37 value_type (*fp)(value_type, value_type, value_type) = boost::math::ellint_rf;
39 boost::math::tools::test_result<value_type> result;
41 result = boost::math::tools::test_hetero<Real>(
43 bind_func<Real>(fp, 0, 1, 2),
44 extract_result<Real>(3));
45 handle_test_result(result, data[result.worst()], result.worst(),
46 type_name, "ellint_rf", test);
48 std::cout << std::endl;
52 template <class Real, typename T>
53 void do_test_ellint_rc(T& data, const char* type_name, const char* test)
55 #if !(defined(ERROR_REPORTING_MODE) && !defined(ELLINT_RC_FUNCTION_TO_TEST))
56 typedef Real value_type;
58 std::cout << "Testing: " << test << std::endl;
60 #ifdef ELLINT_RC_FUNCTION_TO_TEST
61 value_type(*fp)(value_type, value_type) = ELLINT_RC_FUNCTION_TO_TEST;
62 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
63 value_type (*fp)(value_type, value_type) = boost::math::ellint_rc<value_type, value_type>;
65 value_type (*fp)(value_type, value_type) = boost::math::ellint_rc;
67 boost::math::tools::test_result<value_type> result;
69 result = boost::math::tools::test_hetero<Real>(
71 bind_func<Real>(fp, 0, 1),
72 extract_result<Real>(2));
73 handle_test_result(result, data[result.worst()], result.worst(),
74 type_name, "ellint_rc", test);
76 std::cout << std::endl;
80 template <class Real, typename T>
81 void do_test_ellint_rj(T& data, const char* type_name, const char* test)
83 #if !(defined(ERROR_REPORTING_MODE) && !defined(ELLINT_RJ_FUNCTION_TO_TEST))
84 typedef Real value_type;
86 std::cout << "Testing: " << test << std::endl;
88 #ifdef ELLINT_RJ_FUNCTION_TO_TEST
89 value_type(*fp)(value_type, value_type, value_type, value_type) = ELLINT_RJ_FUNCTION_TO_TEST;
90 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
91 value_type (*fp)(value_type, value_type, value_type, value_type) = boost::math::ellint_rj<value_type, value_type, value_type, value_type>;
93 value_type (*fp)(value_type, value_type, value_type, value_type) = boost::math::ellint_rj;
95 boost::math::tools::test_result<value_type> result;
97 result = boost::math::tools::test_hetero<Real>(
99 bind_func<Real>(fp, 0, 1, 2, 3),
100 extract_result<Real>(4));
101 handle_test_result(result, data[result.worst()], result.worst(),
102 type_name, "ellint_rj", test);
104 std::cout << std::endl;
108 template <class Real, typename T>
109 void do_test_ellint_rd(T& data, const char* type_name, const char* test)
111 #if !(defined(ERROR_REPORTING_MODE) && !defined(ELLINT_RD_FUNCTION_TO_TEST))
112 typedef Real value_type;
114 std::cout << "Testing: " << test << std::endl;
116 #ifdef ELLINT_RD_FUNCTION_TO_TEST
117 value_type(*fp)(value_type, value_type, value_type) = ELLINT_RD_FUNCTION_TO_TEST;
118 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
119 value_type (*fp)(value_type, value_type, value_type) = boost::math::ellint_rd<value_type, value_type, value_type>;
121 value_type (*fp)(value_type, value_type, value_type) = boost::math::ellint_rd;
123 boost::math::tools::test_result<value_type> result;
125 result = boost::math::tools::test_hetero<Real>(
127 bind_func<Real>(fp, 0, 1, 2),
128 extract_result<Real>(3));
129 handle_test_result(result, data[result.worst()], result.worst(),
130 type_name, "ellint_rd", test);
132 std::cout << std::endl;
136 template <class Real, typename T>
137 void do_test_ellint_rg(T& data, const char* type_name, const char* test)
139 #if !(defined(ERROR_REPORTING_MODE) && !defined(ELLINT_RD_FUNCTION_TO_TEST))
140 typedef Real value_type;
142 std::cout << "Testing: " << test << std::endl;
144 #ifdef ELLINT_RG_FUNCTION_TO_TEST
145 value_type(*fp)(value_type, value_type, value_type) = ELLINT_RG_FUNCTION_TO_TEST;
146 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
147 value_type(*fp)(value_type, value_type, value_type) = boost::math::ellint_rg<value_type, value_type, value_type>;
149 value_type(*fp)(value_type, value_type, value_type) = boost::math::ellint_rg;
151 boost::math::tools::test_result<value_type> result;
153 result = boost::math::tools::test_hetero<Real>(
155 bind_func<Real>(fp, 0, 1, 2),
156 extract_result<Real>(3));
157 handle_test_result(result, data[result.worst()], result.worst(),
158 type_name, "ellint_rg", test);
160 std::cout << std::endl;
164 #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3) && !defined(TEST4)
173 template <typename T>
174 void t1(T, const char* type_name)
176 #include "ellint_rf_data.ipp"
178 do_test_ellint_rf<T>(ellint_rf_data, type_name, "RF: Random data");
181 template <typename T>
182 void t2(T, const char* type_name)
184 #include "ellint_rf_xxx.ipp"
186 do_test_ellint_rf<T>(ellint_rf_xxx, type_name, "RF: x = y = z");
189 template <typename T>
190 void t3(T, const char* type_name)
192 #include "ellint_rf_xyy.ipp"
194 do_test_ellint_rf<T>(ellint_rf_xyy, type_name, "RF: x = y or y = z or x = z");
197 template <typename T>
198 void t4(T, const char* type_name)
200 #include "ellint_rf_0yy.ipp"
202 do_test_ellint_rf<T>(ellint_rf_0yy, type_name, "RF: x = 0, y = z");
205 template <typename T>
206 void t5(T, const char* type_name)
208 #include "ellint_rf_xy0.ipp"
210 do_test_ellint_rf<T>(ellint_rf_xy0, type_name, "RF: z = 0");
216 template <typename T>
217 void t6(T, const char* type_name)
219 #include "ellint_rc_data.ipp"
221 do_test_ellint_rc<T>(ellint_rc_data, type_name, "RC: Random data");
224 template <typename T>
225 void t7(T, const char* type_name)
227 #include "ellint_rj_data.ipp"
229 do_test_ellint_rj<T>(ellint_rj_data, type_name, "RJ: Random data");
232 template <typename T>
233 void t8(T, const char* type_name)
235 #include "ellint_rj_e4.ipp"
237 do_test_ellint_rj<T>(ellint_rj_e4, type_name, "RJ: 4 Equal Values");
240 template <typename T>
241 void t9(T, const char* type_name)
243 #include "ellint_rj_e3.ipp"
245 do_test_ellint_rj<T>(ellint_rj_e3, type_name, "RJ: 3 Equal Values");
248 template <typename T>
249 void t10(T, const char* type_name)
251 #include "ellint_rj_e2.ipp"
253 do_test_ellint_rj<T>(ellint_rj_e2, type_name, "RJ: 2 Equal Values");
256 template <typename T>
257 void t11(T, const char* type_name)
259 #include "ellint_rj_zp.ipp"
261 do_test_ellint_rj<T>(ellint_rj_zp, type_name, "RJ: Equal z and p");
267 template <typename T>
268 void t12(T, const char* type_name)
270 #include "ellint_rd_data.ipp"
272 do_test_ellint_rd<T>(ellint_rd_data, type_name, "RD: Random data");
275 template <typename T>
276 void t13(T, const char* type_name)
278 #include "ellint_rd_xyy.ipp"
280 do_test_ellint_rd<T>(ellint_rd_xyy, type_name, "RD: y = z");
283 template <typename T>
284 void t14(T, const char* type_name)
286 #include "ellint_rd_xxz.ipp"
288 do_test_ellint_rd<T>(ellint_rd_xxz, type_name, "RD: x = y");
291 template <typename T>
292 void t15(T, const char* type_name)
294 #include "ellint_rd_0yy.ipp"
296 do_test_ellint_rd<T>(ellint_rd_0yy, type_name, "RD: x = 0, y = z");
299 template <typename T>
300 void t16(T, const char* type_name)
302 #include "ellint_rd_xxx.ipp"
304 do_test_ellint_rd<T>(ellint_rd_xxx, type_name, "RD: x = y = z");
307 template <typename T>
308 void t17(T, const char* type_name)
310 #include "ellint_rd_0xy.ipp"
312 do_test_ellint_rd<T>(ellint_rd_0xy, type_name, "RD: x = 0");
318 template <typename T>
319 void t18(T, const char* type_name)
321 #include "ellint_rg.ipp"
323 do_test_ellint_rg<T>(ellint_rg, type_name, "RG: Random Data");
326 template <typename T>
327 void t19(T, const char* type_name)
329 #include "ellint_rg_00x.ipp"
331 do_test_ellint_rg<T>(ellint_rg_00x, type_name, "RG: two values 0");
334 template <typename T>
335 void t20(T, const char* type_name)
337 #include "ellint_rg_xxx.ipp"
339 do_test_ellint_rg<T>(ellint_rg_xxx, type_name, "RG: All values the same or zero");
342 template <typename T>
343 void t21(T, const char* type_name)
345 #include "ellint_rg_xyy.ipp"
347 do_test_ellint_rg<T>(ellint_rg_xyy, type_name, "RG: two values the same");
350 template <typename T>
351 void t22(T, const char* type_name)
353 #include "ellint_rg_xy0.ipp"
355 do_test_ellint_rg<T>(ellint_rg_xy0, type_name, "RG: one value zero");
360 template <typename T>
361 void test_spots(T val, const char* type_name)
364 using namespace boost::math;
366 // Spot values from Numerical Computation of Real or Complex
367 // Elliptic Integrals, B. C. Carlson: http://arxiv.org/abs/math.CA/9409227
369 T tolerance = (std::max)(T(1e-13f), tools::epsilon<T>() * 5) * 100; // Note 5eps expressed as a persentage!!!
370 T eps2 = 5 * tools::epsilon<T>();
371 BOOST_CHECK_CLOSE(ellint_rf(T(1), T(2), T(0)), T(1.3110287771461), tolerance);
372 BOOST_CHECK_CLOSE(ellint_rf(T(0.5), T(1), T(0)), T(1.8540746773014), tolerance);
373 BOOST_CHECK_CLOSE(ellint_rf(T(2), T(3), T(4)), T(0.58408284167715), tolerance);
375 BOOST_CHECK_CLOSE_FRACTION(ellint_rc(T(0), T(1)/4), boost::math::constants::pi<T>(), eps2);
376 BOOST_CHECK_CLOSE_FRACTION(ellint_rc(T(9)/4, T(2)), boost::math::constants::ln_two<T>(), eps2);
377 BOOST_CHECK_CLOSE_FRACTION(ellint_rc(T(1) / 4, T(-2)), boost::math::constants::ln_two<T>() / 3, eps2);
379 BOOST_CHECK_CLOSE(ellint_rj(T(0), T(1), T(2), T(3)), T(0.77688623778582), tolerance);
380 BOOST_CHECK_CLOSE(ellint_rj(T(2), T(3), T(4), T(5)), T(0.14297579667157), tolerance);
381 BOOST_CHECK_CLOSE(ellint_rj(T(2), T(3), T(4), T(-0.5)), T(0.24723819703052), tolerance);
382 BOOST_CHECK_CLOSE(ellint_rj(T(2), T(3), T(4), T(-5)), T(-0.12711230042964), tolerance);
384 BOOST_CHECK_CLOSE(ellint_rd(T(0), T(2), T(1)), T(1.7972103521034), tolerance);
385 BOOST_CHECK_CLOSE(ellint_rd(T(2), T(3), T(4)), T(0.16510527294261), tolerance);
387 // Sanity/consistency checks from Numerical Computation of Real or Complex
388 // Elliptic Integrals, B. C. Carlson: http://arxiv.org/abs/math.CA/9409227
390 boost::uniform_real<float> ur(0, 1000);
391 T eps40 = 40 * tools::epsilon<T>();
393 for(unsigned i = 0; i < 1000; ++i)
399 T mu = x * y / lambda;
401 T s1 = ellint_rf(x+lambda, y+lambda, lambda) +
402 ellint_rf(x + mu, y + mu, mu);
403 T s2 = ellint_rf(x, y, T(0));
404 BOOST_CHECK_CLOSE_FRACTION(s1, s2, eps40);
405 // RC is degenerate case of RF:
406 s1 = ellint_rc(x, y);
407 s2 = ellint_rf(x, y, y);
408 BOOST_CHECK_CLOSE_FRACTION(s1, s2, eps40);
409 // RC, eq 50 (Note have to assume y = x):
410 T mu2 = x * x / lambda;
411 s1 = ellint_rc(lambda, x+lambda)
412 + ellint_rc(mu2, x + mu2);
413 s2 = ellint_rc(T(0), x);
414 BOOST_CHECK_CLOSE_FRACTION(s1, s2, eps40);
416 T p = ????; // no closed form for a, b and p???
417 s1 = ellint_rj(x+lambda, y+lambda, lambda, p+lambda)
418 + ellint_rj(x+mu, y+mu, mu, p+mu);
419 s2 = ellint_rj(x, y, T(0), p)
420 - 3 * ellint_rc(a, b);
423 s1 = ellint_rd(lambda, x+lambda, y+lambda)
424 + ellint_rd(mu, x+mu, y+mu);
425 s2 = ellint_rd(T(0), x, y)
426 - 3 / (y * sqrt(x+y+lambda+mu));
427 BOOST_CHECK_CLOSE_FRACTION(s1, s2, eps40);
428 // RD is degenerate case of RJ:
429 s1 = ellint_rd(x, y, z);
430 s2 = ellint_rj(x, y, z, z);
431 BOOST_CHECK_CLOSE_FRACTION(s1, s2, eps40);
435 // Now random spot values: