Module: KXI::Math

Defined in:
lib/kxi/math/math.rb,
lib/kxi/math/matrix.rb,
lib/kxi/math/vector.rb,
lib/kxi/math/polynomial.rb

Defined Under Namespace

Classes: Matrix, Polynomial, Vector

Class Method Summary collapse

Class Method Details

.choose(n, k) ⇒ Numeric

Computes the binomial coefficient



30
31
32
# File 'lib/kxi/math/math.rb', line 30

def self.choose(n, k)
  return pfact(n, k) / fact(n - k)
end

.fact(n) ⇒ Integer

Computes factorial



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

def self.fact(n)
  n <= 0 ? 1 : n * fact(n - 1)
end

.permutation(n, k) ⇒ Numeric

Computes the permutation of two numbers



38
39
40
41
# File 'lib/kxi/math/math.rb', line 38

def self.permutation(n, k)
  return 0 if k > n
  pfact(n, n - k)
end

.pfact(n, m) ⇒ Numeric

Computes fraction of factorials (!n / !m)



20
21
22
23
24
# File 'lib/kxi/math/math.rb', line 20

def self.pfact(n, m)
  return 1 if n == m
  return (1.to_f / lfact(m, m - n)) if m > n
  return lfact(n, n - m)
end