Create a multidimensional array.
var array = require( '@stdlib/ndarray/array' );
Returns a multidimensional array.
// Create a 2x2 matrix:
var arr = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
// returns <ndarray>
To initialize multidimensional array data, provide a buffer
argument, which may be a generic array, typed array, Buffer, or ndarray.
var Float64Array = require( '@stdlib/array/float64' );
var alloc = require( '@stdlib/buffer/alloc' );
// Create an ndarray from a generic array linear data buffer:
var arr = array( [ 1.0, 2.0, 3.0, 4.0 ], { 'shape': [ 2, 2 ] } );
// returns <ndarray>
// Create an ndarray from a typed array linear data buffer:
arr = array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ), { 'shape': [ 2, 2 ] } );
// returns <ndarray>
// Create an ndarray as a view over a Buffer:
arr = array( alloc( 4 ), { 'shape': [ 2, 2 ] } );
// returns <ndarray>
// Create an ndarray from another ndarray:
arr = array( array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ) );
// returns <ndarray>
The function accepts the following options
:
buffer
: data source. If provided along with abuffer
argument, the argument takes precedence.dtype
: underlying storage data type. If the input data source is not of the same type, this option specifies the data type to which to cast the input data. Any time a cast is required, thecopy
option is set totrue
, as memory must be copied from the data source to an output data buffer. Default:'float64'
.order
: specifies the memory layout of the data source as either row-major (C-style) or column-major (Fortran-style). If set to'any'
, if a data source is column-major and not row-major, the order of the returned array is column-major; otherwise, the order of the returned array is always row-major. If set to'same'
, the order of the returned array matches the order of an input data source. If set to either'row-major'
or'column-major'
, the order of the returned array is set to the option value. Note that specifying an order which differs from the order of a provided data source does not entail a conversion from one memory layout to another. In short, this option is descriptive, not prescriptive. Default:'row-major'
.shape
: array shape (dimensions). If a shape is not specified, the function attempts to infer a shape based on a provided data source. For example, if provided a nested array, the function resolves nested array dimensions. If provided a multidimensional array data source, the function uses the array's associated shape. For most use cases, such inference suffices. In the remaining use cases, specifying a shape is necessary. For example, provide a shape to create a multidimensional array view over a linear data buffer, ignoring any existing shape meta data associated with a provided data source.flatten
:boolean
indicating whether to automatically flatten generic array data sources. If an array shape is not specified, the shape is inferred from the dimensions of nested arrays prior to flattening. If a use case requires partial flattening, partially flatten prior to invoking this function and set the option value tofalse
to prevent further flattening during invocation. Default:true
.copy
:boolean
indicating whether to (shallow) copy source data to a new data buffer. The function does not perform a deep copy. To prevent undesired shared changes in state for generic arrays containing objects, perform a deep copy prior to invoking this function. Default:false
.codegen
:boolean
indicating whether to use code generation. Default:true
.mode
: specifies how to handle indices which exceed array dimensions. Default:'throw'
.submode
: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions. If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. Default:[ options.mode ]
.
The function supports the following modes
:
throw
: specifies that anndarray
instance should throw an error when an index exceeds array dimensions.wrap
: specifies that anndarray
instance should wrap around an index exceeding array dimensions using modulo arithmetic.clamp
: specifies that anndarray
instance should set an index exceeding array dimensions to either0
(minimum index) or the maximum index.
By default, the function returns ndarray
instances which use code generation for fast element lookup. To disable code generation, set the codegen
option to false
.
var opts = {
'codegen': false
};
var arr = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], opts );
// returns <ndarray>
By default, an ndarray
instance throws when provided an index which exceeds array dimensions. To support alternative indexing behavior, set the mode
option, which will affect all public methods for getting and setting array elements.
var opts = {
'mode': 'clamp'
};
var arr = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], opts );
// returns <ndarray>
// Attempt to access an out-of-bounds linear index (clamped):
var v = arr.iget( 10 );
// returns 4.0
By default, the mode
option is applied to subscripts which exceed array dimensions. To specify behavior for each dimension, set the submode
option.
var opts = {
'submode': [ 'wrap', 'clamp' ]
};
var arr = array( [ [[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]] ], opts );
// returns <ndarray>
// Attempt to access out-of-bounds subscripts:
var v = arr.get( -2, 10, -1 ); // linear index: 3
// returns 4.0
By default, the function automatically flattens generic array data sources. To prevent flattening, set the flatten
option to false
.
var opts = {
'flatten': false
};
// Create a generic array which will serve as our ndarray data source:
var buf = [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ];
// Create a 2-element vector:
var arr = array( buf, opts );
// returns <ndarray>
// Retrieve the first vector element:
var v = arr.get( 0 );
// returns [ 1.0, 2.0 ]
var bool = ( v === buf[ 0 ] );
// returns true
- Code generation can boost performance, but may be problematic in browser contexts enforcing a strict content security policy (CSP). If running in or targeting an environment with a CSP, set the
codegen
option tofalse
. - The implementation uses a variety of techniques to boost multidimensional creation performance; however, especially for highly specialized high-dimensional
ndarray
instances, better performance may be realized by using lower-level constructors. - The number of elements in a data source
buffer
must agree with a specified arrayshape
(i.e., the function assumes a single-segment contiguousndarray
). To create arbitrary multidimensional views over linear data buffers, use a lower-level constructor.
var array = require( '@stdlib/ndarray/array' );
// Create a 4-dimensional array containing single-precision floating-point numbers:
var arr = array({
'dtype': 'float32',
'shape': [ 3, 3, 3, 3 ]
});
// Retrieve an array value:
var v = arr.get( 1, 2, 1, 2 );
// returns 0.0
// Set an array value:
arr.set( 1, 2, 1, 2, 10.0 );
// Retrieve the array value:
v = arr.get( 1, 2, 1, 2 );
// returns 10.0
// Serialize the array as a string:
var str = arr.toString();
// returns ndarray( new Float32Array( [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] ), [ 3, 3, 3, 3 ], [ 27, 9, 3, 1 ], 0, 'row-major' )
// Serialize the array as JSON:
str = JSON.stringify( arr.toJSON() );
// returns {"type":"ndarray","dtype":"float32","flags":{},"order":"row-major","shape":[3,3,3,3],"strides":[27,9,3,1],"data":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]}