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

Parameters:

  • n (Integer)

    First (upper) term of coefficient

  • k (Integer)

    Second (lower) term of coefficient

Returns:

  • (Numeric)

    Binomial coefficient (n choose k)



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

Parameters:

  • n (Integer)

    Factorial base

Returns:

  • (Integer)

    Value of 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

Parameters:

  • n (Integer)

    First term of permutation

  • k (Integer)

    Second term of permutation

Returns:

  • (Numeric)

    Permutation (k-permutations of n)



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)

Parameters:

  • n (Integer)

    Factorial base of numerator

  • m (Integer)

    Factorial base of denominator

Returns:

  • (Numeric)

    Result of fraction



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