Module: Combinatorics::Permute
- Defined in:
- lib/combinatorics/permute/cardinality.rb,
lib/combinatorics/permute/mixin.rb
Overview
Defined Under Namespace
Modules: Mixin
Class Method Summary collapse
-
.cardinality(n, r = nil) ⇒ Fixnum
Mathematically determine the number of elements in a r-permutations set.
-
.cardinality_all(n, c = (1..n)) ⇒ Array
Compute cardinality of all r-permutations for a set with cardinality c.
- .N(n, r = nil) ⇒ Object
- .N_all(c) ⇒ Object
- .NR(n, r = nil) ⇒ Object
- .NR_all(c) ⇒ Object
- .R(n, r = nil) ⇒ Object
- .R_all(c) ⇒ Object
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.
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
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."
65 |
# File 'lib/combinatorics/permute/cardinality.rb', line 65 def self.N(n,r=nil); cardinality(n,r); end |
.N_all(c) ⇒ Object
96 |
# File 'lib/combinatorics/permute/cardinality.rb', line 96 def self.N_all(c); cardinality_all(c); end |
.NR(n, r = nil) ⇒ Object
66 |
# File 'lib/combinatorics/permute/cardinality.rb', line 66 def self.NR(n,r=nil); cardinality(n,r); end |
.NR_all(c) ⇒ Object
97 |
# File 'lib/combinatorics/permute/cardinality.rb', line 97 def self.NR_all(c); cardinality_all(c); end |
.R(n, r = nil) ⇒ Object
67 |
# File 'lib/combinatorics/permute/cardinality.rb', line 67 def self.R(n,r=nil); cardinality(n,r); end |
.R_all(c) ⇒ Object
98 |
# File 'lib/combinatorics/permute/cardinality.rb', line 98 def self.R_all(c); cardinality_all(c); end |