Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Convert an ndarray to an object supporting fancy indexing.

License

NotificationsYou must be signed in to change notification settings

stdlib-js/ndarray-to-fancy

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!

ndarray2fancy

NPM versionBuild StatusCoverage Status

Convert an ndarray to an object supporting fancy indexing.

A fancy ndarray is anndarray which supports slicing via indexing expressions.

varndarray2array=require('@stdlib/ndarray-to-array');varndarray=require('@stdlib/ndarray-ctor');// 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 ]

Installation

npm install @stdlib/ndarray-to-fancy

Alternatively,

  • To load the package in a website via ascript tag without installation and bundlers, use theES Module available on theesm branch (seeREADME).
  • If you are using Deno, visit thedeno branch (seeREADME for usage intructions).
  • For use in Observable, or in browser/node environments, use theUniversal Module Definition (UMD) build available on theumd branch (seeREADME).

Thebranches.md file summarizes the available branches and displays a diagram illustrating their relationships.

To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.

Usage

varndarray2fancy=require('@stdlib/ndarray-to-fancy');

ndarray2fancy( x[, options] )

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 aget method which accepts a single argument: a string identifier associated with an ndarray index.

    If an ndarray index associated with a provided identifier exists, theget method 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, theget method 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');

ndarray2fancy.factory( [options] )

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 aget method which accepts a single argument: a string identifier associated with an ndarray index.

    If an ndarray index associated with a provided identifier exists, theget method 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, theget method 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.

ndarray2fancy.idx( x[, options] )

Wraps a provided ndarray as an ndarray index object.

console.log('TODO');

For documentation and usage, seendindex.


Notes

  • 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 byoptions.
  • A fancy ndarray supports indexing using positive and negative integers (both numeric literals and strings),Slice andMultiSlice instances,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 supportProxy objects, the use of indexing expressions isnot supported.

Bounds Checking

// TODO: see array/to-fancy

Linear Indexing

// TODO: only applies to non-zero-dimensional ndarrays. In non-strict mode, out-of-bounds indices returnundefined and fail to assign.

Broadcasting

// TODO: see array/to-fancy

Casting

// TODO: see array/to-fancy


Examples

varS=require('@stdlib/slice-ctor');varE=require('@stdlib/slice-multi');vartoArray=require('@stdlib/ndarray-to-array');varndarray=require('@stdlib/ndarray-ctor');varndarray2fancy=require('@stdlib/ndarray-to-fancy');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 ] ]

See Also


Notice

This package is part ofstdlib, a standard library for JavaScript and Node.js, 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.

Community

Chat


License

SeeLICENSE.

Copyright

Copyright © 2016-2025. The StdlibAuthors.

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp