[index-ja] [finite-map-ja] Algebra::Set / Enumerable
集合のクラス
集合を表現するクラスです。2つの集合 s, t に関して、 s が t に含まれる事は all? を使って、
s.all?{|x| t.member?(x)}
で表現されます。
::[[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, ...]]])
::new_a(a)
::new_h(h)
self.empty_set
::phi
::null
::singleton(x)
empty_set
phi
null
empty?
phi?
empty_set?
null?
singleton(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
map_s
集合の各要素をブロックパラメータに渡し、ブロックの値によって 構成される集合を返します。
例:
require "finite-set" include Algebra p Set[0, 1, 2, 3].map_s{|x| x % 2 + 1} #=> {2, 1}
pick
shift
集合の要素から一つ選んで取り出し値とします。どの要素が選ばれる かは不定です。
例:
require "finite-set" include Algebra s = Set[0, 1, 2, 3] p s.shift #=> 2 p s #=> {0, 1, 3}
dup
append!(x)
push
<<
append(x)
concat(other)
rehash
eql?(other)
self >= other and self <= other
と同値です。==
hash
include?(x)
member?
has?
contains?
superset?(other)
other.all{|x| member?(x)}
と同値です。>=
incl?
superset?
のエイリアスです。subset?(other)
self.all{|x| other.member?(x)}
と同値です。<=
part_of?
<(other)
>(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
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
difference(other)
-
each_pair
集合から異なる2つの要素を取り出して、ブロックパラメータに 代入して繰り返します。
例:
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)
例: 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]
*
equiv_class([equiv])
ブロックの評価値を真にする同値関係
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}}
引数に与えられたオブジェクトに対するメソッド 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}}
引数に与えられた 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_a
to_ary
sort
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
**
identity_map
surjections(other)
injections(other)
bijections(other)
any?
all?
全ての要素についてブロックが真であるとき、真を返します。
!any?{|x| !yield(x)}
と定義されています。(built-in of ruby-1.8)