@stdlib/ndarray-base-ind2sub

Convert a linear index to an array of subscripts.

https://github.com/stdlib-js/ndarray-base-ind2sub

Science Score: 44.0%

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

  • CITATION.cff file
    Found CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.4%) to scientific vocabulary

Keywords

array base ind2sub javascript multidimensional ndarray node node-js nodejs offset shape stdlib strides sub2ind subscript types util utilities utility utils
Last synced: 4 months ago · JSON representation ·

Repository

Convert a linear index to an array of subscripts.

Basic Info
Statistics
  • Stars: 5
  • Watchers: 3
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Topics
array base ind2sub javascript multidimensional ndarray node node-js nodejs offset shape stdlib strides sub2ind subscript types util utilities utility utils
Created over 4 years ago · Last pushed 4 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!

ind2sub

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

Convert a linear index to an array of subscripts.

## Installation ```bash npm install @stdlib/ndarray-base-ind2sub ``` 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 ind2sub = require( '@stdlib/ndarray-base-ind2sub' ); ``` #### ind2sub( shape, strides, offset, order, idx, mode ) Converts a linear index to an array of subscripts. ```javascript var shape = [ 2, 2 ]; var order = 'row-major'; var strides = [ 2, 1 ]; var offset = 0; var subscripts = ind2sub( shape, strides, offset, order, 1, 'throw' ); // returns [ 0, 1 ] ``` The function supports the following modes: - **throw**: specifies that the function should throw an error when a linear index exceeds array dimensions. - **normalize**: specifies that the function should normalize negative indices and throw an error when a linear index exceeds array dimensions. - **wrap**: specifies that the function should wrap around a linear index exceeding array dimensions using modulo arithmetic. - **clamp**: specifies that the function should set a linear index exceeding array dimensions to either `0` (minimum linear index) or the maximum linear index. ```javascript var shape = [ 2, 2 ]; var order = 'row-major'; var strides = [ 2, 1 ]; var offset = 0; var idx = ind2sub( shape, strides, offset, order, -2, 'wrap' ); // returns [ 1, 0 ] idx = ind2sub( shape, strides, offset, order, 10, 'clamp' ); // returns [ 1, 1 ] ``` The `order` parameter specifies whether an array is `row-major` (C-style) or `column-major` (Fortran-style). ```javascript var shape = [ 2, 2 ]; var order = 'column-major'; var strides = [ 1, 2 ]; var offset = 0; var idx = ind2sub( shape, strides, offset, order, 2, 'throw' ); // returns [ 0, 1 ] ``` #### ind2sub.assign( shape, strides, offset, order, idx, mode, out ) Converts a linear index to an array of subscripts and assigns results to a provided output array. ```javascript var shape = [ 2, 2 ]; var order = 'row-major'; var strides = [ 2, 1 ]; var offset = 0; var out = [ 0, 0 ]; var subscripts = ind2sub.assign( shape, strides, offset, order, 1, 'throw', out ); // returns [ 0, 1 ] var bool = ( subscripts === out ); // returns true ```
## Notes - When provided a stride array containing negative strides, if an `offset` is greater than `0`, the function interprets the linear index as an index into the underlying data buffer for the array, thus returning subscripts from the perspective of that buffer. If an `offset` is equal to `0`, the function treats the linear index as an index into an array view, thus returning subscripts from the perspective of that view. ```text Dims: 2x2 Buffer: [ 1, 2, 3, 4 ] View = [ a00, a01, a10, a11 ] Strides: 2,1 Offset: 0 View = [ 1, 2, 3, 4 ] Strides: 2,-1 Offset: 1 View = [ 2, 1, 4, 3 ] Strides: -2,1 Offset: 2 View = [ 3, 4, 1, 2 ] Strides: -2,-1 Offset: 3 View = [ 4, 3, 2, 1 ] ``` ```javascript var shape = [ 2, 2 ]; var order = 'row-major'; var strides = [ -2, 1 ]; var offset = 2; var mode = 'throw'; // From the perspective of a view... var s = ind2sub( shape, strides, 0, order, 0, mode ); // returns [ 0, 0 ] s = ind2sub( shape, strides, 0, order, 1, mode ); // returns [ 0, 1 ] s = ind2sub( shape, strides, 0, order, 2, mode ); // returns [ 1, 0 ] s = ind2sub( shape, strides, 0, order, 3, mode ); // returns [ 1, 1 ] // From the perspective of an underlying buffer... s = ind2sub( shape, strides, offset, order, 0, mode ); // returns [ 1, 0 ] s = ind2sub( shape, strides, offset, order, 1, mode ); // returns [ 1, 1 ] s = ind2sub( shape, strides, offset, order, 2, mode ); // returns [ 0, 0 ] s = ind2sub( shape, strides, offset, order, 3, mode ); // returns [ 0, 1 ] ``` In short, from the perspective of a view, view data is always ordered.
## Examples ```javascript var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); var numel = require( '@stdlib/ndarray-base-numel' ); var randu = require( '@stdlib/random-base-randu' ); var abs = require( '@stdlib/math-base-special-abs' ); var ind2sub = require( '@stdlib/ndarray-base-ind2sub' ); // Specify array characteristics: var shape = [ 3, 3, 3 ]; var order = 'row-major'; // Compute array meta data: var ndims = shape.length; var strides = shape2strides( shape, order ); var len = numel( shape ); // Determine stride indices to be used for formatting how views are displayed... var s0; var s1; if ( order === 'column-major' ) { s0 = ndims - 1; s1 = s0 - 1; } else { // row-major s0 = 0; s1 = s0 + 1; } // Initialize a linear array... var arr = []; var i; for ( i = 0; i < len; i++ ) { arr.push( 0 ); } // Generate random views and display the mapping of elements in the linear array to view subscripts... var offset; var row; var j; var s; for ( i = 0; i < 20; i++ ) { // Randomly flip the sign of one of the strides... j = discreteUniform( 0, ndims-1 ); strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; offset = strides2offset( shape, strides ); // Print view meta data... console.log( '' ); console.log( 'Dimensions: %s.', shape.join( 'x' ) ); console.log( 'Strides: %s.', strides.join( ',' ) ); console.log( 'View (subscripts):' ); // Print the mapping of elements in the linear array to view subscripts... row = ' '; for ( j = 0; j < len; j++ ) { s = ind2sub( shape, strides, offset, order, j, 'throw' ); row += '(' + s.join( ',' ) + ')'; if ( ndims === 1 && j === len-1 ) { console.log( row ); } else if ( ndims === 2 && (j+1)%abs( strides[ s0 ] ) === 0 ) { console.log( row ); row = ' '; } else if ( ndims > 2 && (j+1)%abs( strides[ s1 ] ) === 0 ) { console.log( row ); if ( (j+1)%abs( strides[ s0 ] ) === 0 ) { console.log( '' ); } row = ' '; } else { row += ', '; } } } ```

## C APIs
### Usage ```c #include "stdlib/ndarray/base/ind2sub.h" ``` #### stdlib_ndarray_ind2sub( ndims, \*shape, \*strides, offset, order, idx, mode, \*out ) Computes the minimum and maximum linear indices in an underlying data buffer accessible to an array view. ```c #include "stdlib/ndarray/index_modes.h" #include "stdlib/ndarray/orders.h" #include int64_t ndims = 2; int64_t shape[] = { 3, 3 }; int64_t strides[] = { -3, 1 }; int64_t offset = 6; int64_t out[ 2 ]; int8_t status = stdlib_ndarray_ind2sub( ndims, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, 7, STDLIB_NDARRAY_INDEX_ERROR, out ); if ( status == -1 ) { // Handle error... } ``` The function accepts the following arguments: - **ndims**: `[in] int64_t` number of dimensions. - **shape**: `[in] int64_t*` array shape (dimensions). - **strides**: `[in] int64_t*` array strides. - **offset**: `[in] int64_t` index offset. - **order**: `[in] enum STDLIB_NDARRAY_ORDER` specifies whether an array is row-major (C-style) or column-major (Fortran-style). - **idx**: `[in] int64_t` linear index in an array view. - **mode**: `[in] enum STDLIB_NDARRAY_INDEX_MODE` specifies how to handle a linear index which exceeds array dimensions. - **out**: `[out] int64_t*` output array. ```c int8_t stdlib_ndarray_ind2sub( const int64_t ndims, const int64_t *shape, const int64_t *strides, const int64_t offset, const enum STDLIB_NDARRAY_ORDER order, const int64_t idx, const enum STDLIB_NDARRAY_INDEX_MODE mode, int64_t *out ); ```
### Examples ```c #include "stdlib/ndarray/base/ind2sub.h" #include "stdlib/ndarray/index_modes.h" #include "stdlib/ndarray/orders.h" #include #include #include int main( void ) { int64_t ndims = 2; int64_t shape[] = { 3, 3 }; int64_t strides[] = { -3, 1 }; int64_t offset = 6; int64_t out[ 2 ]; stdlib_ndarray_ind2sub( ndims, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, 7, STDLIB_NDARRAY_INDEX_ERROR, out ); int i; printf( "subscripts = { " ); for ( i = 0; i < ndims; i++ ) { printf( "%"PRId64"", out[ i ] ); if ( i < ndims-1 ) { printf( ", " ); } } printf( " }\n" ); } ```

