class MoreMath::Subset
def self.for(collection, rank = 0)
def self.for(collection, rank = 0) subset = new(collection.size, rank) subset.instance_variable_set(:@collection, collection) subset end
def self.power_set(collection)
def self.power_set(collection) self.for(collection).map(&:value) end
def initialize(size, rank = 0)
Returns a Subset instance for a collection of size +size+ with the rank
def initialize(size, rank = 0) @size, self.rank = size, rank @last = (1 << size) - 1 end
def project(data = nil)
subset's indices determined by its rank and returns the result, while
This method maps elements from a given dataset based on the
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
def rank=(m)
m
to the rank attribute of this object.
def rank=(m) @rank = m % (1 << size) end
def value
Returns the subset for rank #rank and #collection. (If no collection was
def value result = [] c = @collection || (0...size).to_a r = @rank 0.upto(size) do |i| r[i] == 1 and result << c[i] end result end