Method: MoreMath::Functions.gammaP_regularized
- Defined in:
- lib/more_math/functions.rb
.gammaP_regularized(x, a, epsilon: 1E-16, max_iterations: 1 << 16) ⇒ Object
Return an approximation of the regularized gammaP function for x and a with an error of <= epsilon, but only iterate max_iterations-times.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/more_math/functions.rb', line 92 def gammaP_regularized(x, a, epsilon: 1E-16, max_iterations: 1 << 16) x, a = x.to_f, a.to_f case when a.nan? || x.nan? || a <= 0 || x < 0 0 / 0.0 when x == 0 0.0 when 1 <= a && a < x 1 - gammaQ_regularized(x, a, epsilon: epsilon, max_iterations: max_iterations) else n = 0 an = 1 / a sum = an while an.abs > epsilon && n < max_iterations n += 1 an *= x / (a + n) sum += an end if n >= max_iterations raise Errno::ERANGE else exp(-x + a * log(x) - log_gamma(a)) * sum end end rescue Errno::ERANGE, Errno::EDOM 0 / 0.0 end |