68 lines
2.0 KiB
Markdown

---
layout: default
---
<!-- Note: This file is automatically generated from source code comments. Changes made in this file will be overridden. -->
<h1 id="function-sparse">Function sparse <a href="#function-sparse" title="Permalink">#</a></h1>
Create a Sparse Matrix. The function creates a new `math.Matrix` object from
an `Array`. A Matrix has utility functions to manipulate the data in the
matrix, like getting the size and getting or setting values in the matrix.
Note that a Sparse Matrix is always 2-dimensional, so for example if
you create one from a plain array of _n_ numbers, you get an _n_ by 1
Sparse "column vector".
<h2 id="syntax">Syntax <a href="#syntax" title="Permalink">#</a></h2>
```js
math.sparse() // creates an empty sparse matrix.
math.sparse(data) // creates a sparse matrix with initial data.
math.sparse(data, 'number') // creates a sparse matrix with initial data, number datatype.
```
<h3 id="parameters">Parameters <a href="#parameters" title="Permalink">#</a></h3>
Parameter | Type | Description
--------- | ---- | -----------
`data` | Array &#124; Matrix | A two dimensional array
<h3 id="returns">Returns <a href="#returns" title="Permalink">#</a></h3>
Type | Description
---- | -----------
Matrix | The created matrix
<h3 id="throws">Throws <a href="#throws" title="Permalink">#</a></h3>
Type | Description
---- | -----------
<h2 id="examples">Examples <a href="#examples" title="Permalink">#</a></h2>
```js
let m = math.sparse([[1, 2], [3, 4]])
m.size() // Array [2, 2]
m.resize([3, 2], 5)
m.valueOf() // Array [[1, 2], [3, 4], [5, 5]]
m.get([1, 0]) // number 3
let v = math.sparse([0, 0, 1])
v.size() // Array [3, 1]
v.get([2, 0]) // number 1
```
<h2 id="see-also">See also <a href="#see-also" title="Permalink">#</a></h2>
[bignumber](bignumber.html),
[boolean](boolean.html),
[complex](complex.html),
[index](index.html),
[number](number.html),
[string](string.html),
[unit](unit.html),
[matrix](matrix.html)