"""
Yet another Python module for playing around with
group theory concepts.

K. Urner, March 2005

For background see:

"""

class P(object):

    modulus = 23
    
    def __init__(self, value):
        self.value = value % P.modulus

    def __mul__(self, other):
        return P(self.value * other.value)

    def __div__(self, other):
        # defined in terms of multiplication        

        return self * other.inverse()
    
    def __pow__(self, n):
        p = P(self.value)
        if n==0:      # 0th power

            return P(1)
        elif n < 0:   # negative power

            p = self.inverse()            
        for i in range(n-1):
            p *= P(self.value)
        return p
    
    def inverse(self):
        n = 0
        for i in range(P.modulus):  # seek/return inverse

            if (self.value * i) % P.modulus == 1:
                return P(i)

    def __cmp__(self, other):
        return cmp(self.value, other.value)
                   
    def __repr__(self):
        return "%s" % self.value

def inverse(p):
    return p.inverse()

def table(thelist, op="*"):
    rowlen = len(thelist)
    format = "%3s " * rowlen
    header = "-" * len(format)
    vals = [eval('(a' + op + 'b)') for a in thelist for b in thelist]
    print "%3s | " % op + format % tuple(thelist)
    print "     " + header
    for i in range(rowlen):
        rowvals = vals[i*rowlen:(i+1)*rowlen]
        print "%3s | " % thelist[i] + format % tuple(rowvals)
# code highlighted using py2html.py version 0.8