Module: MoreMath::Functions
- Extended by:
- Math
- Included in:
- CantorPairingFunctionTest, EntropyTest, FunctionsTest, ChiSquareDistribution, NormalDistribution, NumberifyStringFunction, TDistribution, NumberifyStringFunctionTest
- Defined in:
- lib/more_math/functions.rb
Overview
Provides mathematical functions and special functions for scientific computing.
This module includes implementations of various mathematical functions commonly used in statistics, numerical analysis, and scientific computing. It extends Ruby’s built-in Math module with additional functionality and provides specialized implementations for better accuracy and performance.
Class Method Summary collapse
-
.beta(a, b) ⇒ Float
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) ⇒ Float
Return an approximation value of Euler’s regularized beta function for
x,a, andbwith an error <=epsilon, but only iteratemax_iterations-times. -
.cantor_pairing(*xs) ⇒ Integer
Returns Cantor’s tuple function for the tuple *xs (the size must be at least 2).
-
.cantor_pairing_inv(c, n = 2) ⇒ Array<Integer>
Returns the inverse of Cantor’s tuple function for the value
c. -
.gamma(x) ⇒ Float
Returns the value of the gamma function, extended to a negative domain.
-
.gammaP_regularized(x, a, epsilon: 1E-16, max_iterations: 1 << 16) ⇒ Float
Return an approximation of the regularized gammaP function for
xandawith an error of <=epsilon, but only iteratemax_iterations-times. -
.gammaQ_regularized(x, a, epsilon: 1E-16, max_iterations: 1 << 16) ⇒ Float
Return an approximation of the regularized gammaQ function for
xandawith an error of <=epsilon, but only iteratemax_iterations-times. -
.log_beta(a, b) ⇒ Float
Returns the natural logarithm of the beta function value for (a, b).
-
.log_ceil(n, b = 2) ⇒ Integer
Computes the ceiling of the base
blogarithm ofn. -
.log_floor(n, b = 2) ⇒ Integer
Computes the floor of the base
blogarithm ofn. -
.logb(x, b = 2) ⇒ Float
Returns the base
blogarithm of the numberx. -
.numberify_string(string, alphabet = 'a'..'z') ⇒ Integer
Computes a Gödel number from
stringin thealphabetand returns it. -
.stringify_number(number, alphabet = 'a'..'z') ⇒ String
Computes the string in the
alphabetfrom a Gödel numbernumberand returns it.
Instance Method Summary collapse
-
#beta(a, b) ⇒ Float
private
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) ⇒ Float
private
Return an approximation value of Euler’s regularized beta function for
x,a, andbwith an error <=epsilon, but only iteratemax_iterations-times. -
#cantor_pairing(*xs) ⇒ Integer
private
Returns Cantor’s tuple function for the tuple *xs (the size must be at least 2).
-
#cantor_pairing_inv(c, n = 2) ⇒ Array<Integer>
private
Returns the inverse of Cantor’s tuple function for the value
c. -
#erf(x) ⇒ Float
Returns an approximate value for the error function’s value for
xunless provided by Ruby. -
#erfc(x) ⇒ Float
Returns the complementary error function value for
xunless provided by Ruby. -
#gamma(x) ⇒ Float
private
Returns the value of the gamma function, extended to a negative domain.
-
#gammaP_regularized(x, a, epsilon: 1E-16, max_iterations: 1 << 16) ⇒ Float
private
Return an approximation of the regularized gammaP function for
xandawith an error of <=epsilon, but only iteratemax_iterations-times. -
#gammaQ_regularized(x, a, epsilon: 1E-16, max_iterations: 1 << 16) ⇒ Float
private
Return an approximation of the regularized gammaQ function for
xandawith an error of <=epsilon, but only iteratemax_iterations-times. -
#log_beta(a, b) ⇒ Float
private
Returns the natural logarithm of the beta function value for (a, b).
-
#log_ceil(n, b = 2) ⇒ Integer
private
Computes the ceiling of the base
blogarithm ofn. -
#log_floor(n, b = 2) ⇒ Integer
private
Computes the floor of the base
blogarithm ofn. -
#log_gamma(x) ⇒ Object
Returns the natural logarithm of Euler gamma function value for
xusing the Lanczos approximation if not provided by Ruby. -
#logb(x, b = 2) ⇒ Float
private
Returns the base
blogarithm of the numberx. -
#numberify_string(string, alphabet = 'a'..'z') ⇒ Integer
private
Computes a Gödel number from
stringin thealphabetand returns it. -
#stringify_number(number, alphabet = 'a'..'z') ⇒ String
private
Computes the string in the
alphabetfrom a Gödel numbernumberand returns it.
Class Method Details
.beta(a, b) ⇒ Float
Returns the value of the beta function for (a, b), +a > 0, b > 0’.
The beta function B(a,b) = Gamma(a)*Gamma(b)/Gamma(a+b) is used in statistical distributions and probability theory.
100 101 102 103 104 105 106 |
# File 'lib/more_math/functions.rb', line 100 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) ⇒ Float
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.
The regularized incomplete beta function I_x(a,b) = B(x;a,b)/B(a,b) is used in statistics to compute probabilities for the beta distribution.
121 122 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 121 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) ⇒ Integer
Returns Cantor’s tuple function for the tuple *xs (the size must be at least 2).
The Cantor pairing function uniquely encodes pairs of natural numbers into a single natural number. This implementation extends it to tuples.
307 308 309 |
# File 'lib/more_math/functions.rb', line 307 def cantor_pairing(*xs) CantorPairingFunction.cantor_pairing(*xs) end |
.cantor_pairing_inv(c, n = 2) ⇒ Array<Integer>
Returns the inverse of Cantor’s tuple function for the value c. n is the length of the tuple (defaults to 2, a pair).
Decodes a natural number back into its original tuple representation.
319 320 321 |
# File 'lib/more_math/functions.rb', line 319 def cantor_pairing_inv(c, n = 2) CantorPairingFunction.cantor_pairing_inv(c, n) end |
.gamma(x) ⇒ Float
Returns the value of the gamma function, extended to a negative domain.
The gamma function is defined for all complex numbers except non-positive integers. For positive real numbers, it extends the factorial: Gamma(n) = (n-1)!
70 71 72 73 74 75 76 |
# File 'lib/more_math/functions.rb', line 70 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) ⇒ Float
Return an approximation of the regularized gammaP function for x and a with an error of <= epsilon, but only iterate max_iterations-times.
The regularized lower incomplete gamma function P(a,x) = γ(a,x)/Γ(a) where γ(a,x) is the lower incomplete gamma function. This is used in statistics to compute probabilities for the gamma distribution.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/more_math/functions.rb', line 158 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) ⇒ Float
Return an approximation of the regularized gammaQ function for x and a with an error of <= epsilon, but only iterate max_iterations-times.
The regularized upper incomplete gamma function Q(a,x) = Γ(a,x)/Γ(a) where Γ(a,x) is the upper incomplete gamma function. This is used in statistics to compute probabilities for the gamma distribution.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/more_math/functions.rb', line 199 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) ⇒ Float
Returns the natural logarithm of the beta function value for (a, b).
The beta function is defined as B(a,b) = Gamma(a)*Gamma(b)/Gamma(a+b) and is commonly used in statistics and probability theory.
86 87 88 89 90 |
# File 'lib/more_math/functions.rb', line 86 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) ⇒ Integer
Computes the ceiling of the base b logarithm of n.
Returns the smallest integer k such that b^k >= n.
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/more_math/functions.rb', line 259 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) ⇒ Integer
Computes the floor of the base b logarithm of n.
Returns the largest integer k such that b^k <= n.
278 279 280 281 282 283 284 285 286 287 |
# File 'lib/more_math/functions.rb', line 278 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) ⇒ Float
Returns the base b logarithm of the number x. b defaults to base 2, binary logarithm.
295 296 297 |
# File 'lib/more_math/functions.rb', line 295 def logb(x, b = 2) Math.log(x) / Math.log(b) end |
.numberify_string(string, alphabet = 'a'..'z') ⇒ Integer
Computes a Gödel number from string in the alphabet and returns it.
Implements Gödel numbering to convert strings into unique natural numbers.
330 331 332 |
# File 'lib/more_math/functions.rb', line 330 def numberify_string(string, alphabet = 'a'..'z') NumberifyStringFunction.numberify_string(string, alphabet) end |
.stringify_number(number, alphabet = 'a'..'z') ⇒ String
Computes the string in the alphabet from a Gödel number number and returns it. This is the inverse function of numberify_string.
Converts a natural number back to its original string representation.
342 343 344 |
# File 'lib/more_math/functions.rb', line 342 def stringify_number(number, alphabet = 'a'..'z') NumberifyStringFunction.stringify_number(number, alphabet) end |
Instance Method Details
#beta(a, b) ⇒ Float (private)
Returns the value of the beta function for (a, b), +a > 0, b > 0’.
The beta function B(a,b) = Gamma(a)*Gamma(b)/Gamma(a+b) is used in statistical distributions and probability theory.
100 101 102 103 104 105 106 |
# File 'lib/more_math/functions.rb', line 100 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) ⇒ Float (private)
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.
The regularized incomplete beta function I_x(a,b) = B(x;a,b)/B(a,b) is used in statistics to compute probabilities for the beta distribution.
121 122 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 121 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) ⇒ Integer (private)
Returns Cantor’s tuple function for the tuple *xs (the size must be at least 2).
The Cantor pairing function uniquely encodes pairs of natural numbers into a single natural number. This implementation extends it to tuples.
307 308 309 |
# File 'lib/more_math/functions.rb', line 307 def cantor_pairing(*xs) CantorPairingFunction.cantor_pairing(*xs) end |
#cantor_pairing_inv(c, n = 2) ⇒ Array<Integer> (private)
Returns the inverse of Cantor’s tuple function for the value c. n is the length of the tuple (defaults to 2, a pair).
Decodes a natural number back into its original tuple representation.
319 320 321 |
# File 'lib/more_math/functions.rb', line 319 def cantor_pairing_inv(c, n = 2) CantorPairingFunction.cantor_pairing_inv(c, n) end |
#erf(x) ⇒ Float
Returns an approximate value for the error function’s value for x unless provided by Ruby.
The error function erf(x) = (2/sqrt(pi)) * ∫₀ˣ e^(-t²) dt is used in probability, statistics, and partial differential equations.
230 231 232 233 234 |
# File 'lib/more_math/functions.rb', line 230 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) ⇒ Float
Returns the complementary error function value for x unless provided by Ruby.
The complementary error function erfc(x) = 1 - erf(x) is used in probability and statistics when computing tail probabilities.
246 247 248 |
# File 'lib/more_math/functions.rb', line 246 def erfc(x) 1.0 - erf(x) end |
#gamma(x) ⇒ Float (private)
Returns the value of the gamma function, extended to a negative domain.
The gamma function is defined for all complex numbers except non-positive integers. For positive real numbers, it extends the factorial: Gamma(n) = (n-1)!
70 71 72 73 74 75 76 |
# File 'lib/more_math/functions.rb', line 70 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) ⇒ Float (private)
Return an approximation of the regularized gammaP function for x and a with an error of <= epsilon, but only iterate max_iterations-times.
The regularized lower incomplete gamma function P(a,x) = γ(a,x)/Γ(a) where γ(a,x) is the lower incomplete gamma function. This is used in statistics to compute probabilities for the gamma distribution.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/more_math/functions.rb', line 158 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) ⇒ Float (private)
Return an approximation of the regularized gammaQ function for x and a with an error of <= epsilon, but only iterate max_iterations-times.
The regularized upper incomplete gamma function Q(a,x) = Γ(a,x)/Γ(a) where Γ(a,x) is the upper incomplete gamma function. This is used in statistics to compute probabilities for the gamma distribution.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/more_math/functions.rb', line 199 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) ⇒ Float (private)
Returns the natural logarithm of the beta function value for (a, b).
The beta function is defined as B(a,b) = Gamma(a)*Gamma(b)/Gamma(a+b) and is commonly used in statistics and probability theory.
86 87 88 89 90 |
# File 'lib/more_math/functions.rb', line 86 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) ⇒ Integer (private)
Computes the ceiling of the base b logarithm of n.
Returns the smallest integer k such that b^k >= n.
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/more_math/functions.rb', line 259 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) ⇒ Integer (private)
Computes the floor of the base b logarithm of n.
Returns the largest integer k such that b^k <= n.
278 279 280 281 282 283 284 285 286 287 |
# File 'lib/more_math/functions.rb', line 278 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 |
#log_gamma(x) ⇒ Object
Returns the natural logarithm of Euler gamma function value for x using the Lanczos approximation if not provided by Ruby.
36 37 38 |
# File 'lib/more_math/functions.rb', line 36 def log_gamma(x) lgamma(x).first end |
#logb(x, b = 2) ⇒ Float (private)
Returns the base b logarithm of the number x. b defaults to base 2, binary logarithm.
295 296 297 |
# File 'lib/more_math/functions.rb', line 295 def logb(x, b = 2) Math.log(x) / Math.log(b) end |
#numberify_string(string, alphabet = 'a'..'z') ⇒ Integer (private)
Computes a Gödel number from string in the alphabet and returns it.
Implements Gödel numbering to convert strings into unique natural numbers.
330 331 332 |
# File 'lib/more_math/functions.rb', line 330 def numberify_string(string, alphabet = 'a'..'z') NumberifyStringFunction.numberify_string(string, alphabet) end |
#stringify_number(number, alphabet = 'a'..'z') ⇒ String (private)
Computes the string in the alphabet from a Gödel number number and returns it. This is the inverse function of numberify_string.
Converts a natural number back to its original string representation.
342 343 344 |
# File 'lib/more_math/functions.rb', line 342 def stringify_number(number, alphabet = 'a'..'z') NumberifyStringFunction.stringify_number(number, alphabet) end |