Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

descriptor

Create a plain object describing how to interpret a data buffer as an n-dimensional array.

Usage

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

descriptor( dtype, buffer, shape, strides, offset, order )

Returns a plain object describing how to interpret a data buffer as an n-dimensional array.

var buffer = [ 1, 2, 3, 4, 5, 6 ];
var shape = [ 3, 2 ];
var strides = [ 2, 1 ];
var offset = 0;

var out = descriptor( 'generic', buffer, shape, strides, offset, 'row-major' );
// returns {...}

The function accepts the following arguments:

  • dtype: data type.
  • buffer: data buffer.
  • shape: array shape (dimensions).
  • strides: array strides which are index offsets specifying how to access along corresponding dimensions.
  • offset: index offset specifying the location of the first indexed element in the data buffer.
  • order: array order. Must be either row-major (C-style) or column-major (Fortran-style).

Notes

  • The returned object has the following properties:

    • dtype: data type.
    • data: data buffer.
    • shape: array shape.
    • strides: array strides.
    • offset: index offset.
    • order: array order.
  • This function is intended as a potential performance optimization. In V8, for example, even if two objects share common properties, if those properties were added in different orders or if one object has additional properties not shared by the other object, then those objects will have different "hidden" classes. If a function is provided many objects having different "shapes", some JavaScript VMs (e.g., V8) will consider the function "megamorphic" and fail to perform various runtime optimizations. Accordingly, the intent of this function is to create an object containing the minimal amount of meta data necessary to interpret a linear data buffer as an n-dimensional array and to ensure that internal functions operating on ndarrays are provided consistent argument "shapes".

Examples

var discreteUniform = require( '@stdlib/random/discrete-uniform' );
var getDType = require( '@stdlib/ndarray/dtype' );
var getData = require( '@stdlib/ndarray/data-buffer' );
var getShape = require( '@stdlib/ndarray/shape' );
var getStrides = require( '@stdlib/ndarray/strides' );
var getOffset = require( '@stdlib/ndarray/offset' );
var getOrder = require( '@stdlib/ndarray/order' );
var ndarray2array = require( '@stdlib/ndarray/to-array' );
var descriptor = require( '@stdlib/ndarray/base/descriptor' );

var x = discreteUniform( [ 3, 3 ], 0, 10, {
    'dtype': 'generic'
});
console.log( ndarray2array( x ) );

var obj = descriptor( getDType( x ), getData( x ), getShape( x ), getStrides( x ), getOffset( x ), getOrder( x ) );
console.log( obj );