Matrix
Matrices.
This module exports a Matrix
data structure for efficient storage and computation of numeric values. The data structure provides an interface for accessing and modifying one or more stored values. Matrices find common use in linear algebra, numerical analysis, image manipulation, machine learning, and data processing.
- Installation
- Usage
- Notes
- Examples
- Tests
- License
Installation
$ npm install dstructs-matrix
For use in the browser, use browserify.
Usage
var matrix = require( 'dstructs-matrix' );
matrix( [data,] shape[, dtype] )
Creates a new Matrix
having a specified shape
(dimensions => [rows,cols]
).
var mat = matrix( [3,2] );
By default, the matrix elements are floating-point 64-bit numbers (float64
). To specify a different data type, provide a dtype
.
var mat = matrix( [2,2], 'int8' );
The following dtypes
are accepted:
int8
uint8
uint8_clamped
int16
uint16
int32
uint32
float32
float64
If a linear numeric array
is not provided, the function initializes a zero-filled matrix. To initialize a matrix, provide a typed input data
array, whose length matches the specified shape
.
var data = new Int8Array( 6 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = i;
}
var mat = matrix( data, [2,3] );
To cast an input data
array to a different data type, provide a dtype
.
var mat = matrix( data, [2,2], 'uint32' );
If provided an Array
instead of a typed array and no dtype
is specified, the input data
array is cast to float64
.
var data = [ 10, 20, 30, 40, 50, 60 ];
var mat = matrix( data, [3,2] );
var dtype = mat.dtype;
Properties
A Matrix
has the following properties...
dtype
A read-only property returning the underlying storage data type.
var dtype = mat.dtype;
ndims
A read-only property returning the number of dimensions.
var ndims = mat.ndims;
shape
A read-only property returning the matrix shape
.
var shape = mat.shape;
offset
A property returning the offset
used to index into the underlying data store.
var offset = mat.offset;
By default, the offset
is 0
. While not read-only, most consumers should treat the offset
as a read-only property.
strides
A read-only property returning the strides
used to index into the underlying data store.
var strides = mat.strides;
While not frozen, most consumers should treat the strides
elements as read-only elements.
length
A read-only property returning the matrix length
; i.e., how many elements are in the Matrix
, similar to Array#length
.
var len = mat.length;
Note: while a Matrix
has a length
property, a Matrix
should not be considered array-like
, as array
indexing with not work as expected.
var data = new Float32Array( 10 );
var mat = matrix( data, [10,1] );
var value = mat.get( 3, 1 );
value = mat[ 3 ];
nbytes
A read-only property returning the number of bytes consumed by the Matrix
elements.
var nbytes = mat.nbytes;
data
A read-only property pointing to the underlying storage array.
var data = mat.data;
Methods
A Matrix
has the following methods...
Set Methods
These methods mutate a Matrix
:
Matrix.prototype.set( i, j, value )
Sets a Matrix
element located at a row and column index.
mat.set( 3, 1, 20 );
Set methods return the Matrix
instance and are thus chainable.
mat
.set( 3, 1, 21 )
.set( 3, 1, 22 )
.set( 3, 1, 23 )
.set( 3, 1, 24 )
.get( 3, 1 );
Note: out-of-bounds row and column indices will silently fail.
Matrix.prototype.iset( index, value )
Sets a Matrix
element located at a specified index
. If index < 0
, the index refers to a position relative to the Matrix
length, where index = -1
corresponds to the last element.
mat.iset( 7, 25 );
mat.iset( -3, 20 );
Note: out-of-bounds indices will silently fail.
Matrix.prototype.mset( idx[, cols], value[, thisArg] )
Sets multiple Matrix
elements. If provided a single array
, idx
is treated as an array
of linear indices. The value
argument may be either a number
primitive, a Matrix
containing values to set, or a callback function
.
var data = new Int8Array( 10*10 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = i;
}
var mat = matrix( data, [10,10] );
var submat = mat.mget( [0,2,4], [1,4,5] );
mat.mset( [1,4,5,21,24,25,41,44,45], 5 );
submat = mat.mget( [0,2,4], [1,4,5] );
var zeros = matrix( [1,3], 'int8' );
mat.mset( [2], [1,4,5], zeros );
submat = mat.mget( [0,2,4], [1,4,5] );
A callback is provided four arguments:
- d: current value
- i: row index
- j: column index
- idx: linear index
and is expected to return a number
primitive or a value which can be cast to a number
primitive.
function set( d, i, j, idx ) {
return '' + j + i;
}
mat.mset( [0], [1,4,5], set );
mat.mget( [0,2,4], [1,4,5] );
By default, the callback this
context is set to the Matrix
instance. To specify a different this
context, provide a thisArg
.
function set( d, i, j, idx ) {
console.log( this );
return '' + j + i;
}
mat.mset( [0], [1,4,5], set, null );
Notes:
- Negative indices are not permitted.
- Out-of-bounds row and column indices will silently fail.
- Values which are set are cast to the target
Matrix
data type. - A value
Matrix
must have dimensions which match the submatrix defined by row and column indices. - If linear indices are provided, a value
Matrix
must have a length
equal to the number of provided indices.
Matrix.prototype.sset( subsequence, value[, thisArg] )
Sets Matrix
elements according to a specified subsequence
. The subsequence
must specify both row and column subsequences; e.g., '3:7,5:9'
, where 3:7
corresponds to row indices 3,4,5,6
and 5:9
corresponds to column indices 5,6,7,8
. The second argument may be either a number
primitive, a Matrix
containing values to set, or a callback function
.
var data = new Float32Array( 10*10 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = i;
}
var mat = matrix( data, [10,10] );
var submat = mat.sget( '3:7,5:9' );
var zeros = matrix( [2,2], 'float32' );
mat.sset( '4:6,6:8', zeros );
submat = mat.sget( '3:7,5:9' );
A callback is provided four arguments:
- d: value at a subsequence index
- i: row index
- j: column index
- idx: linear index
and is expected to return a number
primitive or a value which can be cast to a number
primitive.
function set( d, i, j, idx ) {
return '' + j + i;
}
mat.sset( '4:6,6:8', set );
submat = mat.sget( '3:7,5:9' );
By default, the callback this
context is set to the Matrix
instance. To specify a different this
context, provide a thisArg
.
function set( d, i, j, idx ) {
console.log( this );
return '' + j + i;
}
mat.sset( '4:6,6:8', set, null );
Notes:
- Values which are set are cast to the target
Matrix
data type. - Out-of-bounds row and column indices will silently fail.
- A provided
Matrix
must have dimensions which match the submatrix defined by row and column subsequences. - For further subsequence documentation, see compute-indexspace.
===
Get Methods
These methods provide access to Matrix
elements:
Matrix.prototype.get( i, j )
Returns a Matrix
element located at a row and column index.
var data = new Float32Array( 10 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = i;
}
var mat = matrix( data, [5,2] );
var values = mat.get( 3, 1 );
Note: out-of-bounds row and column indices will return a value of undefined
.
Matrix.prototype.iget( index )
Returns a Matrix
element located at a specified index
. If index < 0
, the index refers to a position relative to the Matrix
length, where index = -1
corresponds to the last element.
var value = mat.iget( 7 );
value = mat.iget( -3 );
Note: out-of-bounds indices will return a value of undefined
.
Matrix.prototype.mget( idx[, cols] )
Returns multiple Matrix
elements. If provided a single argument, the method treats idx
as an array
of linear indices (idx[i] >= 0
) and returns a new Matrix
instance having a single row. Otherwise, idx
and cols
are integer
arrays which specify row and column indices and the method returns a new Matrix
instance having dimensions determined by the number of defined rows and columns.
var data = new Int8Array( 10 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = i*2;
}
var mat = matrix( data, [5,2] );
var vals = mat.mget( [1,5,3,9,7] );
var mat1 = mat.mget( [1,3,2], [1] );
If idx
and/or cols
is null
, all rows (columns) are extracted.
var rep = mat.mget( null, [1,1,1,1,1] );
var tile = mat.mget( [1,2,1,2], [0,1,0,1] );
Note: out-of-bounds indices are ignored.
Matrix.prototype.sget( subsequence )
Returns Matrix
elements in a new Matrix
according to a specified subsequence
. The subsequence
must specify both row and column subsequences; e.g., '3:7,5:9'
, where 3:7
corresponds to row indices 3,4,5,6
and 5:9
corresponds to column indices 5,6,7,8
. If a subsequence
does not correspond to any Matrix
elements, the method returns an empty Matrix
.
var submatrix;
submatrix = mat.sget( ':,:' );
submatrix = mat.sget( '1:4,:' );
submatrix = mat.sget( '::-1,:' );
submatrix = mat.sget( ':,::-1' );
submatrix = mat.sget( '50:100,:' );
Notes:
- Out-of-bounds indices are ignored.
- For further subsequence documentation, see compute-indexspace.
===
Accessor Methods
These methods do not mutate a Matrix
and return some representation of a Matrix
:
Matrix.prototype.toString()
Returns a string
representation of a Matrix
. This method is similar to Array#toString
, except that rows are delineated by semicolons and column values are delineated by commas.
var data = new Int8Array( 10 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = i;
}
var mat = matrix( data, [5,2] );
var str = mat.toString();
To construct an array
of arrays
from the string
representation,
var rows,
cols,
i, j;
rows = str.split( ';' );
for ( i = 0; i < rows.length; i++ ) {
cols = rows[ i ].split( ',' );
rows[ i ] = new Array( cols.length );
for ( j = 0; j < cols.length; j++ ) {
rows[ i ][ j ] = parseFloat( cols[ j ] );
}
}
Matrix.prototype.toJSON()
Returns a JSON
representation of a Matrix
. JSON#stringify
implicitly calls this method when stringifying a Matrix
instance.
var data = new Int8Array( 10 );
for ( var i = 0; i < data.length; i++ ) {
data[ i ] = i;
}
var mat = matrix( data, [5,2] );
var json = mat.toJSON();
To a revive a Matrix
from a JSON
string,
var reviver = require( 'dstructs-matrix-reviver' );
var str = JSON.stringify( mat );
var mat = JSON.parse( str, reviver );
Constructor
A Matrix
has a constructor having the following interface...
mat.constructor( data, dtype, shape, offset, strides )
Creates a new Matrix
having a specified shape
, offset
, strides
, dtype
, and underlying typed data
store.
var data = new Float32Array( 10 );
var mat1 = matrix( data, [5,2] );
var mat2 = new mat1.constructor( data, mat1.dtype, [2,5], 0, [5,1] );
Note: while more performant, constructing a Matrix
in this manner should be carefully considered. Arguments are not validated or sanity checked.
Raw
For performance, a lower-level interface is provided which forgoes some of the guarantees of the above API, such as input argument validation and measures to prevent Matrices
from becoming corrupted. While use of the above API is encouraged in REPL environments, use of the lower-level interface may be warranted when arguments are of a known type or when many Matrices
must be created.
matrix.raw( [data,] shape[, dtype] )
Creates a new Matrix
having a specified shape
.
var data = new Float32Array( 10 );
var mat = matrix.raw( data, [5,2] );
If the input data
type is known, Matrix
creation is significantly faster.
var mat = matrix.raw( data, [5,2], 'float32' );
Notes:
- The
shape
and dtype
parameters are the same as for the higher-level Matrix
interface. - Specifying a
dtype
does not cast the data to a different storage type. Instead, providing the argument circumvents the need to determine the input data
type, resulting in increased performance. - Input
data
must be a typed array. Unlike the higher-level Matrix
interface, plain arrays
are not cast to float64
. Providing a plain array
can lead to subtle bugs and affect performance. Matrix
properties and methods are the same as for the higher-level API, with the exception that Matrix
properties are no longer read-only and methods do not perform input argument validation.- Setting properties is not recommended as the
Matrix
can become corrupted; e.g., incompatible dimensions, out-of-bounds indexing, etc. In contrast to the strict API above, setting Matrix
properties will not result in an error
being thrown. Accordingly, property modification may introduce silent bugs. - The lower-level
Matrix
constructor has the same interface as the higher-level Matrix
constructor.
Notes
Linear Indexing
A linear index
corresponds to an element position in a flattened Matrix
arranged in row-major order. For example, consider a zero-filled 5x2 matrix, its subscripts, and its corresponding linear indices.
Examples
var matrix = require( 'dstructs-matrix' );
var mat = matrix( [2,2] );
console.log( mat );
console.log( mat.get( 1, 1 ) );
console.log( mat.set( 1, 1, 5 ) );
console.log( mat.get( 1, 1 ) );
console.log( mat.toString() );
console.log( mat.toJSON() );
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.