UNPKG

talisman

Version:

Straightforward fuzzy matching, information retrieval and NLP building blocks for JavaScript.

51 lines (46 loc) 1.4 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: false }); exports.squared = squared; exports.default = euclidean; /** * Talisman metrics/euclidean * =========================== * * Function computing the euclidean distance. * * [Reference]: https://en.wikipedia.org/wiki/Euclidean_distance * * [Tags]: metric, string metric. */ /** * Function returning the squared euclidean distance between two vectors. * * @param {mixed} a - The first vector. * @param {mixed} b - The second vector. * @return {number} - The squared euclidean distance between a & b. * * @throws {Error} The function expects vectors of same dimension. */ function squared(a, b) { if (a.length !== b.length) throw Error('talisman/metrics/distance/euclidean: the given vectors are not of the same dimension.'); var distance = 0; for (var i = 0, l = a.length; i < l; i++) { distance += Math.pow(a[i] - b[i], 2); }return distance; } /** * Function returning the euclidean distance between two vectors. * * @param {mixed} a - The first vector. * @param {mixed} b - The second vector. * @return {number} - The euclidean distance between a & b. * * @throws {Error} The function expects vector of same dimensions. */ function euclidean(a, b) { return Math.sqrt(squared(a, b)); } module.exports = exports['default']; exports['default'].squared = exports.squared;