{"id":41249989,"url":"https://github.com/dstructs/matrix","last_synced_at":"2026-01-23T01:36:36.181Z","repository":{"id":32870325,"uuid":"36464351","full_name":"dstructs/matrix","owner":"dstructs","description":"Matrices.","archived":false,"fork":false,"pushed_at":"2020-02-20T19:50:38.000Z","size":131,"stargazers_count":18,"open_issues_count":4,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-10-08T09:52:53.625Z","etag":null,"topics":["javascript","math","matrix","nodejs","numeric"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dstructs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-28T20:30:13.000Z","updated_at":"2024-01-09T10:25:52.000Z","dependencies_parsed_at":"2022-08-31T22:22:58.481Z","dependency_job_id":null,"html_url":"https://github.com/dstructs/matrix","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/dstructs/matrix","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dstructs%2Fmatrix","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dstructs%2Fmatrix/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dstructs%2Fmatrix/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dstructs%2Fmatrix/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dstructs","download_url":"https://codeload.github.com/dstructs/matrix/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dstructs%2Fmatrix/sbom","scorecard":{"id":358141,"data":{"date":"2025-08-11","repo":{"name":"github.com/dstructs/matrix","commit":"56a60fbbc8015dd0ef68958b178b22f83ae53f0a"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":3,"checks":[{"name":"Token-Permissions","score":-1,"reason":"No tokens found","details":null,"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Dangerous-Workflow","score":-1,"reason":"no workflows found","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Code-Review","score":0,"reason":"Found 0/13 approved changesets -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Pinned-Dependencies","score":-1,"reason":"no dependencies found","details":null,"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":0,"reason":"branch protection not enabled on development/release branches","details":["Warn: branch protection not enabled for branch 'master'"],"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 19 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-18T10:07:29.964Z","repository_id":32870325,"created_at":"2025-08-18T10:07:29.964Z","updated_at":"2025-08-18T10:07:29.964Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28677710,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-23T01:00:35.747Z","status":"ssl_error","status_checked_at":"2026-01-23T01:00:19.529Z","response_time":144,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["javascript","math","matrix","nodejs","numeric"],"created_at":"2026-01-23T01:36:35.405Z","updated_at":"2026-01-23T01:36:36.171Z","avatar_url":"https://github.com/dstructs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Matrix\n===\n[![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Coverage Status][codecov-image]][codecov-url] [![Dependencies][dependencies-image]][dependencies-url]\n\n\u003e Matrices.\n\n\u003ca target=\"_blank\" rel=\"nofollow\" href=\"https://app.codesponsor.io/link/2GH3ESgBANYoWNMCAicW2LQk/dstructs/matrix\"\u003e\u003cimg alt=\"Sponsor\" width=\"888\" height=\"68\" src=\"https://app.codesponsor.io/embed/2GH3ESgBANYoWNMCAicW2LQk/dstructs/matrix.svg\"/\u003e\u003c/a\u003e\n\nThis 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.\n\n---\n1. [Installation](#installation)\n1. [Usage](#usage)\n\t-\t[Matrix](#matrix)\n\t\t*\t[matrix()](#matrix)\n\t-\t[Properties](#properties)\n\t\t*\t[dtype](#matrix-dtype)\n\t\t*\t[ndims](#matrix-ndims)\n\t\t*\t[shape](#matrix-shape)\n\t\t*\t[offset](#matrix-offset)\n\t\t*\t[strides](#matrix-strides)\n\t\t*\t[length](#matrix-length)\n\t\t*\t[nbytes](#matrix-nbytes)\n\t\t*\t[data](#matrix-data)\n\t-\t[Methods](#methods)\n\t\t*\t[Set](#set-methods)\n\t\t\t-\t[Matrix.prototype.set()](#matrix-set)\n\t\t\t-\t[Matrix.prototype.iset()](#matrix-iset)\n\t\t\t-\t[Matrix.prototype.mset()](#matrix-mset)\n\t\t\t-\t[Matrix.prototype.sset()](#matrix-sset)\n\t\t*\t[Get](#get-methods)\n\t\t\t-\t[Matrix.prototype.get()](#matrix-get)\n\t\t\t-\t[Matrix.prototype.iget()](#matrix-iget)\n\t\t\t-\t[Matrix.prototype.mget()](#matrix-mget)\n\t\t\t-\t[Matrix.prototype.sget()](#matrix-sget)\n\t\t*\t[Accessor](#accessor-methods)\n\t\t\t-\t[Matrix.prototype.toString()](#matrix-tostring)\n\t\t\t-\t[Matrix.prototype.toJSON()](#matrix-tojson)\n\t-\t[Constructor](#matrix-constructor)\n\t-\t[Raw](#raw)\n\t\t*\t[matrix.raw()](#matrix-raw)\n1.\t[Notes](#notes)\n\t-\t[Linear Indexing](#linear-indexing)\n1. \t[Examples](#examples)\n1.\t[Tests](#tests)\n\t-\t[Unit](#unit)\n\t-\t[Coverage](#test-coverage)\n1. \t[License](#license)\n\n---\n## Installation\n\n``` bash\n$ npm install dstructs-matrix\n```\n\nFor use in the browser, use [browserify][browserify].\n\n\n## Usage\n\n``` javascript\nvar matrix = require( 'dstructs-matrix' );\n```\n\n\u003ca name=\"matrix\"\u003e\u003c/a\u003e\n#### matrix( [data,] shape[, dtype] )\n\nCreates a new `Matrix` having a specified `shape` (dimensions =\u003e `[rows,cols]`).\n\n``` javascript\nvar mat = matrix( [3,2] );\n/*\n\t[ 0 0\n\t  0 0\n\t  0 0 ]\n*/\n```\n\nBy default, the matrix elements are floating-point 64-bit numbers (`float64`). To specify a different data type, provide a `dtype`.\n\n``` javascript\nvar mat = matrix( [2,2], 'int8' );\n/*\n\t[ 0 0\n\t  0 0 ]\n*/\n```\n\nThe following `dtypes` are accepted:\n\n*\t`int8`\n*\t`uint8`\n*\t`uint8_clamped`\n*\t`int16`\n*\t`uint16`\n*\t`int32`\n*\t`uint32`\n*\t`float32`\n*\t`float64`\n\n\nIf 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`.\n\n``` javascript\nvar data = new Int8Array( 6 );\n\nfor ( var i = 0; i \u003c data.length; i++ ) {\n\tdata[ i ] = i;\n}\n\nvar mat = matrix( data, [2,3] ); // 2*3 = 6\n/*\n\t[ 0 1 2\n\t  3 4 5 ]\n*/\n```\n\nTo cast an input `data` array to a different data type, provide a `dtype`.\n\n``` javascript\nvar mat = matrix( data, [2,2], 'uint32' );\n/*\n\t[ 0 1\n\t  2 3 ]\n*/\n```\n\nIf provided an `Array` instead of a typed array and no `dtype` is specified, the input `data` array is [cast][cast-arrays] to `float64`.\n\n``` javascript\nvar data = [ 10, 20, 30, 40, 50, 60 ];\n\nvar mat = matrix( data, [3,2] );\n/*\n\t[ 10 20\n\t  30 40\n\t  50 60 ]\n*/\n\nvar dtype = mat.dtype;\n// returns 'float64'\n```\n\n\n---\n## Properties\n\nA `Matrix` has the following properties...\n\n\n\u003ca name=\"matrix-dtype\" class=\"read-only-property\"\u003e\u003c/a\u003e\n#### dtype\n\nA __read-only__ property returning the underlying storage data type.\n\n``` javascript\nvar dtype = mat.dtype;\n// returns \u003cstring\u003e\n```\n\n\u003ca name=\"matrix-ndims\" class=\"read-only-property\"\u003e\u003c/a\u003e\n#### ndims\n\nA __read-only__ property returning the number of dimensions.\n\n``` javascript\nvar ndims = mat.ndims;\n// returns 2\n```\n\n\u003ca name=\"matrix-shape\" class=\"read-only-property\"\u003e\u003c/a\u003e\n#### shape\n\nA __read-only__ property returning the matrix `shape`.\n\n``` javascript\nvar shape = mat.shape;\n// returns [...]\n```\n\n\u003ca name=\"matrix-offset\"\u003e\u003c/a\u003e\n#### offset\n\nA property returning the `offset` used to index into the underlying data store.\n\n``` javascript\nvar offset = mat.offset;\n// returns 0\n```\n\nBy default, the `offset` is `0`. While not __read-only__, most consumers should treat the `offset` as a __read-only__ property.\n\n\n\u003ca name=\"matrix-strides\" class=\"read-only-property\"\u003e\u003c/a\u003e\n#### strides\n\nA __read-only__ property returning the `strides` used to index into the underlying data store.\n\n``` javascript\nvar strides = mat.strides;\n// returns [...]\n```\n\nWhile not __frozen__, most consumers should treat the `strides` elements as __read-only__ elements.\n\n\n\u003ca name=\"matrix-length\" class=\"read-only-property\"\u003e\u003c/a\u003e\n#### length\n\nA __read-only__ property returning the matrix `length`; i.e., how many elements are in the `Matrix`, similar to [`Array#length`][array-length].\n\n``` javascript\nvar len = mat.length;\n// returns \u003cnumber\u003e\n```\n\n__Note__: while a `Matrix` has a `length` property, a `Matrix` should __not__ be considered `array-like`, as `array` indexing with __not__ work as expected.\n\n``` javascript\nvar data = new Float32Array( 10 );\n\nvar mat = matrix( data, [1,10] );\n/*\n    [ 0 0 0 0 0 0 0 0 0 0 ]\n*/\n\nvar value = mat.get( 1, 3 );\n// returns 0\n\nvalue = mat[ 3 ];\n// returns undefined\n```\n\n\u003ca name=\"matrix-nbytes\" class=\"read-only-property\"\u003e\u003c/a\u003e\n#### nbytes\n\nA __read-only__ property returning the number of bytes consumed by the `Matrix` elements.\n\n``` javascript\nvar nbytes = mat.nbytes;\n// returns \u003cnumber\u003e\n```\n\n\u003ca name=\"matrix-data\" class=\"read-only-property\"\u003e\u003c/a\u003e\n#### data\n\nA __read-only__ property pointing to the underlying storage array.\n\n``` javascript\nvar data = mat.data;\n// returns \u003cTypedArray\u003e\n```\n\n---\n## Methods\n\nA `Matrix` has the following methods...\n\n\n### Set Methods\n\nThese methods mutate a `Matrix`:\n\n\u003ca name=\"matrix-set\"\u003e\u003c/a\u003e\n#### Matrix.prototype.set( i, j, value )\n\nSets a `Matrix` element located at a row and column index.\n\n``` javascript\nmat.set( 3, 1, 20 );\n/*\n\t[ 0 1\n\t  2 3\n\t  4 5\n\t  6 20\n\t  8 9 ]\n*/\n```\n\nSet methods return the `Matrix` instance and are thus chainable.\n\n``` javascript\nmat\n\t.set( 3, 1, 21 )\n\t.set( 3, 1, 22 )\n\t.set( 3, 1, 23 )\n\t.set( 3, 1, 24 )\n\t.get( 3, 1 );\n// returns 24\n```\n\n__Note__: out-of-bounds row and column indices will silently fail.\n\n\n\u003ca name=\"matrix-iset\"\u003e\u003c/a\u003e\n#### Matrix.prototype.iset( index, value )\n\nSets a `Matrix` element located at a specified [`index`](#linear-indexing). If `index \u003c 0`, the index refers to a position relative to the `Matrix` length, where `index = -1` corresponds to the last element.\n\n``` javascript\nmat.iset( 7, 25 );\n/*\n\t[ 0 1\n\t  2 3\n\t  4 5\n\t  6 25\n\t  8 9 ]\n*/\n\nmat.iset( -3, 20 );\n/*\n\t[ 0 1\n\t  2 3\n\t  4 5\n\t  6 20\n\t  8 9 ]\n*/\n```\n\n__Note__: out-of-bounds indices will silently fail.\n\n\n\u003ca name=\"matrix-mset\"\u003e\u003c/a\u003e\n#### Matrix.prototype.mset( idx[, cols], value[, thisArg] )\n\nSets multiple `Matrix` elements. If provided a single `array`, `idx` is treated as an `array` of [linear indices](#linear-indexing). The `value` argument may be either a `number` primitive, a `Matrix` containing values to set, or a callback `function`.\n\n``` javascript\nvar data = new Int8Array( 10*10 );\n\nfor ( var i = 0; i \u003c data.length; i++ ) {\n\tdata[ i ] = i;\n}\n// Create a 10x10 matrix:\nvar mat = matrix( data, [10,10] );\n\nvar submat = mat.mget( [0,2,4], [1,4,5] );\n/*\n\t[  1  4  5\n\t  21 24 25\n\t  41 44 45 ]\n*/\n\nmat.mset( [1,4,5,21,24,25,41,44,45], 5 );\n\nsubmat = mat.mget( [0,2,4], [1,4,5] );\n/*\n\t[ 5 5 5\n\t  5 5 5\n\t  5 5 5 ]\n*/\n\nvar zeros = matrix( [1,3], 'int8' );\n/*\n\t[ 0 0 0 ]\n*/\n\nmat.mset( [2], [1,4,5], zeros );\n\nsubmat = mat.mget( [0,2,4], [1,4,5] );\n/*\n\t[ 5 5 5\n\t  0 0 0\n\t  5 5 5 ]\n*/\n```\n\nA callback is provided four arguments:\n*\t__d__: current value\n*\t__i__: row index\n*\t__j__: column index\n*\t__idx__: linear index\n\nand is __expected__ to return a `number` primitive or a value which can be cast to a `number` primitive.\n\n``` javascript\nfunction set( d, i, j, idx ) {\n\treturn '' + j + i;\n}\n\nmat.mset( [0], [1,4,5], set );\n\nmat.mget( [0,2,4], [1,4,5] );\n/*\n\t[ 10 40 50\n\t   0  0  0\n\t   5  5  5 ]\n*/\n```\n\nBy default, the callback `this` context is set to the `Matrix` instance. To specify a different `this` context, provide a `thisArg`.\n\n``` javascript\nfunction set( d, i, j, idx ) {\n\tconsole.log( this );\n\t// returns null\n\treturn '' + j + i;\n}\n\nmat.mset( [0], [1,4,5], set, null );\n```\n\n\n__Notes__:\n*\tNegative indices are __not__ permitted.\n* \tOut-of-bounds row and column indices will silently fail.\n*\tValues which are set are cast to the target `Matrix` data type.\n*\tA value `Matrix` must have dimensions which match the submatrix defined by row and column indices.\n*\tIf linear indices are provided, a value `Matrix` must have a `length` equal to the number of provided indices.\n\n\n\n\u003ca name=\"matrix-sset\"\u003e\u003c/a\u003e\n#### Matrix.prototype.sset( subsequence, value[, thisArg] )\n\nSets `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`.\n\n``` javascript\nvar data = new Float32Array( 10*10 );\n\nfor ( var i = 0; i \u003c data.length; i++ ) {\n\tdata[ i ] = i;\n}\n// Create a 10x10 matrix:\nvar mat = matrix( data, [10,10] );\n\nvar submat = mat.sget( '3:7,5:9' );\n/*\n\t[ 35 36 37 38\n\t  45 46 47 48\n\t  55 56 57 58\n\t  65 66 67 68 ]\n*/\n\nvar zeros = matrix( [2,2], 'float32' );\n/*\n\t[ 0 0\n\t  0 0 ]\n*/\n\nmat.sset( '4:6,6:8', zeros );\n\nsubmat = mat.sget( '3:7,5:9' );\n/*\n\t[ 35 36 37 38\n\t  45  0  0 48\n\t  55  0  0 58\n\t  65 66 67 68 ]\n*/\n```\n\nA callback is provided four arguments:\n*\t__d__: value at a subsequence index\n*\t__i__: row index\n*\t__j__: column index\n*\t__idx__: linear index\n\nand is __expected__ to return a `number` primitive or a value which can be cast to a `number` primitive.\n\n``` javascript\nfunction set( d, i, j, idx ) {\n\treturn '' + j + i;\n}\n\nmat.sset( '4:6,6:8', set );\n\nsubmat = mat.sget( '3:7,5:9' );\n/*\n\t[ 35 36 37 38\n\t  45 64 74 48\n\t  55 65 75 58\n\t  65 66 67 68 ]\n*/\n```\n\nBy default, the callback `this` context is set to the `Matrix` instance. To specify a different `this` context, provide a `thisArg`.\n\n``` javascript\nfunction set( d, i, j, idx ) {\n\tconsole.log( this );\n\t// returns null\n\treturn '' + j + i;\n}\n\nmat.sset( '4:6,6:8', set, null );\n```\n\n\n__Notes__:\n*\tValues which are set are cast to the target `Matrix` data type.\n* \tOut-of-bounds row and column indices will silently fail.\n*\tA provided `Matrix` must have dimensions which match the submatrix defined by row and column subsequences.\n*\tFor further subsequence documentation, see [compute-indexspace][indexspace].\n\n\n===\n### Get Methods\n\nThese methods provide access to `Matrix` elements:\n\n\u003ca name=\"matrix-get\"\u003e\u003c/a\u003e\n#### Matrix.prototype.get( i, j )\n\nReturns a `Matrix` element located at a row and column index.\n\n``` javascript\nvar data = new Float32Array( 10 );\n\nfor ( var i = 0; i \u003c data.length; i++ ) {\n\tdata[ i ] = i;\n}\n\nvar mat = matrix( data, [5,2] );\n/*\n\t[ 0 1\n\t  2 3\n\t  4 5\n\t  6 7\n\t  8 9 ]\n*/\n\nvar values = mat.get( 3, 1 );\n// returns 7\n```\n\n__Note__: out-of-bounds row and column indices will return a value of `undefined`.\n\n\n\u003ca name=\"matrix-iget\"\u003e\u003c/a\u003e\n#### Matrix.prototype.iget( index )\n\nReturns a `Matrix` element located at a specified [`index`](#linear-indexing). If `index \u003c 0`, the index refers to a position relative to the `Matrix` length, where `index = -1` corresponds to the last element.\n\n``` javascript\nvar value = mat.iget( 7 );\n// returns 7\n\nvalue = mat.iget( -3 );\n// returns 7\n```\n\n__Note__: out-of-bounds indices will return a value of `undefined`.\n\n\n\u003ca name=\"matrix-mget\"\u003e\u003c/a\u003e\n#### Matrix.prototype.mget( idx[, cols] )\n\nReturns multiple `Matrix` elements. If provided a single argument, the method treats `idx` as an `array` of [linear indices](#linear-indexing) (`idx[i] \u003e= 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.\n\n``` javascript\nvar data = new Int8Array( 10 );\nfor ( var i = 0; i \u003c data.length; i++ ) {\n\tdata[ i ] = i*2;\n}\n\nvar mat = matrix( data, [5,2] );\n/*\n\t[  0  2\n\t   4  6\n\t   8 10\n\t  12 14\n\t  16 18 ]\n*/\n\n// Scramble the second column:\nvar vals = mat.mget( [1,5,3,9,7] );\n/*\n\t[ 2, 10, 6, 18, 14 ]\n*/\n\n// Extract select rows and columns in arbitrary order:\nvar mat1 = mat.mget( [1,3,2], [1] );\n/*\n\t[  4\n\t  14\n\t   8 ]\n*/\n```\n\nIf `idx` and/or `cols` is `null`, all rows (columns) are extracted.\n\n``` javascript\n// Replicate a column:\nvar rep = mat.mget( null, [1,1,1,1,1] );\n/*\n\t[  2  2  2  2  2\n\t   6  6  6  6  6\n\t  10 10 10 10 10\n\t  14 14 14 14 14\n\t  18 18 18 18 18 ]\n*/\n\n// Tile select rows and columns:\nvar tile = mat.mget( [1,2,1,2], [0,1,0,1] );\n/*\n\t[\n\t\t4  6 4  6\n\t\t8 10 8 10\n\t\t4  6 4  6\n\t\t8 10 8 10\n\t]\n*/\n```\n\n__Note__: out-of-bounds indices are ignored.\n\n\n\u003ca name=\"matrix-sget\"\u003e\u003c/a\u003e\n#### Matrix.prototype.sget( subsequence )\n\nReturns `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`.\n\n``` javascript\nvar submatrix;\n\nsubmatrix = mat.sget( ':,:' ); // Copy a matrix\n/*\n\t[ 0 1\n\t  2 3\n\t  4 5\n\t  6 7\n\t  8 9 ]\n*/\n\nsubmatrix = mat.sget( '1:4,:' );\n/*\n\t[ 2 3\n\t  4 5\n\t  6 7 ]\n*/\n\nsubmatrix = mat.sget( '::-1,:' ); // flip top-to-bottom\n/*\n\t[ 8 9\n\t  6 7\n\t  4 5\n\t  2 3\n\t  0 1 ]\n*/\n\nsubmatrix = mat.sget( ':,::-1' ); // flip left-to-right\n/*\n\t[ 1 0\n\t  3 2\n\t  5 4\n\t  7 6\n\t  9 8 ]\n*/\n\nsubmatrix = mat.sget( '50:100,:' );\n/*\n\t[]\n*/\n```\n\n__Notes__:\n*\tOut-of-bounds indices are ignored.\n*\tFor further subsequence documentation, see [compute-indexspace][indexspace].\n\n\n===\n### Accessor Methods\n\nThese methods do **not** mutate a `Matrix` and return some representation of a `Matrix`:\n\n\u003ca name=\"matrix-tostring\"\u003e\u003c/a\u003e\n#### Matrix.prototype.toString()\n\nReturns 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__.\n\n``` javascript\nvar data = new Int8Array( 10 );\nfor ( var i = 0; i \u003c data.length; i++ ) {\n\tdata[ i ] = i;\n}\n\nvar mat = matrix( data, [5,2] );\n\nvar str = mat.toString();\n// 0,1;2,3;4,5;6,7;8,9\n```\n\nTo construct an `array` of `arrays` from the `string` representation,\n\n``` javascript\nvar rows,\n\tcols,\n\ti, j;\n\nrows = str.split( ';' );\nfor ( i = 0; i \u003c rows.length; i++ ) {\n\tcols = rows[ i ].split( ',' );\n\trows[ i ] = new Array( cols.length );\n\tfor ( j = 0; j \u003c cols.length; j++ ) {\n\t\trows[ i ][ j ] = parseFloat( cols[ j ] );\n\t}\n}\n```\n\n\n\u003ca name=\"matrix-tojson\"\u003e\u003c/a\u003e\n#### Matrix.prototype.toJSON()\n\nReturns a [`JSON`][json] representation of a `Matrix`. [`JSON#stringify`][json-stringify] implicitly calls this method when stringifying a `Matrix` instance.\n\n``` javascript\nvar data = new Int8Array( 10 );\nfor ( var i = 0; i \u003c data.length; i++ ) {\n\tdata[ i ] = i;\n}\n\nvar mat = matrix( data, [5,2] );\n/*\n\t[ 0 1\n\t  2 3\n\t  4 5\n\t  6 7\n\t  8 9 ]\n*/\n\nvar json = mat.toJSON();\n/*\n\t{\n\t\t\"type\": \"Matrix\",\n\t\t\"dtype\": \"int8\",\n\t\t\"shape\": [5,2],\n\t\t\"offset\": 0,\n\t\t\"strides\": [2,1],\n\t\t\"raw\": false,\n\t\t\"data\": [0,1,2,3,4,5,6,7,8,9]\n\t}\n*/\n```\n\nTo a [revive][json-parse] a `Matrix` from a [`JSON`][json] string,\n\n``` javascript\n// Matrix reviver:\nvar reviver = require( 'dstructs-matrix-reviver' );\n\n// Stringify a matrix (implicitly calls `.toJSON`):\nvar str = JSON.stringify( mat );\n// 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]}'\n\n// Revive a Matrix from a JSON string:\nvar mat = JSON.parse( str, reviver );\n/*\n\t[ 0 1\n\t  2 3\n\t  4 5\n\t  6 7\n\t  8 9 ]\n*/\n```\n\n\n---\n\u003ca name=\"matrix-constructor\"\u003e\u003c/a\u003e\n## Constructor\n\nA `Matrix` has a constructor having the following interface...\n\n\n#### mat.constructor( data, dtype, shape, offset, strides )\n\nCreates a new `Matrix` having a specified `shape`, `offset`, `strides`, `dtype`, and underlying typed `data` store.\n\n``` javascript\nvar data = new Float32Array( 10 );\n\nvar mat1 = matrix( data, [5,2] );\n/*\n\t[ 0 0\n\t  0 0\n\t  0 0\n\t  0 0\n\t  0 0 ]\n*/\n\nvar mat2 = new mat1.constructor( data, mat1.dtype, [2,5], 0, [5,1] );\n/*\n\t[ 0 0 0 0 0\n\t  0 0 0 0 0 ]\n*/\n```\n\n__Note__: while more performant, constructing a `Matrix` in this manner should be carefully considered. Arguments are not validated or sanity checked.\n\n\n---\n## Raw\n\nFor 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.\n\n\n\n\u003ca name=\"matrix-raw\"\u003e\u003c/a\u003e\n#### matrix.raw( [data,] shape[, dtype] )\n\nCreates a new `Matrix` having a specified `shape`.\n\n``` javascript\nvar data = new Float32Array( 10 );\n\nvar mat = matrix.raw( data, [5,2] );\n/*\n\t[ 0 0\n\t  0 0\n\t  0 0\n\t  0 0\n\t  0 0 ]\n*/\n```\n\nIf the input `data` type is known, `Matrix` creation is significantly faster.\n\n``` javascript\nvar mat = matrix.raw( data, [5,2], 'float32' );\n/*\n\t[ 0 0\n\t  0 0\n\t  0 0\n\t  0 0\n\t  0 0 ]\n*/\n```\n\n__Notes__:\n* \tThe `shape` and `dtype` parameters are the same as for the higher-level `Matrix` interface.\n*\tSpecifying 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.\n*\tInput `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.\n*\t`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.\n* \tSetting 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.\n*\tThe lower-level `Matrix` constructor has the same interface as the higher-level `Matrix` constructor.\n\n\n\n---\n## Notes\n\n#### Linear Indexing\n\nA 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.\n\n``` javascript\n/*\n\tMatrix      Subscripts      Indices\n\n\t[ 0 0       [ a00 a01       [ 0 1\n\t  0 0         a10 a11         2 3\nA =\t  0 0   =\u003e    a20 a21   =\u003e    4 5\n\t  0 0         a30 a31         6 7\n\t  0 0 ]       a40 a41 ]       8 9 ]\n*/\n```\n\n\n---\n## Examples\n\n``` javascript\nvar matrix = require( 'dstructs-matrix' );\n\n// Create a new 2x2 matrix:\nvar mat = matrix( [2,2] );\nconsole.log( mat );\n\n// Inspect the initialized matrix elements:\nconsole.log( mat.get( 1, 1 ) );\n\n// Set a matrix element:\nconsole.log( mat.set( 1, 1, 5  ) );\n\n// Confirm that the matrix element was set:\nconsole.log( mat.get( 1, 1 ) );\n\n// Convert the matrix to a string:\nconsole.log( mat.toString() );\n\n// Convert the matrix to JSON:\nconsole.log( mat.toJSON() );\n```\n\nTo run the example code from the top-level application directory,\n\n``` bash\n$ node ./examples/index.js\n```\n\n\n---\n## Tests\n\n### Unit\n\nUnit 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:\n\n``` bash\n$ make test\n```\n\nAll new feature development should have corresponding unit tests to validate correct functionality.\n\n\n### Test Coverage\n\nThis 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:\n\n``` bash\n$ make test-cov\n```\n\nIstanbul creates a `./reports/coverage` directory. To access an HTML version of the report,\n\n``` bash\n$ make view-cov\n```\n\n\n---\n## License\n\n[MIT license][mit-license].\n\n\n## Copyright\n\nCopyright \u0026copy; 2015. The [Compute.io][compute-io] Authors.\n\n\n[npm-image]: http://img.shields.io/npm/v/dstructs-matrix.svg\n[npm-url]: https://npmjs.org/package/dstructs-matrix\n\n[travis-image]: http://img.shields.io/travis/dstructs/matrix/master.svg\n[travis-url]: https://travis-ci.org/dstructs/matrix\n\n[codecov-image]: https://img.shields.io/codecov/c/github/dstructs/matrix/master.svg\n[codecov-url]: https://codecov.io/github/dstructs/matrix?branch=master\n\n[dependencies-image]: http://img.shields.io/david/dstructs/matrix.svg\n[dependencies-url]: https://david-dm.org/dstructs/matrix\n\n[dev-dependencies-image]: http://img.shields.io/david/dev/dstructs/matrix.svg\n[dev-dependencies-url]: https://david-dm.org/dev/dstructs/matrix\n\n[github-issues-image]: http://img.shields.io/github/issues/dstructs/matrix.svg\n[github-issues-url]: https://github.com/dstructs/matrix/issues\n\n[matrix]: https://en.wikipedia.org/wiki/Matrix_(mathematics)\n[browserify]: https://github.com/substack/node-browserify\n[cast-arrays]: https://github.com/compute-io/cast-arrays\n[array-length]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length\n[indexspace]: https://github.com/compute-io/indexspace\n[array-string]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString\n[json]: http://www.json.org/\n[json-stringify]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify\n[json-parse]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse\n[row-major-order]: https://en.wikipedia.org/wiki/Row-major_order\n[zeros]: https://github.com/compute-io/zeros\n[mocha]: http://mochajs.org/\n[chai]: http://chaijs.com\n[istanbul]: https://github.com/gotwarlost/istanbul\n[mit-license]: http://opensource.org/licenses/MIT\n[compute-io]: https://github.com/compute-io\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdstructs%2Fmatrix","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdstructs%2Fmatrix","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdstructs%2Fmatrix/lists"}