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!
Return a new ndarray filled with linearly spaced values over a specified interval along one or more ndarray dimensions.
npm install @stdlib/blas-ext-linspaceAlternatively,
- 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 linspace = require( '@stdlib/blas-ext-linspace' );Returns a new ndarray filled with linearly spaced values over a specified interval along one or more ndarray dimensions.
var ndarray2array = require( '@stdlib/ndarray-to-array' );
var x = linspace( [ 4 ], 1.0, 4.0 );
// returns <ndarray>
var arr = ndarray2array( x );
// returns [ 1.0, 2.0, 3.0, 4.0 ]The function has the following parameters:
- shape: array shape.
- start: start of interval. May be either a number, a complex number, or an ndarray having a numeric or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined by
options.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], a start ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided input shape, a start ndarray must be a zero-dimensional ndarray. - stop: end of interval. May be either a number, a complex number, or an ndarray having a numeric or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined by
options.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], a stop ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided input shape, a stop ndarray must be a zero-dimensional ndarray. - endpoint: specifies whether to include the end of the interval when writing values to the output ndarray (optional). May be either a boolean or an ndarray having a boolean or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined by
options.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], an endpoint ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided input shape, an endpoint ndarray must be a zero-dimensional ndarray. Default:true. - options: function options (optional).
The function accepts the following options:
- dims: list of dimensions over which to perform operation. If not provided, the function generates linearly spaced values along the last dimension. Default:
[-1]. - dtype: output ndarray data type. If both
startandstopare real-valued, the output array data type may be any floating-point data type or "generic". However, if eitherstartorstopare complex-valued, the output array type must be a complex floating-point data type or "generic". If a data type is provided,startandstopare cast to the specified data type. If a data type is not provided and bothstartandstopare the same type (either'float64','complex64', or'complex128'), the default output array data type is the same type as the input values (either'float64','complex64', or'complex128', respectively). Otherwise, if a data type is not provided andstartandstophave different types, the default output array data type is determined according to type promotion rules. - order: specifies whether an ndarray is
'row-major'(C-style) or'column-major'(Fortran-style). Ifstart,stop, andendpointare scalar values, the default order is'row-major'. Ifstart,stop, and/orendpointarrays have the same memory layout, the default order is the same layout. Otherwise, the default order is'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, the constructor recycles modes using modulo arithmetic. Default:[ options.mode ].
By default, the function always includes the end of the interval in the list of values written to an output ndarray. To exclude the end of the interval, provide an endpoint argument.
var ndarray2array = require( '@stdlib/ndarray-to-array' );
var x = linspace( [ 4 ], 1.0, 5.0, false );
// returns <ndarray>
var arr = ndarray2array( x );
// returns [ 1.0, 2.0, 3.0, 4.0 ]When provided scalar or zero-dimensional ndarray start, stop, and endpoint arguments, the values are broadcast across all elements in the shape defined by the complement of those dimensions specified by options.dims. To specify separate sub-array configurations, provide non-zero-dimensional ndarray arguments.
var array = require( '@stdlib/ndarray-array' );
var BooleanArray = require( '@stdlib/array-bool' );
var ndarray2array = require( '@stdlib/ndarray-to-array' );
var start = array( [ 1.0, 5.0 ] );
var end = array( [ 3.0, 8.0 ] );
var endpoint = array( new BooleanArray( [ true, false ] ) );
var x = linspace( [ 2, 3 ], start, end, endpoint );
// returns <ndarray>
var arr = ndarray2array( x );
// returns [ [ 1.0, 2.0, 3.0 ], [ 5.0, 6.0, 7.0 ] ]By default, the function generates linearly spaced values along the last dimension of an output ndarray. To perform the operation over specific dimensions, provide a dims option.
var ndarray2array = require( '@stdlib/ndarray-to-array' );
var x = linspace( [ 2, 2 ], 1.0, 4.0, {
'dims': [ 0, 1 ]
});
// returns <ndarray>
var arr = ndarray2array( x );
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]To specify the output ndarray data type, provide a dtype option.
var ndarray2array = require( '@stdlib/ndarray-to-array' );
var x = linspace( [ 4 ], 1.0, 4.0, {
'dtype': 'float32'
});
// returns <ndarray>
var arr = ndarray2array( x );
// returns [ 1.0, 2.0, 3.0, 4.0 ]Fills an ndarray with linearly spaced values over a specified interval along one or more ndarray dimensions.
var ndarray2array = require( '@stdlib/ndarray-to-array' );
var zeros = require( '@stdlib/ndarray-zeros' );
var x = zeros( [ 4 ] );
// returns <ndarray>
var out = linspace.assign( x, 1.0, 4.0 );
// returns <ndarray>
var arr = ndarray2array( out );
// returns [ 1.0, 2.0, 3.0, 4.0 ]
var bool = ( x === out );
// returns trueThe function has the following parameters:
- out: output ndarray. Must have a floating-point or "generic" data type.
- start: start of interval. May be either a number, a complex number, or an ndarray having a numeric or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined by
options.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], a start ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided output ndarray, a start ndarray must be a zero-dimensional ndarray. - stop: end of interval. May be either a number, a complex number, or an ndarray having a numeric or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined by
options.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], a stop ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided output ndarray, a stop ndarray must be a zero-dimensional ndarray. - endpoint: specifies whether to include the end of the interval when writing values to the output ndarray (optional). May be either a boolean or an ndarray having a boolean or "generic" data type. If provided an ndarray, the value must have a shape which is broadcast-compatible with the complement of the shape defined by
options.dims. For example, given the input shape[2, 3, 4]andoptions.dims=[0], an endpoint ndarray must have a shape which is broadcast-compatible with the shape[3, 4]. Similarly, when performing the operation over all elements in a provided output ndarray, an endpoint ndarray must be a zero-dimensional ndarray. Default:true. - options: function options (optional).
The function accepts the following options:
- dims: list of dimensions over which to perform operation. If not provided, the function generates linearly spaced values along the last dimension. Default:
[-1].
-
Let
Mbe the number of linearly spaced values to be written along one or more ndarray dimensions. The spacing between values is thus given byΔ = (stop-start)/(M-1) -
If an output ndarray has a single element and the function is supposed to include the end of the interval, the set of values written to an output ndarray only includes the end of the interval, but not the start of the interval.
-
Otherwise, when an output ndarray has a single element and the function is not supposed to include the end of the interval, the set of values written to an output ndarray only includes the start of the interval, but not the end of the interval.
-
For a real-valued output ndarray, if the start of the interval is less than end of the interval, the set of values written to an output ndarray will be written in ascending order, and, if the start of the interval is greater than the end of the interval, the set of written values will be in descending order.
-
When an output ndarray contains at least two values and the function is supposed to include the end of the interval, the set of values written to an output ndarray is guaranteed to include the start and end interval values. Beware, however, that values between the interval bounds are subject to floating-point rounding errors.
-
When writing to a complex floating-point output ndarray, real-valued start and stop values are treated as complex numbers having a real component equaling the provided value and having an imaginary component equaling zero.
-
When generating linearly spaced complex floating-point numbers, the real and imaginary components are generated separately.
-
Both
startandstopare cast to the data type of the output ndarray. -
The function iterates over ndarray elements according to the memory layout of an output ndarray. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional output ndarray. In such scenarios, one may want to copy an output ndarray to contiguous memory before filling with linearly spaced values.
var ndarray2array = require( '@stdlib/ndarray-to-array' );
var linspace = require( '@stdlib/blas-ext-linspace' );
// Create two vectors defining interval bounds:
var start = linspace( [ 5 ], 1, 5, true );
var end = linspace( [ 5 ], 5, 9, true );
// Create a grid:
var out = linspace( [ 5, 5 ], start, end, true );
console.log( ndarray2array( out ) );
// Generate linearly spaced values over multiple dimensions:
out = linspace( [ 5, 5 ], 1, 25, true, {
'dims': [ 0, 1 ]
});
console.log( ndarray2array( out ) );
// Generate linearly spaced values over multiple dimensions in column-major order:
out = linspace( [ 5, 5 ], 1, 25, true, {
'dims': [ 0, 1 ],
'order': 'column-major'
});
console.log( ndarray2array( out ) );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-2025. The Stdlib Authors.