erfinv.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package math
  5. /*
  6. Inverse of the floating-point error function.
  7. */
  8. // This implementation is based on the rational approximation
  9. // of percentage points of normal distribution available from
  10. // https://www.jstor.org/stable/2347330.
  11. const (
  12. // Coefficients for approximation to erf in |x| <= 0.85
  13. a0 = 1.1975323115670912564578e0
  14. a1 = 4.7072688112383978012285e1
  15. a2 = 6.9706266534389598238465e2
  16. a3 = 4.8548868893843886794648e3
  17. a4 = 1.6235862515167575384252e4
  18. a5 = 2.3782041382114385731252e4
  19. a6 = 1.1819493347062294404278e4
  20. a7 = 8.8709406962545514830200e2
  21. b0 = 1.0000000000000000000e0
  22. b1 = 4.2313330701600911252e1
  23. b2 = 6.8718700749205790830e2
  24. b3 = 5.3941960214247511077e3
  25. b4 = 2.1213794301586595867e4
  26. b5 = 3.9307895800092710610e4
  27. b6 = 2.8729085735721942674e4
  28. b7 = 5.2264952788528545610e3
  29. // Coefficients for approximation to erf in 0.85 < |x| <= 1-2*exp(-25)
  30. c0 = 1.42343711074968357734e0
  31. c1 = 4.63033784615654529590e0
  32. c2 = 5.76949722146069140550e0
  33. c3 = 3.64784832476320460504e0
  34. c4 = 1.27045825245236838258e0
  35. c5 = 2.41780725177450611770e-1
  36. c6 = 2.27238449892691845833e-2
  37. c7 = 7.74545014278341407640e-4
  38. d0 = 1.4142135623730950488016887e0
  39. d1 = 2.9036514445419946173133295e0
  40. d2 = 2.3707661626024532365971225e0
  41. d3 = 9.7547832001787427186894837e-1
  42. d4 = 2.0945065210512749128288442e-1
  43. d5 = 2.1494160384252876777097297e-2
  44. d6 = 7.7441459065157709165577218e-4
  45. d7 = 1.4859850019840355905497876e-9
  46. // Coefficients for approximation to erf in 1-2*exp(-25) < |x| < 1
  47. e0 = 6.65790464350110377720e0
  48. e1 = 5.46378491116411436990e0
  49. e2 = 1.78482653991729133580e0
  50. e3 = 2.96560571828504891230e-1
  51. e4 = 2.65321895265761230930e-2
  52. e5 = 1.24266094738807843860e-3
  53. e6 = 2.71155556874348757815e-5
  54. e7 = 2.01033439929228813265e-7
  55. f0 = 1.414213562373095048801689e0
  56. f1 = 8.482908416595164588112026e-1
  57. f2 = 1.936480946950659106176712e-1
  58. f3 = 2.103693768272068968719679e-2
  59. f4 = 1.112800997078859844711555e-3
  60. f5 = 2.611088405080593625138020e-5
  61. f6 = 2.010321207683943062279931e-7
  62. f7 = 2.891024605872965461538222e-15
  63. )
  64. // Erfinv returns the inverse error function of x.
  65. //
  66. // Special cases are:
  67. //
  68. // Erfinv(1) = +Inf
  69. // Erfinv(-1) = -Inf
  70. // Erfinv(x) = NaN if x < -1 or x > 1
  71. // Erfinv(NaN) = NaN
  72. func Erfinv(x float64) float64 {
  73. // special cases
  74. if IsNaN(x) || x <= -1 || x >= 1 {
  75. if x == -1 || x == 1 {
  76. return Inf(int(x))
  77. }
  78. return NaN()
  79. }
  80. sign := false
  81. if x < 0 {
  82. x = -x
  83. sign = true
  84. }
  85. var ans float64
  86. if x <= 0.85 { // |x| <= 0.85
  87. r := 0.180625 - 0.25*x*x
  88. z1 := ((((((a7*r+a6)*r+a5)*r+a4)*r+a3)*r+a2)*r+a1)*r + a0
  89. z2 := ((((((b7*r+b6)*r+b5)*r+b4)*r+b3)*r+b2)*r+b1)*r + b0
  90. ans = (x * z1) / z2
  91. } else {
  92. var z1, z2 float64
  93. r := Sqrt(Ln2 - Log(1.0-x))
  94. if r <= 5.0 {
  95. r -= 1.6
  96. z1 = ((((((c7*r+c6)*r+c5)*r+c4)*r+c3)*r+c2)*r+c1)*r + c0
  97. z2 = ((((((d7*r+d6)*r+d5)*r+d4)*r+d3)*r+d2)*r+d1)*r + d0
  98. } else {
  99. r -= 5.0
  100. z1 = ((((((e7*r+e6)*r+e5)*r+e4)*r+e3)*r+e2)*r+e1)*r + e0
  101. z2 = ((((((f7*r+f6)*r+f5)*r+f4)*r+f3)*r+f2)*r+f1)*r + f0
  102. }
  103. ans = z1 / z2
  104. }
  105. if sign {
  106. return -ans
  107. }
  108. return ans
  109. }
  110. // Erfcinv returns the inverse of [Erfc](x).
  111. //
  112. // Special cases are:
  113. //
  114. // Erfcinv(0) = +Inf
  115. // Erfcinv(2) = -Inf
  116. // Erfcinv(x) = NaN if x < 0 or x > 2
  117. // Erfcinv(NaN) = NaN
  118. func Erfcinv(x float64) float64 {
  119. return Erfinv(1 - x)
  120. }