numas.js
numas.js
is JavaScript numeric library implementing interface for numas
(numas.wasm
).
Usage
Initialize numas
It's important to initialize numas first by calling init
const numas init =
It's possible to set configuration for initialization, by default it is set to numas.config.defaultNode
.
Initializing array
Example of initializing array
// creates two dimensional array of 6 elements of type i32let array = numas // creates two dimensional array of 9 elements filled with value 5let filled = numasfactory // creates three dimensional array of 8 elements filled with zeroslet ones = numasfactory // etc.
Array can be also initialized by performing some operations like add, mul, sub, div, eq, neq, le, ge, etc.
let first = numaslet second = numas // performs addition of two arrays resulting in new array with elements [2, 4]let third = first
Collecting array
To get values from array you need to perform collecting
let array = numas// ... some operationslet jsArray = array // Returns typed array [4, 5] (second row, first and second column)
Garbage collection
To free up WebAssembly memory you need to call free
method on instance of NDArray
let array = numasarray// Memory is free, do NOT use array instance...
Because that might be hard to manage with builder patter, there is checkpoint system. Checkpoint system can free up arrays created in given checkpoint.
// By default checkpoint id is 0let array1 = numas numascleanerlet array2 = numas// Frees array2numascleaner let checkpointId = numascleaner let array3 = numaslet array4 = numas // end checkpoint, dont free memorynumascleanerlet array5 = array3 // Frees array3 and array4// this way we can use result of some operations outside of checkpoint scopenumascleaner // Frees array1 and array5numascleaner