Sha256: cd60dcb4d26d75a4a1f51f6fbe1552b2fed5b86bc2c3631d8bb7280adacdc12d

Contents?: true

Size: 841 Bytes

Versions: 12

Compression:

Stored size: 841 Bytes

Contents

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Copyright Steffie Dorn <mail@muflax.com>, 2018
# License: GNU APGLv3 (or later) <http://www.gnu.org/copyleft/gpl.html>

module Math
  class << self
    def harmonic n
      (1..n).reduce(0.0){|s, i| s + (1.0 / i)}
    end

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

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

    # TODO meh iterative solution
    def sufficiency exceptions
      n = 1
      n += 1 while θ(n) < exceptions
      n
    end
    alias :sufficient :sufficiency

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

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

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
muflax-0.5.0 lib/muflax/math.rb
muflax-0.4.2 lib/muflax/math.rb
muflax-0.4.1 lib/muflax/math.rb
muflax-0.4.0 lib/muflax/math.rb
muflax-0.3.23 lib/muflax/math.rb
muflax-0.3.22 lib/muflax/math.rb
muflax-0.3.21 lib/muflax/math.rb
muflax-0.3.20 lib/muflax/math.rb
muflax-0.3.19 lib/muflax/math.rb
muflax-0.3.18 lib/muflax/math.rb
muflax-0.3.17 lib/muflax/math.rb
muflax-0.3.16 lib/muflax/math.rb