|
12 | 12 | f*: RbfFunc |
13 | 13 |
|
14 | 14 | RbfGrid*[T] = object |
15 | | - grid*: seq[seq[T]] |
| 15 | + indices*: seq[seq[int]] |
| 16 | + values*: Tensor[T] |
| 17 | + points*: Tensor[float] |
16 | 18 | gridSize*, gridDim*: int |
17 | 19 | gridDelta*: float |
18 | 20 |
|
|
25 | 27 | template km(point: Tensor[float], index: int, delta: float): int = |
26 | 28 | int(ceil(point[0, index] / delta)) |
27 | 29 |
|
| 30 | +iterator neighbours*[T](grid: RbfGrid[T], k: int): int = |
| 31 | + discard |
| 32 | + |
28 | 33 | proc findIndex*[T](grid: RbfGrid[T], point: Tensor[float]): int = |
29 | 34 | result = km(point, grid.gridDim - 1, grid.gridDelta) - 1 |
30 | 35 | for i in 0 ..< grid.gridDim - 1: |
31 | 36 | result += (km(point, i, grid.gridDelta) - 1) * grid.gridSize ^ (grid.gridDim - i - 1) |
32 | 37 |
|
33 | | -proc newRbfGrid*[T](points: Tensor[float], values: seq[T], gridSize: int = 0): RbfGrid[T] = |
| 38 | +proc newRbfGrid*[T](points: Tensor[float], values: Tensor[T], gridSize: int = 0): RbfGrid[T] = |
34 | 39 | let nPoints = points.shape[0] |
35 | 40 | let nDims = points.shape[1] |
36 | 41 | let gridSize = |
37 | 42 | if gridSize > 0: |
38 | 43 | gridSize |
39 | 44 | else: |
40 | | - int(round(pow(nPoints.float, 1 / nDims) / 2)) |
| 45 | + max(int(round(pow(nPoints.float, 1 / nDims) / 2)), 1) |
41 | 46 | let delta = 1 / gridSize |
42 | | - result = RbfGrid[T](gridSize: gridSize, gridDim: nDims, gridDelta: delta, grid: newSeq[seq[T]](gridSize ^ nDims)) |
| 47 | + result = RbfGrid[T](gridSize: gridSize, gridDim: nDims, gridDelta: delta, indices: newSeq[seq[int]](gridSize ^ nDims)) |
43 | 48 | for row in 0 ..< nPoints: |
44 | 49 | let index = result.findIndex(points[row, _]) |
45 | | - result.grid[index].add values[row] |
| 50 | + result.indices[index].add row |
| 51 | + result.values = values |
| 52 | + result.points = points |
46 | 53 |
|
47 | 54 |
|
48 | 55 | # Idea: blocked distance matrix for better cache friendliness |
@@ -106,6 +113,6 @@ when isMainModule: |
106 | 113 | let rbfPu = newRbfPu(x1, values, 3) |
107 | 114 |
|
108 | 115 | echo "----------------" |
109 | | - let xGrid = [[0.1, 0.1], [0.9, 0.9], [0.4, 0.4]].toTensor |
110 | | - let valuesGrid = @[0, 9, 5] |
| 116 | + let xGrid = [[0.1, 0.1], [0.2, 0.3], [0.9, 0.9], [0.4, 0.4]].toTensor |
| 117 | + let valuesGrid = @[0, 1, 9, 5].toTensor.reshape(4, 1) |
111 | 118 | echo newRbfGrid(xGrid, valuesGrid, 3) |
0 commit comments