Class: MoreMath::Subset
- Includes:
- Enumerable, RankingCommon
- Defined in:
- lib/more_math/subset.rb
Overview
A subset represents a selection of elements from a set, where each element can either be included (1) or excluded (0) from the subset. Subsets are ranked in lexicographic order, allowing efficient enumeration and indexing.
Instance Attribute Summary
Attributes included from RankingCommon
#collection, #last, #rank, #size
Class Method Summary collapse
-
.for(collection, rank = 0) ⇒ Subset
Creates a new Subset instance for a collection of size
sizewith the rankrank. -
.power_set(collection) ⇒ Array<Array>
Returns the power set of the collection
collection.
Instance Method Summary collapse
-
#initialize(size, rank = 0) ⇒ Subset
constructor
Returns a Subset instance for a collection of size
sizewith the rankrank. -
#project(data = nil) ⇒ Array
This method maps elements from a given dataset based on the subset’s indices determined by its rank and returns the result, while ensuring the input data size matches the subset’s size.
-
#rank=(m) ⇒ Integer
Assigns
mto the rank attribute of this Subset instance. -
#value ⇒ Array<Integer>
Returns the subset for rank #rank and #collection.
Methods included from RankingCommon
#each, #each!, #next, #next!, #pred, #pred!, #random, #random!
Constructor Details
#initialize(size, rank = 0) ⇒ Subset
Returns a Subset instance for a collection of size size with the rank rank. Creates a new Subset instance of size (and ranked with rank).
36 37 38 39 |
# File 'lib/more_math/subset.rb', line 36 def initialize(size, rank = 0) @size, self.rank = size, rank @last = (1 << size) - 1 end |
Class Method Details
.for(collection, rank = 0) ⇒ Subset
Creates a new Subset instance for a collection of size size with the rank rank.
47 48 49 50 51 |
# File 'lib/more_math/subset.rb', line 47 def self.for(collection, rank = 0) subset = new(collection.size, rank) subset.instance_variable_set(:@collection, collection) subset end |
.power_set(collection) ⇒ Array<Array>
Returns the power set of the collection collection.
57 58 59 |
# File 'lib/more_math/subset.rb', line 57 def self.power_set(collection) self.for(collection).map(&:value) end |
Instance Method Details
#project(data = nil) ⇒ Array
This method maps elements from a given dataset based on the subset’s indices determined by its rank and returns the result, while ensuring the input data size matches the subset’s size.
91 92 93 94 95 |
# File 'lib/more_math/subset.rb', line 91 def project(data = nil) data ||= @collection || (0...size).to_a raise ArgumentError, "data size is != #{size}!" if data.size != size value.map { |i| data[i] } end |
#rank=(m) ⇒ Integer
Assigns m to the rank attribute of this Subset instance. That implies that the indices produced by a call to the Subset#value method of this instance is the subset ranked with this new rank.
67 68 69 |
# File 'lib/more_math/subset.rb', line 67 def rank=(m) @rank = m % (1 << size) end |
#value ⇒ Array<Integer>
Returns the subset for rank #rank and #collection. (If no collection was set it applies to the array [ 0, 1, …, size - 1 ] instead.)
75 76 77 78 79 80 81 82 83 |
# File 'lib/more_math/subset.rb', line 75 def value result = [] c = @collection || (0...size).to_a r = @rank size.times do |i| r[i] == 1 and result << c[i] end result end |