erf_inv.hpp 21 KB

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