Method: MoreMath::Functions.gammaQ_regularized

Defined in:
lib/more_math/functions.rb

.gammaQ_regularized(x, a, epsilon: 1E-16, max_iterations: 1 << 16) ⇒ Object

Return an approximation of the regularized gammaQ function for x and a with an error of <= epsilon, but only iterate max_iterations-times.



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/more_math/functions.rb', line 123

def gammaQ_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
    1.0
  when a > x || a < 1
    1 - gammaP_regularized(x, a, epsilon: epsilon, max_iterations: max_iterations)
  else
    fraction = ContinuedFraction.for_a do |n, y|
      (2 * n + 1) - a + y
    end.for_b do |n, y|
      n * (a - n)
    end
    exp(-x + a * log(x) - log_gamma(a)) *
      fraction[x, epsilon: epsilon, max_iterations: max_iterations] ** -1
  end
rescue Errno::ERANGE, Errno::EDOM
  0 / 0.0
end