@thi.ng/distance
Advanced tools
Comparing version 1.0.7 to 2.0.0
@@ -1,4 +0,4 @@ | ||
import type { Fn } from "@thi.ng/api"; | ||
import type { Fn, NumericArray } from "@thi.ng/api"; | ||
import type { ReadonlyVec } from "@thi.ng/vectors"; | ||
import type { IDistance } from "./api"; | ||
import type { IDistance, Metric } from "./api"; | ||
/** | ||
@@ -18,3 +18,4 @@ * Takes a vector `p`, array of `samples` and a `dist`ance function. Computes | ||
*/ | ||
export declare const argmin: (p: ReadonlyVec, samples: ReadonlyVec[], { metric: dist }?: IDistance<ReadonlyVec>) => number; | ||
export declare const argmin: (p: ReadonlyVec, samples: ReadonlyVec[], dist?: Metric<ReadonlyVec> | IDistance<ReadonlyVec>) => number; | ||
export declare const argminN: (p: number, samples: NumericArray, dist?: Metric<number> | IDistance<number>) => number; | ||
/** | ||
@@ -34,3 +35,3 @@ * Similar to {@link argmin}, but for non-vector inputs and taking an additional | ||
*/ | ||
export declare const argminT: <T>(p: T, samples: T[], key: Fn<T, ReadonlyVec>, dist?: IDistance<ReadonlyVec> | undefined) => number; | ||
export declare const argminT: <T>(p: T, samples: T[], key: Fn<T, ReadonlyVec>, dist?: Metric<ReadonlyVec> | IDistance<ReadonlyVec> | undefined) => number; | ||
/** | ||
@@ -37,0 +38,0 @@ * Similar to {@link argmin}, but for k-nearest queries. |
@@ -0,3 +1,4 @@ | ||
import { isFunction } from "@thi.ng/checks/is-function"; | ||
import { knearest } from "./knearest"; | ||
import { DIST_SQ } from "./squared"; | ||
import { DIST_SQ, DIST_SQ1 } from "./squared"; | ||
/** | ||
@@ -17,7 +18,8 @@ * Takes a vector `p`, array of `samples` and a `dist`ance function. Computes | ||
*/ | ||
export const argmin = (p, samples, { metric: dist } = DIST_SQ) => { | ||
export const argmin = (p, samples, dist = DIST_SQ) => { | ||
const distFn = isFunction(dist) ? dist : dist.metric; | ||
let minD = Infinity; | ||
let minArg = -1; | ||
for (let i = 0, n = samples.length; i < n; i++) { | ||
const d = dist(p, samples[i]); | ||
const d = distFn(p, samples[i]); | ||
if (d < minD) { | ||
@@ -30,2 +32,15 @@ minD = d; | ||
}; | ||
export const argminN = (p, samples, dist = DIST_SQ1) => { | ||
const distFn = isFunction(dist) ? dist : dist.metric; | ||
let minD = Infinity; | ||
let minArg = -1; | ||
for (let i = 0, n = samples.length; i < n; i++) { | ||
const d = distFn(p, samples[i]); | ||
if (d < minD) { | ||
minD = d; | ||
minArg = i; | ||
} | ||
} | ||
return minArg; | ||
}; | ||
/** | ||
@@ -32,0 +47,0 @@ * Similar to {@link argmin}, but for non-vector inputs and taking an additional |
@@ -6,52 +6,66 @@ # Change Log | ||
## [1.0.7](https://github.com/thi-ng/umbrella/compare/@thi.ng/distance@1.0.6...@thi.ng/distance@1.0.7) (2021-09-03) | ||
# [2.0.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/distance@1.0.7...@thi.ng/distance@2.0.0) (2021-10-12) | ||
**Note:** Version bump only for package @thi.ng/distance | ||
### Build System | ||
* major update of ALL pkgs (export maps, ESM only) ([0d1d6ea](https://github.com/thi-ng/umbrella/commit/0d1d6ea9fab2a645d6c5f2bf2591459b939c09b6)) | ||
# [0.3.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/distance@0.2.2...@thi.ng/distance@0.3.0) (2021-04-19) | ||
### Features | ||
* **distance:** add argmin*() fns ([72ed376](https://github.com/thi-ng/umbrella/commit/72ed3760c7a6982bcab7d94666957cad90f4f0ef)) | ||
* **distance:** replace HAVERSINE w/ alts ([3a9a77a](https://github.com/thi-ng/umbrella/commit/3a9a77ab0fd06484f2fda5d67c7b151645436a32)) | ||
* **distance:** add/update argmin fns/params ([9c0f003](https://github.com/thi-ng/umbrella/commit/9c0f0032d8fbce3634971a36497ef22a7343edbb)) | ||
### BREAKING CHANGES | ||
* discontinue CommonJS & UMD versions | ||
- only ESM modules will be published from now on | ||
- CJS obsolete due to ESM support in recent versions of node: | ||
- i.e. launch NodeJS via: | ||
- `node --experimental-specifier-resolution=node --experimental-repl-await` | ||
- in the node REPL use `await import(...)` instead of `require()` | ||
- UMD obsolete due to widespread browser support for ESM | ||
# [0.2.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/distance@0.1.11...@thi.ng/distance@0.2.0) (2021-03-30) | ||
Also: | ||
- normalize/restructure/reorg all package.json files | ||
- cleanup all build scripts, remove obsolete | ||
- switch from mocha to @thi.ng/testament for all tests | ||
### Features | ||
* **distance:** add HAVERSINE preset, update readme ([cfc771e](https://github.com/thi-ng/umbrella/commit/cfc771eb21cf2574eaa2476eaee7920674cae9c3)) | ||
## [1.0.7](https://github.com/thi-ng/umbrella/compare/@thi.ng/distance@1.0.6...@thi.ng/distance@1.0.7) (2021-09-03) | ||
**Note:** Version bump only for package @thi.ng/distance | ||
## [0.1.9](https://github.com/thi-ng/umbrella/compare/@thi.ng/distance@0.1.8...@thi.ng/distance@0.1.9) (2021-03-17) | ||
# [0.3.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/distance@0.2.2...@thi.ng/distance@0.3.0) (2021-04-19) | ||
### Features | ||
### Bug Fixes | ||
- **distance:** add argmin*() fns ([72ed376](https://github.com/thi-ng/umbrella/commit/72ed3760c7a6982bcab7d94666957cad90f4f0ef)) | ||
- **distance:** replace HAVERSINE w/ alts ([3a9a77a](https://github.com/thi-ng/umbrella/commit/3a9a77ab0fd06484f2fda5d67c7b151645436a32)) | ||
* **distance:** update KNearest ctor & heap handling ([#283](https://github.com/thi-ng/umbrella/issues/283)) ([e7cd6f1](https://github.com/thi-ng/umbrella/commit/e7cd6f134bb05d5d5e37e7e7ba241f984d94d98c)) | ||
# [0.2.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/distance@0.1.11...@thi.ng/distance@0.2.0) (2021-03-30) | ||
### Features | ||
- **distance:** add HAVERSINE preset, update readme ([cfc771e](https://github.com/thi-ng/umbrella/commit/cfc771eb21cf2574eaa2476eaee7920674cae9c3)) | ||
## [0.1.9](https://github.com/thi-ng/umbrella/compare/@thi.ng/distance@0.1.8...@thi.ng/distance@0.1.9) (2021-03-17) | ||
### Bug Fixes | ||
# 0.1.0 (2021-01-21) | ||
- **distance:** update KNearest ctor & heap handling ([#283](https://github.com/thi-ng/umbrella/issues/283)) ([e7cd6f1](https://github.com/thi-ng/umbrella/commit/e7cd6f134bb05d5d5e37e7e7ba241f984d94d98c)) | ||
# 0.1.0 (2021-01-21) | ||
### Features | ||
### Features | ||
* **distance:** add Manhattan metric, rename types, add docs ([4f0b199](https://github.com/thi-ng/umbrella/commit/4f0b1992ccd3ee76fce7d9c7a5433adb80b029a2)) | ||
* **distance:** add new package ([1b41aa4](https://github.com/thi-ng/umbrella/commit/1b41aa46a8e2228f69df400195a08d05d2a9f235)) | ||
* **distance:** clamp search radius, minor other changes ([4a09a0f](https://github.com/thi-ng/umbrella/commit/4a09a0f6e7ab8f2276daca58758f86b68a050caf)) | ||
* **distance:** update INeighborhood, KNearest ([be3e43d](https://github.com/thi-ng/umbrella/commit/be3e43dcaf6a25f6de0f6ffb9f241d2f09362ecb)) | ||
- **distance:** add Manhattan metric, rename types, add docs ([4f0b199](https://github.com/thi-ng/umbrella/commit/4f0b1992ccd3ee76fce7d9c7a5433adb80b029a2)) | ||
- **distance:** add new package ([1b41aa4](https://github.com/thi-ng/umbrella/commit/1b41aa46a8e2228f69df400195a08d05d2a9f235)) | ||
- **distance:** clamp search radius, minor other changes ([4a09a0f](https://github.com/thi-ng/umbrella/commit/4a09a0f6e7ab8f2276daca58758f86b68a050caf)) | ||
- **distance:** update INeighborhood, KNearest ([be3e43d](https://github.com/thi-ng/umbrella/commit/be3e43dcaf6a25f6de0f6ffb9f241d2f09362ecb)) |
@@ -1,2 +0,2 @@ | ||
import { ReadonlyVec } from "@thi.ng/vectors"; | ||
import type { ReadonlyVec } from "@thi.ng/vectors"; | ||
import type { IDistance, Metric } from "./api"; | ||
@@ -3,0 +3,0 @@ export declare class Eucledian<T> implements IDistance<T> { |
@@ -1,3 +0,4 @@ | ||
import { dist, dist2, dist3 } from "@thi.ng/vectors"; | ||
import { dist, dist2, dist3 } from "@thi.ng/vectors/dist"; | ||
export class Eucledian { | ||
metric; | ||
constructor(metric) { | ||
@@ -4,0 +5,0 @@ this.metric = metric; |
@@ -1,2 +0,2 @@ | ||
import { ReadonlyVec } from "@thi.ng/vectors"; | ||
import type { ReadonlyVec } from "@thi.ng/vectors"; | ||
import { Eucledian } from "./eucledian"; | ||
@@ -3,0 +3,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
import { distHaversineLatLon, distHaversineLonLat, } from "@thi.ng/vectors"; | ||
import { distHaversineLatLon, distHaversineLonLat, } from "@thi.ng/vectors/dist-haversine"; | ||
import { Eucledian } from "./eucledian"; | ||
@@ -3,0 +3,0 @@ /** |
@@ -1,3 +0,3 @@ | ||
import { IDeref } from "@thi.ng/api"; | ||
import { Heap } from "@thi.ng/heaps"; | ||
import type { IDeref } from "@thi.ng/api"; | ||
import { Heap } from "@thi.ng/heaps/heap"; | ||
import type { ReadonlyVec } from "@thi.ng/vectors"; | ||
@@ -4,0 +4,0 @@ import type { IDistance, INeighborhood, Neighbor } from "./api"; |
@@ -1,4 +0,4 @@ | ||
import { assert } from "@thi.ng/api"; | ||
import { Heap } from "@thi.ng/heaps"; | ||
import { clamp0 } from "@thi.ng/math"; | ||
import { assert } from "@thi.ng/errors/assert"; | ||
import { Heap } from "@thi.ng/heaps/heap"; | ||
import { clamp0 } from "@thi.ng/math/interval"; | ||
import { DIST_SQ, DIST_SQ1, DIST_SQ2, DIST_SQ3 } from "./squared"; | ||
@@ -21,2 +21,11 @@ /** | ||
export class KNearest { | ||
dist; | ||
target; | ||
k; | ||
sorted; | ||
radius; | ||
_currR; | ||
_heap = new Heap(null, { | ||
compare: (a, b) => b[0] - a[0], | ||
}); | ||
constructor(dist, target, k, radius = Infinity, sorted = false) { | ||
@@ -27,5 +36,2 @@ this.dist = dist; | ||
this.sorted = sorted; | ||
this._heap = new Heap(null, { | ||
compare: (a, b) => b[0] - a[0], | ||
}); | ||
assert(k > 0, `invalid k (must be > 0)`); | ||
@@ -32,0 +38,0 @@ this.radius = clamp0(radius); |
@@ -1,2 +0,2 @@ | ||
import { ReadonlyVec } from "@thi.ng/vectors"; | ||
import type { ReadonlyVec } from "@thi.ng/vectors"; | ||
import type { IDistance, Metric } from "./api"; | ||
@@ -3,0 +3,0 @@ /** |
@@ -1,2 +0,2 @@ | ||
import { distManhattan, distManhattan2, distManhattan3, } from "@thi.ng/vectors"; | ||
import { distManhattan, distManhattan2, distManhattan3, } from "@thi.ng/vectors/dist-manhattan"; | ||
/** | ||
@@ -45,2 +45,5 @@ * Manhattan distance metric and conversion to/from Eucledian distances. | ||
export class Manhattan { | ||
dim; | ||
metric; | ||
_invD; | ||
constructor(dim, metric) { | ||
@@ -47,0 +50,0 @@ this.dim = dim; |
@@ -1,2 +0,2 @@ | ||
import { clamp0 } from "@thi.ng/math"; | ||
import { clamp0 } from "@thi.ng/math/interval"; | ||
import { DIST_SQ, DIST_SQ1, DIST_SQ2, DIST_SQ3 } from "./squared"; | ||
@@ -12,2 +12,7 @@ /** | ||
export class Nearest { | ||
dist; | ||
target; | ||
radius; | ||
_currR; | ||
value; | ||
constructor(dist, target, radius = Infinity) { | ||
@@ -14,0 +19,0 @@ this.dist = dist; |
{ | ||
"name": "@thi.ng/distance", | ||
"version": "1.0.7", | ||
"version": "2.0.0", | ||
"description": "N-dimensional distance metrics & K-nearest neighborhoods for point queries", | ||
"type": "module", | ||
"module": "./index.js", | ||
"main": "./lib/index.js", | ||
"umd:main": "./lib/index.umd.js", | ||
"typings": "./index.d.ts", | ||
"sideEffects": false, | ||
"repository": { | ||
@@ -27,26 +27,22 @@ "type": "git", | ||
"scripts": { | ||
"build": "yarn clean && yarn build:es6 && node ../../scripts/bundle-module", | ||
"build:release": "yarn clean && yarn build:es6 && node ../../scripts/bundle-module all", | ||
"build:es6": "tsc --declaration", | ||
"build:test": "rimraf build && tsc -p test/tsconfig.json", | ||
"build:check": "tsc --isolatedModules --noEmit", | ||
"test": "mocha test", | ||
"cover": "nyc mocha test && nyc report --reporter=lcov", | ||
"clean": "rimraf *.js *.d.ts *.map .nyc_output build coverage doc lib", | ||
"doc:readme": "ts-node -P ../../tools/tsconfig.json ../../tools/src/readme.ts", | ||
"build": "yarn clean && tsc --declaration", | ||
"clean": "rimraf *.js *.d.ts *.map doc", | ||
"doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts", | ||
"doc:ae": "mkdir -p .ae/doc .ae/temp && node_modules/.bin/api-extractor run --local --verbose", | ||
"doc": "typedoc --excludePrivate --out doc --theme ../../tools/doc/typedoc-theme src/index.ts", | ||
"pub": "yarn build:release && yarn publish --access public" | ||
"doc:readme": "yarn doc:stats && ../../scripts/node-esm ../../tools/src/readme.ts", | ||
"doc:stats": "../../scripts/node-esm ../../tools/src/module-stats.ts", | ||
"pub": "yarn build && yarn publish --access public", | ||
"test": "testament test" | ||
}, | ||
"dependencies": { | ||
"@thi.ng/api": "^7.2.0", | ||
"@thi.ng/heaps": "^1.3.1", | ||
"@thi.ng/math": "^4.0.6", | ||
"@thi.ng/vectors": "^6.2.0" | ||
"@thi.ng/api": "^8.0.0", | ||
"@thi.ng/checks": "^3.0.0", | ||
"@thi.ng/errors": "^2.0.0", | ||
"@thi.ng/heaps": "^2.0.0", | ||
"@thi.ng/math": "^5.0.0", | ||
"@thi.ng/vectors": "^7.0.0" | ||
}, | ||
"files": [ | ||
"*.js", | ||
"*.d.ts", | ||
"lib" | ||
], | ||
"devDependencies": { | ||
"@thi.ng/testament": "^0.1.0" | ||
}, | ||
"keywords": [ | ||
@@ -68,3 +64,35 @@ "distance", | ||
}, | ||
"sideEffects": false, | ||
"files": [ | ||
"*.js", | ||
"*.d.ts" | ||
], | ||
"exports": { | ||
".": { | ||
"import": "./index.js" | ||
}, | ||
"./api": { | ||
"import": "./api.js" | ||
}, | ||
"./argmin": { | ||
"import": "./argmin.js" | ||
}, | ||
"./eucledian": { | ||
"import": "./eucledian.js" | ||
}, | ||
"./haversine": { | ||
"import": "./haversine.js" | ||
}, | ||
"./knearest": { | ||
"import": "./knearest.js" | ||
}, | ||
"./manhattan": { | ||
"import": "./manhattan.js" | ||
}, | ||
"./nearest": { | ||
"import": "./nearest.js" | ||
}, | ||
"./squared": { | ||
"import": "./squared.js" | ||
} | ||
}, | ||
"thi.ng": { | ||
@@ -78,3 +106,3 @@ "related": [ | ||
}, | ||
"gitHead": "d971cb3f9b215a95483f78f1a8614015e331146f" | ||
"gitHead": "9ac1344b38b565eb894306fbf72233b6c0b2d115" | ||
} |
@@ -57,3 +57,3 @@ <!-- This file is generated - DO NOT EDIT! --> | ||
also use one of the given distance metrics and implement the widely used | ||
[`IDeref`](https://github.com/thi-ng/umbrella/tree/develop/packages/api/src/api/deref.ts) | ||
[`IDeref`](https://github.com/thi-ng/umbrella/tree/develop/packages/api/src/deref.ts) | ||
interface to obtain the final query results. | ||
@@ -106,11 +106,19 @@ | ||
ES module import: | ||
```html | ||
// ES module | ||
<script type="module" src="https://unpkg.com/@thi.ng/distance?module" crossorigin></script> | ||
<script type="module" src="https://cdn.skypack.dev/@thi.ng/distance"></script> | ||
``` | ||
// UMD | ||
<script src="https://unpkg.com/@thi.ng/distance/lib/index.umd.js" crossorigin></script> | ||
[Skypack documentation](https://docs.skypack.dev/) | ||
For NodeJS (v14.6+): | ||
```text | ||
node --experimental-specifier-resolution=node --experimental-repl-await | ||
> const distance = await import("@thi.ng/distance"); | ||
``` | ||
Package sizes (gzipped, pre-treeshake): ESM: 1.06 KB / CJS: 1.17 KB / UMD: 1.21 KB | ||
Package sizes (gzipped, pre-treeshake): ESM: 1.16 KB | ||
@@ -120,2 +128,4 @@ ## Dependencies | ||
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api) | ||
- [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks) | ||
- [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors) | ||
- [@thi.ng/heaps](https://github.com/thi-ng/umbrella/tree/develop/packages/heaps) | ||
@@ -122,0 +132,0 @@ - [@thi.ng/math](https://github.com/thi-ng/umbrella/tree/develop/packages/math) |
@@ -1,2 +0,2 @@ | ||
import { ReadonlyVec } from "@thi.ng/vectors"; | ||
import type { ReadonlyVec } from "@thi.ng/vectors"; | ||
import type { IDistance, Metric } from "./api"; | ||
@@ -3,0 +3,0 @@ export declare class Squared<T> implements IDistance<T> { |
@@ -1,3 +0,4 @@ | ||
import { distSq, distSq2, distSq3 } from "@thi.ng/vectors"; | ||
import { distSq, distSq2, distSq3 } from "@thi.ng/vectors/distsq"; | ||
export class Squared { | ||
metric; | ||
constructor(metric) { | ||
@@ -4,0 +5,0 @@ this.metric = metric; |
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
0
182
Yes
51078
6
1
22
817
+ Added@thi.ng/checks@^3.0.0
+ Added@thi.ng/errors@^2.0.0
+ Added@thi.ng/api@8.11.21(transitive)
+ Added@thi.ng/arrays@2.10.18(transitive)
+ Added@thi.ng/binary@3.4.44(transitive)
+ Added@thi.ng/checks@3.7.1(transitive)
+ Added@thi.ng/compare@2.4.13(transitive)
+ Added@thi.ng/compose@3.0.24(transitive)
+ Added@thi.ng/equiv@2.1.77(transitive)
+ Added@thi.ng/errors@2.5.27(transitive)
+ Added@thi.ng/heaps@2.1.93(transitive)
+ Added@thi.ng/hex@2.3.65(transitive)
+ Added@thi.ng/math@5.11.21(transitive)
+ Added@thi.ng/memoize@4.0.11(transitive)
+ Added@thi.ng/random@4.1.12(transitive)
+ Added@thi.ng/strings@3.9.6(transitive)
+ Added@thi.ng/timestamp@1.1.6(transitive)
+ Added@thi.ng/transducers@9.2.21(transitive)
+ Added@thi.ng/vectors@7.12.23(transitive)
- Removed@thi.ng/api@7.2.0(transitive)
- Removed@thi.ng/arrays@1.0.3(transitive)
- Removed@thi.ng/binary@2.2.11(transitive)
- Removed@thi.ng/checks@2.9.11(transitive)
- Removed@thi.ng/compare@1.3.34(transitive)
- Removed@thi.ng/compose@1.4.36(transitive)
- Removed@thi.ng/equiv@1.0.45(transitive)
- Removed@thi.ng/errors@1.3.4(transitive)
- Removed@thi.ng/heaps@1.3.1(transitive)
- Removed@thi.ng/hex@1.0.4(transitive)
- Removed@thi.ng/math@4.0.6(transitive)
- Removed@thi.ng/memoize@2.1.21(transitive)
- Removed@thi.ng/random@2.4.8(transitive)
- Removed@thi.ng/transducers@7.9.2(transitive)
- Removed@thi.ng/vectors@6.2.0(transitive)
Updated@thi.ng/api@^8.0.0
Updated@thi.ng/heaps@^2.0.0
Updated@thi.ng/math@^5.0.0
Updated@thi.ng/vectors@^7.0.0