class Costas: def __init__(self): pass def BuildWelch(self, p, root): costas = [0] * (p-1) for i in range(p-1): costas[i] = pow(root, i+1) % p return costas def isCostas(self, array): a_size = len(array) for c in range(1, a_size): hash = [0] * (2 * a_size) for i in range(a_size - c): dif = array[i+c] - array[i] #print dif, if hash[dif] == 1: return 0 else: hash[dif] = 1 #print return 1 def CostasExtendedCrossCorrelation(self, costa1, costa2, n): cross = 0 for h in range(n): hash = [0] * (n - 1) for i in range(n): if costa1[(i+h) % n] == "*" or costa2[i] == "*": continue dif = (costa1[(i + h) % n] - costa2[i]) % (n- 1) hash[dif]+=1 if cross < hash[dif]: cross = hash[dif] return cross def isExtendedCostas(self, costas, n): for h in range(n): if h == 0: continue hash = [0] * (n-1) for i in range(n): if costas[(i + h) % n] == "*" or costas[i] == "*": continue dif = (costas[(i+h) % n] - costas[i]) % (n-1) if hash[dif] == 1: return 0 else: hash[dif] = 1 return 1 def CostasColumnSymetries(self, a, p): costaList = [] for i in range(2, p): costas = [0] * p for x in range(p): costas[(x*i) % p] = a[x] costaList.append(costas) return costaList def is3DCostas(self, costas, n): for h1 in range(n): for h2 in range(n): if h1 == 0 and h2 == 0: continue hash = [0] * ((n*n) - 1) for i in range(n): for j in range(n): #if i == 0 and j == 0: # continue #if (i+h1) % n == 0 and (j+h2) % n == 0: # Fix for * anyware on the grid. if costas[(i+h1) % n][(j+h2) % n] == "*" or costas[i][j] == "*": continue dif = (costas[((i + h1) % n)][((j+h2) % n)] - costas[i][j]) % (pow(n,2) - 1) #print dif, if hash[dif] == 1: return 0 else: hash[dif] =1 #print return 1 def Costas3DCrossCorrelation(self, costa1, costa2, n): cross = 0 for h1 in range(n): for h2 in range(n): #if h1 == 0 and h2 == 0: # continue hash = [0] * ((n*n) - 1) for i in range(n): for j in range(n): #if i == 0 and j == 0: # continue #if (i+h1) % n == 0 and (j+h2) % n == 0: # Fix for * anyware on the grid. if costa1[(i+h1) % n][(j+h2) % n] == "*" or costa2[i][j] == "*": continue dif = (costa1[((i + h1) % n)][((j+h2) % n)] - costa2[i][j]) % (pow(n,2) - 1) #print dif, hash[dif]+=1 if cross < hash[dif]: cross = hash[dif] #print #print hash return cross def is3DCostas1D(self, costas, n): for h1 in range(n): for h2 in range(n): if h1 == 0 and h2 == 0: continue hash = [0] * ((n*n) - 1) for i in range(n): for j in range(n): if i == 0 and j == 0: continue if (i+h1) % n == 0 and (j+h2) % n == 0: continue dif = (costas[ n *((i + h1) % n) + ((j+h2) % n)] - costas[n*i+j]) % (pow(n,2) - 1) #print dif, if hash[dif] == 1: return 0 else: hash[dif] =1 #print return 1 def Costas3DRowSymetries(self, a, p): # Given a Welch 3dCostas return all its column symetries costaList = [] for i in range(2,p): costas2d = [] for j in range(p): costas2d.append([0] * p) for x in range(p): for y in range(p): if x == 0 and y == 0: costas2d[x][y] = "*" else: costas2d[(x*i) % p][y] = a[x][y] costaList.append(costas2d) return costaList def Costas3DRowShiftPermutation(self, a, p): # Given a Welch 3dCostas return all its row power permutations costaList = [] for i in range(1,p): costas2d = [] costas2d.append(a[0][:]) for j in range(1, p): costas2d.append(a[j][-(((i*j)%p)) :] + a[j][:-((i*j)%p)]) costaList.append(costas2d) return costaList def Costas3DColumnSymetries(self, a, p): costaList = [] for i in range(2, p): costas2d = [] for j in range(p): costas2d.append([0] * p) for y in range(p): for x in range(p): if x == 0 and y == 0: costas2d[x][y] = "*" else: costas2d[x][(y*i) % p] = a[x][y] costaList.append(costas2d) return costaList def Costas3DColumnShiftPermutation(self, a, p): costaList = [] for i in range(1, p): costas2d = [] for j in range(p): costas2d.append([0] * p) for y in range(p): for x in range(p): if x == 0 and y == 0: costas2d[x][y] = "*" else: costas2d[(x+(y*i)) % p][y] = a[x][y] costaList.append(costas2d) return costaList def CostasAdditionSymetries(self, a, p): #Given a Welch 3dCostas return all its Addition symetries excluding a costaList = [] for i in range(1, p): costas2d = [] for j in range(p): costas2d.append([0] * p) for x in range(p): for y in range(p): if x == 0 and y == 0: costas2d[x][y] = "*" else: costas2d[x][y] = (a[x][y] + i) % (pow(p,2)-1) costaList.append(costas2d) return costaList def CostasMultiplicationSymetries(self, a, p1, primes): costaList = [] for p in primes: costas2d = [] for j in range(p1): costas2d.append([0] * p1) for x in range(p1): for y in range(p1): if x == 0 and y == 0: costas2d[0][0] = "*" else: costas2d[x][y] = (a[x][y] * p) % (pow(p1,2)-1) costaList.append(costas2d) return costaList def Costas3D90DegreeSymetry(self, a, p): # Given a Welch 3DCostas return its 90Degree Symetry costas = [] * p for i in range(p): costas.append([0] * p) for i in range(p): for j in range(p): costas[i][j] = a[j][i] return costas def Costas3DDihedralSymetry(self, a, p): costas = [] * p for i in range(p): costas.append([0] * p) for i in range(p): costas[i] = a[i][:] costas[i].reverse() return costas def Costas2Legendre(self, a, p): for i in range(p): for j in range(p): if a[i][j] == "*": continue if(a[i][j] % 2 == 0): a[i][j]=1 else: a[i][j]=-1 def GetLegendre(self, a, p): # Given a Welch 3DCostas return its Legendre costas = [] * p for i in range(p): costas.append([0] * p) for i in range(p): for j in range(p): if a[i][j] == "*": costas[i][j] = "*" continue if(a[i][j] % 2 == 0): costas[i][j]=1 else: costas[i][j]=-1 return costas def IsCalabro(self, a, p): # Check if Legendre array is Calabro similar = (p-1)/2 counter = [0] * p for i in range(1, p-1): for j in range(i+1, p): if a[i] == a[j]: if counter[i] == 0: counter[i]+=2 else: counter[i]+=1 if counter[j] == 0: counter[j]+=2 else: counter[j]+=1 if similar in counter: return 1, counter else: return 0, counter def LegendreCorrelation(self, legendre, p): cor = 0 cor_pos = 0 cor_neg = 0 for h1 in range(p): for h2 in range(p): if h1 == 0 and h2 == 0: continue tmp_cor = 0 for i in range(p): for j in range(p): tmp_cor += legendre[(i + h1) % p][(j + h2) % p] * legendre[i][j] if tmp_cor < 0: if tmp_cor < cor_neg: cor_neg = tmp_cor else: if tmp_cor > cor_pos: cor_pos = tmp_cor if abs(tmp_cor) > cor: cor = abs(tmp_cor) return cor, cor_neg, cor_pos def LegendreCrossCorrelation(self, legendre1, legendre2, p): cor = 0 cor_pos = 0 cor_neg = 0 for h1 in range(p): for h2 in range(p): #if h1 == 0 and h2 == 0: # continue tmp_cor = 0 for i in range(p): for j in range(p): if legendre1[(i + h1) % p][(j + h2) % p] == "*" or legendre2[i][j] == "*": continue tmp_cor += legendre1[(i + h1) % p][(j + h2) % p] * legendre2[i][j] if tmp_cor < 0: if tmp_cor < cor_neg: cor_neg = tmp_cor else: if tmp_cor > cor_pos: cor_pos = tmp_cor if abs(tmp_cor) > cor: cor = abs(tmp_cor) return cor, cor_neg, cor_pos def LegendreCrossCorrelationCoincidences(self, legendre1, legendre2, p): cor = 0 for h1 in range(p): for h2 in range(p): #if h1 == 0 and h2 == 0: # continue tmp_cor = 0 for i in range(p): for j in range(p): if legendre1[(i + h1) % p][(j + h2) % p] == legendre2[i][j]: tmp_cor+=1 if tmp_cor > cor: cor = tmp_cor return cor #c = Costas() #print c.isCostas([4,3,2,1])