dstructs-matrix
Advanced tools
Comparing version 2.0.2 to 2.1.0
@@ -6,3 +6,3 @@ 'use strict'; | ||
/** | ||
* FUNCTION: Matrix( data, shape, dtype ) | ||
* FUNCTION: Matrix( data, dtype, shape, offset, strides ) | ||
* Matrix constructor. | ||
@@ -102,3 +102,4 @@ * | ||
Matrix.prototype.toString = require( './toString.js' ); | ||
Matrix.prototype.toString = require( './tostring.js' ); | ||
Matrix.prototype.toJSON = require( './tojson.js' ); | ||
@@ -105,0 +106,0 @@ |
@@ -6,3 +6,3 @@ 'use strict'; | ||
/** | ||
* FUNCTION: Matrix( data, shape, dtype ) | ||
* FUNCTION: Matrix( data, dtype, shape, offset, strides ) | ||
* Matrix constructor. | ||
@@ -46,7 +46,7 @@ * | ||
Matrix.prototype.toString = require( './toString.js' ); | ||
Matrix.prototype.toString = require( './tostring.js' ); | ||
Matrix.prototype.toJSON = require( './tojson.js' ); | ||
// EXPORTS // | ||
module.exports = Matrix; |
@@ -97,2 +97,4 @@ 'use strict'; | ||
data = cast( data, dtype ); | ||
} else { | ||
dtype = dt; | ||
} | ||
@@ -99,0 +101,0 @@ } else { |
@@ -11,2 +11,3 @@ 'use strict'; | ||
* @param {Number} v - numeric value | ||
* @returns {Void} | ||
*/ | ||
@@ -13,0 +14,0 @@ function mset1( mat, idx, v ) { |
@@ -12,2 +12,3 @@ 'use strict'; | ||
* @param {Object} ctx - `this` context when invoking the provided callback | ||
* @returns {Void} | ||
*/ | ||
@@ -14,0 +15,0 @@ function mset2( mat, idx, clbk, ctx ) { |
@@ -11,2 +11,3 @@ 'use strict'; | ||
* @param {Matrix} m - Matrix instance | ||
* @returns {Void} | ||
*/ | ||
@@ -13,0 +14,0 @@ function mset3( mat, idx, m ) { |
@@ -13,2 +13,3 @@ 'use strict'; | ||
* @param {Object} ctx - `this` context when invoking the provided callback | ||
* @returns {Void} | ||
*/ | ||
@@ -15,0 +16,0 @@ function mset4( mat, rows, cols, clbk, ctx ) { |
@@ -12,2 +12,3 @@ 'use strict'; | ||
* @param {Number} v - numeric value | ||
* @returns {Void} | ||
*/ | ||
@@ -14,0 +15,0 @@ function mset5( mat, rows, cols, v ) { |
@@ -12,2 +12,3 @@ 'use strict'; | ||
* @param {Matrix} m - Matrix instance | ||
* @returns {Void} | ||
*/ | ||
@@ -14,0 +15,0 @@ function mset6( mat, rows, cols, m ) { |
{ | ||
"name": "dstructs-matrix", | ||
"version": "2.0.2", | ||
"version": "2.1.0", | ||
"description": "Matrices.", | ||
@@ -5,0 +5,0 @@ "author": { |
149
README.md
@@ -7,3 +7,44 @@ Matrix | ||
This module exports a [`Matrix`][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. | ||
--- | ||
1. [Installation](#installation) | ||
1. [Usage](#usage) | ||
- [Matrix](#matrix) | ||
* [matrix()](#matrix) | ||
- [Properties](#properties) | ||
* [dtype](#matrix-dtype) | ||
* [ndims](#matrix-ndims) | ||
* [shape](#matrix-shape) | ||
* [offset](#matrix-offset) | ||
* [strides](#matrix-strides) | ||
* [length](#matrix-length) | ||
* [nbytes](#matrix-nbytes) | ||
* [data](#matrix-data) | ||
- [Methods](#methods) | ||
* [Set](#set-methods) | ||
- [Matrix.prototype.set()](#matrix-set) | ||
- [Matrix.prototype.iset()](#matrix-iset) | ||
- [Matrix.prototype.mset()](#matrix-mset) | ||
- [Matrix.prototype.sset()](#matrix-sset) | ||
* [Get](#get-methods) | ||
- [Matrix.prototype.get()](#matrix-get) | ||
- [Matrix.prototype.iget()](#matrix-iget) | ||
- [Matrix.prototype.mget()](#matrix-mget) | ||
- [Matrix.prototype.sget()](#matrix-sget) | ||
* [Accessor](#accessor-methods) | ||
- [Matrix.prototype.toString()](#matrix-tostring) | ||
- [Matrix.prototype.toJSON()](#matrix-tojson) | ||
- [Constructor](#matrix-constructor) | ||
- [Raw](#raw) | ||
* [matrix.raw()](#matrix-raw) | ||
1. [Notes](#notes) | ||
- [Linear Indexing](#linear-indexing) | ||
1. [Examples](#examples) | ||
1. [Tests](#tests) | ||
- [Unit](#unit) | ||
- [Coverage](#test-coverage) | ||
1. [License](#license) | ||
--- | ||
## Installation | ||
@@ -15,3 +56,3 @@ | ||
For use in the browser, use [browserify](https://github.com/substack/node-browserify). | ||
For use in the browser, use [browserify][browserify]. | ||
@@ -88,3 +129,3 @@ | ||
If provided an `Array` instead of a typed array and no `dtype` is specified, the input `data` array is [cast](https://github.com/compute-io/cast-arrays) to `float64`. | ||
If provided an `Array` instead of a typed array and no `dtype` is specified, the input `data` array is [cast][cast-arrays] to `float64`. | ||
@@ -171,3 +212,3 @@ ``` javascript | ||
A __read-only__ property returning the matrix `length`; i.e., how many elements are in the `Matrix`, similar to [`Array#length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length). | ||
A __read-only__ property returning the matrix `length`; i.e., how many elements are in the `Matrix`, similar to [`Array#length`][array-length]. | ||
@@ -222,2 +263,4 @@ ``` javascript | ||
These methods mutate a `Matrix`: | ||
<a name="matrix-set"></a> | ||
@@ -375,3 +418,3 @@ #### Matrix.prototype.set( i, j, value ) | ||
Sets `Matrix` elements according to a specified [`subsequence`](https://github.com/compute-io/indexspace). 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`. | ||
Sets `Matrix` elements according to a specified [`subsequence`][indexspace]. 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`. | ||
@@ -453,3 +496,3 @@ ``` javascript | ||
* A provided `Matrix` must have dimensions which match the submatrix defined by row and column subsequences. | ||
* For further subsequence documentation, see [compute-indexspace](https://github.com/compute-io/indexspace). | ||
* For further subsequence documentation, see [compute-indexspace][indexspace]. | ||
@@ -460,2 +503,4 @@ | ||
These methods provide access to `Matrix` elements: | ||
<a name="matrix-get"></a> | ||
@@ -571,3 +616,3 @@ #### Matrix.prototype.get( i, j ) | ||
Returns `Matrix` elements in a new `Matrix` according to a specified [`subsequence`](https://github.com/compute-io/indexspace). 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`. | ||
Returns `Matrix` elements in a new `Matrix` according to a specified [`subsequence`][indexspace]. 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`. | ||
@@ -619,3 +664,3 @@ ``` javascript | ||
* Out-of-bounds indices are ignored. | ||
* For further subsequence documentation, see [compute-indexspace](https://github.com/compute-io/indexspace). | ||
* For further subsequence documentation, see [compute-indexspace][indexspace]. | ||
@@ -626,6 +671,8 @@ | ||
These methods do **not** mutate a `Matrix` and return some representation of a `Matrix`: | ||
<a name="matrix-tostring"></a> | ||
#### Matrix.prototype.toString() | ||
Returns a `string` representation of a `Matrix`. This method is similar to [`Array#toString`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString), except that rows are delineated by __semicolons__ and column values are delineated by __commas__. | ||
Returns a `string` representation of a `Matrix`. This method is similar to [`Array#toString`][array-string], except that rows are delineated by __semicolons__ and column values are delineated by __commas__. | ||
@@ -662,2 +709,58 @@ ``` javascript | ||
<a name="matrix-tojson"></a> | ||
#### Matrix.prototype.toJSON() | ||
Returns a [`JSON`][json] representation of a `Matrix`. [`JSON#stringify`][json-stringify] implicitly calls this method when stringifying a `Matrix` instance. | ||
``` javascript | ||
var data = new Int8Array( 10 ); | ||
for ( var i = 0; i < data.length; i++ ) { | ||
data[ i ] = i; | ||
} | ||
var mat = matrix( data, [5,2] ); | ||
/* | ||
[ 0 1 | ||
2 3 | ||
4 5 | ||
6 7 | ||
8 9 ] | ||
*/ | ||
var json = mat.toJSON(); | ||
/* | ||
{ | ||
"type": "Matrix", | ||
"dtype": "int8", | ||
"shape": [5,2], | ||
"offset": 0, | ||
"strides": [2,1], | ||
"raw": false, | ||
"data": [0,1,2,3,4,5,6,7,8,9] | ||
} | ||
*/ | ||
``` | ||
To a [revive][json-parse] a `Matrix` from a [`JSON`][json] string, | ||
``` javascript | ||
// Matrix reviver: | ||
var reviver = require( 'dstructs-matrix-reviver' ); | ||
// Stringify a matrix (implicitly calls `.toJSON`): | ||
var str = JSON.stringify( mat ); | ||
// returns '{"type":"Matrix","dtype":"int8","shape":[5,2],"offset":0,"strides":[2,1],"raw":false,"data":[0,1,2,3,4,5,6,7,8,9]}' | ||
// Revive a Matrix from a JSON string: | ||
var mat = JSON.parse( str, reviver ); | ||
/* | ||
[ 0 1 | ||
2 3 | ||
4 5 | ||
6 7 | ||
8 9 ] | ||
*/ | ||
``` | ||
--- | ||
@@ -749,3 +852,3 @@ <a name="matrix-constructor"></a> | ||
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. | ||
A linear `index` corresponds to an element position in a flattened `Matrix` arranged in [__row-major__][row-major-order] order. For example, consider a [__zero-filled__][zeros] 5x2 matrix, its subscripts, and its corresponding linear indices. | ||
@@ -786,2 +889,5 @@ ``` javascript | ||
console.log( mat.toString() ); | ||
// Convert the matrix to JSON: | ||
console.log( mat.toJSON() ); | ||
``` | ||
@@ -801,3 +907,3 @@ | ||
Unit tests use the [Mocha](http://mochajs.org/) test framework with [Chai](http://chaijs.com) assertions. To run the tests, execute the following command in the top-level application directory: | ||
Unit tests use the [Mocha][mocha] test framework with [Chai][chai] assertions. To run the tests, execute the following command in the top-level application directory: | ||
@@ -813,3 +919,3 @@ ``` bash | ||
This repository uses [Istanbul](https://github.com/gotwarlost/istanbul) as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory: | ||
This repository uses [Istanbul][istanbul] as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory: | ||
@@ -830,3 +936,3 @@ ``` bash | ||
[MIT license](http://opensource.org/licenses/MIT). | ||
[MIT license][mit-license]. | ||
@@ -836,3 +942,3 @@ | ||
Copyright © 2015. The [Compute.io](https://github.com/compute-io) Authors. | ||
Copyright © 2015. The [Compute.io][compute-io] Authors. | ||
@@ -857,1 +963,18 @@ | ||
[github-issues-url]: https://github.com/dstructs/matrix/issues | ||
[matrix]: https://en.wikipedia.org/wiki/Matrix_(mathematics) | ||
[browserify]: https://github.com/substack/node-browserify | ||
[cast-arrays]: https://github.com/compute-io/cast-arrays | ||
[array-length]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length | ||
[indexspace]: https://github.com/compute-io/indexspace | ||
[array-string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString | ||
[json]: http://www.json.org/ | ||
[json-stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify | ||
[json-parse]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse | ||
[row-major-order]: https://en.wikipedia.org/wiki/Row-major_order | ||
[zeros]: https://github.com/compute-io/zeros | ||
[mocha]: http://mochajs.org/ | ||
[chai]: http://chaijs.com | ||
[istanbul]: https://github.com/gotwarlost/istanbul | ||
[mit-license]: http://opensource.org/licenses/MIT | ||
[compute-io]: https://github.com/compute-io |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
67057
34
1522
962