lib/distribution/math_extension.rb in distribution-0.7.3 vs lib/distribution/math_extension.rb in distribution-0.8.0
- old
+ new
@@ -12,20 +12,17 @@
require 'distribution/math_extension/incomplete_gamma'
require 'distribution/math_extension/incomplete_beta'
require 'distribution/math_extension/log_utilities'
module Distribution
-
# Useful additions to Math
module MathExtension
-
# Factorization based on Prime Swing algorithm, by Luschny (the king of factorial numbers analysis :P )
# == Reference
# * The Homepage of Factorial Algorithms. (C) Peter Luschny, 2000-2010
# == URL: http://www.luschny.de/math/factorial/csharp/FactorialPrimeSwing.cs.html
class SwingFactorial
-
SmallOddSwing = [1, 1, 1, 3, 3, 15, 5, 35, 35, 315, 63, 693, 231, 3003,
429, 6435, 6435, 109_395, 12_155, 230_945, 46_189,
969_969, 88_179, 2_028_117, 676_039, 16_900_975,
1_300_075, 35_102_025, 5_014_575, 145_422_675,
9_694_845, 300_540_195, 300_540_195]
@@ -74,13 +71,11 @@
if (prime <= sqrtN)
q = n
_p = 1
while (q = (q / prime).truncate) > 0
- if q.odd?
- _p *= prime
- end
+ _p *= prime if q.odd?
end
if _p > 1
@prime_list[count] = _p
count += 1
end
@@ -117,11 +112,10 @@
# Module to calculate approximated factorial
# Based (again) on Luschny formula, with 16 digits of precision
# == Reference
# * http://www.luschny.de/math/factorial/approx/SimpleCases.html
module ApproxFactorial
-
class << self
def stieltjes_ln_factorial(z)
a0 = 1.quo(12); a1 = 1.quo(30); a2 = 53.quo(210); a3 = 195.quo(371)
a4 = 22_999.quo(22_737); a5 = 29_944_523.quo(19_733_142)
a6 = 109_535_241_009.quo(48_264_275_462)
@@ -145,13 +139,11 @@
# Valid upto 11 digits
def stieltjes_factorial(x)
y = x
_p = 1
- while y < 8
- _p *= y; y += 1
- end
+ _p *= y; y += 1 while y < 8
lr = stieltjes_ln_factorial(y)
r = Math.exp(lr)
if r.infinite?
@@ -217,11 +209,10 @@
(m..n).inject(0) do|sum, j|
sum + (binomial_coefficient(n, j) * x**j * (1 - x)**(n - j))
end
end
-
# Incomplete beta function: B(x;a,b)
# +a+ and +b+ are parameters and +x+ is
# integration upper limit.
def incomplete_beta(x, a, b)
IncompleteBeta.evaluate(a, b, x) * beta(a, b)
@@ -328,10 +319,10 @@
# Necessary on Ruby 1.9
module CMath # :nodoc:
include Distribution::MathExtension
module_function :factorial, :beta, :loggamma, :unnormalized_incomplete_gamma,
- :incomplete_gamma, :gammp, :gammq, :erfc_e, :binomial_coefficient,
- :binomial_coefficient_gamma, :incomplete_beta, :exact_regularized_beta,
- :regularized_beta, :permutations, :rising_factorial, :fast_factorial,
- :combinations, :logbeta, :lbeta
+ :incomplete_gamma, :gammp, :gammq, :erfc_e, :binomial_coefficient,
+ :binomial_coefficient_gamma, :incomplete_beta, :exact_regularized_beta,
+ :regularized_beta, :permutations, :rising_factorial, :fast_factorial,
+ :combinations, :logbeta, :lbeta
end