[index] [finite-map] Algebra::Set / Enumerable
Class of Set
This is the class of sets. The conclusion relationship is determined by each and member?, that is, s is the subset of t if and only if
s.all?{|x| t.member?(x)}
is true.
::[[obj0, [obj1, [obj2, ...]]]]Creates Set objects from parameters.
Example: Create {"a", [1, 2], 0}
require "finite-set"
p Algebra::Set[0, "a", [1, 2]]
p Algebra::Set.new(0, "a", [1, 2])
p Algebra::Set.new_a([0, "a", [1, 2]])
p Algebra::Set.new_h({0=>true, "a"=>true, [1, 2]=>true})::new([obj0, [obj1, [obj2, ...]]])::new_a(a)::new_h(h)::empty_set::phi::null::singleton(x)empty_setphinullempty?phi?empty_set?null?singleton(x)singleton?sizeeachIterates the block with the block parameter of each element. The order of iteration is indefinite.
Example:
require "finite-set" include Algebra Set[0, 1, 2].each do |x| p x #=> 1, 0, 2 end
separateReturns the set of the elements which make the block true.
Example:
require "finite-set"
include Algebra
p Set[0, 1, 2, 3].separate{|x| x % 2 == 0} #=> {2, 0}select_sfind_all_smap_sReturn the set of the values of the block.
Example:
require "finite-set"
include Algebra
p Set[0, 1, 2, 3].map_s{|x| x % 2 + 1} #=> {2, 1}pickshiftTakes an element from self and returns it.
Example:
require "finite-set"
include Algebra
s = Set[0, 1, 2, 3]
p s.shift #=> 2
p s #=> {0, 1, 3}dupappend!(x)push<<append(x)concat(other)rehasheql?(other) self >= other and self <= other.==hashinclude?(x)member?has?contains?superset?(other)nother.all{|x| member?(x)}.>=incl?superset?.subset?(other)<=part_of?<(other)>(other)union(other = nil)Returns the union of self and other. If other is omitted, returns the union of the self the set of sets.
Example:
require "finite-set"
include Algebra
p Set[0, 2, 4].cup Set[1, 3] #=> {0, 1, 2, 3, 4}
s = Set[*(0...15).to_a]
s2 = s.separate{|x| x % 2 == 0}
s3 = s.separate{|x| x % 3 == 0}
s5 = s.separate{|x| x % 5 == 0}
p Set[s2, s3, s5].union #=> {1, 7, 11, 13}|+cupintersection(other = nil)Returns the intersection of self and other. If other is omitted, returns the intersection of the self the set of sets.
Example:
require "finite-set"
include Algebra
p Set[0, 2, 4].cap(Set[4, 2, 0]) #=> {0, 2, 4}
s = Set[*(0..30).to_a]
s2 = s.separate{|x| x % 2 == 0}
s3 = s.separate{|x| x % 3 == 0}
s5 = s.separate{|x| x % 5 == 0}
p Set[s2, s3, s5].cap #=> {0, 30}&capdifference(other)-each_pairIterates with each two different elements of self.
Example:
require "finite-set" include Algebra s = Set.phi Set[0, 1, 2].each_pair do |x, y| s.push [x, y] end p s == Set[[0, 1], [0, 2], [1, 2]] #=> true
each_member(n)Iterates with each n different elements of self.
Example:
require "finite-set" include Algebra s = Set.phi Set[0, 1, 2].each_member(2) do |x, y| s.push [x, y] end p s == Set[[0, 1], [0, 2], [1, 2]] #=> true
each_subsetIterates over each subset of self.
Example:
require "finite-set" include Algebra s = Set.phi Set[0, 1, 2].each_subset do |t| s.append! t end p s.size = 2**3 #=> true
each_non_trivial_subsetpower_seteach_product(other)Iterates over for each x in self and each y in other
Exameple:
require "finite-set" include Algebra Set[0, 1].each_prodct(Set[0, 1]) do |x, y| p [x, y] #=> [0,0], [0,1], [1,0], [1,1] end
product(other)Returns the product set of self and other.
The elements are the arrays of type [x, y].
If the block is given, it returns the set which consists
of the value of the block.
Example:
require "finite-set"
include Algebra
p Set[0, 1].product(Set[0, 1]) #=> {[0,0], [0,1], [1,0], [1,1]}
p Set[0, 1].product(Set[0, 1]){|x, y| x + 2*y} #=> {0, 1, 2, 3]*equiv_class([equiv])The evaluation of the block:
require "finite-set"
include Algebra
s = Set[0, 1, 2, 3, 4, 5]
p s.equiv_class{|a, b| (a - b) % 3 == 0} #=> {{0, 3}, {1, 4}, {2, 5}}The value of the instance method call(x, y) of the parameter.
require "finite-set"
include Algebra
o = Object.new
def o.call(x, y)
(x - y) % 3 == 0
end
s = Set[0, 1, 2, 3, 4, 5]
p s.equiv_class(o) #=> {{0, 3}, {1, 4}, {2, 5}}The value of method indicated Symbol.
require "finite-set"
include Algebra
s = Set[0, 1, 2, 3, 4, 5]
def q(x, y)
(x - y) % 3 == 0
end
p s.equiv_class(:q) #=> {{0, 3}, {1, 4}, {2, 5}}/to_ato_arysortto_a.sort.power(other)Returns the all maps from other to self. The maps are the instances of Map.
Example:
require "finite-map" include Algebra a = Set[0, 1, 2, 3] b = Set[0, 1, 2] s = p( (a ** b).size ) #=> 4 ** 3 = 64 p b.surjections(a).size #=> S(3, 4) = 36 p a.injections(b).size #=> 4P3 = 24
** poweridentity_mapsurjections(other)injections(other)bijections(other)any?all?Returns true when the block is true for all elements. This is defined by:
!any?{|x| !yield(x)}
(These are built-in methods of ruby-1.8).