Create a plain object describing how to interpret a data buffer as an n-dimensional array.
var descriptor = require( '@stdlib/ndarray/base/descriptor' );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) orcolumn-major(Fortran-style).
-
The returned object has the following properties:
-
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".
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 );