diff --git a/3nar.py b/3nar.py index effb63f..7209a43 100644 --- a/3nar.py +++ b/3nar.py @@ -21,6 +21,17 @@ class Nnnar: raise AttributeError("Error: wrong number of dimensions") self.space[*self.getSpaceIdxFromCoord(coord)].append(Point(coord, value)) + def getValueOfPoint(self, coord,nbNearest): + points = self.getNNearest(coord, nbNearest) + value = points[0].value + for idx in range(1,len(points)): + tvalue = points[idx].value + for i in range(len(tvalue)): + value[i] += tvalue[i] + for i in range(len(value)): + value[i] = value[i] / nbNearest + return value + def getNNearest(self, coord, nbNearest): idx = self.getSpaceIdxFromCoord(coord) nbAround = 0 @@ -29,15 +40,14 @@ class Nnnar: selected = [] subSpaceIdxList = getSpaceIdxAround(idx, nbAround, self) for subSpaceIdx in subSpaceIdxList: - print(subSpaceIdx) - print(self.space[subSpaceIdx]) - selected += selectPointsInRange(self.space[subSpaceIdx],coord,nbAround*self.unit) + selected += selectPointsInRange(self.space[tuple(subSpaceIdx)],coord,nbAround*self.unit,self.space) nbAround += 1 found = [] dist = [] i=0 while len(found) < nbNearest: - found += selected[i] + found.append(selected[i]) + dist.append(selected[i].getDistFromCoord(coord)) i+=1 maxDistIdx = foundMaxIndex(dist) maxDist = max(dist) @@ -73,9 +83,9 @@ class Point: +n = Nnnar(3, 0, 3, 2) -n = Nnnar(2, 0, 3, 2) - -n.addPoint(np.array([1, 0.8]), 1) +n.addPoint(np.array([1, 0.8,1.5]), [0.2,0.5]) +n.addPoint(np.array([0.2, 1.8,1.2]), [0.4,0.7]) -print(n.getNNearest(np.array([1,0.8]),1)) \ No newline at end of file +print(n.getValueOfPoint(np.array([1, 1, 1]), 2)) \ No newline at end of file diff --git a/function.py b/function.py index 5babd25..e43f81a 100644 --- a/function.py +++ b/function.py @@ -8,8 +8,11 @@ def getSpaceIdxAround(center, nbAround, space): space.calculatedSpaceAroundIdx.append(spaceAround) return applySapceAroundToCase(center,spaceAround,space) -def selectPointsInRange(points, coord, distance): +def selectPointsInRange(points, coord, distance,space): found = [] + for point in points: + if point.getDistFromCoord(coord) <= distance: + found.append(point) return found def foundMaxIndex(dist): diff --git a/spaceAround.py b/spaceAround.py index 93dd1f7..09a6110 100644 --- a/spaceAround.py +++ b/spaceAround.py @@ -1,68 +1,70 @@ +import numpy as np from sympy.utilities.iterables import multiset_permutations def findCoordAround(center, nbAround): + nbDimention = len(center) find = [] - for i in range(len(center)): - find = getNCoordDiff(center,find,i,nbAround) + for i in range(1, nbDimention + 1): + find = getNCoordDifferente(center, find, i, nbAround) + # print(len(find),(nbAround*2+1)*(nbAround*2+1)) + # input() return find -def getNCoordDiff(center,find,nbDiffValue, nbAround): +def getNCoordDifferente(center, find, nbDifferanteValue, nbAround): coord = [] - if (-nbAround+nbAround >= nbDiffValue): - return find - for i in range(nbDiffValue): - coord.append(-nbAround+i) + for i in range(nbDifferanteValue): + if -nbAround + i > nbAround: + return find + coord.append(-nbAround + i) + find = getCompletVariationOfCoord(center, find, coord) + while True: modified = False - cursor = len(coord)-1 - while(not modified): - if (cursor<0): + cursor = len(coord) - 1 + while not modified: + if cursor < 0 or cursor == len(coord): return find - if (coord[cursor]+len(coord)-1-cursor