Skip to content

compute-io/outer

Repository files navigation

outer

NPM version Build Status Coverage Status Dependencies

Computes the outer product.

The outer product between two vectors x and y is denoted as x ⊗ y and is equivalent to the matrix multiplication x y^t. For a vector x of length M and y of length N, it is equal to

Equation for the outer product.

Installation

$ npm install compute-outer

For use in the browser, use browserify.

Usage

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

outer( x, y[, opts] )

Computes the outer product between x and y. x and y have to either numbers, arrays or typed arrays. Passing a single number either as x or y will be treated as when passing a vector of length one. The function returns a matrix with M rows and N columns, where M is the length of x and N the length of y.

outer( [ 1, 2, 3], 2 )
/*
	[ 2
	  4
	  6 ]
*/
outer( 2, [ 1, 2, 3] )
/*
	[ 2 4 6 ]
*/

outer( [ 2, 4 ], [ 1, 3 ] )
/*
	[ 2  6
	  4 12 ]
*/

The function accepts the following two options:

  • accessor: accessor function for accessing array values.
  • dtype: output matrix data type. Default: float64.

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

var data = [
	['beep', 5],
	['boop', 3],
	['bip', 8],
	['bap', 3],
	['baz', 2]
];

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

var out = outer( data, 2, {
	'accessor': getValue
});
/*
	[ 10
	   6
	  16
	   6
	   4 ]
*/

When calculating the outer product between two object arrays, provide an accessor function which accepts 3 arguments.

var data = [
	['beep', 2],
	['boop', 4],
];

var arr = [
	{'x': 1},
	{'x': 3},
];

function getValue( d, i, j ) {
	if ( j === 0 ) {
		return d[ 1 ];
	}
	return d.x;
}

var out = outer( data, arr, {
	'accessor': getValue
});
/*
	[ 2  6
	  4 12 ]
*/

Note: j corresponds to the input array index, where j=0 is the index for the first input array and j=1 is the index for the second input array.

By default, the output data structure is float64 in order to preserve precision. To specify a different data type, set the dtype option (see matrix for a list of acceptable data types).

var data, out;

out = outer( [1,2,3], 2, {
	'dtype': 'int32'
});
/*
	[ 2
	  4
	  6 ]
*/

out.dtype === 'int32'
// returns true

Examples

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

var data,
	out,
	i;

// Plain arrays...
data = new Array( 10);
for ( i = 0; i < data.length; i++ ) {
	data[ i ] = i;
}
out = outer( data, data );

// Object arrays (accessors)...
function getValue( d ) {
	return d.x;
}
for ( i = 0; i < data.length; i++ ) {
	data[ i ] = {
		'x': data[ i ]
	};
}
out = outer( data, data, {
	'accessor': getValue
});

// Typed arrays...
data = new Int32Array( 10 );
for ( i = 0; i < data.length; i++ ) {
	data[ i ] = i;
}
out = outer( data, data );

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 © 2015. The Compute.io Authors.

About

Computes the outer product.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published