Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
159 lines (126 sloc) 3.47 KB
Type TGroup Abstract
Method Finite() Abstract
Method Order() Abstract
Method Elements:TGroupElement[]() Abstract
Method Generators:TGroupElement[]() Abstract
Method Contains(e:TGroupElement) Abstract
Method Subset(G:TGroup) Abstract
Method Equal(G:TGroup) Abstract
Method Identity:TGroupElement() Abstract
Method Inverse:TGroupElement(e:TGroupElement) Abstract
Method Product:TGroupElement(l:TGroupElement, r:TGroupElement) Abstract
End Type
Type TFiniteGroup Extends TGroup Abstract
Method Finite()
Return True
End Method
Method Subset(G:TGroup)
If Not G.Finite() Return False
For e:TGroupElement=EachIn G.Elements()
If Not contains(e) Return False
Return True
End Method
Method Equal(G:TGroup)
If Not G.Finite() Return False
If G.Order() <> Order() Return False
For e:TGroupElement=EachIn G.Elements()
If Not Contains(e) Return False
For e:TGroupElement=EachIn Elements()
If Not G.Contains(e) Return False
Return True
End Method
End Type
Type TGroupElement Abstract
Method repr$() Abstract
End Type
Type CyclicGroup Extends TFiniteGroup
Field size
Function Create:CyclicGroup(size)
G:CyclicGroup = New CyclicGroup
G.size = size
Return G
End Function
Method Order()
Return size
End Method
Method Elements:TGroupElement[]()
Local elements:TGroupElement[]=New TGroupElement[size]
For n=0 To size-1
Return elements
End Method
Method Generators:TGroupElement[]()
Local generators:TGroupElement[]=[IntegerElement.Create(1)]
Return generators
End Method
Method Contains(e:TGroupElement)
n:IntegerElement = IntegerElement( e )
If Not n Return False
If n.number>=size Return False
Return True
End Method
Method Identity:TGroupElement()
Return IntegerElement.Create( 0 )
End Method
Method Inverse:TGroupElement(e:TGroupElement)
n:IntegerElement = IntegerElement(e)
Return IntegerElement.Create( size - n.number )
End Method
Method Product:TGroupElement(l:TGroupElement, r:TGroupElement)
n1:IntegerElement = IntegerElement(l)
n2:IntegerElement = IntegerElement(r)
Return IntegerElement.Create( (n1.number + n2.number) Mod size )
End Method
Method Random:TGroupElement()
Local k:TGroupElement[] = elements()
Local names$[n]
For i=0 To n-1
Print ", ".join(names)
For t=1 To 100
If j>=i j:+1
l:TGroupElement = k[i]
r:TGroupElement = k[j]
If Rand(0,1)=1 r=Inverse(r)
If Rand(0,1)=1 'left
k[i] = Product( l, r )
k[i] = Product( r, l )
Return k[ Rand(0,n-1) ]
End Method
End Type
Type IntegerElement Extends TGroupElement
Field number
Function Create:IntegerElement( number )
i:IntegerElement = New IntegerElement
i.number = number
Return i
End Function
Method repr$()
Return String(number)
End Method
End Type
SeedRnd MilliSecs()
Print "order: "+cg.order()
Print "finite: "+cg.finite()
Print "elements:"
For e:TGroupElement=EachIn cg.Elements()
Print e.repr()+": inverse = "+cg.inverse(e).repr()
Print "generators: "
For e:TGroupElement=EachIn cg.Generators()
Print e.repr()
Print "4+3 = "+cg.product( integerelement.Create(4), integerelement.Create(3) ).repr()
Print cg.random().repr()
Something went wrong with that request. Please try again.