Skip to content

Instantly share code, notes, and snippets.

@kitayuta
Last active December 11, 2015 23:11
Show Gist options
  • Save kitayuta/2360c8f87e37381cab7b to your computer and use it in GitHub Desktop.
Save kitayuta/2360c8f87e37381cab7b to your computer and use it in GitHub Desktop.
部分集合の族が位相であるか判定するやつ
require 'set'
class Array
def subsets
(0..self.length).flat_map {|n| self.combination(n).to_a}
end
end
def is_topology(x, o)
o.to_a.subsets.all? do |osets|
intersection = osets.inject(x, :&)
union = osets.inject(Set[], :+)
o.include?(intersection) && o.include?(union)
end
end
# 集合と位相演習の問題より
x = Set[:a, :b, :c, :d, :e]
os = [
Set[Set[], Set[:a], Set[:a, :b], Set[:a, :c], x],
Set[Set[], Set[:a, :b, :c], Set[:a, :b, :d], Set[:a, :b, :c, :d], x],
Set[Set[], Set[:a], Set[:a, :b], Set[:a, :c, :d], Set[:a, :b, :c, :d], x],
Set[Set[], Set[:a], Set[:e], Set[:b, :c], Set[:a, :b, :c, :e], x],
Set[Set[], Set[:b], Set[:b, :c], Set[:a, :b, :c], Set[:b, :d, :e], Set[:b, :c, :d, :e], x],
]
p os.map {|o| is_topology(x, o)} # => [false, false, true, false, true]
# 密着位相と離散位相
y = Set[:a, :b, :c]
p [Set[Set[], y], Set[*(y.to_a.subsets.map {|a| Set[*a]})]].map {|o| is_topology(y, o)} # => [true, true]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment