1// Copyright (c) 2006 Xiaogang Zhang
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#ifndef BOOST_MATH_BESSEL_J0_HPP
7#define BOOST_MATH_BESSEL_J0_HPP
8
9#ifdef _MSC_VER
10#pragma once
11#endif
12
13#include <boost/math/constants/constants.hpp>
14#include <boost/math/tools/rational.hpp>
15#include <boost/math/tools/big_constant.hpp>
16#include <boost/assert.hpp>
17
18#if defined(__GNUC__) && defined(BOOST_MATH_USE_FLOAT128)
19//
20// This is the only way we can avoid
21// warning: non-standard suffix on floating constant [-Wpedantic]
22// when building with -Wall -pedantic. Neither __extension__
23// nor #pragma diagnostic ignored work :(
24//
25#pragma GCC system_header
26#endif
27
28// Bessel function of the first kind of order zero
29// x <= 8, minimax rational approximations on root-bracketing intervals
30// x > 8, Hankel asymptotic expansion in Hart, Computer Approximations, 1968
31
32namespace boost { namespace math { namespace detail{
33
34template <typename T>
35T bessel_j0(T x);
36
37template <class T>
38struct bessel_j0_initializer
39{
40 struct init
41 {
42 init()
43 {
44 do_init();
45 }
46 static void do_init()
47 {
48 bessel_j0(T(1));
49 }
50 void force_instantiate()const{}
51 };
52 static const init initializer;
53 static void force_instantiate()
54 {
55 initializer.force_instantiate();
56 }
57};
58
59template <class T>
60const typename bessel_j0_initializer<T>::init bessel_j0_initializer<T>::initializer;
61
62template <typename T>
63T bessel_j0(T x)
64{
65 bessel_j0_initializer<T>::force_instantiate();
66
67 static const T P1[] = {
68 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -4.1298668500990866786e+11)),
69 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.7282507878605942706e+10)),
70 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -6.2140700423540120665e+08)),
71 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 6.6302997904833794242e+06)),
72 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -3.6629814655107086448e+04)),
73 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0344222815443188943e+02)),
74 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.2117036164593528341e-01))
75 };
76 static const T Q1[] = {
77 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.3883787996332290397e+12)),
78 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.6328198300859648632e+10)),
79 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.3985097372263433271e+08)),
80 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.5612696224219938200e+05)),
81 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 9.3614022392337710626e+02)),
82 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0)),
83 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 0.0))
84 };
85 static const T P2[] = {
86 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.8319397969392084011e+03)),
87 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.2254078161378989535e+04)),
88 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -7.2879702464464618998e+03)),
89 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0341910641583726701e+04)),
90 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.1725046279757103576e+04)),
91 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.4176707025325087628e+03)),
92 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 7.4321196680624245801e+02)),
93 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.8591703355916499363e+01))
94 };
95 static const T Q2[] = {
96 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -3.5783478026152301072e+05)),
97 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.4599102262586308984e+05)),
98 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.4055062591169562211e+04)),
99 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.8680990008359188352e+04)),
100 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.9458766545509337327e+03)),
101 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.3307310774649071172e+02)),
102 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.5258076240801555057e+01)),
103 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0))
104 };
105 static const T PC[] = {
106 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.2779090197304684302e+04)),
107 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.1345386639580765797e+04)),
108 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1170523380864944322e+04)),
109 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.4806486443249270347e+03)),
110 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.5376201909008354296e+02)),
111 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 8.8961548424210455236e-01))
112 };
113 static const T QC[] = {
114 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.2779090197304684318e+04)),
115 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 4.1370412495510416640e+04)),
116 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.1215350561880115730e+04)),
117 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 3.5028735138235608207e+03)),
118 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.5711159858080893649e+02)),
119 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0))
120 };
121 static const T PS[] = {
122 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.9226600200800094098e+01)),
123 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.8591953644342993800e+02)),
124 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.1183429920482737611e+02)),
125 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -2.2300261666214198472e+01)),
126 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.2441026745835638459e+00)),
127 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -8.8033303048680751817e-03))
128 };
129 static const T QS[] = {
130 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.7105024128512061905e+03)),
131 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.1951131543434613647e+04)),
132 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 7.2642780169211018836e+03)),
133 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.4887231232283756582e+03)),
134 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 9.0593769594993125859e+01)),
135 static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.0))
136 };
137 static const T x1 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 2.4048255576957727686e+00)),
138 x2 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.5200781102863106496e+00)),
139 x11 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 6.160e+02)),
140 x12 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, -1.42444230422723137837e-03)),
141 x21 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1.4130e+03)),
142 x22 = static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 5.46860286310649596604e-04));
143
144 T value, factor, r, rc, rs;
145
146 BOOST_MATH_STD_USING
147 using namespace boost::math::tools;
148 using namespace boost::math::constants;
149
150 if (x < 0)
151 {
152 x = -x; // even function
153 }
154 if (x == 0)
155 {
156 return static_cast<T>(1);
157 }
158 if (x <= 4) // x in (0, 4]
159 {
160 T y = x * x;
161 BOOST_ASSERT(sizeof(P1) == sizeof(Q1));
162 r = evaluate_rational(P1, Q1, y);
163 factor = (x + x1) * ((x - x11/256) - x12);
164 value = factor * r;
165 }
166 else if (x <= 8.0) // x in (4, 8]
167 {
168 T y = 1 - (x * x)/64;
169 BOOST_ASSERT(sizeof(P2) == sizeof(Q2));
170 r = evaluate_rational(P2, Q2, y);
171 factor = (x + x2) * ((x - x21/256) - x22);
172 value = factor * r;
173 }
174 else // x in (8, \infty)
175 {
176 T y = 8 / x;
177 T y2 = y * y;
178 BOOST_ASSERT(sizeof(PC) == sizeof(QC));
179 BOOST_ASSERT(sizeof(PS) == sizeof(QS));
180 rc = evaluate_rational(PC, QC, y2);
181 rs = evaluate_rational(PS, QS, y2);
182 factor = constants::one_div_root_pi<T>() / sqrt(x);
183 //
184 // What follows is really just:
185 //
186 // T z = x - pi/4;
187 // value = factor * (rc * cos(z) - y * rs * sin(z));
188 //
189 // But using the addition formulae for sin and cos, plus
190 // the special values for sin/cos of pi/4.
191 //
192 T sx = sin(x);
193 T cx = cos(x);
194 value = factor * (rc * (cx + sx) - y * rs * (sx - cx));
195 }
196
197 return value;
198}
199
200}}} // namespaces
201
202#endif // BOOST_MATH_BESSEL_J0_HPP
203
204

source code of include/boost/math/special_functions/detail/bessel_j0.hpp