simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

sparsematrix.md (11340B)


      1 <a name="SparseMatrix"></a>
      2 ## SparseMatrix
      3 Sparse Matrix implementation. This type implements a Compressed Column Storage format
      4 for sparse matrices.
      5 
      6 * _instance_
      7 	* [.storage()](#SparseMatrix+storage) ⇒ <code>string</code>
      8 	* [.datatype()](#SparseMatrix+datatype) ⇒ <code>string</code>
      9 	* [.create(data, [datatype])](#SparseMatrix+create)
     10 	* [.density()](#SparseMatrix+density) ⇒ <code>number</code>
     11 	* [.subset(index, [replacement], [defaultValue])](#SparseMatrix+subset)
     12 	* [.get(index)](#SparseMatrix+get) ⇒ <code>\*</code>
     13 	* [.set(index, value, [defaultValue])](#SparseMatrix+set) ⇒ <code>[SparseMatrix](#SparseMatrix)</code>
     14 	* [.resize(size, [defaultValue], [copy])](#SparseMatrix+resize) ⇒ <code>Matrix</code>
     15 	* [.clone()](#SparseMatrix+clone) ⇒ <code>[SparseMatrix](#SparseMatrix)</code>
     16 	* [.size()](#SparseMatrix+size) ⇒ <code>Array.&lt;number&gt;</code>
     17 	* [.map(callback, [skipZeros])](#SparseMatrix+map) ⇒ <code>[SparseMatrix](#SparseMatrix)</code>
     18 	* [.forEach(callback, [skipZeros])](#SparseMatrix+forEach)
     19 	* [.toArray()](#SparseMatrix+toArray) ⇒ <code>Array</code>
     20 	* [.valueOf()](#SparseMatrix+valueOf) ⇒ <code>Array</code>
     21 	* [.format([options])](#SparseMatrix+format) ⇒ <code>string</code>
     22 	* [.toString()](#SparseMatrix+toString) ⇒ <code>string</code>
     23 	* [.toJSON()](#SparseMatrix+toJSON) ⇒ <code>Object</code>
     24 	* [.diagonal([k])](#SparseMatrix+diagonal) ⇒ <code>Matrix</code>
     25 	* [.swapRows(i, j)](#SparseMatrix+swapRows) ⇒ <code>Matrix</code>
     26 * _static_
     27 	* [.fromJSON(json)](#SparseMatrix.fromJSON) ⇒ <code>[SparseMatrix](#SparseMatrix)</code>
     28 	* [.diagonal(size, value, [k], [datatype])](#SparseMatrix.diagonal) ⇒ <code>[SparseMatrix](#SparseMatrix)</code>
     29 
     30 <a name="SparseMatrix+storage"></a>
     31 ### sparseMatrix.storage() ⇒ <code>string</code>
     32 Get the storage format used by the matrix.
     33 
     34 Usage:
     35 ```js
     36 const format = matrix.storage()  // retrieve storage format
     37 ```
     38 
     39 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
     40 **Returns**: <code>string</code> - The storage format.  
     41 <a name="SparseMatrix+datatype"></a>
     42 ### sparseMatrix.datatype() ⇒ <code>string</code>
     43 Get the datatype of the data stored in the matrix.
     44 
     45 Usage:
     46 ```js
     47 const format = matrix.datatype()  // retrieve matrix datatype
     48 ```
     49 
     50 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
     51 **Returns**: <code>string</code> - The datatype.  
     52 <a name="SparseMatrix+create"></a>
     53 ### sparseMatrix.create(data, [datatype])
     54 Create a new SparseMatrix
     55 
     56 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
     57 
     58 | Param | Type |
     59 | --- | --- |
     60 | data | <code>Array</code> | 
     61 | [datatype] | <code>string</code> | 
     62 
     63 <a name="SparseMatrix+density"></a>
     64 ### sparseMatrix.density() ⇒ <code>number</code>
     65 Get the matrix density.
     66 
     67 Usage:
     68 ```js
     69 const density = matrix.density()  // retrieve matrix density
     70 ```
     71 
     72 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
     73 **Returns**: <code>number</code> - The matrix density.  
     74 <a name="SparseMatrix+subset"></a>
     75 ### sparseMatrix.subset(index, [replacement], [defaultValue])
     76 Get a subset of the matrix, or replace a subset of the matrix.
     77 
     78 Usage:
     79 ```js
     80 const subset = matrix.subset(index)               // retrieve subset
     81 const value = matrix.subset(index, replacement)   // replace subset
     82 ```
     83 
     84 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
     85 
     86 | Param | Type | Default | Description |
     87 | --- | --- | --- | --- |
     88 | index | <code>Index</code> |  |  |
     89 | [replacement] | <code>Array</code> &#124; <code>Maytrix</code> &#124; <code>\*</code> |  |  |
     90 | [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. |
     91 
     92 <a name="SparseMatrix+get"></a>
     93 ### sparseMatrix.get(index) ⇒ <code>\*</code>
     94 Get a single element from the matrix.
     95 
     96 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
     97 **Returns**: <code>\*</code> - value  
     98 
     99 | Param | Type | Description |
    100 | --- | --- | --- |
    101 | index | <code>Array.&lt;number&gt;</code> | Zero-based index |
    102 
    103 <a name="SparseMatrix+set"></a>
    104 ### sparseMatrix.set(index, value, [defaultValue]) ⇒ <code>[SparseMatrix](#SparseMatrix)</code>
    105 Replace a single element in the matrix.
    106 
    107 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    108 **Returns**: <code>[SparseMatrix](#SparseMatrix)</code> - self  
    109 
    110 | Param | Type | Description |
    111 | --- | --- | --- |
    112 | index | <code>Array.&lt;number&gt;</code> | Zero-based index |
    113 | value | <code>\*</code> |  |
    114 | [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. |
    115 
    116 <a name="SparseMatrix+resize"></a>
    117 ### sparseMatrix.resize(size, [defaultValue], [copy]) ⇒ <code>Matrix</code>
    118 Resize the matrix to the given size. Returns a copy of the matrix when 
    119 `copy=true`, otherwise return the matrix itself (resize in place).
    120 
    121 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    122 **Returns**: <code>Matrix</code> - The resized matrix  
    123 
    124 | Param | Type | Default | Description |
    125 | --- | --- | --- | --- |
    126 | size | <code>Array.&lt;number&gt;</code> |  | The new size the matrix should have. |
    127 | [defaultValue] | <code>\*</code> | <code>0</code> | Default value, filled in on new entries.                                  If not provided, the matrix elements will                                  be filled with zeros. |
    128 | [copy] | <code>boolean</code> |  | Return a resized copy of the matrix |
    129 
    130 <a name="SparseMatrix+clone"></a>
    131 ### sparseMatrix.clone() ⇒ <code>[SparseMatrix](#SparseMatrix)</code>
    132 Create a clone of the matrix
    133 
    134 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    135 **Returns**: <code>[SparseMatrix](#SparseMatrix)</code> - clone  
    136 <a name="SparseMatrix+size"></a>
    137 ### sparseMatrix.size() ⇒ <code>Array.&lt;number&gt;</code>
    138 Retrieve the size of the matrix.
    139 
    140 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    141 **Returns**: <code>Array.&lt;number&gt;</code> - size  
    142 <a name="SparseMatrix+map"></a>
    143 ### sparseMatrix.map(callback, [skipZeros]) ⇒ <code>[SparseMatrix](#SparseMatrix)</code>
    144 Create a new matrix with the results of the callback function executed on
    145 each entry of the matrix.
    146 
    147 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    148 **Returns**: <code>[SparseMatrix](#SparseMatrix)</code> - matrix  
    149 
    150 | Param | Type | Description |
    151 | --- | --- | --- |
    152 | 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. |
    153 | [skipZeros] | <code>boolean</code> | Invoke callback function for non-zero values only. |
    154 
    155 <a name="SparseMatrix+forEach"></a>
    156 ### sparseMatrix.forEach(callback, [skipZeros])
    157 Execute a callback function on each entry of the matrix.
    158 
    159 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    160 
    161 | Param | Type | Description |
    162 | --- | --- | --- |
    163 | 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. |
    164 | [skipZeros] | <code>boolean</code> | Invoke callback function for non-zero values only. |
    165 
    166 <a name="SparseMatrix+toArray"></a>
    167 ### sparseMatrix.toArray() ⇒ <code>Array</code>
    168 Create an Array with a copy of the data of the SparseMatrix
    169 
    170 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    171 **Returns**: <code>Array</code> - array  
    172 <a name="SparseMatrix+valueOf"></a>
    173 ### sparseMatrix.valueOf() ⇒ <code>Array</code>
    174 Get the primitive value of the SparseMatrix: a two dimensions array
    175 
    176 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    177 **Returns**: <code>Array</code> - array  
    178 <a name="SparseMatrix+format"></a>
    179 ### sparseMatrix.format([options]) ⇒ <code>string</code>
    180 Get a string representation of the matrix, with optional formatting options.
    181 
    182 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    183 **Returns**: <code>string</code> - str  
    184 
    185 | Param | Type | Description |
    186 | --- | --- | --- |
    187 | [options] | <code>Object</code> &#124; <code>number</code> &#124; <code>function</code> | Formatting options. See                                                lib/utils/number:format for a                                                description of the available                                                options. |
    188 
    189 <a name="SparseMatrix+toString"></a>
    190 ### sparseMatrix.toString() ⇒ <code>string</code>
    191 Get a string representation of the matrix
    192 
    193 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    194 **Returns**: <code>string</code> - str  
    195 <a name="SparseMatrix+toJSON"></a>
    196 ### sparseMatrix.toJSON() ⇒ <code>Object</code>
    197 Get a JSON representation of the matrix
    198 
    199 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    200 <a name="SparseMatrix+diagonal"></a>
    201 ### sparseMatrix.diagonal([k]) ⇒ <code>Matrix</code>
    202 Get the kth Matrix diagonal.
    203 
    204 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    205 **Returns**: <code>Matrix</code> - The matrix vector with the diagonal values.  
    206 
    207 | Param | Type | Default | Description |
    208 | --- | --- | --- | --- |
    209 | [k] | <code>number</code> &#124; <code>BigNumber</code> | <code>0</code> | The kth diagonal where the vector will retrieved. |
    210 
    211 <a name="SparseMatrix+swapRows"></a>
    212 ### sparseMatrix.swapRows(i, j) ⇒ <code>Matrix</code>
    213 Swap rows i and j in Matrix.
    214 
    215 **Kind**: instance method of <code>[SparseMatrix](#SparseMatrix)</code>  
    216 **Returns**: <code>Matrix</code> - The matrix reference  
    217 
    218 | Param | Type | Description |
    219 | --- | --- | --- |
    220 | i | <code>number</code> | Matrix row index 1 |
    221 | j | <code>number</code> | Matrix row index 2 |
    222 
    223 <a name="SparseMatrix.fromJSON"></a>
    224 ### SparseMatrix.fromJSON(json) ⇒ <code>[SparseMatrix](#SparseMatrix)</code>
    225 Generate a matrix from a JSON object
    226 
    227 **Kind**: static method of <code>[SparseMatrix](#SparseMatrix)</code>  
    228 
    229 | Param | Type | Description |
    230 | --- | --- | --- |
    231 | json | <code>Object</code> | An object structured like                       `{"mathjs": "SparseMatrix", "values": [], "index": [], "ptr": [], "size": []}`,                       where mathjs is optional |
    232 
    233 <a name="SparseMatrix.diagonal"></a>
    234 ### SparseMatrix.diagonal(size, value, [k], [datatype]) ⇒ <code>[SparseMatrix](#SparseMatrix)</code>
    235 Create a diagonal matrix.
    236 
    237 **Kind**: static method of <code>[SparseMatrix](#SparseMatrix)</code>  
    238 
    239 | Param | Type | Default | Description |
    240 | --- | --- | --- | --- |
    241 | size | <code>Array</code> |  | The matrix size. |
    242 | value | <code>number</code> &#124; <code>Array</code> &#124; <code>Matrix</code> |  | The values for the diagonal. |
    243 | [k] | <code>number</code> &#124; <code>BigNumber</code> | <code>0</code> | The kth diagonal where the vector will be filled in. |
    244 | [datatype] | <code>string</code> |  | The Matrix datatype, values must be of this datatype. |
    245