shape2strides

Generate a stride array from an array shape.

Usage

var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );

shape2strides( [out,] shape, order )

Generates a stride array from an array shape.

var strides = shape2strides( [ 3, 2 ], 'row-major' );
// returns [ 2, 1 ]

The order parameter specifies whether an array is row-major (C-style) or column-major (Fortran-style).

var strides = shape2strides( [ 3, 2 ], 'column-major' );
// returns [ 1, 3 ]

By default, the function returns strides in a new array. To avoid unnecessary memory allocation, the function supports providing an output (destination) object.

var shape = [ 3, 2 ];
var strides = new Array( shape.length );

var out = shape2strides( strides, shape, 'row-major' );
// returns [ 2, 1 ]

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

Examples

var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );

var strides;
var shape;
var i;

shape = new Array( 3 );
for ( i = 0; i < 100; i++ ) {
    shape[ 0 ] = discreteUniform( 1, 10 );
    shape[ 1 ] = discreteUniform( 1, 10 );
    shape[ 2 ] = discreteUniform( 1, 10 );
    strides = shape2strides( shape, 'row-major' );
    console.log( 'shape: %s. strides: %s.', shape.join( 'x' ), strides.join( ', ' ) );
}