Skip to content

Instantly share code, notes, and snippets.

@krishnanraman
Created February 20, 2013 01:07
Show Gist options
  • Save krishnanraman/4991798 to your computer and use it in GitHub Desktop.
Save krishnanraman/4991798 to your computer and use it in GitHub Desktop.
Abelian group
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