About stdlib...
We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.
The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.
When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.
To join us in bringing numerical computing to the web, get started by checking us out on GitHub, and please consider financially supporting stdlib. We greatly appreciate your continued support!
Arrays.
npm install @stdlib/array
Alternatively,
- To load the package in a website via a
script
tag without installation and bundlers, use the ES Module available on theesm
branch. - If you are using Deno, visit the
deno
branch. - For use in Observable, or in browser/node environments, use the Universal Module Definition (UMD) build available on the
umd
branch.
The branches.md file summarizes the available branches and displays a diagram illustrating their relationships.
var ns = require( '@stdlib/array' );
Arrays.
var o = ns;
// returns {...}
The namespace exports the following array constructors:
ArrayBuffer( size )
: constructor which returns an object used to represent a generic, fixed-length raw binary data buffer.Float32Array()
: typed array constructor which returns a typed array representing an array of single-precision floating-point numbers in the platform byte order.Float64Array()
: typed array constructor which returns a typed array representing an array of double-precision floating-point numbers in the platform byte order.Int16Array()
: typed array constructor which returns a typed array representing an array of twos-complement 16-bit signed integers in the platform byte order.Int32Array()
: typed array constructor which returns a typed array representing an array of twos-complement 32-bit signed integers in the platform byte order.Int8Array()
: typed array constructor which returns a typed array representing an array of twos-complement 8-bit signed integers in the platform byte order.SharedArrayBuffer( size )
: constructor returning an object used to represent a generic, fixed-length raw binary data buffer which can be used to create views of shared memory.Uint16Array()
: typed array constructor which returns a typed array representing an array of 16-bit unsigned integers in the platform byte order.Uint32Array()
: typed array constructor which returns a typed array representing an array of 32-bit unsigned integers in the platform byte order.Uint8Array()
: typed array constructor which returns a typed array representing an array of 8-bit unsigned integers in the platform byte order.Uint8ClampedArray()
: typed array constructor which returns a typed array representing an array of 8-bit unsigned integers in the platform byte order clamped to 0-255.
var arr = new ns.Int32Array( 5 );
// returns <Int32Array>[ 0, 0, 0, 0, 0 ]
Alternatively, use the typedarray
function to create a typed array of a given data type:
typedarray()
: create a typed array.
var arr1 = ns.typedarray( 5 );
// returns <Float64Array>[ 0.0, 0.0, 0.0, 0.0, 0.0 ]
var arr2 = ns.typedarray( 5, 'uint8' );
// returns <Uint8Array>[ 0, 0, 0, 0, 0 ]
The namespace contains functions to create arrays pre-filled with spaced values:
datespace( start, stop[, length][, opts] )
: generate an array of linearly spaced dates.incrspace( start, stop[, increment] )
: generate a linearly spaced numeric array using a provided increment.linspace( start, stop, length[, options] )
: generate a linearly spaced array over a specified interval.logspace( a, b[, length] )
: generate a logarithmically spaced numeric array.
You can use the following functions to retrieve a list of available data types:
arrayDataTypes()
: list of array data types.complexarrayDataTypes()
: list of complex typed array data types.typedarrayDataTypes()
: list of typed array data types.floatarrayDataTypes()
: list of typed array floating-point data types.intarrayDataTypes()
: list of typed array integer data types.realarrayDataTypes()
: list of typed array data types.realarrayFloatDataTypes()
: list of typed array real-valued floating-point data types.intarraySignedDataTypes()
: list of typed array signed integer data types.intarrayUnsignedDataTypes()
: list of typed array unsigned integer data types.
var DTYPES = ns.arrayDataTypes();
// returns [ 'float32', 'float64', 'generic', 'int16', 'int32', 'int8', 'uint16', 'uint32', 'uint8', 'uint8c' ]
Furthermore, the namespace contains utility functions to retrieve a given constructor:
arrayCtors( dtype )
: array constructors.complexarrayCtors( dtype )
: complex typed array constructors.typedarrayCtors( dtype )
: typed array constructors.floatarrayCtors( dtype )
: floating-point typed array constructors.intarrayCtors( dtype )
: integer-valued typed array constructors.realarrayCtors( dtype )
: typed array constructors.realarrayFloatCtors( dtype )
: real-valued floating-point typed array constructors.intarraySignedCtors( dtype )
: signed integer typed array constructors.intarrayUnsignedCtors( dtype )
: unsigned integer typed array constructors.
var ctor = ns.typedarrayCtors( 'float64' );
// returns <Function>
ctor = ns.typedarrayCtors( 'int' );
// returns null
Lastly, the namespace contains various other functions for dealing with arrays, including functions to convert arrays from one data type to another or to serialize them as JSON and vice versa.
base
: base (i.e., lower-level) array utilities.Complex128Array()
: 128-bit complex number array.Complex64Array()
: 64-bit complex number array.convertArraySame( x, y )
: convert an array to the same data type as a second input array.convertArray( arr, dtype )
: convert an array to an array of a different data type.DataView( buffer[, byteOffset[, byteLength]] )
: constructor which returns a data view representing a provided array buffer.arrayDataType( array )
: return the data type of an array.aemptyLike( x[, dtype] )
: create an uninitialized array having the same length and data type as a provided array.aempty( length[, dtype] )
: create an uninitialized array having a specified length.filledarrayBy()
: create a filled array according to a provided callback function.filledarray()
: create a filled array.iterator2array( iterator[, out][, mapFcn[, thisArg]] )
: create (or fill) an array from an iterator.afullLike( x, value[, dtype] )
: create a filled array having the same length and data type as a provided array.afull( length, value[, dtype] )
: create a filled array having a specified length.arrayMinDataType( value )
: determine the minimum array data type of the closest "kind" necessary for storing a provided scalar value.anansLike( x[, dtype] )
: create an array filled with NaNs and having the same length and data type as a provided array.anans( length[, dtype] )
: create an array filled with NaNs and having a specified length.arrayNextDataType( [dtype] )
: return the next larger array data type of the same kind.aonesLike( x[, dtype] )
: create an array filled with ones and having the same length and data type as a provided array.aones( length[, dtype] )
: create an array filled with ones and having a specified length.typedarraypool()
: allocate typed arrays from a typed array memory pool.arrayPromotionRules( [dtype1, dtype2] )
: return the array data type with the smallest size and closest "kind" to which array data types can be safely cast.reviveTypedArray( key, value )
: revive a JSON-serialized typed array.arraySafeCasts( [dtype] )
: return a list of array data types to which a provided array data type can be safely cast.arraySameKindCasts( [dtype] )
: return a list of array data types to which a provided array data type can be safely cast or cast within the same "kind".arrayShape( arr )
: determine (nested) array dimensions.circarray2iterator( src[, options][, mapFcn[, thisArg]] )
: create an iterator which repeatedly iterates over the elements of an array-like object.array2iteratorRight( src[, mapFcn[, thisArg]] )
: create an iterator from an array-like object, iterating from right to left.array2iterator( src[, mapFcn[, thisArg]] )
: create an iterator from an array-like object.typedarray2json( typedarray )
: return a JSON representation of a typed array.sparsearray2iteratorRight( src[, mapFcn[, thisArg]] )
: create an iterator from a sparse array-like object, iterating from right to left.sparsearray2iterator( src[, mapFcn[, thisArg]] )
: create an iterator from a sparse array-like object.stridedarray2iterator( N, src, stride, offset[, mapFcn[, thisArg]] )
: create an iterator from a strided array-like object.arrayview2iteratorRight( src[, begin[, end]][, mapFcn[, thisArg]] )
: create an iterator from an array-like object view, iterating from right to left.arrayview2iterator( src[, begin[, end]][, mapFcn[, thisArg]] )
: create an iterator from an array-like object view.complexarray()
: create a complex number typed array.realarray()
: create a typed array.azerosLike( x[, dtype] )
: create a zero-filled array having the same length and data type as a provided array.azeros( length[, dtype] )
: create a zero-filled array having a specified length.
var objectKeys = require( '@stdlib/utils/keys' );
var ns = require( '@stdlib/array' );
console.log( objectKeys( ns ) );
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-2023. The Stdlib Authors.