2.4.72 • Published 3 days ago

@thi.ng/distance v2.4.72

Weekly downloads
16
License
Apache-2.0
Repository
github
Last release
3 days ago

distance

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

N-dimensional distance metrics & K-nearest neighborhoods for point queries.

Distance metrics

The package provides the IDistance interface for custom distance metric implementations & conversions from/to raw distance values. The following preset metrics are provided too:

PresetNumbernD2D3DComments
EUCLEDIANEucledian distance
EUCLEDIAN1
EUCLEDIAN2
EUCLEDIAN3
HAVERSINE_LATLONGreat-circle distance for lat/lon geo locations
HAVERSINE_LONLATGreat-circle distance for lon/lat geo locations
DIST_SQSquared dist (avoids Math.sqrt)
DIST_SQ1
DIST_SQ2
DIST_SQ3
defManhattan(n)Manhattan distance
MANHATTAN2
MANHATTAN3

Neighborhoods

Neighborhoods can be used to select n-D spatial items around a given target location and an optional catchment radius (infinite by default). Neighborhoods also use one of the given distance metrics and implement the widely used IDeref interface to obtain the final query results.

Custom neighborhood selections can be defined via the INeighborhood interface. Currently, there are two different implementations available, each providing several factory functions to instantiate and provide defaults for different dimensions. See documentation and examples below.

Nearest

An INeighborhood implementation for nearest neighbor queries around a given target location, initial query radius and IDistance metric to determine proximity.

KNearest

An INeighborhood implementation for K-nearest neighbor queries around a given target location, initial query radius and IDistance metric to determine proximity. The K-nearest neighbors will be accumulated via an internal heap and results can be optionally returned in order of proximity (via .deref() or .values()). For K=1 it will be more efficient to use Nearest to avoid the additional overhead.

Status

STABLE - used in production

Search or submit any issues for this package

Work is underway integrating this approach into the spatial indexing data structures provided by the @thi.ng/geom-accel package.

Related packages

  • @thi.ng/geom-accel - n-D spatial indexing data structures with a shared ES6 Map/Set-like API
  • @thi.ng/k-means - Configurable k-means & k-medians (with k-means++ initialization) for n-D vectors
  • @thi.ng/vectors - Optimized 2d/3d/4d and arbitrary length vector operations

Installation

yarn add @thi.ng/distance

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/distance"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const distance = await import("@thi.ng/distance");

Package sizes (gzipped, pre-treeshake): ESM: 1.13 KB

Dependencies

API

Generated API docs

import * as d from "@thi.ng/distance";

const items = { a: 5, b: 16, c: 9.5, d: 2, e: 12 };

// collect the 3 nearest numbers for target=10 and using
// infinite selection radius and squared distance metric (defaults)
const k = d.knearestN(10, 3);
// consider each item for inclusion
Object.entries(items).forEach(([id, x]) => k.consider(x, id));

// retrieve result tuples of [distance, value]
k.deref()
// [ [ 25, 'a' ], [ 4, 'e' ], [ 0.25, 'c' ] ]

// result values only
k.values()
// [ 'a', 'e', 'c' ]

// neighborhood around 10, K=3 w/ max radius 5
// also use Eucledian distance and sort results by proximity
const k2 = d.knearestN(10, 3, 5, d.EUCLEDIAN1, true);
Object.entries(items).forEach(([id, x]) => k2.consider(x, id));

k2.deref()
// [ [ 0.5, 'c' ], [ 2, 'e' ], [ 5, 'a' ] ]

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-distance,
  title = "@thi.ng/distance",
  author = "Karsten Schmidt",
  note = "https://thi.ng/distance",
  year = 2021
}

License

© 2021 Karsten Schmidt // Apache Software License 2.0

2.4.72

3 days ago

2.4.71

5 days ago

2.4.70

8 days ago

2.4.69

17 days ago

2.4.68

20 days ago

2.4.67

30 days ago

2.4.66

1 month ago

2.4.65

1 month ago

2.4.64

1 month ago

2.4.63

1 month ago

2.4.62

1 month ago

2.4.61

1 month ago

2.4.60

2 months ago

2.4.59

2 months ago

2.4.58

2 months ago

2.4.57

2 months ago

2.4.56

2 months ago

2.4.55

2 months ago

2.4.54

2 months ago

2.4.53

2 months ago

2.4.52

2 months ago

2.4.50

2 months ago

2.4.51

2 months ago

2.4.49

2 months ago

2.4.48

3 months ago

2.4.47

3 months ago

2.4.46

3 months ago

2.4.43

3 months ago

2.4.45

3 months ago

2.4.44

3 months ago

2.4.42

3 months ago

2.4.41

3 months ago

2.4.39

4 months ago

2.4.40

4 months ago

2.4.36

4 months ago

2.4.38

4 months ago

2.4.37

4 months ago

2.4.35

5 months ago

2.4.34

5 months ago

2.4.33

5 months ago

2.4.32

5 months ago

2.4.3

10 months ago

2.4.5

9 months ago

2.4.4

9 months ago

2.4.18

7 months ago

2.4.17

7 months ago

2.4.19

7 months ago

2.4.14

8 months ago

2.4.13

8 months ago

2.4.16

8 months ago

2.4.15

8 months ago

2.4.10

8 months ago

2.4.12

8 months ago

2.4.11

8 months ago

2.4.7

9 months ago

2.4.6

9 months ago

2.4.9

9 months ago

2.4.8

9 months ago

2.4.29

6 months ago

2.4.28

6 months ago

2.4.25

6 months ago

2.4.24

6 months ago

2.4.27

6 months ago

2.4.26

6 months ago

2.4.21

6 months ago

2.4.20

7 months ago

2.4.23

6 months ago

2.4.22

6 months ago

2.4.31

5 months ago

2.4.30

6 months ago

2.4.1

11 months ago

2.4.0

11 months ago

2.4.2

11 months ago

2.2.14

12 months ago

2.3.0

11 months ago

2.2.13

1 year ago

2.2.12

1 year ago

2.2.11

1 year ago

2.2.7

1 year ago

2.2.6

1 year ago

2.2.10

1 year ago

2.2.9

1 year ago

2.2.8

1 year ago

2.2.5

1 year ago

2.2.4

1 year ago

2.2.3

1 year ago

2.2.1

1 year ago

2.2.0

1 year ago

2.2.2

1 year ago

2.1.38

1 year ago

2.1.39

1 year ago

2.1.36

1 year ago

2.1.37

1 year ago

2.1.35

1 year ago

2.1.27

2 years ago

2.1.28

2 years ago

2.1.25

2 years ago

2.1.26

2 years ago

2.1.29

2 years ago

2.1.34

1 year ago

2.1.32

1 year ago

2.1.33

1 year ago

2.1.30

1 year ago

2.1.31

1 year ago

2.1.23

2 years ago

2.1.24

2 years ago

2.1.22

2 years ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.15

2 years ago

2.1.18

2 years ago

2.1.19

2 years ago

2.1.21

2 years ago

2.1.20

2 years ago

2.1.14

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

2.1.9

2 years ago

2.1.8

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.0.9

2 years ago

2.1.7

2 years ago

2.1.0

2 years ago

2.0.7

2 years ago

2.0.8

2 years ago

2.0.4

3 years ago

2.0.6

3 years ago

2.0.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.3

3 years ago

0.3.6

3 years ago

0.3.5

3 years ago

0.3.4

3 years ago

0.3.3

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.1.11

3 years ago

0.1.10

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago