Module: MoreMath::Functions
- Extended by:
- Math
- Included in:
- ChiSquareDistribution, NormalDistribution, TDistribution
- Defined in:
- lib/more_math/functions.rb
Class Method Summary collapse
-
.beta(a, b) ⇒ Object
Returns the value of the beta function for (a, b), +a > 0, b > 0’.
-
.beta_regularized(x, a, b, epsilon: 1E-16, max_iterations: 1 << 16) ⇒ Object
Return an approximation value of Euler’s regularized beta function for
x
,a
, andb
with an error <=epsilon
, but only iteratemax_iterations
-times. -
.cantor_pairing(*xs) ⇒ Object
Returns Cantor’s tuple function for the tuple *xs (the size must be at least 2).
-
.cantor_pairing_inv(c, n = 2) ⇒ Object
Returns the inverse of Cantor’s tuple function for the value
c
. -
.gamma(x) ⇒ Object
Returns the value of the gamma function, extended to a negative domain.
-
.gammaP_regularized(x, a, epsilon: 1E-16, max_iterations: 1 << 16) ⇒ Object
Return an approximation of the regularized gammaP function for
x
anda
with an error of <=epsilon
, but only iteratemax_iterations
-times. -
.gammaQ_regularized(x, a, epsilon: 1E-16, max_iterations: 1 << 16) ⇒ Object
Return an approximation of the regularized gammaQ function for
x
anda
with an error of <=epsilon
, but only iteratemax_iterations
-times. -
.log_beta(a, b) ⇒ Object
Returns the natural logarithm of the beta function value for (a, b).
- .log_ceil(n, b = 2) ⇒ Object
- .log_floor(n, b = 2) ⇒ Object
-
.logb(x, b = 2) ⇒ Object
Returns the base
b
logarithm of the numberx
. -
.numberify_string(string, alphabet = 'a'..'z') ⇒ Object
Computes a Gödel number from
string
in thealphabet
and returns it. -
.stringify_number(number, alphabet = 'a'..'z') ⇒ Object
Computes the string in the
alphabet
from a Gödel numbernumber
and returns it.
Instance Method Summary collapse
-
#erf(x) ⇒ Object
Returns an approximate value for the error function’s value for
x
. - #erfc(x) ⇒ Object
- #log_gamma(x) ⇒ Object
Class Method Details
.beta(a, b) ⇒ Object
Returns the value of the beta function for (a, b), +a > 0, b > 0’.
54 55 56 57 58 59 60 |
# File 'lib/more_math/functions.rb', line 54 def beta(a, b) if a > 0 && b > 0 exp(log_beta(a, b)) else 0.0 / 0 end end |
.beta_regularized(x, a, b, epsilon: 1E-16, max_iterations: 1 << 16) ⇒ Object
Return an approximation value of Euler’s regularized beta function for x
, a
, and b
with an error <= epsilon
, but only iterate max_iterations
-times.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/more_math/functions.rb', line 65 def beta_regularized(x, a, b, epsilon: 1E-16, max_iterations: 1 << 16) x, a, b = x.to_f, a.to_f, b.to_f case when a.nan? || b.nan? || x.nan? || a <= 0 || b <= 0 || x < 0 || x > 1 0 / 0.0 when x > (a + 1) / (a + b + 2) 1 - beta_regularized(1 - x, b, a, epsilon: epsilon, max_iterations: max_iterations) else fraction = ContinuedFraction.for_b do |n, y| if n % 2 == 0 m = n / 2.0 (m * (b - m) * y) / ((a + (2 * m) - 1) * (a + (2 * m))) else m = (n - 1) / 2.0 -((a + m) * (a + b + m) * y) / ((a + 2 * m) * (a + 2 * m + 1)) end end exp(a * log(x) + b * log(1.0 - x) - log(a) - log_beta(a, b)) / fraction[x, epsilon: epsilon, max_iterations: max_iterations] end rescue Errno::ERANGE, Errno::EDOM 0 / 0.0 end |
.cantor_pairing(*xs) ⇒ Object
Returns Cantor’s tuple function for the tuple *xs (the size must be at least 2).
190 191 192 |
# File 'lib/more_math/functions.rb', line 190 def cantor_pairing(*xs) CantorPairingFunction.cantor_pairing(*xs) end |
.cantor_pairing_inv(c, n = 2) ⇒ Object
Returns the inverse of Cantor’s tuple function for the value c
. n
is the length of the tuple (defaults to 2, a pair).
196 197 198 |
# File 'lib/more_math/functions.rb', line 196 def cantor_pairing_inv(c, n = 2) CantorPairingFunction.cantor_pairing_inv(c, n) end |
.gamma(x) ⇒ Object
Returns the value of the gamma function, extended to a negative domain.
38 39 40 41 42 43 44 |
# File 'lib/more_math/functions.rb', line 38 def gamma(x) if x < 0.0 return PI / (sin(PI * x) * exp(log_gamma(1 - x))) else exp(log_gamma(x)) end end |
.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 |
.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 |
.log_beta(a, b) ⇒ Object
Returns the natural logarithm of the beta function value for (a, b).
47 48 49 50 51 |
# File 'lib/more_math/functions.rb', line 47 def log_beta(a, b) log_gamma(a) + log_gamma(b) - log_gamma(a + b) rescue Errno::ERANGE, Errno::EDOM 0 / 0.0 end |
.log_ceil(n, b = 2) ⇒ Object
160 161 162 163 164 165 166 167 168 169 |
# File 'lib/more_math/functions.rb', line 160 def log_ceil(n, b = 2) raise ArgumentError, "n is required to be > 0" unless n > 0 raise ArgumentError, "b is required to be > 1" unless b > 1 e, result = 1, 0 until e >= n e *= b result += 1 end result end |
.log_floor(n, b = 2) ⇒ Object
171 172 173 174 175 176 177 178 179 180 |
# File 'lib/more_math/functions.rb', line 171 def log_floor(n, b = 2) raise ArgumentError, "n is required to be > 0" unless n > 0 raise ArgumentError, "b is required to be > 1" unless b > 1 e, result = 1, 0 until e * b > n e *= b result += 1 end result end |
.logb(x, b = 2) ⇒ Object
Returns the base b
logarithm of the number x
. b
defaults to base 2, binary logarithm.
184 185 186 |
# File 'lib/more_math/functions.rb', line 184 def logb(x, b = 2) Math.log(x) / Math.log(b) end |
.numberify_string(string, alphabet = 'a'..'z') ⇒ Object
Computes a Gödel number from string
in the alphabet
and returns it.
201 202 203 |
# File 'lib/more_math/functions.rb', line 201 def numberify_string(string, alphabet = 'a'..'z') NumberifyStringFunction.numberify_string(string, alphabet) end |
.stringify_number(number, alphabet = 'a'..'z') ⇒ Object
Computes the string in the alphabet
from a Gödel number number
and returns it. This is the inverse function of numberify_string.
207 208 209 |
# File 'lib/more_math/functions.rb', line 207 def stringify_number(number, alphabet = 'a'..'z') NumberifyStringFunction.stringify_number(number, alphabet) end |
Instance Method Details
#erf(x) ⇒ Object
Returns an approximate value for the error function’s value for x
.
147 148 149 150 151 |
# File 'lib/more_math/functions.rb', line 147 def erf(x) erf_a = MoreMath::Constants::FunctionsConstants::ERF_A r = sqrt(1 - exp(-x ** 2 * (4 / Math::PI + erf_a * x ** 2) / (1 + erf_a * x ** 2))) x < 0 ? -r : r end |
#erfc(x) ⇒ Object
155 156 157 |
# File 'lib/more_math/functions.rb', line 155 def erfc(x) 1.0 - erf(x) end |
#log_gamma(x) ⇒ Object
13 14 15 |
# File 'lib/more_math/functions.rb', line 13 def log_gamma(x) lgamma(x).first end |