Back to Mathjs

Sparsematrix

docs/reference/classes/sparsematrix.md

15.2.011.1 KB
Original Source

<a name="SparseMatrix"></a>

SparseMatrix

Sparse Matrix implementation. This type implements a Compressed Column Storage format for sparse matrices.

<a name="SparseMatrix+storage"></a>

sparseMatrix.storage() ⇒ <code>string</code>

Get the storage format used by the matrix.

Usage:

js
const format = matrix.storage()  // retrieve storage format

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>string</code> - The storage format.
<a name="SparseMatrix+datatype"></a>

sparseMatrix.datatype() ⇒ <code>string</code>

Get the datatype of the data stored in the matrix.

Usage:

js
const format = matrix.datatype()  // retrieve matrix datatype

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>string</code> - The datatype.
<a name="SparseMatrix+create"></a>

sparseMatrix.create(data, [datatype])

Create a new SparseMatrix

Kind: instance method of <code>SparseMatrix</code>

ParamType
data<code>Array</code>
[datatype]<code>string</code>

<a name="SparseMatrix+density"></a>

sparseMatrix.density() ⇒ <code>number</code>

Get the matrix density.

Usage:

js
const density = matrix.density()  // retrieve matrix density

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>number</code> - The matrix density.
<a name="SparseMatrix+subset"></a>

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 <code>SparseMatrix</code>

ParamTypeDefaultDescription
index<code>Index</code>
[replacement]<code>Array</code> | <code>Maytrix</code> | <code>*</code>
[defaultValue]<code>*</code><code>0</code>Default value, filled in on new entries when the matrix is resized. If not provided, new matrix elements will be filled with zeros.

<a name="SparseMatrix+get"></a>

sparseMatrix.get(index) ⇒ <code>*</code>

Get a single element from the matrix.

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>*</code> - value

ParamTypeDescription
index<code>Array.<number></code>Zero-based index

<a name="SparseMatrix+set"></a>

sparseMatrix.set(index, value, [defaultValue]) ⇒ <code>SparseMatrix</code>

Replace a single element in the matrix.

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>SparseMatrix</code> - self

ParamTypeDescription
index<code>Array.<number></code>Zero-based index
value<code>*</code>
[defaultValue]<code>*</code>Default value, filled in on new entries when the matrix is resized. If not provided, new matrix elements will be set to zero.

<a name="SparseMatrix+resize"></a>

sparseMatrix.resize(size, [defaultValue], [copy]) ⇒ <code>Matrix</code>

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 <code>SparseMatrix</code>
Returns: <code>Matrix</code> - The resized matrix

ParamTypeDefaultDescription
size<code>Array.<number></code>The new size the matrix should have.
[defaultValue]<code>*</code><code>0</code>Default value, filled in on new entries. If not provided, the matrix elements will be filled with zeros.
[copy]<code>boolean</code>Return a resized copy of the matrix

<a name="SparseMatrix+clone"></a>

sparseMatrix.clone() ⇒ <code>SparseMatrix</code>

Create a clone of the matrix

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>SparseMatrix</code> - clone
<a name="SparseMatrix+size"></a>

sparseMatrix.size() ⇒ <code>Array.<number></code>

Retrieve the size of the matrix.

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>Array.<number></code> - size
<a name="SparseMatrix+map"></a>

sparseMatrix.map(callback, [skipZeros]) ⇒ <code>SparseMatrix</code>

Create a new matrix with the results of the callback function executed on each entry of the matrix.

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>SparseMatrix</code> - matrix

ParamTypeDescription
callback<code>function</code>The callback function is invoked with three parameters: the value of the element, the index of the element, and the Matrix being traversed.
[skipZeros]<code>boolean</code>Invoke callback function for non-zero values only.

<a name="SparseMatrix+forEach"></a>

sparseMatrix.forEach(callback, [skipZeros])

Execute a callback function on each entry of the matrix.

Kind: instance method of <code>SparseMatrix</code>

ParamTypeDescription
callback<code>function</code>The callback function is invoked with three parameters: the value of the element, the index of the element, and the Matrix being traversed.
[skipZeros]<code>boolean</code>Invoke callback function for non-zero values only.

<a name="SparseMatrix+toArray"></a>

sparseMatrix.toArray() ⇒ <code>Array</code>

Create an Array with a copy of the data of the SparseMatrix

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>Array</code> - array
<a name="SparseMatrix+valueOf"></a>

sparseMatrix.valueOf() ⇒ <code>Array</code>

Get the primitive value of the SparseMatrix: a two dimensions array

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>Array</code> - array
<a name="SparseMatrix+format"></a>

sparseMatrix.format([options]) ⇒ <code>string</code>

Get a string representation of the matrix, with optional formatting options.

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>string</code> - str

ParamTypeDescription
[options]<code>Object</code> | <code>number</code> | <code>function</code>Formatting options. See lib/utils/number:format for a description of the available options.

<a name="SparseMatrix+toString"></a>

sparseMatrix.toString() ⇒ <code>string</code>

Get a string representation of the matrix

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>string</code> - str
<a name="SparseMatrix+toJSON"></a>

sparseMatrix.toJSON() ⇒ <code>Object</code>

Get a JSON representation of the matrix

Kind: instance method of <code>SparseMatrix</code>
<a name="SparseMatrix+diagonal"></a>

sparseMatrix.diagonal([k]) ⇒ <code>Matrix</code>

Get the kth Matrix diagonal.

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>Matrix</code> - The matrix vector with the diagonal values.

ParamTypeDefaultDescription
[k]<code>number</code> | <code>BigNumber</code><code>0</code>The kth diagonal where the vector will retrieved.

<a name="SparseMatrix+swapRows"></a>

sparseMatrix.swapRows(i, j) ⇒ <code>Matrix</code>

Swap rows i and j in Matrix.

Kind: instance method of <code>SparseMatrix</code>
Returns: <code>Matrix</code> - The matrix reference

ParamTypeDescription
i<code>number</code>Matrix row index 1
j<code>number</code>Matrix row index 2

<a name="SparseMatrix.fromJSON"></a>

SparseMatrix.fromJSON(json) ⇒ <code>SparseMatrix</code>

Generate a matrix from a JSON object

Kind: static method of <code>SparseMatrix</code>

ParamTypeDescription
json<code>Object</code>An object structured like {"mathjs": "SparseMatrix", "values": [], "index": [], "ptr": [], "size": []}, where mathjs is optional

<a name="SparseMatrix.diagonal"></a>

SparseMatrix.diagonal(size, value, [k], [datatype]) ⇒ <code>SparseMatrix</code>

Create a diagonal matrix.

Kind: static method of <code>SparseMatrix</code>

ParamTypeDefaultDescription
size<code>Array</code>The matrix size.
value<code>number</code> | <code>Array</code> | <code>Matrix</code>The values for the diagonal.
[k]<code>number</code> | <code>BigNumber</code><code>0</code>The kth diagonal where the vector will be filled in.
[datatype]<code>string</code>The Matrix datatype, values must be of this datatype.