Module: Math

Defined in:
lib/muflax/math.rb

Overview

Copyright Steffie Dorn <[email protected]>, 2018 License: GNU APGLv3 (or later) <www.gnu.org/copyleft/gpl.html>

Class Method Summary collapse

Class Method Details

.choose(k, n) ⇒ Object



35
36
37
# File 'lib/muflax/math.rb', line 35

def choose k, n
  Math.factorial(n) / (Math.factorial(k) * Math.factorial(n - k))
end

.factorial(n) ⇒ Object



31
32
33
# File 'lib/muflax/math.rb', line 31

def factorial n
  (2..n).reduce(1){|f, x| f * x}
end

.harmonic(n) ⇒ Object



8
9
10
# File 'lib/muflax/math.rb', line 8

def harmonic n
  (1..n).reduce(0.0){|s, i| s + (1.0 / i)}
end

.sufficiency(exceptions) ⇒ Object Also known as: sufficient

TODO meh iterative solution



24
25
26
27
28
# File 'lib/muflax/math.rb', line 24

def sufficiency exceptions
  n = 1
  n += 1 while θ(n) < exceptions
  n
end

.tolerance(total) ⇒ Object Also known as: tolerate, θ



16
17
18
19
# File 'lib/muflax/math.rb', line 16

def tolerance total
  θ = total / Math.log(total)
  θ > total ? total : θ.round
end

.zipf(k, n) ⇒ Object



12
13
14
# File 'lib/muflax/math.rb', line 12

def zipf k, n
  1.0 / (k.to_f * harmonic(n))
end