erf_inv.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. // (C) Copyright John Maddock 2006.
  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. #ifndef BOOST_MATH_SF_ERF_INV_HPP
  6. #define BOOST_MATH_SF_ERF_INV_HPP
  7. #ifdef _MSC_VER
  8. #pragma once
  9. #endif
  10. namespace boost{ namespace math{
  11. namespace detail{
  12. //
  13. // The inverse erf and erfc functions share a common implementation,
  14. // this version is for 80-bit long double's and smaller:
  15. //
  16. template <class T, class Policy>
  17. T erf_inv_imp(const T& p, const T& q, const Policy&, const boost::mpl::int_<64>*)
  18. {
  19. BOOST_MATH_STD_USING // for ADL of std names.
  20. T result = 0;
  21. if(p <= 0.5)
  22. {
  23. //
  24. // Evaluate inverse erf using the rational approximation:
  25. //
  26. // x = p(p+10)(Y+R(p))
  27. //
  28. // Where Y is a constant, and R(p) is optimised for a low
  29. // absolute error compared to |Y|.
  30. //
  31. // double: Max error found: 2.001849e-18
  32. // long double: Max error found: 1.017064e-20
  33. // Maximum Deviation Found (actual error term at infinite precision) 8.030e-21
  34. //
  35. static const float Y = 0.0891314744949340820313f;
  36. static const T P[] = {
  37. BOOST_MATH_BIG_CONSTANT(T, 64, -0.000508781949658280665617),
  38. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00836874819741736770379),
  39. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0334806625409744615033),
  40. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0126926147662974029034),
  41. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0365637971411762664006),
  42. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0219878681111168899165),
  43. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00822687874676915743155),
  44. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00538772965071242932965)
  45. };
  46. static const T Q[] = {
  47. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  48. BOOST_MATH_BIG_CONSTANT(T, 64, -0.970005043303290640362),
  49. BOOST_MATH_BIG_CONSTANT(T, 64, -1.56574558234175846809),
  50. BOOST_MATH_BIG_CONSTANT(T, 64, 1.56221558398423026363),
  51. BOOST_MATH_BIG_CONSTANT(T, 64, 0.662328840472002992063),
  52. BOOST_MATH_BIG_CONSTANT(T, 64, -0.71228902341542847553),
  53. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0527396382340099713954),
  54. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0795283687341571680018),
  55. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00233393759374190016776),
  56. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000886216390456424707504)
  57. };
  58. T g = p * (p + 10);
  59. T r = tools::evaluate_polynomial(P, p) / tools::evaluate_polynomial(Q, p);
  60. result = g * Y + g * r;
  61. }
  62. else if(q >= 0.25)
  63. {
  64. //
  65. // Rational approximation for 0.5 > q >= 0.25
  66. //
  67. // x = sqrt(-2*log(q)) / (Y + R(q))
  68. //
  69. // Where Y is a constant, and R(q) is optimised for a low
  70. // absolute error compared to Y.
  71. //
  72. // double : Max error found: 7.403372e-17
  73. // long double : Max error found: 6.084616e-20
  74. // Maximum Deviation Found (error term) 4.811e-20
  75. //
  76. static const float Y = 2.249481201171875f;
  77. static const T P[] = {
  78. BOOST_MATH_BIG_CONSTANT(T, 64, -0.202433508355938759655),
  79. BOOST_MATH_BIG_CONSTANT(T, 64, 0.105264680699391713268),
  80. BOOST_MATH_BIG_CONSTANT(T, 64, 8.37050328343119927838),
  81. BOOST_MATH_BIG_CONSTANT(T, 64, 17.6447298408374015486),
  82. BOOST_MATH_BIG_CONSTANT(T, 64, -18.8510648058714251895),
  83. BOOST_MATH_BIG_CONSTANT(T, 64, -44.6382324441786960818),
  84. BOOST_MATH_BIG_CONSTANT(T, 64, 17.445385985570866523),
  85. BOOST_MATH_BIG_CONSTANT(T, 64, 21.1294655448340526258),
  86. BOOST_MATH_BIG_CONSTANT(T, 64, -3.67192254707729348546)
  87. };
  88. static const T Q[] = {
  89. BOOST_MATH_BIG_CONSTANT(T, 64, 1),
  90. BOOST_MATH_BIG_CONSTANT(T, 64, 6.24264124854247537712),
  91. BOOST_MATH_BIG_CONSTANT(T, 64, 3.9713437953343869095),
  92. BOOST_MATH_BIG_CONSTANT(T, 64, -28.6608180499800029974),
  93. BOOST_MATH_BIG_CONSTANT(T, 64, -20.1432634680485188801),
  94. BOOST_MATH_BIG_CONSTANT(T, 64, 48.5609213108739935468),
  95. BOOST_MATH_BIG_CONSTANT(T, 64, 10.8268667355460159008),
  96. BOOST_MATH_BIG_CONSTANT(T, 64, -22.6436933413139721736),
  97. BOOST_MATH_BIG_CONSTANT(T, 64, 1.72114765761200282724)
  98. };
  99. T g = sqrt(-2 * log(q));
  100. T xs = q - 0.25;
  101. T r = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  102. result = g / (Y + r);
  103. }
  104. else
  105. {
  106. //
  107. // For q < 0.25 we have a series of rational approximations all
  108. // of the general form:
  109. //
  110. // let: x = sqrt(-log(q))
  111. //
  112. // Then the result is given by:
  113. //
  114. // x(Y+R(x-B))
  115. //
  116. // where Y is a constant, B is the lowest value of x for which
  117. // the approximation is valid, and R(x-B) is optimised for a low
  118. // absolute error compared to Y.
  119. //
  120. // Note that almost all code will really go through the first
  121. // or maybe second approximation. After than we're dealing with very
  122. // small input values indeed: 80 and 128 bit long double's go all the
  123. // way down to ~ 1e-5000 so the "tail" is rather long...
  124. //
  125. T x = sqrt(-log(q));
  126. if(x < 3)
  127. {
  128. // Max error found: 1.089051e-20
  129. static const float Y = 0.807220458984375f;
  130. static const T P[] = {
  131. BOOST_MATH_BIG_CONSTANT(T, 64, -0.131102781679951906451),
  132. BOOST_MATH_BIG_CONSTANT(T, 64, -0.163794047193317060787),
  133. BOOST_MATH_BIG_CONSTANT(T, 64, 0.117030156341995252019),
  134. BOOST_MATH_BIG_CONSTANT(T, 64, 0.387079738972604337464),
  135. BOOST_MATH_BIG_CONSTANT(T, 64, 0.337785538912035898924),
  136. BOOST_MATH_BIG_CONSTANT(T, 64, 0.142869534408157156766),
  137. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0290157910005329060432),
  138. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00214558995388805277169),
  139. BOOST_MATH_BIG_CONSTANT(T, 64, -0.679465575181126350155e-6),
  140. BOOST_MATH_BIG_CONSTANT(T, 64, 0.285225331782217055858e-7),
  141. BOOST_MATH_BIG_CONSTANT(T, 64, -0.681149956853776992068e-9)
  142. };
  143. static const T Q[] = {
  144. BOOST_MATH_BIG_CONSTANT(T, 64, 1.0),
  145. BOOST_MATH_BIG_CONSTANT(T, 64, 3.46625407242567245975),
  146. BOOST_MATH_BIG_CONSTANT(T, 64, 5.38168345707006855425),
  147. BOOST_MATH_BIG_CONSTANT(T, 64, 4.77846592945843778382),
  148. BOOST_MATH_BIG_CONSTANT(T, 64, 2.59301921623620271374),
  149. BOOST_MATH_BIG_CONSTANT(T, 64, 0.848854343457902036425),
  150. BOOST_MATH_BIG_CONSTANT(T, 64, 0.152264338295331783612),
  151. BOOST_MATH_BIG_CONSTANT(T, 64, 0.01105924229346489121)
  152. };
  153. T xs = x - 1.125;
  154. T R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  155. result = Y * x + R * x;
  156. }
  157. else if(x < 6)
  158. {
  159. // Max error found: 8.389174e-21
  160. static const float Y = 0.93995571136474609375f;
  161. static const T P[] = {
  162. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0350353787183177984712),
  163. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00222426529213447927281),
  164. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0185573306514231072324),
  165. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00950804701325919603619),
  166. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00187123492819559223345),
  167. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000157544617424960554631),
  168. BOOST_MATH_BIG_CONSTANT(T, 64, 0.460469890584317994083e-5),
  169. BOOST_MATH_BIG_CONSTANT(T, 64, -0.230404776911882601748e-9),
  170. BOOST_MATH_BIG_CONSTANT(T, 64, 0.266339227425782031962e-11)
  171. };
  172. static const T Q[] = {
  173. BOOST_MATH_BIG_CONSTANT(T, 64, 1),
  174. BOOST_MATH_BIG_CONSTANT(T, 64, 1.3653349817554063097),
  175. BOOST_MATH_BIG_CONSTANT(T, 64, 0.762059164553623404043),
  176. BOOST_MATH_BIG_CONSTANT(T, 64, 0.220091105764131249824),
  177. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0341589143670947727934),
  178. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00263861676657015992959),
  179. BOOST_MATH_BIG_CONSTANT(T, 64, 0.764675292302794483503e-4)
  180. };
  181. T xs = x - 3;
  182. T R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  183. result = Y * x + R * x;
  184. }
  185. else if(x < 18)
  186. {
  187. // Max error found: 1.481312e-19
  188. static const float Y = 0.98362827301025390625f;
  189. static const T P[] = {
  190. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0167431005076633737133),
  191. BOOST_MATH_BIG_CONSTANT(T, 64, -0.00112951438745580278863),
  192. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00105628862152492910091),
  193. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000209386317487588078668),
  194. BOOST_MATH_BIG_CONSTANT(T, 64, 0.149624783758342370182e-4),
  195. BOOST_MATH_BIG_CONSTANT(T, 64, 0.449696789927706453732e-6),
  196. BOOST_MATH_BIG_CONSTANT(T, 64, 0.462596163522878599135e-8),
  197. BOOST_MATH_BIG_CONSTANT(T, 64, -0.281128735628831791805e-13),
  198. BOOST_MATH_BIG_CONSTANT(T, 64, 0.99055709973310326855e-16)
  199. };
  200. static const T Q[] = {
  201. BOOST_MATH_BIG_CONSTANT(T, 64, 1),
  202. BOOST_MATH_BIG_CONSTANT(T, 64, 0.591429344886417493481),
  203. BOOST_MATH_BIG_CONSTANT(T, 64, 0.138151865749083321638),
  204. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0160746087093676504695),
  205. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000964011807005165528527),
  206. BOOST_MATH_BIG_CONSTANT(T, 64, 0.275335474764726041141e-4),
  207. BOOST_MATH_BIG_CONSTANT(T, 64, 0.282243172016108031869e-6)
  208. };
  209. T xs = x - 6;
  210. T R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  211. result = Y * x + R * x;
  212. }
  213. else if(x < 44)
  214. {
  215. // Max error found: 5.697761e-20
  216. static const float Y = 0.99714565277099609375f;
  217. static const T P[] = {
  218. BOOST_MATH_BIG_CONSTANT(T, 64, -0.0024978212791898131227),
  219. BOOST_MATH_BIG_CONSTANT(T, 64, -0.779190719229053954292e-5),
  220. BOOST_MATH_BIG_CONSTANT(T, 64, 0.254723037413027451751e-4),
  221. BOOST_MATH_BIG_CONSTANT(T, 64, 0.162397777342510920873e-5),
  222. BOOST_MATH_BIG_CONSTANT(T, 64, 0.396341011304801168516e-7),
  223. BOOST_MATH_BIG_CONSTANT(T, 64, 0.411632831190944208473e-9),
  224. BOOST_MATH_BIG_CONSTANT(T, 64, 0.145596286718675035587e-11),
  225. BOOST_MATH_BIG_CONSTANT(T, 64, -0.116765012397184275695e-17)
  226. };
  227. static const T Q[] = {
  228. BOOST_MATH_BIG_CONSTANT(T, 64, 1),
  229. BOOST_MATH_BIG_CONSTANT(T, 64, 0.207123112214422517181),
  230. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0169410838120975906478),
  231. BOOST_MATH_BIG_CONSTANT(T, 64, 0.000690538265622684595676),
  232. BOOST_MATH_BIG_CONSTANT(T, 64, 0.145007359818232637924e-4),
  233. BOOST_MATH_BIG_CONSTANT(T, 64, 0.144437756628144157666e-6),
  234. BOOST_MATH_BIG_CONSTANT(T, 64, 0.509761276599778486139e-9)
  235. };
  236. T xs = x - 18;
  237. T R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  238. result = Y * x + R * x;
  239. }
  240. else
  241. {
  242. // Max error found: 1.279746e-20
  243. static const float Y = 0.99941349029541015625f;
  244. static const T P[] = {
  245. BOOST_MATH_BIG_CONSTANT(T, 64, -0.000539042911019078575891),
  246. BOOST_MATH_BIG_CONSTANT(T, 64, -0.28398759004727721098e-6),
  247. BOOST_MATH_BIG_CONSTANT(T, 64, 0.899465114892291446442e-6),
  248. BOOST_MATH_BIG_CONSTANT(T, 64, 0.229345859265920864296e-7),
  249. BOOST_MATH_BIG_CONSTANT(T, 64, 0.225561444863500149219e-9),
  250. BOOST_MATH_BIG_CONSTANT(T, 64, 0.947846627503022684216e-12),
  251. BOOST_MATH_BIG_CONSTANT(T, 64, 0.135880130108924861008e-14),
  252. BOOST_MATH_BIG_CONSTANT(T, 64, -0.348890393399948882918e-21)
  253. };
  254. static const T Q[] = {
  255. BOOST_MATH_BIG_CONSTANT(T, 64, 1),
  256. BOOST_MATH_BIG_CONSTANT(T, 64, 0.0845746234001899436914),
  257. BOOST_MATH_BIG_CONSTANT(T, 64, 0.00282092984726264681981),
  258. BOOST_MATH_BIG_CONSTANT(T, 64, 0.468292921940894236786e-4),
  259. BOOST_MATH_BIG_CONSTANT(T, 64, 0.399968812193862100054e-6),
  260. BOOST_MATH_BIG_CONSTANT(T, 64, 0.161809290887904476097e-8),
  261. BOOST_MATH_BIG_CONSTANT(T, 64, 0.231558608310259605225e-11)
  262. };
  263. T xs = x - 44;
  264. T R = tools::evaluate_polynomial(P, xs) / tools::evaluate_polynomial(Q, xs);
  265. result = Y * x + R * x;
  266. }
  267. }
  268. return result;
  269. }
  270. template <class T, class Policy>
  271. struct erf_roots
  272. {
  273. boost::math::tuple<T,T,T> operator()(const T& guess)
  274. {
  275. BOOST_MATH_STD_USING
  276. T derivative = sign * (2 / sqrt(constants::pi<T>())) * exp(-(guess * guess));
  277. T derivative2 = -2 * guess * derivative;
  278. return boost::math::make_tuple(((sign > 0) ? static_cast<T>(boost::math::erf(guess, Policy()) - target) : static_cast<T>(boost::math::erfc(guess, Policy())) - target), derivative, derivative2);
  279. }
  280. erf_roots(T z, int s) : target(z), sign(s) {}
  281. private:
  282. T target;
  283. int sign;
  284. };
  285. template <class T, class Policy>
  286. T erf_inv_imp(const T& p, const T& q, const Policy& pol, const boost::mpl::int_<0>*)
  287. {
  288. //
  289. // Generic version, get a guess that's accurate to 64-bits (10^-19)
  290. //
  291. T guess = erf_inv_imp(p, q, pol, static_cast<mpl::int_<64> const*>(0));
  292. T result;
  293. //
  294. // If T has more bit's than 64 in it's mantissa then we need to iterate,
  295. // otherwise we can just return the result:
  296. //
  297. if(policies::digits<T, Policy>() > 64)
  298. {
  299. boost::uintmax_t max_iter = policies::get_max_root_iterations<Policy>();
  300. if(p <= 0.5)
  301. {
  302. result = tools::halley_iterate(detail::erf_roots<typename remove_cv<T>::type, Policy>(p, 1), guess, static_cast<T>(0), tools::max_value<T>(), (policies::digits<T, Policy>() * 2) / 3, max_iter);
  303. }
  304. else
  305. {
  306. result = tools::halley_iterate(detail::erf_roots<typename remove_cv<T>::type, Policy>(q, -1), guess, static_cast<T>(0), tools::max_value<T>(), (policies::digits<T, Policy>() * 2) / 3, max_iter);
  307. }
  308. policies::check_root_iterations<T>("boost::math::erf_inv<%1%>", max_iter, pol);
  309. }
  310. else
  311. {
  312. result = guess;
  313. }
  314. return result;
  315. }
  316. template <class T, class Policy>
  317. struct erf_inv_initializer
  318. {
  319. struct init
  320. {
  321. init()
  322. {
  323. do_init();
  324. }
  325. static void do_init()
  326. {
  327. boost::math::erf_inv(static_cast<T>(0.25), Policy());
  328. boost::math::erf_inv(static_cast<T>(0.55), Policy());
  329. boost::math::erf_inv(static_cast<T>(0.95), Policy());
  330. boost::math::erfc_inv(static_cast<T>(1e-15), Policy());
  331. if(static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1e-130)) != 0)
  332. boost::math::erfc_inv(static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1e-130)), Policy());
  333. // Some compilers choke on constants that would underflow, even in code that isn't instantiated
  334. // so try and filter these cases out in the preprocessor:
  335. #if LDBL_MAX_10_EXP >= 800
  336. if(static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1e-800)) != 0)
  337. boost::math::erfc_inv(static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1e-800)), Policy());
  338. if(static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1e-900)) != 0)
  339. boost::math::erfc_inv(static_cast<T>(BOOST_MATH_BIG_CONSTANT(T, 64, 1e-900)), Policy());
  340. #else
  341. if(static_cast<T>(BOOST_MATH_HUGE_CONSTANT(T, 64, 1e-800)) != 0)
  342. boost::math::erfc_inv(static_cast<T>(BOOST_MATH_HUGE_CONSTANT(T, 64, 1e-800)), Policy());
  343. if(static_cast<T>(BOOST_MATH_HUGE_CONSTANT(T, 64, 1e-900)) != 0)
  344. boost::math::erfc_inv(static_cast<T>(BOOST_MATH_HUGE_CONSTANT(T, 64, 1e-900)), Policy());
  345. #endif
  346. }
  347. void force_instantiate()const{}
  348. };
  349. static const init initializer;
  350. static void force_instantiate()
  351. {
  352. initializer.force_instantiate();
  353. }
  354. };
  355. template <class T, class Policy>
  356. const typename erf_inv_initializer<T, Policy>::init erf_inv_initializer<T, Policy>::initializer;
  357. } // namespace detail
  358. template <class T, class Policy>
  359. typename tools::promote_args<T>::type erfc_inv(T z, const Policy& pol)
  360. {
  361. typedef typename tools::promote_args<T>::type result_type;
  362. //
  363. // Begin by testing for domain errors, and other special cases:
  364. //
  365. static const char* function = "boost::math::erfc_inv<%1%>(%1%, %1%)";
  366. if((z < 0) || (z > 2))
  367. policies::raise_domain_error<result_type>(function, "Argument outside range [0,2] in inverse erfc function (got p=%1%).", z, pol);
  368. if(z == 0)
  369. return policies::raise_overflow_error<result_type>(function, 0, pol);
  370. if(z == 2)
  371. return -policies::raise_overflow_error<result_type>(function, 0, pol);
  372. //
  373. // Normalise the input, so it's in the range [0,1], we will
  374. // negate the result if z is outside that range. This is a simple
  375. // application of the erfc reflection formula: erfc(-z) = 2 - erfc(z)
  376. //
  377. result_type p, q, s;
  378. if(z > 1)
  379. {
  380. q = 2 - z;
  381. p = 1 - q;
  382. s = -1;
  383. }
  384. else
  385. {
  386. p = 1 - z;
  387. q = z;
  388. s = 1;
  389. }
  390. //
  391. // A bit of meta-programming to figure out which implementation
  392. // to use, based on the number of bits in the mantissa of T:
  393. //
  394. typedef typename policies::precision<result_type, Policy>::type precision_type;
  395. typedef typename mpl::if_<
  396. mpl::or_<mpl::less_equal<precision_type, mpl::int_<0> >, mpl::greater<precision_type, mpl::int_<64> > >,
  397. mpl::int_<0>,
  398. mpl::int_<64>
  399. >::type tag_type;
  400. //
  401. // Likewise use internal promotion, so we evaluate at a higher
  402. // precision internally if it's appropriate:
  403. //
  404. typedef typename policies::evaluation<result_type, Policy>::type eval_type;
  405. typedef typename policies::normalise<
  406. Policy,
  407. policies::promote_float<false>,
  408. policies::promote_double<false>,
  409. policies::discrete_quantile<>,
  410. policies::assert_undefined<> >::type forwarding_policy;
  411. detail::erf_inv_initializer<eval_type, forwarding_policy>::force_instantiate();
  412. //
  413. // And get the result, negating where required:
  414. //
  415. return s * policies::checked_narrowing_cast<result_type, forwarding_policy>(
  416. detail::erf_inv_imp(static_cast<eval_type>(p), static_cast<eval_type>(q), forwarding_policy(), static_cast<tag_type const*>(0)), function);
  417. }
  418. template <class T, class Policy>
  419. typename tools::promote_args<T>::type erf_inv(T z, const Policy& pol)
  420. {
  421. typedef typename tools::promote_args<T>::type result_type;
  422. //
  423. // Begin by testing for domain errors, and other special cases:
  424. //
  425. static const char* function = "boost::math::erf_inv<%1%>(%1%, %1%)";
  426. if((z < -1) || (z > 1))
  427. policies::raise_domain_error<result_type>(function, "Argument outside range [-1, 1] in inverse erf function (got p=%1%).", z, pol);
  428. if(z == 1)
  429. return policies::raise_overflow_error<result_type>(function, 0, pol);
  430. if(z == -1)
  431. return -policies::raise_overflow_error<result_type>(function, 0, pol);
  432. if(z == 0)
  433. return 0;
  434. //
  435. // Normalise the input, so it's in the range [0,1], we will
  436. // negate the result if z is outside that range. This is a simple
  437. // application of the erf reflection formula: erf(-z) = -erf(z)
  438. //
  439. result_type p, q, s;
  440. if(z < 0)
  441. {
  442. p = -z;
  443. q = 1 - p;
  444. s = -1;
  445. }
  446. else
  447. {
  448. p = z;
  449. q = 1 - z;
  450. s = 1;
  451. }
  452. //
  453. // A bit of meta-programming to figure out which implementation
  454. // to use, based on the number of bits in the mantissa of T:
  455. //
  456. typedef typename policies::precision<result_type, Policy>::type precision_type;
  457. typedef typename mpl::if_<
  458. mpl::or_<mpl::less_equal<precision_type, mpl::int_<0> >, mpl::greater<precision_type, mpl::int_<64> > >,
  459. mpl::int_<0>,
  460. mpl::int_<64>
  461. >::type tag_type;
  462. //
  463. // Likewise use internal promotion, so we evaluate at a higher
  464. // precision internally if it's appropriate:
  465. //
  466. typedef typename policies::evaluation<result_type, Policy>::type eval_type;
  467. typedef typename policies::normalise<
  468. Policy,
  469. policies::promote_float<false>,
  470. policies::promote_double<false>,
  471. policies::discrete_quantile<>,
  472. policies::assert_undefined<> >::type forwarding_policy;
  473. //
  474. // Likewise use internal promotion, so we evaluate at a higher
  475. // precision internally if it's appropriate:
  476. //
  477. typedef typename policies::evaluation<result_type, Policy>::type eval_type;
  478. detail::erf_inv_initializer<eval_type, forwarding_policy>::force_instantiate();
  479. //
  480. // And get the result, negating where required:
  481. //
  482. return s * policies::checked_narrowing_cast<result_type, forwarding_policy>(
  483. detail::erf_inv_imp(static_cast<eval_type>(p), static_cast<eval_type>(q), forwarding_policy(), static_cast<tag_type const*>(0)), function);
  484. }
  485. template <class T>
  486. inline typename tools::promote_args<T>::type erfc_inv(T z)
  487. {
  488. return erfc_inv(z, policies::policy<>());
  489. }
  490. template <class T>
  491. inline typename tools::promote_args<T>::type erf_inv(T z)
  492. {
  493. return erf_inv(z, policies::policy<>());
  494. }
  495. } // namespace math
  496. } // namespace boost
  497. #endif // BOOST_MATH_SF_ERF_INV_HPP