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!
Create a matrix (i.e., a two-dimensional ndarray).
npm install @stdlib/ndarray-matrix-ctorAlternatively,
- To load the package in a website via a
scripttag without installation and bundlers, use the ES Module available on theesmbranch (see README). - If you are using Deno, visit the
denobranch (see README for usage intructions). - For use in Observable, or in browser/node environments, use the Universal Module Definition (UMD) build available on the
umdbranch (see README).
The branches.md file summarizes the available branches and displays a diagram illustrating their relationships.
To view installation and usage instructions specific to each branch build, be sure to explicitly navigate to the respective README files on each branch, as linked to above.
var matrix = require( '@stdlib/ndarray-matrix-ctor' );Returns a two-dimensional ndarray having a specified data type.
var getDType = require( '@stdlib/ndarray-dtype' );
var getShape = require( '@stdlib/ndarray-shape' );
var arr = matrix();
// returns <ndarray>
var sh = getShape( arr );
// returns [ 0, 0 ]
var dt = String( getDType( arr ) );
// returns 'float64'By default, the function returns an ndarray having a float64 data type. To specify an alternative data type, provide a dtype argument.
var getDType = require( '@stdlib/ndarray-dtype' );
var getShape = require( '@stdlib/ndarray-shape' );
var arr = matrix( 'int32' );
// returns <ndarray>
var sh = getShape( arr );
// returns [ 0, 0 ]
var dt = String( getDType( arr ) );
// returns 'int32'The function accepts the following options:
- order: specifies whether an ndarray is
'row-major'(C-style) or'column-major'(Fortran-style). Default:'row-major'. - mode: specifies how to handle indices which exceed array dimensions (see
ndarray). Default:'throw'. - submode: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions (see
ndarray). If provided fewer modes than dimensions, anndarray) instance recycles modes using modulo arithmetic. Default:[ options.mode ]. - readonly: boolean indicating whether an array should be read-only. Default:
false.
Returns a two-dimensional ndarray having a specified shape.
var getShape = require( '@stdlib/ndarray-shape' );
var arr1 = matrix( 5, 5 );
// returns <ndarray>
var sh1 = getShape( arr1 );
// returns [ 5, 5 ]
var arr2 = matrix( 3, 3, 'uint8' );
// returns <ndarray>
var sh2 = getShape( arr2 );
// returns [ 3, 3 ]Returns a two-dimensional ndarray having a specified shape.
var getShape = require( '@stdlib/ndarray-shape' );
var arr1 = matrix( [ 5, 5 ] );
// returns <ndarray>
var sh1 = getShape( arr1 );
// returns [ 5, 5 ]
var arr2 = matrix( [ 3, 3 ], 'uint8' );
// returns <ndarray>
var sh2 = getShape( arr2 );
// returns [ 3, 3 ]Creates a two-dimensional ndarray from an array-like object or iterable.
var getShape = require( '@stdlib/ndarray-shape' );
var arr1 = matrix( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
// returns <ndarray>
var sh1 = getShape( arr1 );
// returns [ 2, 2 ]
var arr2 = matrix( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ], 'float32' );
// returns <ndarray>
var sh2 = getShape( arr2 );
// returns [ 2, 2 ]If obj is an array-like object, the value must be a nested array (i.e., an array-like object of array-like objects), where each nested array must have the same number of elements. If obj is an iterable, the iterable must return array-like objects, each of which must have the same number of elements.
Returns a two-dimensional ndarray view of an ArrayBuffer.
var ArrayBuffer = require( '@stdlib/array-buffer' );
var getDType = require( '@stdlib/ndarray-dtype' );
var getShape = require( '@stdlib/ndarray-shape' );
var buf = new ArrayBuffer( 32 );
var arr1 = matrix( buf );
// returns <ndarray>
var sh1 = getShape( arr1 );
// returns [ 1, 4 ]
var dt1 = String( getDType( arr1 ) );
// returns 'float64'
var arr2 = matrix( buf, 'float32' );
// returns <ndarray>
var sh2 = getShape( arr2 );
// returns [ 1, 8 ]
var dt2 = String( getDType( arr2 ) );
// returns 'float32'
var arr3 = matrix( buf, 16 );
// returns <ndarray>
var sh3 = getShape( arr3 );
// returns [ 1, 2 ]
var dt3 = String( getDType( arr3 ) );
// returns 'float64'
var arr4 = matrix( buf, 16, 'float32' );
// returns <ndarray>
var sh4 = getShape( arr4 );
// returns [ 1, 4 ]
var dt4 = String( getDType( arr4 ) );
// returns 'float32'
var arr5 = matrix( buf, 8, 2, 1 );
// returns <ndarray>
var sh5 = getShape( arr5 );
// returns [ 2, 1 ]
var dt5 = String( getDType( arr5 ) );
// returns 'float64'
var arr6 = matrix( buf, 10, 2, 2, 'int16' );
// returns <ndarray>
var sh6 = getShape( arr6 );
// returns [ 2, 2 ]
var dt6 = String( getDType( arr6 ) );
// returns 'int16'Returns a two-dimensional ndarray view of an ArrayBuffer.
var ArrayBuffer = require( '@stdlib/array-buffer' );
var getDType = require( '@stdlib/ndarray-dtype' );
var getShape = require( '@stdlib/ndarray-shape' );
var buf = new ArrayBuffer( 32 );
var arr1 = matrix( buf, 8, [ 2, 1 ] );
// returns <ndarray>
var sh1 = getShape( arr1 );
// returns [ 2, 1 ]
var dt1 = String( getDType( arr1 ) );
// returns 'float64'
var arr2 = matrix( buf, 10, [ 2, 2 ], 'int16' );
// returns <ndarray>
var sh2 = getShape( arr2 );
// returns [ 2, 2 ]
var dt2 = String( getDType( arr2 ) );
// returns 'int16'Returns a function for creating a two-dimensional ndarray.
var getDType = require( '@stdlib/ndarray-dtype' );
var getShape = require( '@stdlib/ndarray-shape' );
var Float32Matrix = matrix.factory( 'float32' );
var arr = new Float32Matrix( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] );
// returns <ndarray>
var dt = String( getDType( arr ) );
// returns 'float32'
var len = getShape( arr );
// returns [ 2, 2 ]The function supports the following parameters:
- dtype: data type.
- options: function options (optional).
The function accepts the following options:
- order: specifies whether the default memory layout for a returned ndarray should be
'row-major'(C-style) or'column-major'(Fortran-style). Default:'row-major'. - mode: specifies the default behavior when handling indices which exceed array dimensions (see
ndarray). Default:'throw'. - submode: specifies the default behavior for each dimension when handling subscripts which exceed array dimensions (see
ndarray). If provided fewer modes than dimensions, anndarray) instance recycles modes using modulo arithmetic. Default:[ options.mode ]. - readonly: boolean indicating whether to return a read-only ndarray by default. Default:
false.
The function returned by the factory method supports the same arguments and options as matrix above, except for the dtype argument, as the returned function always returns a two-dimensional ndarray having the same data type.
When providing options to the returned function, the provided option values override the defaults established during function creation.
- Nested arrays are flattened in lexicographic order, such that, for an input nested array
obj,obj[i][j]corresponds to the elementmat.get(i, j)in the returned two-dimensional ndarray. In other words, an input nested array is assumed to be comprised of rows. Similarly, ifobjis an iterable, each array returned by the iterable is assumed to correspond to a matrix row. - If provided an
ArrayBufferwithout corresponding shape arguments, the leading dimension of the returned ndarray always has a size of one.
var discreteUniform = require( '@stdlib/random-discrete-uniform' );
var cartesianProduct = require( '@stdlib/array-cartesian-product' );
var unzip = require( '@stdlib/utils-unzip' );
var dtypes = require( '@stdlib/ndarray-dtypes' );
var ndarray2array = require( '@stdlib/ndarray-to-array' );
var sum = require( '@stdlib/blas-ext-sum' );
var logEachMap = require( '@stdlib/console-log-each-map' );
var matrix = require( '@stdlib/ndarray-matrix-ctor' );
// Create an array of random shapes:
var shapes = discreteUniform( [ 10, 2 ], 2, 8, {
'dtype': 'int32'
});
// Resolve a list of supported ndarray integer-valued data types:
var dts = dtypes( 'integer_and_generic' );
// Create shape-dtype pairs:
var pairs = cartesianProduct( ndarray2array( shapes ), dts );
// Split the pairs into individual arguments:
var args = unzip( pairs );
// Define a callback to create a random matrix and return the sum of all matrix elements:
function clbk( shape, dtype ) {
var buf = ndarray2array( discreteUniform( shape, 0, 100 ) );
var x = matrix( buf, dtype );
return sum( x ).get();
}
// Apply the callback and print the results:
logEachMap( 'shape: [%3s]. dtype: %7s. sum: %d.', args[ 0 ], args[ 1 ], clbk );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-2026. The Stdlib Authors.