View one-dimensional array data, typed array data and/or multi-dimensional array data as multidimensional tensors of various shapes efficiently.
version: 2.1.3 (13 kB minified)
TensorView is both memory-efficient and speed-efficient since it only creates ways to view array data as multidimensional tensors without actually creating new arrays. One can nevertheless explicitly store a TensorView instance as a single-dimensional or multi-dimensional array using view.toArray() or view.toNDArray() methods.
Example (see /test/demo.js)
constTensorView=require('../src/TensorView.js');constarray=[1,2,3,4,5,6,7,8,9,0];// single-dimensional dataconstndarray=[[1,2,3,4,5],[6,7,8,9,0]];// multi-dimensional dataconsts=TensorView(array,{shape:[2,5]});// create a view with shapeconstsT=s.transpose();// get transposed viewconsole.log(s.toNDArray());console.log(s.toArray());console.log(sT.toNDArray());console.log(sT.toArray());console.log(s.data===sT.data)// uses same dataconstm=TensorView(ndarray,{shape:[5,2]});// create view of ndarray with different shapeconstm2=m.reshape([2,5]);// reshapeconstm3=m2.slice(':','1:2');// get a sliceconsole.log(m.toNDArray());console.log(m.toArray());console.log(m2.toNDArray());console.log(m2.toArray());console.log(m3.toNDArray());console.log(m3.toArray());console.log(m.data===m2.data,m.data===m3.data)// uses same data// iterator protocolfor(let[item,index]ofs)console.log([item,index.slice()]);// index is array of multidimensional indices// same ass.forEach((item,index)=>console.log([item,index.slice()]));// index is array of multidimensional indicesOutput
[ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8, 9, 0 ] ]
[
1, 2, 3, 4, 5,
6, 7, 8, 9, 0
]
[ [ 1, 6 ], [ 2, 7 ], [ 3, 8 ], [ 4, 9 ], [ 5, 0 ] ]
[
1, 6, 2, 7, 3,
8, 4, 9, 5, 0
]
true
[ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 0 ] ]
[
1, 2, 3, 4, 5,
6, 7, 8, 9, 0
]
[ [ 1, 2, 3, 4, 5 ], [ 6, 7, 8, 9, 0 ] ]
[
1, 2, 3, 4, 5,
6, 7, 8, 9, 0
]
[ [ 2, 3 ], [ 7, 8 ] ]
[ 2, 3, 7, 8 ]
true true
[ 1, [ 0, 0 ] ]
[ 2, [ 0, 1 ] ]
[ 3, [ 0, 2 ] ]
[ 4, [ 0, 3 ] ]
[ 5, [ 0, 4 ] ]
[ 6, [ 1, 0 ] ]
[ 7, [ 1, 1 ] ]
[ 8, [ 1, 2 ] ]
[ 9, [ 1, 3 ] ]
[ 0, [ 1, 4 ] ]
[ 1, [ 0, 0 ] ]
[ 2, [ 0, 1 ] ]
[ 3, [ 0, 2 ] ]
[ 4, [ 0, 3 ] ]
[ 5, [ 0, 4 ] ]
[ 6, [ 1, 0 ] ]
[ 7, [ 1, 1 ] ]
[ 8, [ 1, 2 ] ]
[ 9, [ 1, 3 ] ]
[ 0, [ 1, 4 ] ]
Methods:
// data=single value or single-dimensional array or typed array or multi-dimensional array// options={shape?:Array, stride?:Array}// shape array defines desired shape of view (optional)// stride array defines strides for each dimension of view (optional)constview=TensorView(data,options);// underlying data of viewconstdata=view.data;// dimension of view, eg 1 for 1d, 2 for 2d, 3 for 3d, ..constdim=view.dimension;// actual length of view (eg if saved as array)constlength=view.length;// shape array of view along all dimensionsconstshape=view.shape();// shape of view along `axis` dimensionconstshapeForAxis=view.shape(axis);// stride array of view along all dimensionsconststride=view.stride();// stride for `axis` dimensionconststrideForAxis=view.stride(axis);// create single-dimensional array or typed array from viewconstarray=view.toArray(ArrayClass=Array);// create multi-dimensional array from view having the same shapeconstndarray=view.toNDArray();// render view to string,// maxSize defines max number of items to display along a dimension (default Infinity)// stringify defines custom stringifier function (default toString)conststring=view.toString(maxSize=Infinity,stringify=String);// transposed viewconsttransposed=view.transpose();// view with different shapeconstreshaped=view.reshape(new_shape);// view with permuted dimensionsconstpermuted=view.permute(permutation);// view with different in/out orderconstreordered=view.reorder(new_in_order,new_out_order);// sliced view with whole axis,// or only a and b indices,// or from indices a to b (included),// or from indices a to b (included) with step s,// etc..constsliced=view.slice(":","a,b,..","a:b","a:s:b"/*, ..*/);// concatenate multiple views along on_axis axis or "newaxis"constconcatenated=view.concat([view2,view3/*, ..*/],on_axis=0);// get view with any dimension along some axis (after start_axis) of length 1 removedconstsqueezed=view.squeeze(start_axis=0);// get value based on multidimensional indices of same dimension as view shapeconstvalue=view.get(indices);// set value at multidimensional indicesview.set(indices,value);// NOTE: underlying data will change in all views which use this data and all views which depend on views which use this data// set whole view from another array or viewview.setFrom(other);// NOTE: underlying data will change in all views which use this data and all views which depend on views which use this data// forEach method (forward or reverse direction based on `dir` 1 or -1)view.forEach(function(item,index,data,view){/*..*/},dir=1);// similar as iterator protocolfor(let[item,index]ofview){/*..*/}// map method (forward or reverse direction based on `dir` 1 or -1)// returns view of same shapeotherview=view.map(function(item,index,data,view){/*..*/},dir=1);// filter method (forward or reverse direction based on `dir` 1 or -1)// returns single dimensional viewotherview=view.filter(function(item,index,data,view){/*..*/},dir=1);// creating an actual copy and not share data is easy to do in various ways, eg:constviewcopy=TensorView(view.toArray(),{shape: view.shape()});// dispose view if no longer neededview.dispose();// NOTE: will affect any other active views which depend on this view (eg concatenated views, sliced views, ..), so take notesee also:
- Abacus Computer Algebra and Symbolic Computation System for Combinatorics and Algebraic Number Theory for JavaScript and Python
- SciLite Scientific Computing Environment similar to Octave/Matlab/SciPy in pure JavaScript
- TensorView view array data as multidimensional tensors of various shapes efficiently
- FILTER.js video and image processing and computer vision Library in pure JavaScript (browser and nodejs)
- HAAR.js image feature detection based on Haar Cascades in JavaScript (Viola-Jones-Lienhart et al Algorithm)
- HAARPHP image feature detection based on Haar Cascades in PHP (Viola-Jones-Lienhart et al Algorithm)
- MOD3 3D Modifier Library in JavaScript
- Geometrize Computational Geometry and Rendering Library for JavaScript
- Fuzzion a library of fuzzy / approximate string metrics for PHP, JavaScript, Python
- Matchy a library of string matching algorithms for PHP, JavaScript, Python
- Regex Analyzer/Composer Regular Expression Analyzer and Composer for PHP, JavaScript, Python
- PatternMatchingAlgorithms library of Pattern Matching Algorithms in JavaScript using Matchy
- SortingAlgorithms library of Sorting Algorithms in JavaScript
- Xpresion a simple and flexible eXpression parser engine (with custom functions and variables support), based on GrammarTemplate, for PHP, JavaScript, Python
- GrammarTemplate grammar-based templating for PHP, JavaScript, Python
- codemirror-grammar transform a formal grammar in JSON format into a syntax-highlight parser for CodeMirror editor
- ace-grammar transform a formal grammar in JSON format into a syntax-highlight parser for ACE editor
- prism-grammar transform a formal grammar in JSON format into a syntax-highlighter for Prism code highlighter
- highlightjs-grammar transform a formal grammar in JSON format into a syntax-highlight mode for Highlight.js code highlighter
- syntaxhighlighter-grammar transform a formal grammar in JSON format to a highlight brush for SyntaxHighlighter code highlighter
