Getting started with kdtools

Timothy H. Keitt

2018-04-26

The kdtools package can be used to search for multidimensional points in a boxed region and find nearest neighbors in 1 to 9 dimensions. The package uses binary search on a sorted sequence of values. The current package is limited to matrices of real values. If you are interested in using string or mixed types in different dimensions, see the methods vignette.

Using kdtools is straightforward. There are four steps:

Step 1. Convert your matrix of values into a arrayvec object
library(kdtools)
x = matrix(runif(3e3), nc = 3)
y = matrix_to_tuples(x)
y[1:3, c(1, 3)]
#>           [,1]       [,2]
#> [1,] 0.2794384 0.61000754
#> [2,] 0.3759572 0.09410968
#> [3,] 0.5837779 0.38111576

The arrayvec object can be manipulated as if it were a matrix.

Step 2. Sort the data
kd_sort(y, inplace = TRUE, parallel = TRUE)
#>            [,1]       [,2]       [,3]
#> [1,] 0.08981227 0.02873967 0.09959894
#> [2,] 0.17672399 0.08573743 0.14157020
#> [3,] 0.14536850 0.03224999 0.17862416
#> [4,] 0.14159992 0.09464964 0.07982734
#> [5,] 0.11542256 0.15389484 0.07407142
#> (continues for 995 more rows)
Step 3. Search the data
rq = kd_range_query(y, c(0, 0, 0), c(1/4, 1/4, 1/4)); rq
#>            [,1]       [,2]       [,3]
#> [1,] 0.04299774 0.21699342 0.21896023
#> [2,] 0.08981227 0.02873967 0.09959894
#> [3,] 0.17672399 0.08573743 0.14157020
#> [4,] 0.14536850 0.03224999 0.17862416
#> [5,] 0.14159992 0.09464964 0.07982734
#> (continues for 6 more rows)
i = kd_nearest_neighbor(y, c(0, 0, 0)); y[i, ]
#> [1] 0.03224180 0.10670121 0.07489019
nns = kd_nearest_neighbors(y, c(0, 0, 0), 100); nns
#>           [,1]       [,2]       [,3]
#> [1,] 0.3504035 0.46159488 0.09824425
#> [2,] 0.3459476 0.37656850 0.28562291
#> [3,] 0.2827924 0.30600585 0.41061617
#> [4,] 0.1716057 0.02915501 0.55806432
#> [5,] 0.4994874 0.12215556 0.26379350
#> (continues for 95 more rows)

The kd_nearest_neighbor function returns the row-index of the nearest neighbor. The other functions return arrayvec objects.

Step 4. Convert back to a matrix for use in R
head(tuples_to_matrix(rq))
#>            [,1]       [,2]       [,3]
#> [1,] 0.04299774 0.21699342 0.21896023
#> [2,] 0.08981227 0.02873967 0.09959894
#> [3,] 0.17672399 0.08573743 0.14157020
#> [4,] 0.14536850 0.03224999 0.17862416
#> [5,] 0.14159992 0.09464964 0.07982734
#> [6,] 0.11542256 0.15389484 0.07407142
head(tuples_to_matrix(nns))
#>           [,1]       [,2]       [,3]
#> [1,] 0.3504035 0.46159488 0.09824425
#> [2,] 0.3459476 0.37656850 0.28562291
#> [3,] 0.2827924 0.30600585 0.41061617
#> [4,] 0.1716057 0.02915501 0.55806432
#> [5,] 0.4994874 0.12215556 0.26379350
#> [6,] 0.4759145 0.32373558 0.04378942

If you pass a matrix instead of an arrayvec object to any of the functions, it will be converted to an arrayvec object internally and results will be returned as matrices. This is slower and provided for convenience.