Created
February 20, 2013 01:07
-
-
Save krishnanraman/4991798 to your computer and use it in GitHub Desktop.
Abelian group
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Abelian | |
case class Zn(order:Int, zero:Int) extends Abelian { | |
def identity = zero | |
def size = order | |
def elements = (1 to order).toSeq | |
def cayley = { | |
Vector.tabulate(order,order)((x,y)=> { | |
val idx = math.abs(zero - (x+1)) // difference between x & zero | |
val timesToShift = if ((x+1)>=zero) idx else (order-idx) | |
val row = shift(elements, timesToShift ) | |
row(y) | |
}) | |
} | |
private def shift(s:Seq[Int], pos:Int) = { | |
val n = 1+s.indexOf(pos) | |
s.slice(n,s.size)++s.slice(0,n) | |
} | |
def plus(x:Int, y:Int) = cayley(x-1)(y-1) | |
def inverse(x:Int) = cayley(x-1).indexOf(zero)+1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment