Usage
var linspace = require( '@stdlib/array-linspace' );
linspace( start, stop, length[, options] )
Generates a linearly spaced array over a specified interval.
var arr = linspace( 0.0, 100.0, 6 );
If length
is 0
, the function returns an empty array.
var arr = linspace( 0.0, 100.0, 0 );
If length
is 1
, the function returns an array containing stop
, but not start
, when endpoint
is true
; otherwise, the function returns an array containing start
, but not stop
.
var arr = linspace( 0.0, 100.0, 1 );
arr = linspace( 0.0, 100.0, 1, {
'endpoint': true
});
arr = linspace( 0.0, 100.0, 1, {
'endpoint': false
});
For real-valued start
and stop
, if start
is less than stop
, the output array will contain ascending values, and, if start
is greater than stop
, the output array will contain descending values.
var arr = linspace( 0.0, -100.0, 6 );
The function accepts the following options
:
- dtype: output array data type. Must be a floating-point data type or
'generic'
. If both start
and stop
are 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, the default output array data type is 'complex128'
. - endpoint:
boolean
indicating whether to include the stop
value in the output array. If false
, the function generates length + 1
linearly spaced values over the interval [start, stop]
and only writes length
values to the output array, thus excluding stop
from the output array. Accordingly, for a fixed length
, the spacing between adjacent values in the output array changes depending on the value of endpoint
. Default: true
.
By default, the function generates a linearly spaced array over the closed interval [start, stop]
. To generate linearly spaced values over the half-open interval [start, stop)
, set the endpoint
option to false
.
var opts = {
'endpoint': false
};
var arr = linspace( 0.0, 100.0, 5, opts );
When both start
and stop
are real-valued, the default output array data type is 'float64'
. To return an output array having a different data type, set the dtype
option.
var opts = {
'dtype': 'generic'
};
var arr = linspace( 0, 100, 6, opts );
When either start
or stop
is a complex number, the default output array data type is 'complex128'
. To return an output array having a different data type, set the dtype
option.
var Complex128 = require( '@stdlib/complex-float64' );
var real = require( '@stdlib/complex-real' );
var imag = require( '@stdlib/complex-imag' );
var opts = {
'dtype': 'generic'
};
var start = new Complex128( 0.0, 0.0 );
var stop = new Complex128( 100.0, 10.0 );
var arr = linspace( start, stop, 6, opts );
var z = arr[ 0 ];
var re = real( z );
var im = imag( z );
z = arr[ 1 ];
re = real( z );
im = imag( z );
linspace.assign( start, stop, out[, options] )
Generates a linearly spaced sequence over a specified interval and assigns the results to a provided output array.
var Float64Array = require( '@stdlib/array-float64' );
var out = new Float64Array( 6 );
var arr = linspace.assign( 0.0, 100.0, out );
var bool = ( arr === out );
If the provided output array is empty, the function returns the provided output array unchanged.
var arr = linspace.assign( 0.0, 100.0, [] );
If the provided output array contains a single element, the function writes the stop
value, but not start
, when endpoint
is true
; otherwise, the function writes the start
value, but not stop
.
var arr = linspace.assign( 0.0, 100.0, [ -10.0 ] );
arr = linspace.assign( 0.0, 100.0, [ -10.0 ], {
'endpoint': true
});
arr = linspace.assign( 0.0, 100.0, [ -10.0 ], {
'endpoint': false
});
For real-valued start
and stop
, if start
is less than stop
, the output array will contain ascending values, and, if start
is greater than stop
, the output array will contain descending values.
var Float64Array = require( '@stdlib/array-float64' );
var out = new Float64Array( 6 );
var arr = linspace.assign( 0.0, -100.0, out );
The function accepts the following options
:
- endpoint:
boolean
indicating whether to include the stop
value in the output array. If false
, the function generates N + 1
linearly spaced values (where N
is the length of the provided output array) over the interval [start, stop]
and only writes N
values to the output array, thus excluding stop
from the output array. Accordingly, for a fixed N
, the spacing between adjacent values in the output array changes depending on the value of endpoint
. Default: true
.
By default, the function generates a linearly spaced array over the closed interval [start, stop]
. To generate linearly spaced values over the half-open interval [start, stop)
, set the endpoint
option to false
.
var Float64Array = require( '@stdlib/array-float64' );
var opts = {
'endpoint': false
};
var out = new Float64Array( 5 );
var arr = linspace.assign( 0.0, 100.0, out, opts );