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!
Convert a scalar value to a zero-dimensional ndarray having the same data type as a provided ndarray.
import scalar2ndarrayLike from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-from-scalar-like@esm/index.mjs';
Returns a zero-dimensional ndarray containing a provided scalar value
and having the same data type as a provided ndarray.
import zeros from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-zeros@esm/index.mjs';
var x = zeros( 'float32', [ 2, 2 ], 'row-major' );
// returns <ndarray>
var y = scalar2ndarrayLike( x, 1.0 );
// returns <ndarray>
var sh = y.shape;
// returns []
var dt = y.dtype;
// returns 'float32'
var v = y.get();
// returns 1.0
- Along with data type and order, the function infers the "class" of the returned ndarray from the provided ndarray. For example, if provided a "base" ndarray, the function returns a base ndarray. If provided a non-base ndarray, the function returns a non-base ndarray.
- If
value
is a number and a provided ndarray has a complex data type, the function returns a zero-dimensional ndarray containing a complex number whose real component equals the provided scalarvalue
and whose imaginary component is zero.
<!DOCTYPE html>
<html lang="en">
<body>
<script type="module">
import dtypes from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-dtypes@esm/index.mjs';
import empty from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-empty@esm/index.mjs';
import scalar2ndarrayLike from 'https://cdn.jsdelivr.net/gh/stdlib-js/ndarray-base-from-scalar-like@esm/index.mjs';
// Get a list of data types:
var dt = dtypes();
// Generate zero-dimensional arrays...
var x;
var y;
var i;
for ( i = 0; i < dt.length; i++ ) {
x = empty( dt[ i ], [ 2, 2 ], 'row-major' );
y = scalar2ndarrayLike( x, i );
console.log( y.get() );
}
</script>
</body>
</html>
This package is part of stdlib, a standard library 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-2025. The Stdlib Authors.