#!/usr/bin/env sage
"""
The MIT License
Copyright (c) 2008 Conrado Porto Lopes Gouvea
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from math import *
#Uncomment those for elliptic curve
#X = EllipticCurve(GF(503), [4,1])
#ADD = True
#Uncomment those for modular multiplication (the number must be prime)
#X = Zmod(509)
#ADD = False
#Uncomment those for multiplication on GF(5^2)
#G. = GF(5)[]
#X. = GF(5^2, modulus=x^2 + 4*x + 2)
#ADD = False
#Get the number of elements
try:
N = X.order()
except:
N = len(X)
#Sort and enumarate them. Of course, the table will completely change depending on how
#you sort them! Here we use Sage's default sorting, whatever it is
#(e.g. for elliptic curves is on the x,y,z coordinates tuples)
lst = list(enumerate(sorted(X)))
#Reverse dict, so we can find an element given its 'number'
rdic = dict((a,i) for i,a in lst)
f = open('data.txt','w')
#Write the number of points
print >> f, N
for (i,a) in lst:
for (j,b) in lst:
#Multiply or add the ith and jth elements
if ADD:
k = rdic[a + b]
else:
k = rdic[a * b]
#Write the "normalized" result (its index / total number of elements)
print >> f, k/float(N-1)
f.close()
print 'data saved'