## SparseMatrix
Sparse Matrix implementation. This type implements a Compressed Column Storage format
for sparse matrices.
* _instance_
* [.storage()](#SparseMatrix+storage) ⇒ string
* [.datatype()](#SparseMatrix+datatype) ⇒ string
* [.create(data, [datatype])](#SparseMatrix+create)
* [.density()](#SparseMatrix+density) ⇒ number
* [.subset(index, [replacement], [defaultValue])](#SparseMatrix+subset)
* [.get(index)](#SparseMatrix+get) ⇒ \*
* [.set(index, value, [defaultValue])](#SparseMatrix+set) ⇒ [SparseMatrix](#SparseMatrix)
* [.resize(size, [defaultValue], [copy])](#SparseMatrix+resize) ⇒ Matrix
* [.clone()](#SparseMatrix+clone) ⇒ [SparseMatrix](#SparseMatrix)
* [.size()](#SparseMatrix+size) ⇒ Array.<number>
* [.map(callback, [skipZeros])](#SparseMatrix+map) ⇒ [SparseMatrix](#SparseMatrix)
* [.forEach(callback, [skipZeros])](#SparseMatrix+forEach)
* [.toArray()](#SparseMatrix+toArray) ⇒ Array
* [.valueOf()](#SparseMatrix+valueOf) ⇒ Array
* [.format([options])](#SparseMatrix+format) ⇒ string
* [.toString()](#SparseMatrix+toString) ⇒ string
* [.toJSON()](#SparseMatrix+toJSON) ⇒ Object
* [.diagonal([k])](#SparseMatrix+diagonal) ⇒ Matrix
* [.swapRows(i, j)](#SparseMatrix+swapRows) ⇒ Matrix
* _static_
* [.fromJSON(json)](#SparseMatrix.fromJSON) ⇒ [SparseMatrix](#SparseMatrix)
* [.diagonal(size, value, [k], [datatype])](#SparseMatrix.diagonal) ⇒ [SparseMatrix](#SparseMatrix)
### sparseMatrix.storage() ⇒ string
Get the storage format used by the matrix.
Usage:
```js
const format = matrix.storage() // retrieve storage format
```
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: string - The storage format.
### sparseMatrix.datatype() ⇒ string
Get the datatype of the data stored in the matrix.
Usage:
```js
const format = matrix.datatype() // retrieve matrix datatype
```
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: string - The datatype.
### sparseMatrix.create(data, [datatype])
Create a new SparseMatrix
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
| Param | Type |
| --- | --- |
| data | Array |
| [datatype] | string |
### sparseMatrix.density() ⇒ number
Get the matrix density.
Usage:
```js
const density = matrix.density() // retrieve matrix density
```
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: number - The matrix density.
### sparseMatrix.subset(index, [replacement], [defaultValue])
Get a subset of the matrix, or replace a subset of the matrix.
Usage:
```js
const subset = matrix.subset(index) // retrieve subset
const value = matrix.subset(index, replacement) // replace subset
```
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| index | Index | | |
| [replacement] | Array | Maytrix | \* | | |
| [defaultValue] | \* | 0 | Default value, filled in on new entries when the matrix is resized. If not provided, new matrix elements will be filled with zeros. |
### sparseMatrix.get(index) ⇒ \*
Get a single element from the matrix.
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: \* - value
| Param | Type | Description |
| --- | --- | --- |
| index | Array.<number> | Zero-based index |
### sparseMatrix.set(index, value, [defaultValue]) ⇒ [SparseMatrix](#SparseMatrix)
Replace a single element in the matrix.
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: [SparseMatrix](#SparseMatrix) - self
| Param | Type | Description |
| --- | --- | --- |
| index | Array.<number> | Zero-based index |
| value | \* | |
| [defaultValue] | \* | Default value, filled in on new entries when the matrix is resized. If not provided, new matrix elements will be set to zero. |
### sparseMatrix.resize(size, [defaultValue], [copy]) ⇒ Matrix
Resize the matrix to the given size. Returns a copy of the matrix when
`copy=true`, otherwise return the matrix itself (resize in place).
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: Matrix - The resized matrix
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| size | Array.<number> | | The new size the matrix should have. |
| [defaultValue] | \* | 0 | Default value, filled in on new entries. If not provided, the matrix elements will be filled with zeros. |
| [copy] | boolean | | Return a resized copy of the matrix |
### sparseMatrix.clone() ⇒ [SparseMatrix](#SparseMatrix)
Create a clone of the matrix
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: [SparseMatrix](#SparseMatrix) - clone
### sparseMatrix.size() ⇒ Array.<number>
Retrieve the size of the matrix.
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: Array.<number> - size
### sparseMatrix.map(callback, [skipZeros]) ⇒ [SparseMatrix](#SparseMatrix)
Create a new matrix with the results of the callback function executed on
each entry of the matrix.
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: [SparseMatrix](#SparseMatrix) - matrix
| Param | Type | Description |
| --- | --- | --- |
| callback | function | The callback function is invoked with three parameters: the value of the element, the index of the element, and the Matrix being traversed. |
| [skipZeros] | boolean | Invoke callback function for non-zero values only. |
### sparseMatrix.forEach(callback, [skipZeros])
Execute a callback function on each entry of the matrix.
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
| Param | Type | Description |
| --- | --- | --- |
| callback | function | The callback function is invoked with three parameters: the value of the element, the index of the element, and the Matrix being traversed. |
| [skipZeros] | boolean | Invoke callback function for non-zero values only. |
### sparseMatrix.toArray() ⇒ Array
Create an Array with a copy of the data of the SparseMatrix
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: Array - array
### sparseMatrix.valueOf() ⇒ Array
Get the primitive value of the SparseMatrix: a two dimensions array
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: Array - array
### sparseMatrix.format([options]) ⇒ string
Get a string representation of the matrix, with optional formatting options.
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: string - str
| Param | Type | Description |
| --- | --- | --- |
| [options] | Object | number | function | Formatting options. See lib/utils/number:format for a description of the available options. |
### sparseMatrix.toString() ⇒ string
Get a string representation of the matrix
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: string - str
### sparseMatrix.toJSON() ⇒ Object
Get a JSON representation of the matrix
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
### sparseMatrix.diagonal([k]) ⇒ Matrix
Get the kth Matrix diagonal.
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: Matrix - The matrix vector with the diagonal values.
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [k] | number | BigNumber | 0 | The kth diagonal where the vector will retrieved. |
### sparseMatrix.swapRows(i, j) ⇒ Matrix
Swap rows i and j in Matrix.
**Kind**: instance method of [SparseMatrix](#SparseMatrix)
**Returns**: Matrix - The matrix reference
| Param | Type | Description |
| --- | --- | --- |
| i | number | Matrix row index 1 |
| j | number | Matrix row index 2 |
### SparseMatrix.fromJSON(json) ⇒ [SparseMatrix](#SparseMatrix)
Generate a matrix from a JSON object
**Kind**: static method of [SparseMatrix](#SparseMatrix)
| Param | Type | Description |
| --- | --- | --- |
| json | Object | An object structured like `{"mathjs": "SparseMatrix", "values": [], "index": [], "ptr": [], "size": []}`, where mathjs is optional |
### SparseMatrix.diagonal(size, value, [k], [datatype]) ⇒ [SparseMatrix](#SparseMatrix)
Create a diagonal matrix.
**Kind**: static method of [SparseMatrix](#SparseMatrix)
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| size | Array | | The matrix size. |
| value | number | Array | Matrix | | The values for the diagonal. |
| [k] | number | BigNumber | 0 | The kth diagonal where the vector will be filled in. |
| [datatype] | string | | The Matrix datatype, values must be of this datatype. |