erf_inv.hpp 22 KB

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