]>
Commit | Line | Data |
---|---|---|
1 | // Copyright Benjamin Sobotta 2012 | |
2 | ||
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 | ||
7 | #ifndef BOOST_OWENS_T_HPP | |
8 | #define BOOST_OWENS_T_HPP | |
9 | ||
10 | // Reference: | |
11 | // Mike Patefield, David Tandy | |
12 | // FAST AND ACCURATE CALCULATION OF OWEN'S T-FUNCTION | |
13 | // Journal of Statistical Software, 5 (5), 1-25 | |
14 | ||
15 | #ifdef _MSC_VER | |
16 | # pragma once | |
17 | #endif | |
18 | ||
19 | #include <boost/math/special_functions/math_fwd.hpp> | |
20 | #include <boost/config/no_tr1/cmath.hpp> | |
21 | #include <boost/math/special_functions/erf.hpp> | |
22 | #include <boost/math/special_functions/expm1.hpp> | |
23 | #include <boost/throw_exception.hpp> | |
24 | #include <boost/assert.hpp> | |
25 | #include <boost/math/constants/constants.hpp> | |
26 | #include <boost/math/tools/big_constant.hpp> | |
27 | ||
28 | #include <stdexcept> | |
29 | ||
30 | #ifdef BOOST_MSVC | |
31 | #pragma warning(push) | |
32 | #pragma warning(disable:4127) | |
33 | #endif | |
34 | ||
35 | namespace boost | |
36 | { | |
37 | namespace math | |
38 | { | |
39 | namespace detail | |
40 | { | |
41 | // owens_t_znorm1(x) = P(-oo<Z<=x)-0.5 with Z being normally distributed. | |
42 | template<typename RealType> | |
43 | inline RealType owens_t_znorm1(const RealType x) | |
44 | { | |
45 | using namespace boost::math::constants; | |
46 | return erf(x*one_div_root_two<RealType>())*half<RealType>(); | |
47 | } // RealType owens_t_znorm1(const RealType x) | |
48 | ||
49 | // owens_t_znorm2(x) = P(x<=Z<oo) with Z being normally distributed. | |
50 | template<typename RealType> | |
51 | inline RealType owens_t_znorm2(const RealType x) | |
52 | { | |
53 | using namespace boost::math::constants; | |
54 | return erfc(x*one_div_root_two<RealType>())*half<RealType>(); | |
55 | } // RealType owens_t_znorm2(const RealType x) | |
56 | ||
57 | // Auxiliary function, it computes an array key that is used to determine | |
58 | // the specific computation method for Owen's T and the order thereof | |
59 | // used in owens_t_dispatch. | |
60 | template<typename RealType> | |
61 | inline unsigned short owens_t_compute_code(const RealType h, const RealType a) | |
62 | { | |
63 | static const RealType hrange[] = | |
64 | { 0.02f, 0.06f, 0.09f, 0.125f, 0.26f, 0.4f, 0.6f, 1.6f, 1.7f, 2.33f, 2.4f, 3.36f, 3.4f, 4.8f }; | |
65 | ||
66 | static const RealType arange[] = { 0.025f, 0.09f, 0.15f, 0.36f, 0.5f, 0.9f, 0.99999f }; | |
67 | /* | |
68 | original select array from paper: | |
69 | 1, 1, 2,13,13,13,13,13,13,13,13,16,16,16, 9 | |
70 | 1, 2, 2, 3, 3, 5, 5,14,14,15,15,16,16,16, 9 | |
71 | 2, 2, 3, 3, 3, 5, 5,15,15,15,15,16,16,16,10 | |
72 | 2, 2, 3, 5, 5, 5, 5, 7, 7,16,16,16,16,16,10 | |
73 | 2, 3, 3, 5, 5, 6, 6, 8, 8,17,17,17,12,12,11 | |
74 | 2, 3, 5, 5, 5, 6, 6, 8, 8,17,17,17,12,12,12 | |
75 | 2, 3, 4, 4, 6, 6, 8, 8,17,17,17,17,17,12,12 | |
76 | 2, 3, 4, 4, 6, 6,18,18,18,18,17,17,17,12,12 | |
77 | */ | |
78 | // subtract one because the array is written in FORTRAN in mind - in C arrays start @ zero | |
79 | static const unsigned short select[] = | |
80 | { | |
81 | 0, 0 , 1 , 12 ,12 , 12 , 12 , 12 , 12 , 12 , 12 , 15 , 15 , 15 , 8, | |
82 | 0 , 1 , 1 , 2 , 2 , 4 , 4 , 13 , 13 , 14 , 14 , 15 , 15 , 15 , 8, | |
83 | 1 , 1 , 2 , 2 , 2 , 4 , 4 , 14 , 14 , 14 , 14 , 15 , 15 , 15 , 9, | |
84 | 1 , 1 , 2 , 4 , 4 , 4 , 4 , 6 , 6 , 15 , 15 , 15 , 15 , 15 , 9, | |
85 | 1 , 2 , 2 , 4 , 4 , 5 , 5 , 7 , 7 , 16 ,16 , 16 , 11 , 11 , 10, | |
86 | 1 , 2 , 4 , 4 , 4 , 5 , 5 , 7 , 7 , 16 , 16 , 16 , 11 , 11 , 11, | |
87 | 1 , 2 , 3 , 3 , 5 , 5 , 7 , 7 , 16 , 16 , 16 , 16 , 16 , 11 , 11, | |
88 | 1 , 2 , 3 , 3 , 5 , 5 , 17 , 17 , 17 , 17 , 16 , 16 , 16 , 11 , 11 | |
89 | }; | |
90 | ||
91 | unsigned short ihint = 14, iaint = 7; | |
92 | for(unsigned short i = 0; i != 14; i++) | |
93 | { | |
94 | if( h <= hrange[i] ) | |
95 | { | |
96 | ihint = i; | |
97 | break; | |
98 | } | |
99 | } // for(unsigned short i = 0; i != 14; i++) | |
100 | ||
101 | for(unsigned short i = 0; i != 7; i++) | |
102 | { | |
103 | if( a <= arange[i] ) | |
104 | { | |
105 | iaint = i; | |
106 | break; | |
107 | } | |
108 | } // for(unsigned short i = 0; i != 7; i++) | |
109 | ||
110 | // interprete select array as 8x15 matrix | |
111 | return select[iaint*15 + ihint]; | |
112 | ||
113 | } // unsigned short owens_t_compute_code(const RealType h, const RealType a) | |
114 | ||
115 | template<typename RealType> | |
116 | inline unsigned short owens_t_get_order_imp(const unsigned short icode, RealType, const mpl::int_<53>&) | |
117 | { | |
118 | static const unsigned short ord[] = {2, 3, 4, 5, 7, 10, 12, 18, 10, 20, 30, 0, 4, 7, 8, 20, 0, 0}; // 18 entries | |
119 | ||
120 | BOOST_ASSERT(icode<18); | |
121 | ||
122 | return ord[icode]; | |
123 | } // unsigned short owens_t_get_order(const unsigned short icode, RealType, mpl::int<53> const&) | |
124 | ||
125 | template<typename RealType> | |
126 | inline unsigned short owens_t_get_order_imp(const unsigned short icode, RealType, const mpl::int_<64>&) | |
127 | { | |
128 | // method ================>>> {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 5, 6} | |
129 | static const unsigned short ord[] = {3, 4, 5, 6, 8, 11, 13, 19, 10, 20, 30, 0, 7, 10, 11, 23, 0, 0}; // 18 entries | |
130 | ||
131 | BOOST_ASSERT(icode<18); | |
132 | ||
133 | return ord[icode]; | |
134 | } // unsigned short owens_t_get_order(const unsigned short icode, RealType, mpl::int<64> const&) | |
135 | ||
136 | template<typename RealType, typename Policy> | |
137 | inline unsigned short owens_t_get_order(const unsigned short icode, RealType r, const Policy&) | |
138 | { | |
139 | typedef typename policies::precision<RealType, Policy>::type precision_type; | |
140 | typedef typename mpl::if_< | |
141 | mpl::or_< | |
142 | mpl::less_equal<precision_type, mpl::int_<0> >, | |
143 | mpl::greater<precision_type, mpl::int_<53> > | |
144 | >, | |
145 | mpl::int_<64>, | |
146 | mpl::int_<53> | |
147 | >::type tag_type; | |
148 | ||
149 | return owens_t_get_order_imp(icode, r, tag_type()); | |
150 | } | |
151 | ||
152 | // compute the value of Owen's T function with method T1 from the reference paper | |
153 | template<typename RealType, typename Policy> | |
154 | inline RealType owens_t_T1(const RealType h, const RealType a, const unsigned short m, const Policy& pol) | |
155 | { | |
156 | BOOST_MATH_STD_USING | |
157 | using namespace boost::math::constants; | |
158 | ||
159 | const RealType hs = -h*h*half<RealType>(); | |
160 | const RealType dhs = exp( hs ); | |
161 | const RealType as = a*a; | |
162 | ||
163 | unsigned short j=1; | |
164 | RealType jj = 1; | |
165 | RealType aj = a * one_div_two_pi<RealType>(); | |
166 | RealType dj = boost::math::expm1( hs, pol); | |
167 | RealType gj = hs*dhs; | |
168 | ||
169 | RealType val = atan( a ) * one_div_two_pi<RealType>(); | |
170 | ||
171 | while( true ) | |
172 | { | |
173 | val += dj*aj/jj; | |
174 | ||
175 | if( m <= j ) | |
176 | break; | |
177 | ||
178 | j++; | |
179 | jj += static_cast<RealType>(2); | |
180 | aj *= as; | |
181 | dj = gj - dj; | |
182 | gj *= hs / static_cast<RealType>(j); | |
183 | } // while( true ) | |
184 | ||
185 | return val; | |
186 | } // RealType owens_t_T1(const RealType h, const RealType a, const unsigned short m) | |
187 | ||
188 | // compute the value of Owen's T function with method T2 from the reference paper | |
189 | template<typename RealType, class Policy> | |
190 | inline RealType owens_t_T2(const RealType h, const RealType a, const unsigned short m, const RealType ah, const Policy&, const mpl::false_&) | |
191 | { | |
192 | BOOST_MATH_STD_USING | |
193 | using namespace boost::math::constants; | |
194 | ||
195 | const unsigned short maxii = m+m+1; | |
196 | const RealType hs = h*h; | |
197 | const RealType as = -a*a; | |
198 | const RealType y = static_cast<RealType>(1) / hs; | |
199 | ||
200 | unsigned short ii = 1; | |
201 | RealType val = 0; | |
202 | RealType vi = a * exp( -ah*ah*half<RealType>() ) * one_div_root_two_pi<RealType>(); | |
203 | RealType z = owens_t_znorm1(ah)/h; | |
204 | ||
205 | while( true ) | |
206 | { | |
207 | val += z; | |
208 | if( maxii <= ii ) | |
209 | { | |
210 | val *= exp( -hs*half<RealType>() ) * one_div_root_two_pi<RealType>(); | |
211 | break; | |
212 | } // if( maxii <= ii ) | |
213 | z = y * ( vi - static_cast<RealType>(ii) * z ); | |
214 | vi *= as; | |
215 | ii += 2; | |
216 | } // while( true ) | |
217 | ||
218 | return val; | |
219 | } // RealType owens_t_T2(const RealType h, const RealType a, const unsigned short m, const RealType ah) | |
220 | ||
221 | // compute the value of Owen's T function with method T3 from the reference paper | |
222 | template<typename RealType> | |
223 | inline RealType owens_t_T3_imp(const RealType h, const RealType a, const RealType ah, const mpl::int_<53>&) | |
224 | { | |
225 | BOOST_MATH_STD_USING | |
226 | using namespace boost::math::constants; | |
227 | ||
228 | const unsigned short m = 20; | |
229 | ||
230 | static const RealType c2[] = | |
231 | { | |
232 | static_cast<RealType>(0.99999999999999987510), | |
233 | static_cast<RealType>(-0.99999999999988796462), static_cast<RealType>(0.99999999998290743652), | |
234 | static_cast<RealType>(-0.99999999896282500134), static_cast<RealType>(0.99999996660459362918), | |
235 | static_cast<RealType>(-0.99999933986272476760), static_cast<RealType>(0.99999125611136965852), | |
236 | static_cast<RealType>(-0.99991777624463387686), static_cast<RealType>(0.99942835555870132569), | |
237 | static_cast<RealType>(-0.99697311720723000295), static_cast<RealType>(0.98751448037275303682), | |
238 | static_cast<RealType>(-0.95915857980572882813), static_cast<RealType>(0.89246305511006708555), | |
239 | static_cast<RealType>(-0.76893425990463999675), static_cast<RealType>(0.58893528468484693250), | |
240 | static_cast<RealType>(-0.38380345160440256652), static_cast<RealType>(0.20317601701045299653), | |
241 | static_cast<RealType>(-0.82813631607004984866E-01), static_cast<RealType>(0.24167984735759576523E-01), | |
242 | static_cast<RealType>(-0.44676566663971825242E-02), static_cast<RealType>(0.39141169402373836468E-03) | |
243 | }; | |
244 | ||
245 | const RealType as = a*a; | |
246 | const RealType hs = h*h; | |
247 | const RealType y = static_cast<RealType>(1)/hs; | |
248 | ||
249 | RealType ii = 1; | |
250 | unsigned short i = 0; | |
251 | RealType vi = a * exp( -ah*ah*half<RealType>() ) * one_div_root_two_pi<RealType>(); | |
252 | RealType zi = owens_t_znorm1(ah)/h; | |
253 | RealType val = 0; | |
254 | ||
255 | while( true ) | |
256 | { | |
257 | BOOST_ASSERT(i < 21); | |
258 | val += zi*c2[i]; | |
259 | if( m <= i ) // if( m < i+1 ) | |
260 | { | |
261 | val *= exp( -hs*half<RealType>() ) * one_div_root_two_pi<RealType>(); | |
262 | break; | |
263 | } // if( m < i ) | |
264 | zi = y * (ii*zi - vi); | |
265 | vi *= as; | |
266 | ii += 2; | |
267 | i++; | |
268 | } // while( true ) | |
269 | ||
270 | return val; | |
271 | } // RealType owens_t_T3(const RealType h, const RealType a, const RealType ah) | |
272 | ||
273 | // compute the value of Owen's T function with method T3 from the reference paper | |
274 | template<class RealType> | |
275 | inline RealType owens_t_T3_imp(const RealType h, const RealType a, const RealType ah, const mpl::int_<64>&) | |
276 | { | |
277 | BOOST_MATH_STD_USING | |
278 | using namespace boost::math::constants; | |
279 | ||
280 | const unsigned short m = 30; | |
281 | ||
282 | static const RealType c2[] = | |
283 | { | |
284 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.99999999999999999999999729978162447266851932041876728736094298092917625009873), | |
285 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.99999999999999999999467056379678391810626533251885323416799874878563998732905968), | |
286 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.99999999999999999824849349313270659391127814689133077036298754586814091034842536), | |
287 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.9999999999999997703859616213643405880166422891953033591551179153879839440241685), | |
288 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.99999999999998394883415238173334565554173013941245103172035286759201504179038147), | |
289 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.9999999999993063616095509371081203145247992197457263066869044528823599399470977), | |
290 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.9999999999797336340409464429599229870590160411238245275855903767652432017766116267), | |
291 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.999999999574958412069046680119051639753412378037565521359444170241346845522403274), | |
292 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.9999999933226234193375324943920160947158239076786103108097456617750134812033362048), | |
293 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.9999999188923242461073033481053037468263536806742737922476636768006622772762168467), | |
294 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.9999992195143483674402853783549420883055129680082932629160081128947764415749728967), | |
295 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.999993935137206712830997921913316971472227199741857386575097250553105958772041501), | |
296 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.99996135597690552745362392866517133091672395614263398912807169603795088421057688716), | |
297 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.99979556366513946026406788969630293820987757758641211293079784585126692672425362469), | |
298 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.999092789629617100153486251423850590051366661947344315423226082520411961968929483), | |
299 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.996593837411918202119308620432614600338157335862888580671450938858935084316004769854), | |
300 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.98910017138386127038463510314625339359073956513420458166238478926511821146316469589567), | |
301 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.970078558040693314521331982203762771512160168582494513347846407314584943870399016019), | |
302 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.92911438683263187495758525500033707204091967947532160289872782771388170647150321633673), | |
303 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.8542058695956156057286980736842905011429254735181323743367879525470479126968822863), | |
304 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.73796526033030091233118357742803709382964420335559408722681794195743240930748630755), | |
305 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.58523469882837394570128599003785154144164680587615878645171632791404210655891158), | |
306 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.415997776145676306165661663581868460503874205343014196580122174949645271353372263), | |
307 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.2588210875241943574388730510317252236407805082485246378222935376279663808416534365), | |
308 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.1375535825163892648504646951500265585055789019410617565727090346559210218472356689), | |
309 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.0607952766325955730493900985022020434830339794955745989150270485056436844239206648), | |
310 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.0216337683299871528059836483840390514275488679530797294557060229266785853764115), | |
311 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -0.00593405693455186729876995814181203900550014220428843483927218267309209471516256), | |
312 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 0.0011743414818332946510474576182739210553333860106811865963485870668929503649964142), | |
313 | BOOST_MATH_BIG_CONSTANT(RealType, 260, -1.489155613350368934073453260689881330166342484405529981510694514036264969925132e-4), | |
314 | BOOST_MATH_BIG_CONSTANT(RealType, 260, 9.072354320794357587710929507988814669454281514268844884841547607134260303118208e-6) | |
315 | }; | |
316 | ||
317 | const RealType as = a*a; | |
318 | const RealType hs = h*h; | |
319 | const RealType y = 1 / hs; | |
320 | ||
321 | RealType ii = 1; | |
322 | unsigned short i = 0; | |
323 | RealType vi = a * exp( -ah*ah*half<RealType>() ) * one_div_root_two_pi<RealType>(); | |
324 | RealType zi = owens_t_znorm1(ah)/h; | |
325 | RealType val = 0; | |
326 | ||
327 | while( true ) | |
328 | { | |
329 | BOOST_ASSERT(i < 31); | |
330 | val += zi*c2[i]; | |
331 | if( m <= i ) // if( m < i+1 ) | |
332 | { | |
333 | val *= exp( -hs*half<RealType>() ) * one_div_root_two_pi<RealType>(); | |
334 | break; | |
335 | } // if( m < i ) | |
336 | zi = y * (ii*zi - vi); | |
337 | vi *= as; | |
338 | ii += 2; | |
339 | i++; | |
340 | } // while( true ) | |
341 | ||
342 | return val; | |
343 | } // RealType owens_t_T3(const RealType h, const RealType a, const RealType ah) | |
344 | ||
345 | template<class RealType, class Policy> | |
346 | inline RealType owens_t_T3(const RealType h, const RealType a, const RealType ah, const Policy&) | |
347 | { | |
348 | typedef typename policies::precision<RealType, Policy>::type precision_type; | |
349 | typedef typename mpl::if_< | |
350 | mpl::or_< | |
351 | mpl::less_equal<precision_type, mpl::int_<0> >, | |
352 | mpl::greater<precision_type, mpl::int_<53> > | |
353 | >, | |
354 | mpl::int_<64>, | |
355 | mpl::int_<53> | |
356 | >::type tag_type; | |
357 | ||
358 | return owens_t_T3_imp(h, a, ah, tag_type()); | |
359 | } | |
360 | ||
361 | // compute the value of Owen's T function with method T4 from the reference paper | |
362 | template<typename RealType> | |
363 | inline RealType owens_t_T4(const RealType h, const RealType a, const unsigned short m) | |
364 | { | |
365 | BOOST_MATH_STD_USING | |
366 | using namespace boost::math::constants; | |
367 | ||
368 | const unsigned short maxii = m+m+1; | |
369 | const RealType hs = h*h; | |
370 | const RealType as = -a*a; | |
371 | ||
372 | unsigned short ii = 1; | |
373 | RealType ai = a * exp( -hs*(static_cast<RealType>(1)-as)*half<RealType>() ) * one_div_two_pi<RealType>(); | |
374 | RealType yi = 1; | |
375 | RealType val = 0; | |
376 | ||
377 | while( true ) | |
378 | { | |
379 | val += ai*yi; | |
380 | if( maxii <= ii ) | |
381 | break; | |
382 | ii += 2; | |
383 | yi = (static_cast<RealType>(1)-hs*yi) / static_cast<RealType>(ii); | |
384 | ai *= as; | |
385 | } // while( true ) | |
386 | ||
387 | return val; | |
388 | } // RealType owens_t_T4(const RealType h, const RealType a, const unsigned short m) | |
389 | ||
390 | // compute the value of Owen's T function with method T5 from the reference paper | |
391 | template<typename RealType> | |
392 | inline RealType owens_t_T5_imp(const RealType h, const RealType a, const mpl::int_<53>&) | |
393 | { | |
394 | BOOST_MATH_STD_USING | |
395 | /* | |
396 | NOTICE: | |
397 | - The pts[] array contains the squares (!) of the abscissas, i.e. the roots of the Legendre | |
398 | polynomial P_n(x), instead of the plain roots as required in Gauss-Legendre | |
399 | quadrature, because T5(h,a,m) contains only x^2 terms. | |
400 | - The wts[] array contains the weights for Gauss-Legendre quadrature scaled with a factor | |
401 | of 1/(2*pi) according to T5(h,a,m). | |
402 | */ | |
403 | ||
404 | const unsigned short m = 13; | |
405 | static const RealType pts[] = { | |
406 | static_cast<RealType>(0.35082039676451715489E-02), | |
407 | static_cast<RealType>(0.31279042338030753740E-01), static_cast<RealType>(0.85266826283219451090E-01), | |
408 | static_cast<RealType>(0.16245071730812277011), static_cast<RealType>(0.25851196049125434828), | |
409 | static_cast<RealType>(0.36807553840697533536), static_cast<RealType>(0.48501092905604697475), | |
410 | static_cast<RealType>(0.60277514152618576821), static_cast<RealType>(0.71477884217753226516), | |
411 | static_cast<RealType>(0.81475510988760098605), static_cast<RealType>(0.89711029755948965867), | |
412 | static_cast<RealType>(0.95723808085944261843), static_cast<RealType>(0.99178832974629703586) }; | |
413 | static const RealType wts[] = { | |
414 | static_cast<RealType>(0.18831438115323502887E-01), | |
415 | static_cast<RealType>(0.18567086243977649478E-01), static_cast<RealType>(0.18042093461223385584E-01), | |
416 | static_cast<RealType>(0.17263829606398753364E-01), static_cast<RealType>(0.16243219975989856730E-01), | |
417 | static_cast<RealType>(0.14994592034116704829E-01), static_cast<RealType>(0.13535474469662088392E-01), | |
418 | static_cast<RealType>(0.11886351605820165233E-01), static_cast<RealType>(0.10070377242777431897E-01), | |
419 | static_cast<RealType>(0.81130545742299586629E-02), static_cast<RealType>(0.60419009528470238773E-02), | |
420 | static_cast<RealType>(0.38862217010742057883E-02), static_cast<RealType>(0.16793031084546090448E-02) }; | |
421 | ||
422 | const RealType as = a*a; | |
423 | const RealType hs = -h*h*boost::math::constants::half<RealType>(); | |
424 | ||
425 | RealType val = 0; | |
426 | for(unsigned short i = 0; i < m; ++i) | |
427 | { | |
428 | BOOST_ASSERT(i < 13); | |
429 | const RealType r = static_cast<RealType>(1) + as*pts[i]; | |
430 | val += wts[i] * exp( hs*r ) / r; | |
431 | } // for(unsigned short i = 0; i < m; ++i) | |
432 | ||
433 | return val*a; | |
434 | } // RealType owens_t_T5(const RealType h, const RealType a) | |
435 | ||
436 | // compute the value of Owen's T function with method T5 from the reference paper | |
437 | template<typename RealType> | |
438 | inline RealType owens_t_T5_imp(const RealType h, const RealType a, const mpl::int_<64>&) | |
439 | { | |
440 | BOOST_MATH_STD_USING | |
441 | /* | |
442 | NOTICE: | |
443 | - The pts[] array contains the squares (!) of the abscissas, i.e. the roots of the Legendre | |
444 | polynomial P_n(x), instead of the plain roots as required in Gauss-Legendre | |
445 | quadrature, because T5(h,a,m) contains only x^2 terms. | |
446 | - The wts[] array contains the weights for Gauss-Legendre quadrature scaled with a factor | |
447 | of 1/(2*pi) according to T5(h,a,m). | |
448 | */ | |
449 | ||
450 | const unsigned short m = 19; | |
451 | static const RealType pts[] = { | |
452 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.0016634282895983227941), | |
453 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.014904509242697054183), | |
454 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.04103478879005817919), | |
455 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.079359853513391511008), | |
456 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.1288612130237615133), | |
457 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.18822336642448518856), | |
458 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.25586876186122962384), | |
459 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.32999972011807857222), | |
460 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.40864620815774761438), | |
461 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.48971819306044782365), | |
462 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.57106118513245543894), | |
463 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.6505134942981533829), | |
464 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.72596367859928091618), | |
465 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.79540665919549865924), | |
466 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.85699701386308739244), | |
467 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.90909804422384697594), | |
468 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.95032536436570154409), | |
469 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.97958418733152273717), | |
470 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.99610366384229088321) | |
471 | }; | |
472 | static const RealType wts[] = { | |
473 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.012975111395684900835), | |
474 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.012888764187499150078), | |
475 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.012716644398857307844), | |
476 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.012459897461364705691), | |
477 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.012120231988292330388), | |
478 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.011699908404856841158), | |
479 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.011201723906897224448), | |
480 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.010628993848522759853), | |
481 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.0099855296835573320047), | |
482 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.0092756136096132857933), | |
483 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.0085039700881139589055), | |
484 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.0076757344408814561254), | |
485 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.0067964187616556459109), | |
486 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.005871875456524750363), | |
487 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.0049082589542498110071), | |
488 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.0039119870792519721409), | |
489 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.0028897090921170700834), | |
490 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.0018483371329504443947), | |
491 | BOOST_MATH_BIG_CONSTANT(RealType, 64, 0.00079623320100438873578) | |
492 | }; | |
493 | ||
494 | const RealType as = a*a; | |
495 | const RealType hs = -h*h*boost::math::constants::half<RealType>(); | |
496 | ||
497 | RealType val = 0; | |
498 | for(unsigned short i = 0; i < m; ++i) | |
499 | { | |
500 | BOOST_ASSERT(i < 19); | |
501 | const RealType r = 1 + as*pts[i]; | |
502 | val += wts[i] * exp( hs*r ) / r; | |
503 | } // for(unsigned short i = 0; i < m; ++i) | |
504 | ||
505 | return val*a; | |
506 | } // RealType owens_t_T5(const RealType h, const RealType a) | |
507 | ||
508 | template<class RealType, class Policy> | |
509 | inline RealType owens_t_T5(const RealType h, const RealType a, const Policy&) | |
510 | { | |
511 | typedef typename policies::precision<RealType, Policy>::type precision_type; | |
512 | typedef typename mpl::if_< | |
513 | mpl::or_< | |
514 | mpl::less_equal<precision_type, mpl::int_<0> >, | |
515 | mpl::greater<precision_type, mpl::int_<53> > | |
516 | >, | |
517 | mpl::int_<64>, | |
518 | mpl::int_<53> | |
519 | >::type tag_type; | |
520 | ||
521 | return owens_t_T5_imp(h, a, tag_type()); | |
522 | } | |
523 | ||
524 | ||
525 | // compute the value of Owen's T function with method T6 from the reference paper | |
526 | template<typename RealType> | |
527 | inline RealType owens_t_T6(const RealType h, const RealType a) | |
528 | { | |
529 | BOOST_MATH_STD_USING | |
530 | using namespace boost::math::constants; | |
531 | ||
532 | const RealType normh = owens_t_znorm2( h ); | |
533 | const RealType y = static_cast<RealType>(1) - a; | |
534 | const RealType r = atan2(y, static_cast<RealType>(1 + a) ); | |
535 | ||
536 | RealType val = normh * ( static_cast<RealType>(1) - normh ) * half<RealType>(); | |
537 | ||
538 | if( r != 0 ) | |
539 | val -= r * exp( -y*h*h*half<RealType>()/r ) * one_div_two_pi<RealType>(); | |
540 | ||
541 | return val; | |
542 | } // RealType owens_t_T6(const RealType h, const RealType a, const unsigned short m) | |
543 | ||
544 | template <class T, class Policy> | |
545 | std::pair<T, T> owens_t_T1_accelerated(T h, T a, const Policy& pol) | |
546 | { | |
547 | // | |
548 | // This is the same series as T1, but: | |
549 | // * The Taylor series for atan has been combined with that for T1, | |
550 | // reducing but not eliminating cancellation error. | |
551 | // * The resulting alternating series is then accelerated using method 1 | |
552 | // from H. Cohen, F. Rodriguez Villegas, D. Zagier, | |
553 | // "Convergence acceleration of alternating series", Bonn, (1991). | |
554 | // | |
555 | BOOST_MATH_STD_USING | |
556 | static const char* function = "boost::math::owens_t<%1%>(%1%, %1%)"; | |
557 | T half_h_h = h * h / 2; | |
558 | T a_pow = a; | |
559 | T aa = a * a; | |
560 | T exp_term = exp(-h * h / 2); | |
561 | T one_minus_dj_sum = exp_term; | |
562 | T sum = a_pow * exp_term; | |
563 | T dj_pow = exp_term; | |
564 | T term = sum; | |
565 | T abs_err; | |
566 | int j = 1; | |
567 | ||
568 | // | |
569 | // Normally with this form of series acceleration we can calculate | |
570 | // up front how many terms will be required - based on the assumption | |
571 | // that each term decreases in size by a factor of 3. However, | |
572 | // that assumption does not apply here, as the underlying T1 series can | |
573 | // go quite strongly divergent in the early terms, before strongly | |
574 | // converging later. Various "guestimates" have been tried to take account | |
575 | // of this, but they don't always work.... so instead set "n" to the | |
576 | // largest value that won't cause overflow later, and abort iteration | |
577 | // when the last accelerated term was small enough... | |
578 | // | |
579 | int n; | |
580 | #ifndef BOOST_NO_EXCEPTIONS | |
581 | try | |
582 | { | |
583 | #endif | |
584 | n = itrunc(T(tools::log_max_value<T>() / 6)); | |
585 | #ifndef BOOST_NO_EXCEPTIONS | |
586 | } | |
587 | catch(...) | |
588 | { | |
589 | n = (std::numeric_limits<int>::max)(); | |
590 | } | |
591 | #endif | |
592 | n = (std::min)(n, 1500); | |
593 | T d = pow(3 + sqrt(T(8)), n); | |
594 | d = (d + 1 / d) / 2; | |
595 | T b = -1; | |
596 | T c = -d; | |
597 | c = b - c; | |
598 | sum *= c; | |
599 | b = -n * n * b * 2; | |
600 | abs_err = ldexp(fabs(sum), -tools::digits<T>()); | |
601 | ||
602 | while(j < n) | |
603 | { | |
604 | a_pow *= aa; | |
605 | dj_pow *= half_h_h / j; | |
606 | one_minus_dj_sum += dj_pow; | |
607 | term = one_minus_dj_sum * a_pow / (2 * j + 1); | |
608 | c = b - c; | |
609 | sum += c * term; | |
610 | abs_err += ldexp((std::max)(T(fabs(sum)), T(fabs(c*term))), -tools::digits<T>()); | |
611 | b = (j + n) * (j - n) * b / ((j + T(0.5)) * (j + 1)); | |
612 | ++j; | |
613 | // | |
614 | // Include an escape route to prevent calculating too many terms: | |
615 | // | |
616 | if((j > 10) && (fabs(sum * tools::epsilon<T>()) > fabs(c * term))) | |
617 | break; | |
618 | } | |
619 | abs_err += fabs(c * term); | |
620 | if(sum < 0) // sum must always be positive, if it's negative something really bad has happend: | |
621 | policies::raise_evaluation_error(function, 0, T(0), pol); | |
622 | return std::pair<T, T>((sum / d) / boost::math::constants::two_pi<T>(), abs_err / sum); | |
623 | } | |
624 | ||
625 | template<typename RealType, class Policy> | |
626 | inline RealType owens_t_T2(const RealType h, const RealType a, const unsigned short m, const RealType ah, const Policy&, const mpl::true_&) | |
627 | { | |
628 | BOOST_MATH_STD_USING | |
629 | using namespace boost::math::constants; | |
630 | ||
631 | const unsigned short maxii = m+m+1; | |
632 | const RealType hs = h*h; | |
633 | const RealType as = -a*a; | |
634 | const RealType y = static_cast<RealType>(1) / hs; | |
635 | ||
636 | unsigned short ii = 1; | |
637 | RealType val = 0; | |
638 | RealType vi = a * exp( -ah*ah*half<RealType>() ) / root_two_pi<RealType>(); | |
639 | RealType z = owens_t_znorm1(ah)/h; | |
640 | RealType last_z = fabs(z); | |
641 | RealType lim = policies::get_epsilon<RealType, Policy>(); | |
642 | ||
643 | while( true ) | |
644 | { | |
645 | val += z; | |
646 | // | |
647 | // This series stops converging after a while, so put a limit | |
648 | // on how far we go before returning our best guess: | |
649 | // | |
650 | if((fabs(lim * val) > fabs(z)) || ((ii > maxii) && (fabs(z) > last_z)) || (z == 0)) | |
651 | { | |
652 | val *= exp( -hs*half<RealType>() ) / root_two_pi<RealType>(); | |
653 | break; | |
654 | } // if( maxii <= ii ) | |
655 | last_z = fabs(z); | |
656 | z = y * ( vi - static_cast<RealType>(ii) * z ); | |
657 | vi *= as; | |
658 | ii += 2; | |
659 | } // while( true ) | |
660 | ||
661 | return val; | |
662 | } // RealType owens_t_T2(const RealType h, const RealType a, const unsigned short m, const RealType ah) | |
663 | ||
664 | template<typename RealType, class Policy> | |
665 | inline std::pair<RealType, RealType> owens_t_T2_accelerated(const RealType h, const RealType a, const RealType ah, const Policy&) | |
666 | { | |
667 | // | |
668 | // This is the same series as T2, but with acceleration applied. | |
669 | // Note that we have to be *very* careful to check that nothing bad | |
670 | // has happened during evaluation - this series will go divergent | |
671 | // and/or fail to alternate at a drop of a hat! :-( | |
672 | // | |
673 | BOOST_MATH_STD_USING | |
674 | using namespace boost::math::constants; | |
675 | ||
676 | const RealType hs = h*h; | |
677 | const RealType as = -a*a; | |
678 | const RealType y = static_cast<RealType>(1) / hs; | |
679 | ||
680 | unsigned short ii = 1; | |
681 | RealType val = 0; | |
682 | RealType vi = a * exp( -ah*ah*half<RealType>() ) / root_two_pi<RealType>(); | |
683 | RealType z = boost::math::detail::owens_t_znorm1(ah)/h; | |
684 | RealType last_z = fabs(z); | |
685 | ||
686 | // | |
687 | // Normally with this form of series acceleration we can calculate | |
688 | // up front how many terms will be required - based on the assumption | |
689 | // that each term decreases in size by a factor of 3. However, | |
690 | // that assumption does not apply here, as the underlying T1 series can | |
691 | // go quite strongly divergent in the early terms, before strongly | |
692 | // converging later. Various "guestimates" have been tried to take account | |
693 | // of this, but they don't always work.... so instead set "n" to the | |
694 | // largest value that won't cause overflow later, and abort iteration | |
695 | // when the last accelerated term was small enough... | |
696 | // | |
697 | int n; | |
698 | #ifndef BOOST_NO_EXCEPTIONS | |
699 | try | |
700 | { | |
701 | #endif | |
702 | n = itrunc(RealType(tools::log_max_value<RealType>() / 6)); | |
703 | #ifndef BOOST_NO_EXCEPTIONS | |
704 | } | |
705 | catch(...) | |
706 | { | |
707 | n = (std::numeric_limits<int>::max)(); | |
708 | } | |
709 | #endif | |
710 | n = (std::min)(n, 1500); | |
711 | RealType d = pow(3 + sqrt(RealType(8)), n); | |
712 | d = (d + 1 / d) / 2; | |
713 | RealType b = -1; | |
714 | RealType c = -d; | |
715 | int s = 1; | |
716 | ||
717 | for(int k = 0; k < n; ++k) | |
718 | { | |
719 | // | |
720 | // Check for both convergence and whether the series has gone bad: | |
721 | // | |
722 | if( | |
723 | (fabs(z) > last_z) // Series has gone divergent, abort | |
724 | || (fabs(val) * tools::epsilon<RealType>() > fabs(c * s * z)) // Convergence! | |
725 | || (z * s < 0) // Series has stopped alternating - all bets are off - abort. | |
726 | ) | |
727 | { | |
728 | break; | |
729 | } | |
730 | c = b - c; | |
731 | val += c * s * z; | |
732 | b = (k + n) * (k - n) * b / ((k + RealType(0.5)) * (k + 1)); | |
733 | last_z = fabs(z); | |
734 | s = -s; | |
735 | z = y * ( vi - static_cast<RealType>(ii) * z ); | |
736 | vi *= as; | |
737 | ii += 2; | |
738 | } // while( true ) | |
739 | RealType err = fabs(c * z) / val; | |
740 | return std::pair<RealType, RealType>(val * exp( -hs*half<RealType>() ) / (d * root_two_pi<RealType>()), err); | |
741 | } // RealType owens_t_T2_accelerated(const RealType h, const RealType a, const RealType ah, const Policy&) | |
742 | ||
743 | template<typename RealType, typename Policy> | |
744 | inline RealType T4_mp(const RealType h, const RealType a, const Policy& pol) | |
745 | { | |
746 | BOOST_MATH_STD_USING | |
747 | ||
748 | const RealType hs = h*h; | |
749 | const RealType as = -a*a; | |
750 | ||
751 | unsigned short ii = 1; | |
752 | RealType ai = constants::one_div_two_pi<RealType>() * a * exp( -0.5*hs*(1.0-as) ); | |
753 | RealType yi = 1.0; | |
754 | RealType val = 0.0; | |
755 | ||
756 | RealType lim = boost::math::policies::get_epsilon<RealType, Policy>(); | |
757 | ||
758 | while( true ) | |
759 | { | |
760 | RealType term = ai*yi; | |
761 | val += term; | |
762 | if((yi != 0) && (fabs(val * lim) > fabs(term))) | |
763 | break; | |
764 | ii += 2; | |
765 | yi = (1.0-hs*yi) / static_cast<RealType>(ii); | |
766 | ai *= as; | |
767 | if(ii > (std::min)(1500, (int)policies::get_max_series_iterations<Policy>())) | |
768 | policies::raise_evaluation_error("boost::math::owens_t<%1%>", 0, val, pol); | |
769 | } // while( true ) | |
770 | ||
771 | return val; | |
772 | } // arg_type owens_t_T4(const arg_type h, const arg_type a, const unsigned short m) | |
773 | ||
774 | ||
775 | // This routine dispatches the call to one of six subroutines, depending on the values | |
776 | // of h and a. | |
777 | // preconditions: h >= 0, 0<=a<=1, ah=a*h | |
778 | // | |
779 | // Note there are different versions for different precisions.... | |
780 | template<typename RealType, typename Policy> | |
781 | inline RealType owens_t_dispatch(const RealType h, const RealType a, const RealType ah, const Policy& pol, mpl::int_<64> const&) | |
782 | { | |
783 | // Simple main case for 64-bit precision or less, this is as per the Patefield-Tandy paper: | |
784 | BOOST_MATH_STD_USING | |
785 | // | |
786 | // Handle some special cases first, these are from | |
787 | // page 1077 of Owen's original paper: | |
788 | // | |
789 | if(h == 0) | |
790 | { | |
791 | return atan(a) * constants::one_div_two_pi<RealType>(); | |
792 | } | |
793 | if(a == 0) | |
794 | { | |
795 | return 0; | |
796 | } | |
797 | if(a == 1) | |
798 | { | |
799 | return owens_t_znorm2(RealType(-h)) * owens_t_znorm2(h) / 2; | |
800 | } | |
801 | if(a >= tools::max_value<RealType>()) | |
802 | { | |
803 | return owens_t_znorm2(RealType(fabs(h))); | |
804 | } | |
805 | RealType val = 0; // avoid compiler warnings, 0 will be overwritten in any case | |
806 | const unsigned short icode = owens_t_compute_code(h, a); | |
807 | const unsigned short m = owens_t_get_order(icode, val /* just a dummy for the type */, pol); | |
808 | static const unsigned short meth[] = {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 4, 4, 4, 5, 6}; // 18 entries | |
809 | ||
810 | // determine the appropriate method, T1 ... T6 | |
811 | switch( meth[icode] ) | |
812 | { | |
813 | case 1: // T1 | |
814 | val = owens_t_T1(h,a,m,pol); | |
815 | break; | |
816 | case 2: // T2 | |
817 | typedef typename policies::precision<RealType, Policy>::type precision_type; | |
818 | typedef mpl::bool_<(precision_type::value == 0) || (precision_type::value > 64)> tag_type; | |
819 | val = owens_t_T2(h, a, m, ah, pol, tag_type()); | |
820 | break; | |
821 | case 3: // T3 | |
822 | val = owens_t_T3(h,a,ah, pol); | |
823 | break; | |
824 | case 4: // T4 | |
825 | val = owens_t_T4(h,a,m); | |
826 | break; | |
827 | case 5: // T5 | |
828 | val = owens_t_T5(h,a, pol); | |
829 | break; | |
830 | case 6: // T6 | |
831 | val = owens_t_T6(h,a); | |
832 | break; | |
833 | default: | |
834 | BOOST_THROW_EXCEPTION(std::logic_error("selection routine in Owen's T function failed")); | |
835 | } | |
836 | return val; | |
837 | } | |
838 | ||
839 | template<typename RealType, typename Policy> | |
840 | inline RealType owens_t_dispatch(const RealType h, const RealType a, const RealType ah, const Policy& pol, const mpl::int_<65>&) | |
841 | { | |
842 | // Arbitrary precision version: | |
843 | BOOST_MATH_STD_USING | |
844 | // | |
845 | // Handle some special cases first, these are from | |
846 | // page 1077 of Owen's original paper: | |
847 | // | |
848 | if(h == 0) | |
849 | { | |
850 | return atan(a) * constants::one_div_two_pi<RealType>(); | |
851 | } | |
852 | if(a == 0) | |
853 | { | |
854 | return 0; | |
855 | } | |
856 | if(a == 1) | |
857 | { | |
858 | return owens_t_znorm2(RealType(-h)) * owens_t_znorm2(h) / 2; | |
859 | } | |
860 | if(a >= tools::max_value<RealType>()) | |
861 | { | |
862 | return owens_t_znorm2(RealType(fabs(h))); | |
863 | } | |
864 | // Attempt arbitrary precision code, this will throw if it goes wrong: | |
865 | typedef typename boost::math::policies::normalise<Policy, boost::math::policies::evaluation_error<> >::type forwarding_policy; | |
866 | std::pair<RealType, RealType> p1(0, tools::max_value<RealType>()), p2(0, tools::max_value<RealType>()); | |
867 | RealType target_precision = policies::get_epsilon<RealType, Policy>() * 1000; | |
868 | bool have_t1(false), have_t2(false); | |
869 | if(ah < 3) | |
870 | { | |
871 | #ifndef BOOST_NO_EXCEPTIONS | |
872 | try | |
873 | { | |
874 | #endif | |
875 | have_t1 = true; | |
876 | p1 = owens_t_T1_accelerated(h, a, forwarding_policy()); | |
877 | if(p1.second < target_precision) | |
878 | return p1.first; | |
879 | #ifndef BOOST_NO_EXCEPTIONS | |
880 | } | |
881 | catch(const boost::math::evaluation_error&){} // T1 may fail and throw, that's OK | |
882 | #endif | |
883 | } | |
884 | if(ah > 1) | |
885 | { | |
886 | #ifndef BOOST_NO_EXCEPTIONS | |
887 | try | |
888 | { | |
889 | #endif | |
890 | have_t2 = true; | |
891 | p2 = owens_t_T2_accelerated(h, a, ah, forwarding_policy()); | |
892 | if(p2.second < target_precision) | |
893 | return p2.first; | |
894 | #ifndef BOOST_NO_EXCEPTIONS | |
895 | } | |
896 | catch(const boost::math::evaluation_error&){} // T2 may fail and throw, that's OK | |
897 | #endif | |
898 | } | |
899 | // | |
900 | // If we haven't tried T1 yet, do it now - sometimes it succeeds and the number of iterations | |
901 | // is fairly low compared to T4. | |
902 | // | |
903 | if(!have_t1) | |
904 | { | |
905 | #ifndef BOOST_NO_EXCEPTIONS | |
906 | try | |
907 | { | |
908 | #endif | |
909 | have_t1 = true; | |
910 | p1 = owens_t_T1_accelerated(h, a, forwarding_policy()); | |
911 | if(p1.second < target_precision) | |
912 | return p1.first; | |
913 | #ifndef BOOST_NO_EXCEPTIONS | |
914 | } | |
915 | catch(const boost::math::evaluation_error&){} // T1 may fail and throw, that's OK | |
916 | #endif | |
917 | } | |
918 | // | |
919 | // If we haven't tried T2 yet, do it now - sometimes it succeeds and the number of iterations | |
920 | // is fairly low compared to T4. | |
921 | // | |
922 | if(!have_t2) | |
923 | { | |
924 | #ifndef BOOST_NO_EXCEPTIONS | |
925 | try | |
926 | { | |
927 | #endif | |
928 | have_t2 = true; | |
929 | p2 = owens_t_T2_accelerated(h, a, ah, forwarding_policy()); | |
930 | if(p2.second < target_precision) | |
931 | return p2.first; | |
932 | #ifndef BOOST_NO_EXCEPTIONS | |
933 | } | |
934 | catch(const boost::math::evaluation_error&){} // T2 may fail and throw, that's OK | |
935 | #endif | |
936 | } | |
937 | // | |
938 | // OK, nothing left to do but try the most expensive option which is T4, | |
939 | // this is often slow to converge, but when it does converge it tends to | |
940 | // be accurate: | |
941 | #ifndef BOOST_NO_EXCEPTIONS | |
942 | try | |
943 | { | |
944 | #endif | |
945 | return T4_mp(h, a, pol); | |
946 | #ifndef BOOST_NO_EXCEPTIONS | |
947 | } | |
948 | catch(const boost::math::evaluation_error&){} // T4 may fail and throw, that's OK | |
949 | #endif | |
950 | // | |
951 | // Now look back at the results from T1 and T2 and see if either gave better | |
952 | // results than we could get from the 64-bit precision versions. | |
953 | // | |
954 | if((std::min)(p1.second, p2.second) < 1e-20) | |
955 | { | |
956 | return p1.second < p2.second ? p1.first : p2.first; | |
957 | } | |
958 | // | |
959 | // We give up - no arbitrary precision versions succeeded! | |
960 | // | |
961 | return owens_t_dispatch(h, a, ah, pol, mpl::int_<64>()); | |
962 | } // RealType owens_t_dispatch(RealType h, RealType a, RealType ah) | |
963 | template<typename RealType, typename Policy> | |
964 | inline RealType owens_t_dispatch(const RealType h, const RealType a, const RealType ah, const Policy& pol, const mpl::int_<0>&) | |
965 | { | |
966 | // We don't know what the precision is until runtime: | |
967 | if(tools::digits<RealType>() <= 64) | |
968 | return owens_t_dispatch(h, a, ah, pol, mpl::int_<64>()); | |
969 | return owens_t_dispatch(h, a, ah, pol, mpl::int_<65>()); | |
970 | } | |
971 | template<typename RealType, typename Policy> | |
972 | inline RealType owens_t_dispatch(const RealType h, const RealType a, const RealType ah, const Policy& pol) | |
973 | { | |
974 | // Figure out the precision and forward to the correct version: | |
975 | typedef typename policies::precision<RealType, Policy>::type precision_type; | |
976 | typedef typename mpl::if_c< | |
977 | precision_type::value == 0, | |
978 | mpl::int_<0>, | |
979 | typename mpl::if_c< | |
980 | precision_type::value <= 64, | |
981 | mpl::int_<64>, | |
982 | mpl::int_<65> | |
983 | >::type | |
984 | >::type tag_type; | |
985 | return owens_t_dispatch(h, a, ah, pol, tag_type()); | |
986 | } | |
987 | // compute Owen's T function, T(h,a), for arbitrary values of h and a | |
988 | template<typename RealType, class Policy> | |
989 | inline RealType owens_t(RealType h, RealType a, const Policy& pol) | |
990 | { | |
991 | BOOST_MATH_STD_USING | |
992 | // exploit that T(-h,a) == T(h,a) | |
993 | h = fabs(h); | |
994 | ||
995 | // Use equation (2) in the paper to remap the arguments | |
996 | // such that h>=0 and 0<=a<=1 for the call of the actual | |
997 | // computation routine. | |
998 | ||
999 | const RealType fabs_a = fabs(a); | |
1000 | const RealType fabs_ah = fabs_a*h; | |
1001 | ||
1002 | RealType val = 0.0; // avoid compiler warnings, 0.0 will be overwritten in any case | |
1003 | ||
1004 | if(fabs_a <= 1) | |
1005 | { | |
1006 | val = owens_t_dispatch(h, fabs_a, fabs_ah, pol); | |
1007 | } // if(fabs_a <= 1.0) | |
1008 | else | |
1009 | { | |
1010 | if( h <= 0.67 ) | |
1011 | { | |
1012 | const RealType normh = owens_t_znorm1(h); | |
1013 | const RealType normah = owens_t_znorm1(fabs_ah); | |
1014 | val = static_cast<RealType>(1)/static_cast<RealType>(4) - normh*normah - | |
1015 | owens_t_dispatch(fabs_ah, static_cast<RealType>(1 / fabs_a), h, pol); | |
1016 | } // if( h <= 0.67 ) | |
1017 | else | |
1018 | { | |
1019 | const RealType normh = detail::owens_t_znorm2(h); | |
1020 | const RealType normah = detail::owens_t_znorm2(fabs_ah); | |
1021 | val = constants::half<RealType>()*(normh+normah) - normh*normah - | |
1022 | owens_t_dispatch(fabs_ah, static_cast<RealType>(1 / fabs_a), h, pol); | |
1023 | } // else [if( h <= 0.67 )] | |
1024 | } // else [if(fabs_a <= 1)] | |
1025 | ||
1026 | // exploit that T(h,-a) == -T(h,a) | |
1027 | if(a < 0) | |
1028 | { | |
1029 | return -val; | |
1030 | } // if(a < 0) | |
1031 | ||
1032 | return val; | |
1033 | } // RealType owens_t(RealType h, RealType a) | |
1034 | ||
1035 | template <class T, class Policy, class tag> | |
1036 | struct owens_t_initializer | |
1037 | { | |
1038 | struct init | |
1039 | { | |
1040 | init() | |
1041 | { | |
1042 | do_init(tag()); | |
1043 | } | |
1044 | template <int N> | |
1045 | static void do_init(const mpl::int_<N>&){} | |
1046 | static void do_init(const mpl::int_<64>&) | |
1047 | { | |
1048 | boost::math::owens_t(static_cast<T>(7), static_cast<T>(0.96875), Policy()); | |
1049 | boost::math::owens_t(static_cast<T>(2), static_cast<T>(0.5), Policy()); | |
1050 | } | |
1051 | void force_instantiate()const{} | |
1052 | }; | |
1053 | static const init initializer; | |
1054 | static void force_instantiate() | |
1055 | { | |
1056 | initializer.force_instantiate(); | |
1057 | } | |
1058 | }; | |
1059 | ||
1060 | template <class T, class Policy, class tag> | |
1061 | const typename owens_t_initializer<T, Policy, tag>::init owens_t_initializer<T, Policy, tag>::initializer; | |
1062 | ||
1063 | } // namespace detail | |
1064 | ||
1065 | template <class T1, class T2, class Policy> | |
1066 | inline typename tools::promote_args<T1, T2>::type owens_t(T1 h, T2 a, const Policy& pol) | |
1067 | { | |
1068 | typedef typename tools::promote_args<T1, T2>::type result_type; | |
1069 | typedef typename policies::evaluation<result_type, Policy>::type value_type; | |
1070 | typedef typename policies::precision<value_type, Policy>::type precision_type; | |
1071 | typedef typename mpl::if_c< | |
1072 | precision_type::value == 0, | |
1073 | mpl::int_<0>, | |
1074 | typename mpl::if_c< | |
1075 | precision_type::value <= 64, | |
1076 | mpl::int_<64>, | |
1077 | mpl::int_<65> | |
1078 | >::type | |
1079 | >::type tag_type; | |
1080 | ||
1081 | detail::owens_t_initializer<result_type, Policy, tag_type>::force_instantiate(); | |
1082 | ||
1083 | return policies::checked_narrowing_cast<result_type, Policy>(detail::owens_t(static_cast<value_type>(h), static_cast<value_type>(a), pol), "boost::math::owens_t<%1%>(%1%,%1%)"); | |
1084 | } | |
1085 | ||
1086 | template <class T1, class T2> | |
1087 | inline typename tools::promote_args<T1, T2>::type owens_t(T1 h, T2 a) | |
1088 | { | |
1089 | return owens_t(h, a, policies::policy<>()); | |
1090 | } | |
1091 | ||
1092 | ||
1093 | } // namespace math | |
1094 | } // namespace boost | |
1095 | ||
1096 | #ifdef BOOST_MSVC | |
1097 | #pragma warning(pop) | |
1098 | #endif | |
1099 | ||
1100 | #endif | |
1101 | // EOF |