Gathering detailed insights and metrics for @stdlib/ndarray-base-ind2sub
Gathering detailed insights and metrics for @stdlib/ndarray-base-ind2sub
Gathering detailed insights and metrics for @stdlib/ndarray-base-ind2sub
Gathering detailed insights and metrics for @stdlib/ndarray-base-ind2sub
Convert a linear index to an array of subscripts.
npm install @stdlib/ndarray-base-ind2sub
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (61.28%)
C (38.72%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
Apache-2.0 License
5 Stars
65 Commits
3 Watchers
5 Branches
14 Contributors
Updated on May 26, 2025
Latest Version
0.2.2
Package Id
@stdlib/ndarray-base-ind2sub@0.2.2
Unpacked Size
59.69 kB
Size
13.54 kB
File Count
15
NPM Version
8.19.4
Node Version
16.20.2
Published on
Jul 28, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
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!
Convert a linear index to an array of subscripts.
1npm install @stdlib/ndarray-base-ind2sub
1var ind2sub = require( '@stdlib/ndarray-base-ind2sub' );
Converts a linear index to an array of subscripts.
1var shape = [ 2, 2 ]; 2var order = 'row-major'; 3var strides = [ 2, 1 ]; 4var offset = 0; 5 6var subscripts = ind2sub( shape, strides, offset, order, 1, 'throw' ); 7// returns [ 0, 1 ]
The function supports the following modes:
0
(minimum linear index) or the maximum linear index.1var shape = [ 2, 2 ]; 2var order = 'row-major'; 3var strides = [ 2, 1 ]; 4var offset = 0; 5 6var idx = ind2sub( shape, strides, offset, order, -2, 'wrap' ); 7// returns [ 1, 0 ] 8 9idx = ind2sub( shape, strides, offset, order, 10, 'clamp' ); 10// returns [ 1, 1 ]
The order
parameter specifies whether an array is row-major
(C-style) or column-major
(Fortran-style).
1var shape = [ 2, 2 ]; 2var order = 'column-major'; 3var strides = [ 1, 2 ]; 4var offset = 0; 5 6var idx = ind2sub( shape, strides, offset, order, 2, 'throw' ); 7// returns [ 0, 1 ]
Converts a linear index to an array of subscripts and assigns results to a provided output array.
1var shape = [ 2, 2 ]; 2var order = 'row-major'; 3var strides = [ 2, 1 ]; 4var offset = 0; 5 6var out = [ 0, 0 ]; 7var subscripts = ind2sub.assign( shape, strides, offset, order, 1, 'throw', out ); 8// returns [ 0, 1 ] 9 10var bool = ( subscripts === out ); 11// returns true
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.
1Dims: 2x2 2Buffer: [ 1, 2, 3, 4 ] 3 4View = [ a00, a01, 5 a10, a11 ] 6 7Strides: 2,1 8Offset: 0 9 10View = [ 1, 2, 11 3, 4 ] 12 13Strides: 2,-1 14Offset: 1 15 16View = [ 2, 1, 17 4, 3 ] 18 19Strides: -2,1 20Offset: 2 21 22View = [ 3, 4, 23 1, 2 ] 24 25Strides: -2,-1 26Offset: 3 27 28View = [ 4, 3, 29 2, 1 ]
1var shape = [ 2, 2 ]; 2var order = 'row-major'; 3var strides = [ -2, 1 ]; 4var offset = 2; 5var mode = 'throw'; 6 7// From the perspective of a view... 8var s = ind2sub( shape, strides, 0, order, 0, mode ); 9// returns [ 0, 0 ] 10 11s = ind2sub( shape, strides, 0, order, 1, mode ); 12// returns [ 0, 1 ] 13 14s = ind2sub( shape, strides, 0, order, 2, mode ); 15// returns [ 1, 0 ] 16 17s = ind2sub( shape, strides, 0, order, 3, mode ); 18// returns [ 1, 1 ] 19 20// From the perspective of an underlying buffer... 21s = ind2sub( shape, strides, offset, order, 0, mode ); 22// returns [ 1, 0 ] 23 24s = ind2sub( shape, strides, offset, order, 1, mode ); 25// returns [ 1, 1 ] 26 27s = ind2sub( shape, strides, offset, order, 2, mode ); 28// returns [ 0, 0 ] 29 30s = ind2sub( shape, strides, offset, order, 3, mode ); 31// returns [ 0, 1 ]
In short, from the perspective of a view, view data is always ordered.
1var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ); 2var shape2strides = require( '@stdlib/ndarray-base-shape2strides' ); 3var strides2offset = require( '@stdlib/ndarray-base-strides2offset' ); 4var numel = require( '@stdlib/ndarray-base-numel' ); 5var randu = require( '@stdlib/random-base-randu' ); 6var abs = require( '@stdlib/math-base-special-abs' ); 7var ind2sub = require( '@stdlib/ndarray-base-ind2sub' ); 8 9// Specify array characteristics: 10var shape = [ 3, 3, 3 ]; 11var order = 'row-major'; 12 13// Compute array meta data: 14var ndims = shape.length; 15var strides = shape2strides( shape, order ); 16var len = numel( shape ); 17 18// Determine stride indices to be used for formatting how views are displayed... 19var s0; 20var s1; 21if ( order === 'column-major' ) { 22 s0 = ndims - 1; 23 s1 = s0 - 1; 24} else { // row-major 25 s0 = 0; 26 s1 = s0 + 1; 27} 28 29// Initialize a linear array... 30var arr = []; 31var i; 32for ( i = 0; i < len; i++ ) { 33 arr.push( 0 ); 34} 35 36// Generate random views and display the mapping of elements in the linear array to view subscripts... 37var offset; 38var row; 39var j; 40var s; 41for ( i = 0; i < 20; i++ ) { 42 // Randomly flip the sign of one of the strides... 43 j = discreteUniform( 0, ndims-1 ); 44 strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1; 45 offset = strides2offset( shape, strides ); 46 47 // Print view meta data... 48 console.log( '' ); 49 console.log( 'Dimensions: %s.', shape.join( 'x' ) ); 50 console.log( 'Strides: %s.', strides.join( ',' ) ); 51 console.log( 'View (subscripts):' ); 52 53 // Print the mapping of elements in the linear array to view subscripts... 54 row = ' '; 55 for ( j = 0; j < len; j++ ) { 56 s = ind2sub( shape, strides, offset, order, j, 'throw' ); 57 row += '(' + s.join( ',' ) + ')'; 58 if ( ndims === 1 && j === len-1 ) { 59 console.log( row ); 60 } else if ( ndims === 2 && (j+1)%abs( strides[ s0 ] ) === 0 ) { 61 console.log( row ); 62 row = ' '; 63 } else if ( ndims > 2 && (j+1)%abs( strides[ s1 ] ) === 0 ) { 64 console.log( row ); 65 if ( (j+1)%abs( strides[ s0 ] ) === 0 ) { 66 console.log( '' ); 67 } 68 row = ' '; 69 } else { 70 row += ', '; 71 } 72 } 73}
1#include "stdlib/ndarray/base/ind2sub.h"
Computes the minimum and maximum linear indices in an underlying data buffer accessible to an array view.
1#include "stdlib/ndarray/index_modes.h" 2#include "stdlib/ndarray/orders.h" 3#include <stdint.h> 4 5int64_t ndims = 2; 6int64_t shape[] = { 3, 3 }; 7int64_t strides[] = { -3, 1 }; 8int64_t offset = 6; 9 10int64_t out[ 2 ]; 11 12int8_t status = stdlib_ndarray_ind2sub( ndims, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, 7, STDLIB_NDARRAY_INDEX_ERROR, out ); 13if ( status == -1 ) { 14 // Handle error... 15}
The function accepts the following arguments:
[in] int64_t
number of dimensions.[in] int64_t*
array shape (dimensions).[in] int64_t*
array strides.[in] int64_t
index offset.[in] enum STDLIB_NDARRAY_ORDER
specifies whether an array is row-major (C-style) or column-major (Fortran-style).[in] int64_t
linear index in an array view.[in] enum STDLIB_NDARRAY_INDEX_MODE
specifies how to handle a linear index which exceeds array dimensions.[out] int64_t*
output array.1int8_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 );
1#include "stdlib/ndarray/base/ind2sub.h" 2#include "stdlib/ndarray/index_modes.h" 3#include "stdlib/ndarray/orders.h" 4#include <stdint.h> 5#include <stdio.h> 6#include <inttypes.h> 7 8int main( void ) { 9 int64_t ndims = 2; 10 int64_t shape[] = { 3, 3 }; 11 int64_t strides[] = { -3, 1 }; 12 int64_t offset = 6; 13 14 int64_t out[ 2 ]; 15 16 stdlib_ndarray_ind2sub( ndims, shape, strides, offset, STDLIB_NDARRAY_ROW_MAJOR, 7, STDLIB_NDARRAY_INDEX_ERROR, out ); 17 18 int i; 19 printf( "subscripts = { " ); 20 for ( i = 0; i < ndims; i++ ) { 21 printf( "%"PRId64"", out[ i ] ); 22 if ( i < ndims-1 ) { 23 printf( ", " ); 24 } 25 } 26 printf( " }\n" ); 27}
This package is part of 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, see the main project repository.
See LICENSE.
Copyright © 2016-2024. The Stdlib Authors.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
2 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 1
Reason
Found 0/30 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
no SAST tool detected
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
branch protection not enabled on development/release branches
Details
Score
Last Scanned on 2025-06-30
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More