Skip to content

Latest commit

 

History

History
422 lines (290 loc) · 12.8 KB

README.md

File metadata and controls

422 lines (290 loc) · 12.8 KB
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

Convert a linear index to an array of subscripts.

Usage

To use in Observable,

ind2sub = require( 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-ind2sub@umd/browser.js' )

To vendor stdlib functionality and avoid installing dependency trees for Node.js, you can use the UMD server build:

var ind2sub = require( 'path/to/vendor/umd/ndarray-base-ind2sub/index.js' )

To include the bundle in a webpage,

<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-ind2sub@umd/browser.js"></script>

If no recognized module system is present, access bundle contents via the global scope:

<script type="text/javascript">
(function () {
    window.ind2sub;
})();
</script>

ind2sub( shape, strides, offset, order, idx, mode )

Converts a linear index to an array of subscripts.

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.
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).

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.

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.

    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 ]
    
    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

<!DOCTYPE html>
<html lang="en">
<body>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/random-base-discrete-uniform@umd/browser.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-shape2strides@umd/browser.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-strides2offset@umd/browser.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-numel@umd/browser.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@umd/browser.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/math-base-special-abs@umd/browser.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-ind2sub@umd/browser.js"></script>
<script type="text/javascript">
(function () {

// 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 += ', ';
        }
    }
}

})();
</script>
</body>
</html>

Notice

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.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.