
[index-ja] [finite-map-ja] Algebra::Set / Enumerable

Algebra::Set

Υ饹

ɽ륯饹ǤĤν s, t ˴ؤơ s  t ˴ޤޤ all?
Ȥäơ
s.all?{|x| t.has?(x)}

ɽޤ

ե̾:

  finite-set.rb

ѡ饹:

  Object

󥯥롼ɤƤ⥸塼:

  Enumerable

饹᥽å:

::[[obj0, [obj1, [obj2, ...]]]]
    󤫤 Set ֥Ȥޤ
   
    :  {"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, ...]]])
    󤫤 Set ֥Ȥޤ
   
::new_a(a)
     a  Set ֥Ȥޤ
   
::new_h(h)
    Hash  Set ֥Ȥޤ
   
self.empty_set
    롣
   
::phi
::null
    ::empty_set ΥꥢǤ
   
::singleton(x)
    x 층Τߤǹ뽸ޤ
   

᥽å:

empty_set
    롣
   
phi
null
    empty_set ΥꥢǤ
   
empty?
    ǤȤ֤ޤ
   
phi?
empty_set?
null?
    empty? ΥꥢǤ
   
singleton(x)
    x 층Τߤǹ뽸ޤ
   
singleton?
    층Τߤǹ뽸ǤȤ֤ޤ
   
size
    礭֤ޤ
   
each
    γǤ˴ؤƷ֤ޤ֤ν֤Ǥ
   
    :
    require "finite-set"
    include Algebra
    Set[0, 1, 2].each do |x|
      p x #=> 1, 0, 2
    end
   
separate
    γǤ֥åѥ᡼Ϥ֥åͤ򿿤ˤΤǹ
    뽸֤ޤ
   
    :
    require "finite-set"
    include Algebra
    p Set[0, 1, 2, 3].separate{|x| x % 2 == 0} #=> {2, 0}
   
select_s
find_all_s
    separate ΥꥢǤ
   
map_s
    γǤ֥åѥ᡼Ϥ֥åͤˤäƹ뽸
    ֤ޤ
   
    :
    require "finite-set"
    include Algebra
    p Set[0, 1, 2, 3].map_s{|x| x % 2 + 1} #=> {2, 1}
   
pick
    ǤͤȤޤɤǤФ뤫Ǥ
    ФƤ nil ֤ޤ
   
shift
    ǤǼФͤȤޤɤǤФ뤫Ǥ
    
   
    :
    require "finite-set"
    include Algebra
    s = Set[0, 1, 2, 3]
    p s.shift #=> 2
    p s #=> {0, 1, 3}
   
dup
    ʣ֤ޤ Hash ʣˤޤ
   
append!(x)
     x դäޤ֤ͤ self Ǥ
   
push
<<
    append! ΥꥢǤ
   
append(x)
     x դäʣ֤ޤ
   
concat(other)
    ̤ν other Ǥդäޤ+ ˲ǤǤ
   
rehash
     Hash ֥Ȥ rehash ޤ
   
eql?(other)
     other Ȥ֤ޤ self >= other and self <= other 
    ƱͤǤ
   
==
    eql? ΥꥢǤ
   
hash
    Ȥ Hash 뤤 Set ǤȤʤȤѤϥåʹؿǤ
   
include?(x)
    礬 x ǤȤƤȤ֤ޤ
   
has?
contains?
    include? ΥꥢǤ
   
superset?(other)
    礬¾ν other ޤȤ֤ޤ other.all{|x| has?(x)} 
    ƱͤǤ
   
>=
incl?
    superset? ΥꥢǤ
   
subset?(other)
    礬¾ν other ʬǤȤ֤ޤ self.all{|x|
    other.has?(x)} ƱͤǤ
   
<=
part_of?
    subset? ΥꥢǤ
   
<(other)
    self  other οʬλ֤ޤ
   
>(other)
    self  other 򿿤˴ޤ֤ޤ
   
union([other])
    self  other ιʻ֤ޤ other ά줿硢Ȥ򽸹ν
    ȤߤʤƤǤιʻ֤ޤ
   
    :
    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}
   
|
+
cup
    union ΥꥢǤ
   
intersection([other])
    self  other θν֤ޤ other ά줿硢Ȥ򽸹
    νȤߤʤƤǤζʬ֤ޤ
   
    :
    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}
   
&
cap
    intersection ΥꥢǤ
   
difference(other)
    self  other ˴ޤޤǤΤ֤ޤ
   
-
    difference ΥꥢǤ
   
each_pair
    礫ۤʤ룲ĤǤФơ֥åѥ᡼Ʒ֤
    ޤ
   
    :
    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)
    礫ۤʤ n ĤǤФơ֥åѥ᡼Ʒ֤
    ޤ
   
    :
    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_subset
    Ƥʬ֥åѥ᡼Ʒ֤ޤ
   
    :
    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_subset
    ζǤʤʬ֥åѥ᡼Ʒ֤ޤ
   
power_set
    Ƥʬν֤ޤ
   
each_product(other)
    self  other Ƥ x, y ˤĤơ֤ޤ
   

:
   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)
    self  other ѽ֤ޤѽγƸ [x, y] Ǥ֥å
    Ϳ줿ϡ֥åɾͤǹ뽸֤ޤ
   
    :
    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]
   
*
    product ΥꥢǤ
   
equiv_class([equiv])
    Ʊʹطǳä֤ޤƱʹطͿϼΣ̤ꤢ
    
     1. ֥åɾͤ򿿤ˤƱʹط
        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}}
       
     2. Ϳ줿֥ȤФ᥽å call(x, y) οͤˤƱ
        ʹط
        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}}
       
     3. Ϳ줿 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}}
       
   
/
    equiv_class ΥꥢǤ
   
to_a
    ˤ֤ޤǤ¤ӤνǤ
   
to_ary
    to_a ΥꥢǤ
   
sort
    to_a ͤ򥽡Ȥ֤ޤ
   
power(other)
    other  self ؤμƤν֤ޤ Map θȤɽ
    ޤ
   
    :
    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
   
**
    power ΥꥢǤ
   
surjections(other)
    other  self ؤƤν֤ޤ
   
injections(other)
    other  self ؤñƤν֤ޤ
   
bijections(other)
    other  self ؤñƤν֤ޤ
   

Enumerable

ե̾:

  finite-set.rb

᥽å:

any?
    ֥å򿿤ˤǤȤ֤ޤ Enumerable#find ̾Ǥ
    (built-in of ruby-1.8)
   
all?
    ƤǤˤĤƥ֥åǤȤ֤ޤ
    !any?{|x| !yield(x)}
   
    Ƥޤ(built-in of ruby-1.8)
   

