Usage
var ndarray = require( '@stdlib/ndarray-base-ctor' );
ndarray( dtype, buffer, shape, strides, offset, order )
Returns an ndarray
instance.
var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
The constructor has the following parameters:
- dtype: underlying 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, which is either
row-major
(C-style) or column-major
(Fortran-style).
To create a zero-dimensional array, provide an empty shape
and a single strides
element equal to 0
. The order
can be either row-major
or column-major
and has no effect on data storage or access.
var buffer = [ 1 ];
var shape = [];
var order = 'row-major';
var strides = [ 0 ];
var offset = 0;
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
Properties
ndarray.name
String value of the ndarray constructor name.
var str = ndarray.name;
ndarray.prototype.byteLength
Size (in bytes) of the array (if known).
var Float64Array = require( '@stdlib/array-float64' );
var buffer = new Float64Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
var arr = ndarray( 'float64', buffer, shape, strides, offset, order );
var nbytes = arr.byteLength;
If unable to determine the size of the array, the property value is null
.
var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
var nbytes = arr.byteLength;
ndarray.prototype.BYTES_PER_ELEMENT
Size (in bytes) of each array element (if known).
var Float32Array = require( '@stdlib/array-float32' );
var buffer = new Float32Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
var arr = ndarray( 'float32', buffer, shape, strides, offset, order );
var nbytes = arr.BYTES_PER_ELEMENT;
If size of each array element is unknown, the property value is null
.
var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
var nbytes = arr.BYTES_PER_ELEMENT;
ndarray.prototype.data
A reference to the underlying data buffer.
var Int8Array = require( '@stdlib/array-int8' );
var buffer = new Int8Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
var arr = ndarray( 'int8', buffer, shape, strides, offset, order );
var d = arr.data;
var bool = ( d === buffer );
ndarray.prototype.dtype
Underlying data type.
var Uint8Array = require( '@stdlib/array-uint8' );
var buffer = new Uint8Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ -2, 1 ];
var offset = 2;
var arr = ndarray( 'uint8', buffer, shape, strides, offset, order );
var dtype = arr.dtype;
ndarray.prototype.flags
Meta information, such as information concerning the memory layout of the array. The returned object
has the following properties:
- ROW_MAJOR_CONTIGUOUS:
boolean
indicating if an array is row-major contiguous. - COLUMN_MAJOR_CONTIGUOUS:
boolean
indicating if an array is column-major contiguous. - READONLY:
boolean
indicating if an array is read-only.
An array is contiguous if (1) an array is compatible with being stored in a single memory segment and (2) each array element is adjacent to the next array element. Note that an array can be both row-major contiguous and column-major contiguous at the same time (e.g., if an array is a 1-dimensional ndarray with strides = [1]
).
var Int32Array = require( '@stdlib/array-int32' );
var buffer = new Int32Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'column-major';
var strides = [ 1, 2 ];
var offset = 0;
var arr = ndarray( 'int32', buffer, shape, strides, offset, order );
var flg = arr.flags;
ndarray.prototype.length
Number of array elements.
var Uint16Array = require( '@stdlib/array-uint16' );
var buffer = new Uint16Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'column-major';
var strides = [ -1, -2 ];
var offset = 3;
var arr = ndarray( 'uint16', buffer, shape, strides, offset, order );
var len = arr.length;
ndarray.prototype.ndims
Number of dimensions.
var Uint8ClampedArray = require( '@stdlib/array-uint8c' );
var buffer = new Uint8ClampedArray( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ -2, -1 ];
var offset = 3;
var arr = ndarray( 'uint8c', buffer, shape, strides, offset, order );
var ndims = arr.ndims;
ndarray.prototype.offset
Index offset which specifies the buffer
index at which to start iterating over array elements.
var Int16Array = require( '@stdlib/array-int16' );
var buffer = new Int16Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ -2, -1 ];
var offset = 10;
var arr = ndarray( 'int16', buffer, shape, strides, offset, order );
var o = arr.offset;
ndarray.prototype.order
Array order. The array order is either row-major (C-style) or column-major (Fortran-style).
var Uint32Array = require( '@stdlib/array-uint32' );
var buffer = new Uint32Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
var arr = ndarray( 'uint32', buffer, shape, strides, offset, order );
var ord = arr.order;
ndarray.prototype.shape
Returns a copy of the array shape.
var buffer = [ 1, 2, 3, 4, 5, 6 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 2;
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
var dims = arr.shape;
ndarray.prototype.strides
Returns a copy of the array strides which specify how to access data along corresponding array dimensions.
var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'column-major';
var strides = [ -1, 2 ];
var offset = 1;
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
var s = arr.strides;
Methods
ndarray.prototype.get( i, j, k, ... )
Returns an array element specified according to provided subscripts. The number of provided subscripts should equal the number of dimensions.
var buffer = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 2;
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
var v = arr.get( 1, 1 );
ndarray.prototype.iget( idx )
Returns an array element located at a specified linear index.
var buffer = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 2;
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
var v = arr.iget( 3 );
For zero-dimensional arrays, the input argument is ignored and, for clarity, should not be provided.
ndarray.prototype.set( i, j, k, ..., v )
Sets an array element specified according to provided subscripts. The number of provided subscripts should equal the number of dimensions.
var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
arr.set( 1, 1, 40 );
var v = arr.get( 1, 1 );
var d = arr.data;
The method returns the ndarray
instance.
ndarray.prototype.iset( idx, v )
Sets an array element located at a specified linear index.
var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
arr.iset( 3, 40 );
var v = arr.iget( 3 );
var d = arr.data;
For zero-dimensional arrays, the first, and only, argument should be the value v
to set. The method returns the ndarray
instance.
ndarray.prototype.toString()
Serializes an ndarray
as a string
.
var buffer = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
var shape = [ 3, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 2;
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
var str = arr.toString();
The method does not serialize data outside of the buffer region defined by the array configuration.
ndarray.prototype.toJSON()
Serializes an ndarray
as a JSON object
. JSON.stringify()
implicitly calls this method when stringifying an ndarray
instance.
var buffer = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
var shape = [ 3, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 2;
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
var o = arr.toJSON();
The method does not serialize data outside of the buffer region defined by the array configuration.