![Create React App Officially Deprecated Amid React 19 Compatibility Issues](https://cdn.sanity.io/images/cgdhsj6q/production/04fa08cf844d798abc0e1a6391c129363cc7e2ab-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Create React App Officially Deprecated Amid React 19 Compatibility Issues
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
@stdlib/ndarray-base-ctor
Advanced tools
Create a multidimensional array.
npm install @stdlib/ndarray-base-ctor
var ndarray = require( '@stdlib/ndarray-base-ctor' );
Returns an ndarray
instance.
// Specify the array configuration:
var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
// Create a new ndarray:
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
// returns <ndarray>
The constructor has the following parameters:
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;
// Create a new zero-dimensional array:
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
// returns <ndarray>
String value of the ndarray constructor name.
var str = ndarray.name;
// returns 'ndarray'
Size (in bytes) of the array (if known).
var Float64Array = require( '@stdlib/array-float64' );
// Specify the array configuration:
var buffer = new Float64Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
// Create a new ndarray:
var arr = ndarray( 'float64', buffer, shape, strides, offset, order );
// Get the byte length:
var nbytes = arr.byteLength;
// returns 32
If unable to determine the size of the array, the property value is null
.
// Specify the array configuration:
var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
// Create a new ndarray:
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
// Get the byte length:
var nbytes = arr.byteLength;
// returns null
Size (in bytes) of each array element (if known).
var Float32Array = require( '@stdlib/array-float32' );
// Specify the array configuration:
var buffer = new Float32Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
// Create a new ndarray:
var arr = ndarray( 'float32', buffer, shape, strides, offset, order );
// Get the number of bytes per element:
var nbytes = arr.BYTES_PER_ELEMENT;
// returns 4
If size of each array element is unknown, the property value is null
.
// Specify the array configuration:
var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
// Create a new ndarray:
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
// Get the number of bytes per element:
var nbytes = arr.BYTES_PER_ELEMENT;
// returns null
A reference to the underlying data buffer.
var Int8Array = require( '@stdlib/array-int8' );
// Specify the array configuration:
var buffer = new Int8Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
// Create a new ndarray:
var arr = ndarray( 'int8', buffer, shape, strides, offset, order );
// Get the buffer reference:
var d = arr.data;
// returns <Int8Array>[ 1, 2, 3, 4 ]
var bool = ( d === buffer );
// returns true
Underlying data type.
var Uint8Array = require( '@stdlib/array-uint8' );
// Specify the array configuration:
var buffer = new Uint8Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ -2, 1 ];
var offset = 2;
// Create a new ndarray:
var arr = ndarray( 'uint8', buffer, shape, strides, offset, order );
// Get the underlying data type:
var dtype = arr.dtype;
// returns 'uint8'
Information regarding the memory layout of the array. The returned object
has the following properties:
boolean
indicating if an array is row-major contiguous.boolean
indicating if an array is column-major contiguous.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' );
// Specify the array configuration:
var buffer = new Int32Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'column-major';
var strides = [ 1, 2 ];
var offset = 0;
// Create a new ndarray:
var arr = ndarray( 'int32', buffer, shape, strides, offset, order );
// Get the array flags:
var flg = arr.flags;
// returns {...}
Number of array elements.
var Uint16Array = require( '@stdlib/array-uint16' );
// Specify the array configuration:
var buffer = new Uint16Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'column-major';
var strides = [ -1, -2 ];
var offset = 3;
// Create a new ndarray:
var arr = ndarray( 'uint16', buffer, shape, strides, offset, order );
// Get the array length:
var len = arr.length;
// returns 4
Number of dimensions.
var Uint8ClampedArray = require( '@stdlib/array-uint8c' );
// Specify the array configuration:
var buffer = new Uint8ClampedArray( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ -2, -1 ];
var offset = 3;
// Create a new ndarray:
var arr = ndarray( 'uint8c', buffer, shape, strides, offset, order );
// Get the number of dimensions:
var ndims = arr.ndims;
// returns 2
Index offset which specifies the buffer
index at which to start iterating over array elements.
var Int16Array = require( '@stdlib/array-int16' );
// Specify the array configuration:
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;
// Create a new ndarray:
var arr = ndarray( 'int16', buffer, shape, strides, offset, order );
// Get the index offset:
var o = arr.offset;
// returns 10
Array order. The array order is either row-major (C-style) or column-major (Fortran-style).
var Uint32Array = require( '@stdlib/array-uint32' );
// Specify the array configuration:
var buffer = new Uint32Array( [ 1, 2, 3, 4 ] );
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
// Create a new ndarray:
var arr = ndarray( 'uint32', buffer, shape, strides, offset, order );
// Get the array order:
var ord = arr.order;
// returns 'row-major'
Returns a copy of the array shape.
// Specify the array configuration:
var buffer = [ 1, 2, 3, 4, 5, 6 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 2;
// Create a new ndarray:
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
// Get the array shape:
var dims = arr.shape;
// returns [ 2, 2 ]
Returns a copy of the array strides which specify how to access data along corresponding array dimensions.
// Specify the array configuration:
var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'column-major';
var strides = [ -1, 2 ];
var offset = 1;
// Create a new ndarray:
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
// Get the array strides:
var s = arr.strides;
// returns [ -1, 2 ]
Returns an array element specified according to provided subscripts. The number of provided subscripts should equal the number of dimensions.
// Specify the array configuration:
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;
// Create a new ndarray:
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
// Get the element located at (1,1):
var v = arr.get( 1, 1 );
// returns 6
Returns an array element located at a specified linear index.
// Specify the array configuration:
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;
// Create a new ndarray:
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
// Get the element located at index 3:
var v = arr.iget( 3 );
// returns 6
For zero-dimensional arrays, the input argument is ignored and, for clarity, should not be provided.
Sets an array element specified according to provided subscripts. The number of provided subscripts should equal the number of dimensions.
// Specify the array configuration:
var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
// Create a new ndarray:
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
// Set the element located at (1,1):
arr.set( 1, 1, 40 );
var v = arr.get( 1, 1 );
// returns 40
// Get the underlying buffer:
var d = arr.data;
// returns [ 1, 2, 3, 40 ]
The method returns the ndarray
instance.
Sets an array element located at a specified linear index.
// Specify the array configuration:
var buffer = [ 1, 2, 3, 4 ];
var shape = [ 2, 2 ];
var order = 'row-major';
var strides = [ 2, 1 ];
var offset = 0;
// Create a new ndarray:
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
// Set the element located at index 3:
arr.iset( 3, 40 );
var v = arr.iget( 3 );
// returns 40
// Get the underlying buffer:
var d = arr.data;
// returns [ 1, 2, 3, 40 ]
For zero-dimensional arrays, the first, and only, argument should be the value v
to set. The method returns the ndarray
instance.
Serializes an ndarray
as a string
.
// Specify the array configuration:
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;
// Create a new ndarray:
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
// Serialize to a string:
var str = arr.toString();
// returns "ndarray( 'generic', [ 3, 4, 5, 6, 7, 8 ], [ 3, 2 ], [ 2, 1 ], 0, 'row-major' )"
The method does not serialize data outside of the buffer region defined by the array configuration.
Serializes an ndarray
as a JSON object
. JSON.stringify()
implicitly calls this method when stringifying an ndarray
instance.
// Specify the array configuration:
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;
// Create a new ndarray:
var arr = ndarray( 'generic', buffer, shape, strides, offset, order );
// Serialize to JSON:
var o = arr.toJSON();
// returns { 'type': 'ndarray', 'dtype': 'generic', 'flags': {...}, 'offset': 0, 'order': 'row-major', 'shape': [ 3, 2 ], 'strides': [ 2, 1 ], 'data': [ 3, 4, 5, 6, 7, 8 ] }
The method does not serialize data outside of the buffer region defined by the array configuration.
length
property). For data buffers which are not indexed collections (i.e., collections which cannot support direct index access, such as buffer[ index ]
; e.g., Complex64Array
, Complex128Array
, etc), a data buffer should provide #.get( idx )
and #.set( v[, idx] )
methods. Note that, for set
methods, the value to set should be the first argument, followed by the linear index, similar to the native typed array set
method.var Float32Array = require( '@stdlib/array-float32' );
var ndarray = require( '@stdlib/ndarray-base-ctor' );
// Create a data buffer:
var buffer = new Float32Array( (3*3*3*3) + 100 );
// Specify the array shape:
var shape = [ 3, 3, 3, 3 ];
// Specify the array strides:
var strides = [ 27, 9, 3, 1 ];
// Specify the index offset:
var offset = 4;
// Specify the order:
var order = 'row-major'; // C-style
// Create a new ndarray:
var arr = ndarray( 'float32', buffer, shape, strides, offset, order );
// 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( 'float32', 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]}'
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
See LICENSE.
Copyright © 2016-2021. The Stdlib Authors.
0.0.4 (2021-07-07)
No changes reported for this release.
</section> <!-- /.release --> <section class="release" id="v0.0.3">FAQs
Base multidimensional array.
The npm package @stdlib/ndarray-base-ctor receives a total of 1,040 weekly downloads. As such, @stdlib/ndarray-base-ctor popularity was classified as popular.
We found that @stdlib/ndarray-base-ctor demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.