ndarray-to-fancy

Convert an ndarray to an object supporting fancy indexing.

https://github.com/stdlib-js/ndarray-to-fancy

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (13.6%) to scientific vocabulary

Keywords

array data fancy index indexing javascript multidimensional ndarray node node-js nodejs slice slicing stdlib structure subseq subsequence types vector
Last synced: 6 months ago · JSON representation

Repository

Convert an ndarray to an object supporting fancy indexing.

Basic Info
Statistics
  • Stars: 1
  • Watchers: 2
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
array data fancy index indexing javascript multidimensional ndarray node node-js nodejs slice slicing stdlib structure subseq subsequence types vector
Created about 1 year ago · Last pushed 7 months ago
Metadata Files
Readme Changelog Contributing License Code of conduct Citation Security

README.md

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 on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!

ndarray2fancy

NPM version Build Status Coverage Status <!-- dependencies -->

Convert an ndarray to an object supporting fancy indexing.

A fancy ndarray is an [`ndarray`][@stdlib/ndarray/ctor] which supports slicing via indexing expressions. ```javascript var ndarray2array = require( '@stdlib/ndarray-to-array' ); var ndarray = require( '@stdlib/ndarray-ctor' ); // Create a plain ndarray: var buffer = [ 1, 2, 3, 4, 5, 6 ]; var x = new ndarray( 'generic', buffer, [ 6 ], [ 1 ], 0, 'row-major' ); // returns // Convert to a fancy ndarray: var y = ndarray2fancy( x ); // Select the first 3 elements: var z = y[ ':3' ]; // returns var arr = ndarray2array( z ); // returns [ 1, 2, 3 ] // Select every other element, starting with the second element: z = y[ '1::2' ]; // returns arr = ndarray2array( z ); // returns [ 2, 4, 6 ] // Reverse the array, starting with last element and skipping every other element: z = y[ '::-2' ]; // returns arr = ndarray2array( z ); // returns [ 6, 4, 2 ] ```
## Installation ```bash npm install @stdlib/ndarray-to-fancy ``` Alternatively, - To load the package in a website via a `script` tag without installation and bundlers, use the [ES Module][es-module] available on the [`esm`][esm-url] branch (see [README][esm-readme]). - If you are using Deno, visit the [`deno`][deno-url] branch (see [README][deno-readme] for usage intructions). - For use in Observable, or in browser/node environments, use the [Universal Module Definition (UMD)][umd] build available on the [`umd`][umd-url] branch (see [README][umd-readme]). The [branches.md][branches-url] 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 ```javascript var ndarray2fancy = require( '@stdlib/ndarray-to-fancy' ); ``` #### ndarray2fancy( x\[, options] ) Converts an [ndarray][@stdlib/ndarray/ctor] to an object supporting fancy indexing. ```javascript console.log( 'TODO' ); ``` The function supports the following options: - **cache**: cache for resolving ndarray index objects. Must have a `get` method which accepts a single argument: a string identifier associated with an ndarray index. If an ndarray index associated with a provided identifier exists, the `get` 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**: the [data type][@stdlib/ndarray/dtypes] of the underlying ndarray. If an ndarray index is not associated with a provided identifier, the `get` method should return `null`. Default: [`ndindex`][@stdlib/ndarray/index]. - **strict**: boolean indicating whether to enforce strict bounds checking. Default: `false`. By default, the function returns a fancy ndarray which does **not** enforce strict bounds checking. For example, ```javascript console.log( 'TODO' ); ``` To enforce strict bounds checking, set the `strict` option to `true`. ```javascript console.log( 'TODO' ); ``` #### ndarray2fancy.factory( \[options] ) Returns a function for converting an [ndarray][@stdlib/ndarray/ctor] to an object supporting fancy indexing. ```javascript var fcn = ndarray2fancy.factory(); console.log( 'TODO' ); ``` The function supports the following options: - **cache**: default cache for resolving ndarray index objects. Must have a `get` method which accepts a single argument: a string identifier associated with an ndarray index. If an ndarray index associated with a provided identifier exists, the `get` 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**: the [data type][@stdlib/ndarray/dtypes] of the underlying ndarray. If an ndarray index is not associated with a provided identifier, the `get` method should return `null`. Default: [`ndindex`][@stdlib/ndarray/index]. - **strict**: boolean indicating whether to enforce strict bounds checking by default. Default: `false`. By default, the function returns a function which, by default, does **not** enforce strict bounds checking. For example, ```javascript var fcn = ndarray2fancy.factory(); console.log( 'TODO' ); ``` To enforce strict bounds checking by default, set the `strict` option to `true`. ```javascript var fcn = 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. ```javascript console.log( 'TODO' ); ``` For documentation and usage, see [`ndindex`][@stdlib/ndarray/index].
* * * ## Notes - A fancy ndarray shares the **same** data as the provided input [ndarray][@stdlib/ndarray/ctor]. 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), [`Slice`][@stdlib/slice/ctor] and [`MultiSlice`][@stdlib/slice/multi] instances, [subsequence expressions][@stdlib/slice/seq2multislice], and [index arrays][@stdlib/ndarray/index] (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 the [REPL][@stdlib/repl]) and scripting. For performance critical applications, prefer equivalent functional APIs supporting ndarray-like objects. - In older JavaScript environments which do **not** support [`Proxy`][@stdlib/proxy/ctor] objects, the use of indexing expressions is **not** 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 return `undefined` and fail to assign. ### Broadcasting // TODO: see array/to-fancy ### Casting // TODO: see array/to-fancy
* * * ## Examples ```javascript var S = require( '@stdlib/slice-ctor' ); var E = require( '@stdlib/slice-multi' ); var toArray = require( '@stdlib/ndarray-to-array' ); var ndarray = require( '@stdlib/ndarray-ctor' ); var ndarray2fancy = require( '@stdlib/ndarray-to-fancy' ); var buffer = [ 1, 2, 3, 4, // 0 5, 6, // 1 7, 8, // 2 9, 10 ]; var shape = [ 3, 2 ]; var strides = [ 2, 1 ]; var offset = 2; // Create a normal ndarray: var x = new ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); // returns // Convert to a fancy ndarray: var y = ndarray2fancy( x ); // Access an ndarray property: var ndims = y.ndims; // returns 2 // Retrieve an ndarray element: var v = 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 _ = void 0; // Create a multi-dimensional slice: var s = E( S(0,_,2), _ ); // returns // Use the slice to create a view on the original ndarray: var y1 = y[ s ]; console.log( toArray( y1 ) ); // => [ [ 3, 4 ], [ 7, 20 ] ] // Use alternative syntax: var y2 = y[ [ S(0,_,2), _ ] ]; console.log( toArray( y2 ) ); // => [ [ 3, 4 ], [ 7, 20 ] ] // Use alternative syntax: var y3 = y[ '0::2,:' ]; console.log( toArray( y3 ) ); // => [ [ 3, 4 ], [ 7, 20 ] ] // Flip dimensions: var y4 = y[ [ S(_,_,-2), S(_,_,-1) ] ]; console.log( toArray( y4 ) ); // => [ [ 20, 7 ], [ 4, 3 ] ] ```
* * * ## Notice This package is part of [stdlib][stdlib], 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 develop [stdlib][stdlib], see the main project [repository][stdlib]. #### Community [![Chat][chat-image]][chat-url] --- ## License See [LICENSE][stdlib-license]. ## Copyright Copyright © 2016-2025. The Stdlib [Authors][stdlib-authors].

Owner

  • Name: stdlib
  • Login: stdlib-js
  • Kind: organization

Standard library for JavaScript.

GitHub Events

Total
  • Watch event: 1
  • Push event: 13
  • Create event: 6
Last Year
  • Watch event: 1
  • Push event: 13
  • Create event: 6

Dependencies

.github/workflows/benchmark.yml actions
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
.github/workflows/cancel.yml actions
  • styfle/cancel-workflow-action 85880fa0301c86cca9da44039ee3bb12d3bedbfa composite
.github/workflows/close_pull_requests.yml actions
  • superbrothers/close-pull-request 9c18513d320d7b2c7185fb93396d0c664d5d8448 composite
.github/workflows/examples.yml actions
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
.github/workflows/npm_downloads.yml actions
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • actions/upload-artifact 5d5d22a31266ced268874388b861e4b58bb5c2f3 composite
  • distributhor/workflow-webhook 48a40b380ce4593b6a6676528cd005986ae56629 composite
.github/workflows/productionize.yml actions
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • stdlib-js/bundle-action main composite
  • stdlib-js/transform-errors-action main composite
.github/workflows/publish.yml actions
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 composite
  • JS-DevTools/npm-publish 19c28f1ef146469e409470805ea4279d47c3d35c composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • styfle/cancel-workflow-action 85880fa0301c86cca9da44039ee3bb12d3bedbfa composite
.github/workflows/test.yml actions
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
.github/workflows/test_bundles.yml actions
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • denoland/setup-deno 041b854f97b325bd60e53e9dc2de9cb9f9ac0cba composite
.github/workflows/test_coverage.yml actions
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
  • codecov/codecov-action 84508663e988701840491b86de86b666e8a86bed composite
  • distributhor/workflow-webhook 48a40b380ce4593b6a6676528cd005986ae56629 composite
