-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhyperdimNearestNeighbors.py
49 lines (38 loc) · 1.56 KB
/
hyperdimNearestNeighbors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy
import itertools
def nearestNeighborsCalculator(systDim, spinVar):
##### Euclidian space definitions #####
axisVal = numpy.arange((-(spinVar-1)/2), ((spinVar-1)/2+1), 1)
axisLoop = numpy.roll(axisVal, -int(spinVar/2))
if spinVar % 2 == 0:
shiftVal = axisLoop[0]
axisLoop -= shiftVal
axisVal -= shiftVal
axisVal = axisVal.astype(int)
axisLoop = axisLoop.astype(int)
##### Vectorial space definitions #####
indexCoord = list(enumerate(itertools.product(axisVal, repeat=systDim)))
coordIndex = {str(coord): i for i, coord in indexCoord}
##### Nearest neighbors calculator #####
nearestNeighbors = [0 for _ in range(spinVar**systDim)]
for i, coord in indexCoord:
tmp = []
for i1 in range(systDim):
for v in [-1, 1]:
shiftVec = numpy.zeros(systDim, dtype=int)
shiftVec[i1] = -1
nearestNeighbor = coord-shiftVec*v
for i2 in range(systDim):
nearestNeighbor[i2] = axisLoop[nearestNeighbor[i2]]
tmp.append(coordIndex[str(tuple(nearestNeighbor))])
nearestNeighbors[i] = tmp
##### Adiacency Matrix #####
adiacencyMatrix = numpy.zeros((spinVar**systDim, spinVar**systDim), dtype=int)
for i1 in range(spinVar**systDim):
for i2 in range(systDim*2):
adiacencyMatrix[i1, nearestNeighbors[i1][i2]] = 1
return adiacencyMatrix
if __name__ == '__main__':
systDim = 2
spinVar = 3
adiacencyMatrix = nearestNeighborsCalculator(systDim, spinVar)