distance-calc

0.2.0 • Public • Published

distance-calc

A JS module for calculating several distances between two (n-dimensional) points

npm version build status

NPM

Installation

  npm install distance-calc --save

Usage

  var d = require('distance-calc');

p-norm

d.norm(vector1 [ [, vector2] || [, p=2] ])

Arguments

  1. required vector1 (Array)
  2. vector2 (Array): default is the null vector ( [0, 0, 0, ...] )
  3. p (number): the number of norm (p >= 1) (default 2)

Examples

  var vector1 = [2, 2, 2, 2];
  var vector2 = [3, 3, 3, 3];
  var point2D = [5, 5];

  d.norm(vector1); // 4 - Euclidean distance/length
  d.norm(vector1, 2); // 4 - Euclidean distance/length

  d.norm(vector1, 1); // 8 - Manhattan, Taxicab distance/length
  d.norm(vector1, Number.POSITIVE_INFINITY); // 2 -  Maximum, Uniform norm
  d.norm(vector1, 'inf'); // 2 -  Maximum, Uniform norm

  d.norm(vector1, vector2); // 4 - Euclidean distance
  d.norm(vector1, vector2, 2); // 4 - Euclidean distance
  d.norm(vector1, vector2, 'inf'); // 2 - Maximum, Uniform norm

  d.norm(point2D, vector1); // returns NaN if dimensions are different
  d.norm(vector1, 1/2); // returns NaN if p<1

hamming

d.hamming(param1, param2)

Arguments

  1. required param1 (Array | String)
  2. required param2 (Array | String)

Examples

  var array1 = [1, 2, 3, 4];
  var array2 = [1, 1, 1, 1];
  var string1 = 'abcd';
  var string2 = 'aaaa';

  d.hamming(array1, array1); // 0
  d.hamming(array1, array2); // 3

  d.hamming(string1, string1); // 0
  d.hamming(string1, string2); // 3

  d.hamming(array1, string1); // NaN - different types
  d.hamming(array1, [1,2]); // NaN - different size
  d.hamming(string1, 'ab'); // NaN - different size
  d.hamming(123, 123); // NaN - invalid type

Tests

  npm test

Package Sidebar

Install

npm i distance-calc

Weekly Downloads

3

Version

0.2.0

License

MIT

Last publish

Collaborators

  • pataiadam