Skip to content

Compute the minimum and maximum linear indices in an underlying data buffer which are accessible to an array view.

License

Notifications You must be signed in to change notification settings

stdlib-js/ndarray-base-minmax-view-buffer-index

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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!

Min and Max View Buffer Indices

NPM version Build Status Coverage Status

Compute the minimum and maximum linear indices in an underlying data buffer which are accessible to an array view.

Usage

import minmaxViewBufferIndex from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-minmax-view-buffer-index@esm/index.mjs';

You can also import the following named exports from the package:

import { assign } from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-minmax-view-buffer-index@esm/index.mjs';

minmaxViewBufferIndex( shape, strides, offset )

Computes the minimum and maximum linear indices in an underlying data buffer which are accessible to an array view.

// Array shape:
var shape = [ 2, 2 ];

// Stride array:
var strides = [ 2, 1 ];

// Index offset which specifies the location of the first indexed value:
var offset = 0;

var idx = minmaxViewBufferIndex( shape, strides, offset );
// returns [ 0, 3 ]

minmaxViewBufferIndex.assign( shape, strides, offset, out )

Computes the minimum and maximum linear indices in an underlying data buffer which are accessible to an array view and assigns results to a provided output array.

var shape = [ 2, 2 ];
var strides = [ -1, -2 ];
var offset = 3;

var out = [ 0, 0 ];
var idx = minmaxViewBufferIndex.assign( shape, strides, offset, out );
// returns [ 0, 3 ]

var bool = ( idx === out );
// returns true

Examples

<!DOCTYPE html>
<html lang="en">
<body>
<script type="module">

import discreteUniform from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-discrete-uniform@esm/index.mjs';
import shape2strides from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-shape2strides@esm/index.mjs';
import strides2offset from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-strides2offset@esm/index.mjs';
import randu from 'https://cdn.jsdelivr.net/gh/stdlib-js/random-base-randu@esm/index.mjs';
import minmaxViewBufferIndex from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-minmax-view-buffer-index@esm/index.mjs';

var strides;
var offset;
var shape;
var idx;
var i;
var j;

shape = [ 0, 0, 0 ];

for ( i = 0; i < 100; i++ ) {
    // Generate a random array shape:
    shape[ 0 ] = discreteUniform( 1, 10 );
    shape[ 1 ] = discreteUniform( 1, 10 );
    shape[ 2 ] = discreteUniform( 1, 10 );

    // Generate strides:
    if ( randu() < 0.5 ) {
        strides = shape2strides( shape, 'row-major' );
    } else {
        strides = shape2strides( shape, 'column-major' );
    }
    j = discreteUniform( 0, shape.length-1 );
    strides[ j ] *= ( randu() < 0.5 ) ? -1 : 1;

    // Compute the index offset:
    offset = strides2offset( shape, strides ) + 25; // include a view offset

    // Compute the minimum and maximum linear indices:
    idx = minmaxViewBufferIndex( shape, strides, offset );
    console.log( 'Shape: %s. Strides: %s. Offset: %d. Min idx: %d. Max idx: %d.', shape.join( 'x' ), strides.join( ',' ), offset, idx[ 0 ], idx[ 1 ] );
}

</script>
</body>
</html>

Notice

This package is part of stdlib, a standard library 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-2025. The Stdlib Authors.