Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork0
stdlib-js/ndarray-index
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!
ndarray index constructor.
In JavaScript, only strings and symbols are valid property names. When providing values for property names which are not strings or symbols, the values are serialized to stringsprior to attempting to access property values. For example, the following
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';// Create an ndarray:varx=array(newInt32Array([1,2,3,4]));// Define a list of indices for elements we want to retrieve from `x`:vary=[0,2];// Attempt to retrieve the desired elements:varv=x[y];// => desired: <ndarray>[ 1, 3 ]// returns undefined
is equivalent to
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));vary=[0,2];varv=x[y.toString()];// returns undefined// ...which is equivalent to:v=x['0,2'];// returns undefined
Accordingly, in order to circumvent built-in property access behavior and support non-traditional access patterns, one can leverageProxy objects which allow one to intercept property access and to perform transformations before attempting to access elements in a target object.
To support the access pattern shown in the example above, one can leverage built-in string serialization behavior to reconstruct the original property value provided prior to serialization. Thendindex constructor described below provides one such mechanism.
Specifically, instantiatedndindex objects are assigned a unique identifier and stored in a local cache. When provided as property values tondindex consumers, instantiated objects serialize to a string containing their unique identifier.ndindex consumers can then parse the serialized string to obtain the unique identifier and subsequently recover the originalndarray from the local cache.
importndindexfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-index@esm/index.mjs';
You can also import the following named exports from the package:
import{cartesianIndex,linearIndex}from'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-index@esm/index.mjs';
Wraps a providedndarray as an index object.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x);// returns <ndindex>
The constructor accepts the following arguments:
- x: inputndarray.
- options: function options.
The constructor accepts the following options:
kind: specifies whether a providedndarray is a specialized kind of integer inputndarray. This option is only applicable when
xis an integerndarray. Must be one of the following:- cartesian: anndarray containing Cartesian indices.
- linear: anndarray containing indices representing locations in linear memory.
Default:
''.persist: boolean indicating whether to continue persisting an index object after first usage. Default:
false.
By default, anndindex is invalidated and removed from an internal cache immediately after a consumer resolves the underlying data associated with anndindex instance using thendindex.get() static method. Immediate invalidation and cache removal ensures that references to the underlyingndarray data are not the source of memory leaks.
One may, however, want to reuse anndindex instance to avoid additional memory allocation. In order to persist anndindex and prevent automatic cache invalidation, set thepersist option totrue.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x,{'persist':true});// returns <ndindex>// ...varo=ndindex.get(idx.id);// returns {...}// ...o=ndindex.get(idx.id);// returns {...}// ...// Explicitly free the index object:ndindex.free(idx.id);
In order toprevent memory leaks when working with persistedndindex instances, onemust remember to manually free persisted instances using thendindex.free() method.
String value of thendindex constructor name.
varstr=ndindex.name;// returns 'ndindex'
Read-only property returning anndarray view of the underlyingndarray data associated with anndindex instance.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x);// returns <ndindex>varv=idx.data;// returns <ndarray>
Read-only property returning thedata type of the underlyingndarray associated with anndindex instance.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x);// returns <ndindex>vardt=idx.dtype;// returns 'int32'
Read-only property returning the unique identifier associated with anndindex instance.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x);// returns <ndindex>varid=idx.id;// returns <string>
The identifier should be used byndindex consumers to resolve the underlying data associated with anndindex instance.
Read-only property returning a boolean indicating whether anndindex instance is actively cached.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x);// returns <ndindex>varout=idx.isCached;// returns true
Read-only property returning thendarray index kind.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x,{'kind':'linear'});// returns <ndindex>varv=idx.kind;// returns 'linear'
The followingndarray index kinds are supported:
- cartesian: an ndarray index object containing Cartesian indices.
- linear: an ndarray index object for indices representing locations in linear memory.
Read-only property returning thendarray index type.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x);// returns <ndindex>vart=idx.type;// returns 'int'
The followingndarray index types are supported:
- mask: maskndarray, in which a value of zero indicates to include a respective element and a value of one indicates to exclude a respective element. A maskndarray is the complement of a booleanndarray.
- bool: booleanndarray, in which a value of
trueindicates to include a respective element and a value offalseindicates to exclude a respective element. A booleanndarray is the complement of a maskndarray. - int: integerndarray, in which each element is an index indicating the position of an element to include. Elements arenot required to be unique (i.e., more than element may resolve to the same position).
Frees thendindex associated with a provided identifier.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x,{'persist':true});// returns <ndindex>// ...varout=ndindex.free(idx.id);// returns true
Once anndindex is freed, the instance is invalid and can no longer be used. Any subsequentndindex operations (i.e., property and method access) will raise an exception.
Returns thendarray associated with thendindex having a provided identifier.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x,{'persist':true});// returns <ndindex>// ...varo=ndindex.get(idx.id);// returns {...}vard=o.data;// returns <ndarray>vart=o.type;// returns 'int'vardt=o.dtype;// returns 'int32'
The returned object has the following properties:
- data: the underlying "base"ndarray view associated with the
ndindexidentified by the providedid. - type: the type ofndarray index. One of the following:
'int','bool', or'mask'. - kind: thendarray index "kind". One of the following:
'','cartesian', or'linear'. - dtype: the data type of the underlyingndarray.
If thendindex associated with a provided identifier was not explicitly persisted, calling this method will cause thendindex to be invalidated and removed from an internal cache. Any subsequent instance operations (i.e., property and method access) will raise an exception.
Wraps a providedndarray as a Cartesian index object.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]),{'shape':[2,2]});varidx=ndindex.cartesianIndex(x);// returns <ndindex>
This method is a convenience method for creating anndindex with thekind option set to'cartesian'. The function accepts the same arguments and options asndindex above.
Wraps a providedndarray as a linear index object.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=ndindex.linearIndex(x);// returns <ndindex>
This method is a convenience method for creating anndindex with thekind option set to'linear'. The function accepts the same arguments and options asndindex above.
Serializes anndindex as a string.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x);// returns <ndindex>varstr=idx.toString();// e.g., 'ndindex<0>'
Anndindex is intended to be an opaque object used by objects supporting "fancy" indexing (e.g.,fancy ndarrays). As such, when serialized as a string, a serializedndindex includes only the unique identifier associated with the respective instance.
Serializes anndindex as aJSON object.
importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x);// returns <ndindex>varo=idx.toJSON();// returns { 'type': 'ndindex', 'kind': '', 'data': { ... }}
JSON.stringify() implicitly calls this method when stringifying anndindex instance.
ndindexinstances have no explicit functionality; however, they are used by"fancy" ndarrays and other packages for element retrieval and assignment.Because
ndindexinstances leverage an internal cache implementing thesingleton pattern, onemust be sure to use the samendindexconstructor asndindexconsumers. If one uses a differentndindexconstructor, the consumer willnot be able to resolve anndarray view of the originalndarray, as the consumer will attempt to resolve anndindexinstance in the wrong internal cache.Because non-persisted
ndindexinstances are freed after first use, in order to avoid holding onto memory and to allow garbage collection, one should avoid scenarios in which anndindexis never used. For example,importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x);varo;if(x.get(0)===0){// Do something with `idx`...o=ndindex.get(idx.id);// ...}
will leak memory as
idxis only consumed within anifblock which never evaluates. In such scenarios, one should either refactor to avoid inadvertently holding onto memory or explicitly free thendindex.importarrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-array@esm/index.mjs';importInt32Arrayfrom'https://cdn.jsdelivr.net/gh/stdlib-js/array-int32@esm/index.mjs';varx=array(newInt32Array([1,2,3,4]));varidx=newndindex(x);varo;if(x.get(0)===0){// Do something with `idx`...o=ndindex.get(idx.id);// ...}else{ndindex.free(idx.id);}
<!DOCTYPE html><htmllang="en"><body><scripttype="module">importemptyfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-empty@esm/index.mjs';importndindexfrom'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-index@esm/index.mjs';varx=empty([5],{'dtype':'uint8'});vari=newndindex(x);// returns <ndindex>varo=ndindex.get(i.id);// returns {...}console.log('Type: %s. Data type: %s.',o.type,o.dtype);x=empty([5],{'dtype':'generic'});i=newndindex(x);// returns <ndindex>o=ndindex.get(i.id);// returns {...}console.log('Type: %s. Data type: %s.',o.type,o.dtype);x=empty([5],{'dtype':'bool'});i=newndindex(x);// returns <ndindex>o=ndindex.get(i.id);// returns {...}console.log('Type: %s. Data type: %s.',o.type,o.dtype);x=empty([5],{'dtype':'int32'});i=newndindex(x);// returns <ndindex>o=ndindex.get(i.id);// returns {...}console.log('Type: %s. Data type: %s.',o.type,o.dtype);</script></body></html>
@stdlib/ndarray-array:multidimensional arrays.@stdlib/ndarray-ctor:multidimensional array constructor.@stdlib/ndarray-fancy:fancy multidimensional array constructor.@stdlib/ndarray-slice:return a read-only view of an input ndarray.@stdlib/ndarray-to-fancy:convert an ndarray to an object supporting fancy indexing.
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
ndarray index constructor.
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.