
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
@thi.ng/distance
Advanced tools
N-dimensional distance metrics & K-nearest neighborhoods for point queries
[!IMPORTANT] ‼️ Announcing the thi.ng user survey 2024 📋
Please participate in the survey here!
(open until end of February)To achieve a better sample size, I'd highly appreciate if you could circulate the link to this survey in your own networks.
[!NOTE] This is one of 190 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.
🚀 Help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️
N-dimensional distance metrics & K-nearest neighborhoods for point queries.
The package provides the
IDistance
interface for custom distance metric implementations & conversions from/to raw
distance values. The following preset metrics are provided too:
Preset | Number | nD | 2D | 3D | Comments |
---|---|---|---|---|---|
EUCLEDIAN | ✅ | Eucledian distance | |||
EUCLEDIAN1 | ✅ | ||||
EUCLEDIAN2 | ✅ | ||||
EUCLEDIAN3 | ✅ | ||||
HAVERSINE_LATLON | ✅ | Great-circle distance for lat/lon geo locations | |||
HAVERSINE_LONLAT | ✅ | Great-circle distance for lon/lat geo locations | |||
DIST_SQ | ✅ | Squared dist (avoids Math.sqrt ) | |||
DIST_SQ1 | ✅ | ||||
DIST_SQ2 | ✅ | ||||
DIST_SQ3 | ✅ | ||||
defManhattan(n) | ✅ | Manhattan distance | |||
MANHATTAN2 | ✅ | ||||
MANHATTAN3 | ✅ |
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.
An INeighborhood
implementation for nearest neighbor queries around a given
target location, initial query radius and IDistance
metric to determine
proximity.
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.
An unbounded and unsorted version of KNearest
, selecting all
items around the target location and given search radius. Qualifying neighbors
will be accumulated in order of processing via an internal array.
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.
yarn add @thi.ng/distance
ES module import:
<script type="module" src="https://cdn.skypack.dev/@thi.ng/distance"></script>
For Node.js REPL:
const distance = await import("@thi.ng/distance");
Package sizes (brotli'd, pre-treeshake): ESM: 1.38 KB
One project in this repo's /examples directory is using this package:
Screenshot | Description | Live demo | Source |
---|---|---|---|
![]() | K-nearest neighbor search in an hash grid | Demo | Source |
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' ] ]
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
}
© 2021 - 2024 Karsten Schmidt // Apache License 2.0
FAQs
N-dimensional distance metrics & K-nearest neighborhoods for point queries
The npm package @thi.ng/distance receives a total of 566 weekly downloads. As such, @thi.ng/distance popularity was classified as not popular.
We found that @thi.ng/distance demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.