UNPKG

talisman

Version:

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

40 lines (35 loc) 1.18 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = minkowski; /** * Talisman metrics/minkowski * =========================== * * Function computing the Minkowski distance. * * [Reference]: https://en.wikipedia.org/wiki/Minkowski_distance * * [Tags]: metric, vector space. */ /** * Function returning the Minkowski distance between two vectors. * * @param {number} p - The value for p. * @param {mixed} a - The first vector. * @param {mixed} b - The second vector. * @return {number} - The Minkowski distance between a & b. * * @throw {Error} The function expects a p value >= 1. * @throws {Error} The function expects vectors of same dimension. */ function minkowski(p, a, b) { if (p < 1) throw Error('talisman/metrics/distance/minkowski: the given p-value should be >= 1.'); if (a.length !== b.length) throw Error('talisman/metrics/distance/minkowski: the given vectors are not of the same dimension.'); var sum = 0; for (var i = 0, l = a.length; i < l; i++) { sum += Math.pow(Math.abs(a[i] - b[i]), p); }return Math.pow(sum, 1 / p); } module.exports = exports['default'];