.github/workflows/test_install.yml actions
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
.github/workflows/test_published_package.yml actions
  • 8398a7/action-slack 28ba43ae48961b90635b50953d216767a6bea486 composite
  • actions/checkout 8ade135a41bc03ea155e62e844d188df1ea18608 composite
  • actions/setup-node b39b52d1213e96004bfcb1c61a8a6fa8ab84f3e8 composite
package.json npm
  • @stdlib/assert-has-proxy-support ^0.2.2 development
  • @stdlib/bench-harness ^0.2.2 development
  • @stdlib/ndarray-to-array ^0.2.1 development
  • @stdlib/ndarray-zeros ^0.3.0 development
  • @stdlib/slice-ctor ^0.2.2 development
  • istanbul ^0.4.1 development
  • tap-min git+https://github.com/Planeshifter/tap-min.git development
  • tape git+https://github.com/kgryte/tape.git#fix/globby development
  • @stdlib/assert-has-own-property ^0.2.2
  • @stdlib/assert-has-property ^0.2.2
  • @stdlib/assert-is-boolean ^0.2.2
  • @stdlib/assert-is-complex-like ^0.2.2
  • @stdlib/assert-is-function ^0.2.2
  • @stdlib/assert-is-integer ^0.2.2
  • @stdlib/assert-is-method-in ^0.2.2
  • @stdlib/assert-is-ndarray-like ^0.2.2
  • @stdlib/assert-is-number ^0.2.2
  • @stdlib/assert-is-plain-object ^0.2.2
  • @stdlib/assert-is-string ^0.2.2
  • @stdlib/complex-dtype ^0.2.2
  • @stdlib/constants-int16-max ^0.2.2
  • @stdlib/constants-int32-max ^0.3.0
  • @stdlib/constants-int8-max ^0.2.2
  • @stdlib/error-tools-fmtprodmsg ^0.2.2
  • @stdlib/ndarray-base-assert-is-boolean-data-type ^0.1.0
  • @stdlib/ndarray-base-assert-is-complex-floating-point-data-type ^0.2.2
  • @stdlib/ndarray-base-assert-is-floating-point-data-type ^0.2.2
  • @stdlib/ndarray-base-assert-is-real-floating-point-data-type ^0.2.2
  • @stdlib/ndarray-base-assert-is-safe-data-type-cast ^0.2.2
  • @stdlib/ndarray-base-assert-is-signed-integer-data-type ^0.2.2
  • @stdlib/ndarray-base-assert-is-unsigned-integer-data-type ^0.2.2
  • @stdlib/ndarray-base-from-scalar-like github:stdlib-js/ndarray-base-from-scalar-like#main
  • @stdlib/ndarray-base-ind2sub ^0.2.2
  • @stdlib/ndarray-base-min-signed-integer-dtype github:stdlib-js/ndarray-base-min-signed-integer-dtype#main
  • @stdlib/ndarray-base-ndarraylike2object ^0.2.2
  • @stdlib/ndarray-base-normalize-index ^0.2.2
  • @stdlib/ndarray-base-numel ^0.2.2
  • @stdlib/ndarray-base-shape ^0.2.2
  • @stdlib/ndarray-base-slice ^0.2.2
  • @stdlib/ndarray-base-slice-assign ^0.2.1
  • @stdlib/ndarray-ctor ^0.2.2
  • @stdlib/ndarray-from-scalar ^0.2.1
  • @stdlib/ndarray-index github:stdlib-js/ndarray-index#main
  • @stdlib/ndarray-min-dtype ^0.3.0
  • @stdlib/ndarray-order ^0.2.2
  • @stdlib/ndarray-strides ^0.2.2
  • @stdlib/object-assign ^0.2.2
  • @stdlib/proxy-ctor ^0.2.2
  • @stdlib/slice-base-sargs2multislice ^0.3.2
  • @stdlib/slice-base-seq2multislice ^0.2.2
  • @stdlib/slice-base-str2multislice ^0.2.2
  • @stdlib/slice-base-str2slice ^0.2.2
  • @stdlib/slice-multi ^0.2.2
  • @stdlib/string-base-replace ^0.2.2
  • @stdlib/string-base-starts-with ^0.2.2
  • @stdlib/string-base-trim ^0.2.2
  • @stdlib/string-format ^0.2.2
  • @stdlib/utils-define-nonenumerable-read-only-property ^0.2.2