* * * ## 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.

Citation (CITATION.cff)

cff-version: 1.2.0
title: stdlib
message: >-
  If you use this software, please cite it using the
  metadata from this file.

type: software

authors:
  - name: The Stdlib Authors
    url: https://github.com/stdlib-js/stdlib/graphs/contributors

repository-code: https://github.com/stdlib-js/stdlib
url: https://stdlib.io

abstract: |
  Standard library for JavaScript and Node.js.

keywords:
  - JavaScript
  - Node.js
  - TypeScript
  - standard library
  - scientific computing
  - numerical computing
  - statistical computing

license: Apache-2.0 AND BSL-1.0

date-released: 2016

GitHub Events

Total
  • Push event: 34
Last Year
  • Push event: 34

Committers

Last synced: almost 2 years ago

All Time
  • Total Commits: 49
  • Total Committers: 1
  • Avg Commits per committer: 49.0
  • Development Distribution Score (DDS): 0.0
Past Year
  • Commits: 15
  • Committers: 1
  • Avg Commits per committer: 15.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
stdlib-bot n****y@s****o 49
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 4 months ago

All Time
  • Total issues: 0
  • Total pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Total issue authors: 0
  • Total pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 0
  • Pull requests: 0
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 0
  • Pull request authors: 0
  • Average comments per issue: 0
  • Average comments per pull request: 0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
Pull Request Authors
Top Labels
Issue Labels
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • npm 5,249 last-month
  • Total dependent packages: 5
  • Total dependent repositories: 3
  • Total versions: 13
  • Total maintainers: 4
npmjs.org: @stdlib/ndarray-base-ind2sub

Convert a linear index to an array of subscripts.

  • Homepage: https://stdlib.io
  • License: Apache-2.0
  • Latest release: 0.2.2
    published over 1 year ago
  • Versions: 13
  • Dependent Packages: 5
  • Dependent Repositories: 3
  • Downloads: 5,249 Last month
Rankings
Dependent packages count: 3.7%
Downloads: 4.6%
Dependent repos count: 6.4%
Average: 8.3%
Stargazers count: 11.5%
Forks count: 15.4%
Funding
  • type: opencollective
  • url: https://opencollective.com/stdlib
Last synced: 4 months ago