Module: Combinatorics::Permute

Defined in:
lib/combinatorics/permute/cardinality.rb,
lib/combinatorics/permute/mixin.rb

Overview

Author:

Since:

  • 0.4.0

Defined Under Namespace

Modules: Mixin

Class Method Summary collapse

Class Method Details

.cardinality(n, r = nil) ⇒ Fixnum

Note:

This function is well-known within fields of academic inquiry such as discrete mathematics and set theory. It is represented in "chalkboard" notation by the letter "P."

Mathematically determine the number of elements in a r-permutations set.

Examples:

Calculate total 4-permutations for a set whose cardinality is 6

cardinality(6, 4)
# => 360

Parameters:

  • n (Fixnum)

    The number of elements in the input set.

  • r (Fixnum) (defaults to: nil)

    Cardinality of permuted subsets.

Returns:

  • (Fixnum)

    The product of the first r factors of n.

Raises:

  • (RangeError)

    n must be non-negative.

  • (RangeError)

    r must be non-negative.

  • (RangeError)

    r must be less than or equal to n.

See Also:

Since:

  • 0.4.0



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/combinatorics/permute/cardinality.rb', line 45

def self.cardinality(n,r=nil) 
  raise(RangeError,"n must be non-negative") if n < 0

  case r
  when 0   then 0
  when nil then Math.factorial(n)
  else
    raise(RangeError,"r must be non-negative") if r < 0
    raise(RangeError,"r must be less than or equal to n") if r > n

    Math.factorial(n) / Math.factorial(n - r)
  end
end

.cardinality_all(n, c = (1..n)) ⇒ Array

Note:

sum of elements will equal factorial(c)

Compute cardinality of all r-permutations for a set with cardinality c

Examples:

cardinality_all(4)

# => [4, 3, 10, 1]

Parameters:

  • c (Fixnum) (defaults to: (1..n))

    Input set cardinality.

Returns:

  • (Array)

    Elements are cardinalities for each subset 1 .. c.

Raises:

  • (RangeError)

    c must be non-negative.

See Also:

Since:

  • 0.4.0



88
89
90
91
92
93
94
# File 'lib/combinatorics/permute/cardinality.rb', line 88

def self.cardinality_all(n,c=(1..n))
  if n < 0
    raise(RangeError,"n must be non-negative")
  end

  c.map { |r| cardinality(n,r) }
end

.N(n, r = nil) ⇒ Object

Note:

In the study of set theory, permutations are often referenced by the name of an associated algorithm called "n-choose-r."

See Also:

Since:

  • 0.4.0



65
# File 'lib/combinatorics/permute/cardinality.rb', line 65

def self.N(n,r=nil);  cardinality(n,r); end

.N_all(c) ⇒ Object

Since:

  • 0.4.0



96
# File 'lib/combinatorics/permute/cardinality.rb', line 96

def self.N_all(c);  cardinality_all(c); end

.NR(n, r = nil) ⇒ Object

Since:

  • 0.4.0



66
# File 'lib/combinatorics/permute/cardinality.rb', line 66

def self.NR(n,r=nil); cardinality(n,r); end

.NR_all(c) ⇒ Object

Since:

  • 0.4.0



97
# File 'lib/combinatorics/permute/cardinality.rb', line 97

def self.NR_all(c); cardinality_all(c); end

.R(n, r = nil) ⇒ Object

Since:

  • 0.4.0



67
# File 'lib/combinatorics/permute/cardinality.rb', line 67

def self.R(n,r=nil);  cardinality(n,r); end

.R_all(c) ⇒ Object

Since:

  • 0.4.0



98
# File 'lib/combinatorics/permute/cardinality.rb', line 98

def self.R_all(c);  cardinality_all(c); end