Skip to content

Instantly share code, notes, and snippets.

@krtx
Last active December 16, 2015 09:38
Show Gist options
  • Save krtx/5414184 to your computer and use it in GitHub Desktop.
Save krtx/5414184 to your computer and use it in GitHub Desktop.
the ruby program which calculates conjugacy classes of symmetry groups and alternate groups
require 'pp'
require 'set'
class Array
def rotmin
self.rotate(self.index(self.min))
end
end
class Permutation < Array
def *(other)
raise if self.length != other.length
t = Permutation.new(self.length)
self.length.times do |i|
t[i] = self[other[i]]
end
return t
end
def **(n)
t = Permutation.new((0...self.length).to_a)
s = self
while n > 0
t *= s if n & 1
s *= s
n >>= 1
end
return t
end
def inv
t = Permutation.new(self.length)
self.length.times do |i|
t[self[i]] = i
end
return t
end
def to_cyclic
t = []
seen = Array.new(self.length, false)
self.length.times do |i|
next if seen[i]
cur = [self[i]]
seen[self[i]] = true
while true
if cur[0] == self[cur.last]
t << cur
break
else
cur << self[cur.last]
seen[cur.last] = true
end
end
end
t.delete_if {|x| x.length == 1}
return t
end
# cyclic notation
def to_s(offset=1)
c = self.to_cyclic
if c.length == 0
"()"
else
c.collect {|s| "(" + s.rotmin.map{|x| x + offset}.join(" ") + ")"}.join
end
end
def sgn
ret = 1
self.to_cyclic.each do |c|
ret *= -1 if c.length.even?
end
return ret
end
end
def sym_group(n)
ret = []
(0...n).to_a.permutation do |a|
ret << Permutation.new(a)
end
ret
end
def alt_group(n)
sym_group(n).delete_if {|s| s.sgn == -1}
end
def conj_class(group)
seen = Set.new
ret = []
group.each_with_index do |x|
next if seen.include?(x)
r = Set.new
group.each do |g|
h = g * x * g.inv
seen.add(h)
r.add(h)
end
ret << r
end
return ret
end
def pp_conjclass(group)
conj_class(group).each do |c|
puts "{#{c.map {|d| d.to_s}.join(", ")}}"
end
end
pp_conjclass(alt_group(5))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment