Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / math / test / test_bessel_k.hpp
1 //  (C) Copyright John Maddock 2007.
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
6 #define BOOST_MATH_OVERFLOW_ERROR_POLICY ignore_error
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/type_traits/is_floating_point.hpp>
13 #include <boost/array.hpp>
14 #include "functor.hpp"
15
16 #include "handle_test_result.hpp"
17 #include "table_type.hpp"
18
19 #ifndef SC_
20 #  define SC_(x) static_cast<typename table_type<T>::type>(BOOST_JOIN(x, L))
21 #endif
22
23 template <class T>
24 T cyl_bessel_k_int_wrapper(T v, T x)
25 {
26 #ifdef BESSEL_KN_FUNCTION_TO_TEST
27    return static_cast<T>(
28       BESSEL_KN_FUNCTION_TO_TEST(
29       boost::math::itrunc(v), x));
30 #else
31    return static_cast<T>(
32       boost::math::cyl_bessel_k(
33       boost::math::itrunc(v), x));
34 #endif
35 }
36
37 template <class Real, class T>
38 void do_test_cyl_bessel_k(const T& data, const char* type_name, const char* test_name)
39 {
40 #if !(defined(ERROR_REPORTING_MODE) && !defined(BESSEL_K_FUNCTION_TO_TEST))
41    typedef Real                   value_type;
42
43    typedef value_type (*pg)(value_type, value_type);
44 #ifdef BESSEL_K_FUNCTION_TO_TEST
45    pg funcp = BESSEL_K_FUNCTION_TO_TEST;
46 #elif defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
47    pg funcp = boost::math::cyl_bessel_k<value_type, value_type>;
48 #else
49    pg funcp = boost::math::cyl_bessel_k;
50 #endif
51
52    boost::math::tools::test_result<value_type> result;
53
54    std::cout << "Testing " << test_name << " with type " << type_name
55       << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
56
57    //
58    // test cyl_bessel_k against data:
59    //
60    result = boost::math::tools::test_hetero<Real>(
61       data, 
62       bind_func<Real>(funcp, 0, 1), 
63       extract_result<Real>(2));
64    handle_test_result(result, data[result.worst()], result.worst(), type_name, "cyl_bessel_k", test_name);
65    std::cout << std::endl;
66
67 #endif
68 }
69
70 template <class Real, class T>
71 void do_test_cyl_bessel_k_int(const T& data, const char* type_name, const char* test_name)
72 {
73 #if !(defined(ERROR_REPORTING_MODE) && !defined(BESSEL_KN_FUNCTION_TO_TEST))
74    typedef Real                   value_type;
75
76    typedef value_type (*pg)(value_type, value_type);
77 #if defined(BOOST_MATH_NO_DEDUCED_FUNCTION_POINTERS)
78    pg funcp = cyl_bessel_k_int_wrapper<value_type>;
79 #else
80    pg funcp = cyl_bessel_k_int_wrapper;
81 #endif
82
83    boost::math::tools::test_result<value_type> result;
84
85    std::cout << "Testing " << test_name << " with type " << type_name
86       << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
87
88    //
89    // test cyl_bessel_k against data:
90    //
91    result = boost::math::tools::test_hetero<Real>(
92       data, 
93       bind_func<Real>(funcp, 0, 1), 
94       extract_result<Real>(2));
95    handle_test_result(result, data[result.worst()], result.worst(), type_name, "cyl_bessel_k (integer orders)", test_name);
96    std::cout << std::endl;
97 #endif
98 }
99
100 template <class T>
101 void test_bessel(T, const char* name)
102 {
103     // function values calculated on http://functions.wolfram.com/
104     static const boost::array<boost::array<typename table_type<T>::type, 3>, 9> k0_data = {{
105         {{ SC_(0.0), SC_(1.0), SC_(0.421024438240708333335627379212609036136219748226660472298970) }},
106         {{ SC_(0.0), SC_(2.0), SC_(0.113893872749533435652719574932481832998326624388808882892530) }},
107         {{ SC_(0.0), SC_(4.0), SC_(0.0111596760858530242697451959798334892250090238884743405382553) }},
108         {{ SC_(0.0), SC_(8.0), SC_(0.000146470705222815387096584408698677921967305368833759024089154) }},
109         {{ SC_(0.0), SC_(0.000030517578125) /*T(std::ldexp(1.0, -15))*/, SC_(10.5131392267382037062459525561594822400447325776672021972753) }},
110         {{ SC_(0.0), SC_(9.31322574615478515625e-10) /*T(std::ldexp(1.0, -30))*/, SC_(20.9103469324567717360787328239372191382743831365906131108531) }},
111         {{ SC_(0.0), SC_(8.67361737988403547205962240695953369140625e-19) /*T(std::ldexp(1.0, -60))*/, SC_(41.7047623492551310138446473188663682295952219631968830346918) }},
112         {{ SC_(0.0), SC_(50.0), SC_(3.41016774978949551392067551235295223184502537762334808993276e-23) }},
113         {{ SC_(0.0), SC_(100.0), SC_(4.65662822917590201893900528948388635580753948544211387402671e-45) }},
114     }};
115     static const boost::array<boost::array<typename table_type<T>::type, 3>, 9> k1_data = {{
116         {{ SC_(1.0), SC_(1.0), SC_(0.601907230197234574737540001535617339261586889968106456017768) }},
117         {{ SC_(1.0), SC_(2.0), SC_(0.139865881816522427284598807035411023887234584841515530384442) }},
118         {{ SC_(1.0), SC_(4.0), SC_(0.0124834988872684314703841799808060684838415849886258457917076) }},
119         {{ SC_(1.0), SC_(8.0), SC_(0.000155369211805001133916862450622474621117065122872616157079566) }},
120         {{ SC_(1.0), SC_(0.000030517578125) /*T(std::ldexp(1.0, -15))*/, SC_(32767.9998319528316432647441316539139725104728341577594326513) }},
121         {{ SC_(1.0), SC_(9.31322574615478515625e-10) /*T(std::ldexp(1.0, -30))*/, SC_(1.07374182399999999003003028572687332810353799544215073362305e9) }},
122         {{ SC_(1.0), SC_(8.67361737988403547205962240695953369140625e-19) /*T(std::ldexp(1.0, -60))*/, SC_(1.15292150460684697599999999999999998169660198868126604634036e18) }},
123         {{ SC_(1.0), SC_(50.0), SC_(3.44410222671755561259185303591267155099677251348256880221927e-23) }},
124         {{ SC_(1.0), SC_(100.0), SC_(4.67985373563690928656254424202433530797494354694335352937465e-45) }},
125     }};
126     static const boost::array<boost::array<typename table_type<T>::type, 3>, 9> kn_data = {{
127         {{ SC_(2.0), SC_(9.31322574615478515625e-10) /*T(std::ldexp(1.0, -30))*/, SC_(2.30584300921369395150000000000000000234841952009593636868109e18) }},
128         {{ SC_(5.0), SC_(10.0), SC_(0.0000575418499853122792763740236992723196597629124356739596921536) }},
129         {{ SC_(-5.0), SC_(100.0), SC_(5.27325611329294989461777188449044716451716555009882448801072e-45) }},
130         {{ SC_(10.0), SC_(10.0), SC_(0.00161425530039067002345725193091329085443750382929208307802221) }},
131         {{ SC_(10.0), SC_(9.31322574615478515625e-10) /*T(std::ldexp(1.0, -30))*/, SC_(3.78470202927236255215249281534478864916684072926050665209083e98) }},
132         {{ SC_(-10.0), SC_(1.0), SC_(1.80713289901029454691597861302340015908245782948536080022119e8) }},
133         {{ SC_(100.0), SC_(5.0), SC_(7.03986019306167654653386616796116726248616158936088056952477e115) }},
134         {{ SC_(100.0), SC_(80.0), SC_(8.39287107246490782848985384895907681748152272748337807033319e-12) }},
135         {{ SC_(-1000.0), SC_(700.0), SC_(6.51561979144735818903553852606383312984409361984128221539405e-31) }},
136     }};
137     static const boost::array<boost::array<typename table_type<T>::type, 3>, 11> kv_data = {{
138         {{ SC_(0.5), SC_(0.875), SC_(0.558532231646608646115729767013630967055657943463362504577189) }},
139         {{ SC_(0.5), SC_(1.125), SC_(0.383621010650189547146769320487006220295290256657827220786527) }},
140         {{ SC_(2.25), SC_(9.31322574615478515625e-10) /*T(std::ldexp(1.0, -30))*/, SC_(5.62397392719283271332307799146649700147907612095185712015604e20) }},
141         {{ SC_(5.5), SC_(3.1416015625) /*3217/1024*/, SC_(1.30623288775012596319554857587765179889689223531159532808379) }},
142         {{ SC_(-5.5), SC_(10.0), SC_(0.0000733045300798502164644836879577484533096239574909573072142667) }},
143         {{ SC_(-5.5), SC_(100.0), SC_(5.41274555306792267322084448693957747924412508020839543293369e-45) }},
144         {{ SC_(10.0), SC_(0.0009765625) /*1/1024*/, SC_(2.35522579263922076203415803966825431039900000000993410734978e38) }},
145         {{ SC_(10.0), SC_(10.0), SC_(0.00161425530039067002345725193091329085443750382929208307802221) }},
146         {{ SC_(141.3994140625) /*T(144793)/1024)*/, SC_(100.0), SC_(1.39565245860302528069481472855619216759142225046370312329416e-6) }},
147         {{ SC_(141.3994140625) /*T(144793)/1024)*/, SC_(200.0), SC_(9.11950412043225432171915100042647230802198254567007382956336e-68) }},
148         {{ SC_(-141.3994140625) /*T(-144793)/1024*/, SC_(50.0), SC_(1.30185229717525025165362673848737761549946548375142378172956e42) }},
149     }};
150     static const boost::array<boost::array<typename table_type<T>::type, 3>, 5> kv_large_data = {{
151         // Bug report https://svn.boost.org/trac/boost/ticket/5560:
152         {{ SC_(-1.0), SC_(3.729170365600103371645482657731466918688235767300203447135759166603139192535059152468087445200213901e-155) /*static_cast<T>(ldexp(0.5, -512))*/, SC_(2.68156158598851941991480499964116922549587316411847867554471e154) }},
153         {{ SC_(1.0),  SC_(3.729170365600103371645482657731466918688235767300203447135759166603139192535059152468087445200213901e-155) /*static_cast<T>(ldexp(0.5, -512))*/, SC_(2.68156158598851941991480499964116922549587316411847867554471e154) }},
154         {{ SC_(-1.125), SC_(3.729170365600103371645482657731466918688235767300203447135759166603139192535059152468087445200213901e-155) /*static_cast<T>(ldexp(0.5, -512))*/, SC_(5.53984048006472105611199242328122729730752165907526178753978e173) }},
155         {{ SC_(1.125),  SC_(3.729170365600103371645482657731466918688235767300203447135759166603139192535059152468087445200213901e-155) /*static_cast<T>(ldexp(0.5, -512))*/, SC_(5.53984048006472105611199242328122729730752165907526178753978e173) }},
156         {{ SC_(0.5),  SC_(1.2458993688871959419388378518880931736878259938089494331010226962863582408064841833232475731084062642684629e-206) /*static_cast<T>(ldexp(0.5, -683))*/, SC_(1.12284149973980088540335945247019177715948513804063794284101e103) }},
157     }};
158
159     do_test_cyl_bessel_k<T>(k0_data, name, "Bessel K0: Mathworld Data");
160     do_test_cyl_bessel_k<T>(k1_data, name, "Bessel K1: Mathworld Data");
161     do_test_cyl_bessel_k<T>(kn_data, name, "Bessel Kn: Mathworld Data");
162
163     do_test_cyl_bessel_k_int<T>(k0_data, name, "Bessel K0: Mathworld Data (Integer Version)");
164     do_test_cyl_bessel_k_int<T>(k1_data, name, "Bessel K1: Mathworld Data (Integer Version)");
165     do_test_cyl_bessel_k_int<T>(kn_data, name, "Bessel Kn: Mathworld Data (Integer Version)");
166
167     do_test_cyl_bessel_k<T>(kv_data, name, "Bessel Kv: Mathworld Data");
168     if(0 != static_cast<T>(ldexp(0.5, -512)))
169       do_test_cyl_bessel_k<T>(kv_large_data, name, "Bessel Kv: Mathworld Data (large values)");
170 #include "bessel_k_int_data.ipp"
171     do_test_cyl_bessel_k<T>(bessel_k_int_data, name, "Bessel Kn: Random Data");
172 #include "bessel_k_data.ipp"
173     do_test_cyl_bessel_k<T>(bessel_k_data, name, "Bessel Kv: Random Data");
174 }
175