Skip to content

compute-io/pdist

Repository files navigation

pdist

NPM version Build Status Coverage Status Dependencies

Computes pairwise distances between sequences

Installation

$ npm install compute-pdist

For use in the browser, use browserify.

Usage

var pdist = require( 'compute-pdist' );

pdist( X[, options] )

This function by default computes distances between pairs of elements of input array X. It returns an array holding the pairwise distances between the elements of X.

The function accepts the following options:

The returned array which holds all the pairwise distances is of length m(m–1)/2, where m is the number of elements in X. It holds the elements of the lower left triangle of the m-by-m distance matrix in column order. The returned array comes with a helper method to extract the (i, j) element of the corresponding distance matrix:

.get( i, j )

This method returns the distance between the ith and jth element of the orignal input X.

To obtain the full distance matrix, use the squareform function.

For object arrays, provide an accessor function for accessing numeric values.

var X = [
		[1,2],
		[2,4],
		[3,5]
	],
	[
		[1,1],
		[2,2],
		[3,7]
	]
];

function getValue( d, i ) {
	return d[ 1 ];
}

var dist = pdist( X, {
	'accessor': getValue
});
// returns 3

The accessor function is provided two arguments:

  • d: current datum.
  • i: current datum index.

Examples

var pdist = require( 'compute-pdist' );

var X = [
	[ 2, 4, 3, 1],
	[ 1, 2, 2, 1],
	[ 7, 3, 9, 7],
	[ 11, 9, 9, 8],
	[ 3, 2, 3, 1]
];

var d = pdist( X );
// returns [ 2.449, 9.899, .. ]

// distance between element 0 and 2:
d.get( 0, 2 );
// returns 9.899

// distance matrix:
var dMat = d.toMatrix();

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

Unit tests use the Mocha test framework with Chai assertions. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

License

MIT license.

Copyright

Copyright © 2014-2015. The Compute.io Authors.

About

Computes pairwise distances between arrays.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published