talisman
Version:
Straightforward fuzzy matching, information retrieval and NLP building blocks for JavaScript.
36 lines (32 loc) • 980 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = chebyshev;
/**
* Talisman metrics/chebyshev
* ===========================
*
* Function computing the Chebyshev distance.
*
* [Reference]: https://en.wikipedia.org/wiki/Chebyshev_distance
*
* [Tags]: metric, vector space.
*/
/**
* Function returning the Chebyshev distance between two vectors.
*
* @param {mixed} a - The first vector.
* @param {mixed} b - The second vector.
* @return {number} - The Chebyshev distance between a & b.
*
* @throws {Error} The function expects vectors of same dimension.
*/
function chebyshev(a, b) {
if (a.length !== b.length) throw Error('talisman/metrics/distance/chebyshev: the given vectors are not of the same dimension.');
var distance = 0;
for (var i = 0, l = a.length; i < l; i++) {
distance = Math.max(distance, Math.abs(a[i] - b[i]));
}return distance;
}
module.exports = exports['default'];