Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
Convert an ndarray to an object supporting fancy indexing.
License
stdlib-js/ndarray-to-fancy
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out onGitHub, and please considerfinancially supporting stdlib. We greatly appreciate your continued support!
Convert an ndarray to an object supporting fancy indexing.
A fancy ndarray is anndarray which supports slicing via indexing expressions.
importndarray2arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-to-array@esm/index.mjs';importndarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-ctor@esm/index.mjs';// Create a plain ndarray:varbuffer=[1,2,3,4,5,6];varx=newndarray('generic',buffer,[6],[1],0,'row-major');// returns <ndarray>// Convert to a fancy ndarray:vary=ndarray2fancy(x);// Select the first 3 elements:varz=y[':3'];// returns <ndarray>vararr=ndarray2array(z);// returns [ 1, 2, 3 ]// Select every other element, starting with the second element:z=y['1::2'];// returns <ndarray>arr=ndarray2array(z);// returns [ 2, 4, 6 ]// Reverse the array, starting with last element and skipping every other element:z=y['::-2'];// returns <ndarray>arr=ndarray2array(z);// returns [ 6, 4, 2 ]
importndarray2fancyfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-to-fancy@esm/index.mjs';
You can also import the following named exports from the package:
import{factory,idx}from'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-to-fancy@esm/index.mjs';
Converts anndarray to an object supporting fancy indexing.
console.log('TODO');
The function supports the following options:
cache: cache for resolving ndarray index objects. Must have a
getmethod which accepts a single argument: a string identifier associated with an ndarray index.If an ndarray index associated with a provided identifier exists, the
getmethod should return an object having the following properties:- data: the underlying index ndarray.
- type: the index type. Must be either
'mask','bool', or'int'. - kind: the index kind. Must be either
'','cartesian', or'linear'. - dtype: thedata type of the underlying ndarray.
If an ndarray index is not associated with a provided identifier, the
getmethod should returnnull.Default:
ndindex.strict: boolean indicating whether to enforce strict bounds checking. Default:
false.
By default, the function returns a fancy ndarray which doesnot enforce strict bounds checking. For example,
console.log('TODO');
To enforce strict bounds checking, set thestrict option totrue.
console.log('TODO');
Returns a function for converting anndarray to an object supporting fancy indexing.
varfcn=ndarray2fancy.factory();console.log('TODO');
The function supports the following options:
cache: default cache for resolving ndarray index objects. Must have a
getmethod which accepts a single argument: a string identifier associated with an ndarray index.If an ndarray index associated with a provided identifier exists, the
getmethod should return an object having the following properties:- data: the underlying index ndarray.
- type: the index type. Must be either
'mask','bool', or'int'. - kind: the index kind. Must be either
'','cartesian', or'linear'. - dtype: thedata type of the underlying ndarray.
If an ndarray index is not associated with a provided identifier, the
getmethod should returnnull.Default:
ndindex.strict: boolean indicating whether to enforce strict bounds checking by default. Default:
false.
By default, the function returns a function which, by default, doesnot enforce strict bounds checking. For example,
varfcn=ndarray2fancy.factory();console.log('TODO');
To enforce strict bounds checking by default, set thestrict option totrue.
varfcn=ndarray2fancy.factory({'strict':true});console.log('TODO');
The returned function supports the same options as above. When the returned function is provided option values, those values override the factory method defaults.
Wraps a provided ndarray as an ndarray index object.
console.log('TODO');
For documentation and usage, seendindex.
- A fancy ndarray shares thesame data as the provided inputndarray. Hence, any mutations to the returned ndarray will affect the underlying input ndarray and vice versa.
- For operations returning a new ndarray (e.g., when slicing or invoking an instance method), a fancy ndarray returns a new fancy ndarray having the same configuration as specified by
options. - A fancy ndarray supports indexing using positive and negative integers (both numeric literals and strings),
SliceandMultiSliceinstances,subsequence expressions, andindex arrays (boolean, mask, and integer). - A fancy ndarray supports all properties and methods of the input ndarray, and, thus, a fancy ndarray can be consumed by any API which supports ndarray-like objects.
- Indexing expressions provide a convenient and powerful means for creating and operating on ndarray views; however, their use does entail a performance cost. Indexing expressions are best suited for interactive use (e.g., in theREPL) and scripting. For performance critical applications, prefer equivalent functional APIs supporting ndarray-like objects.
- In older JavaScript environments which donot support
Proxyobjects, the use of indexing expressions isnot supported.
// TODO: see array/to-fancy
// TODO: only applies to non-zero-dimensional ndarrays. In non-strict mode, out-of-bounds indices returnundefined and fail to assign.
// TODO: see array/to-fancy
// TODO: see array/to-fancy
<!DOCTYPE html><htmllang="en"><body><scripttype="module">importSfrom'https://cdn.jsdelivr.net/gh/stdlib-js/slice-ctor@esm/index.mjs';importEfrom'https://cdn.jsdelivr.net/gh/stdlib-js/slice-multi@esm/index.mjs';importtoArrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-to-array@esm/index.mjs';importndarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-ctor@esm/index.mjs';importndarray2fancyfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-to-fancy@esm/index.mjs';varbuffer=[1,2,3,4,// 05,6,// 17,8,// 29,10];varshape=[3,2];varstrides=[2,1];varoffset=2;// Create a normal ndarray:varx=newndarray('generic',buffer,shape,strides,offset,'row-major');// returns <ndarray>// Convert to a fancy ndarray:vary=ndarray2fancy(x);// Access an ndarray property:varndims=y.ndims;// returns 2// Retrieve an ndarray element:varv=y.get(2,1);// returns 8// Set an ndarray element:y.set(2,1,20);v=y.get(2,1);// returns 20// Create an alias for `undefined` for more concise slicing expressions:var_=void0;// Create a multi-dimensional slice:vars=E(S(0,_,2),_);// returns <MultiSlice>// Use the slice to create a view on the original ndarray:vary1=y[s];console.log(toArray(y1));// => [ [ 3, 4 ], [ 7, 20 ] ]// Use alternative syntax:vary2=y[[S(0,_,2),_]];console.log(toArray(y2));// => [ [ 3, 4 ], [ 7, 20 ] ]// Use alternative syntax:vary3=y['0::2,:'];console.log(toArray(y3));// => [ [ 3, 4 ], [ 7, 20 ] ]// Flip dimensions:vary4=y[[S(_,_,-2),S(_,_,-1)]];console.log(toArray(y4));// => [ [ 20, 7 ], [ 4, 3 ] ]</script></body></html>
@stdlib/array-to-fancy:convert an array to an object supporting fancy indexing.@stdlib/ndarray-array:multidimensional arrays.@stdlib/ndarray-ctor:multidimensional array constructor.@stdlib/ndarray-fancy:fancy multidimensional array constructor.
This package is part ofstdlib, a standard library with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to developstdlib, see the main projectrepository.
SeeLICENSE.
Copyright © 2016-2025. The StdlibAuthors.
About
Convert an ndarray to an object supporting fancy indexing.
Topics
Resources
License
Code of conduct
Contributing
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Sponsor this project
Uh oh!
There was an error while loading.Please reload this page.
Packages0
Uh oh!
There was an error while loading.Please reload this page.