Return a new ndarray filled with linearly spaced numeric elements which increment by
1starting from a specified value along one or more ndarray dimensions.
var unitspace = require( '@stdlib/blas/ext/unitspace' );Returns a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions.
var x = unitspace( [ 4 ], 1.0 );
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ]The function has the following parameters:
- shape: array shape.
- start: starting value. 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. - 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. Must be a numeric or "generic" data type. If a data type is provided,
startis cast to the specified data type. If a data type is not provided, the default output array data type is the same as the data type ofstart. - order: specifies whether an ndarray is
'row-major'(C-style) or'column-major'(Fortran-style). Ifstartis a scalar value, the default order is'row-major'. Ifstartis an ndarray, the default order is the same as the memory layout ofstart. - 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 function recycles modes using modulo arithmetic. Default:[ options.mode ].
When provided a scalar or zero-dimensional ndarray start argument, the value is broadcast across all elements in the shape defined by the complement of those dimensions specified by options.dims. To specify separate sub-array starting values, provide a non-zero-dimensional ndarray argument.
var array = require( '@stdlib/ndarray/array' );
var start = array( [ 1.0, 5.0 ] );
// returns <ndarray>[ 1.0, 5.0 ]
var x = unitspace( [ 2, 3 ], start );
// returns <ndarray>[ [ 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 x = unitspace( [ 2, 2 ], 1.0, {
'dims': [ 0, 1 ]
});
// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]To specify the output ndarray data type, provide a dtype option.
var x = unitspace( [ 4 ], 1.0, {
'dtype': 'float32'
});
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ]Fills an ndarray with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions.
var zeros = require( '@stdlib/ndarray/zeros' );
var x = zeros( [ 4 ] );
// returns <ndarray>[ 0.0, 0.0, 0.0, 0.0 ]
var out = unitspace.assign( x, 1.0 );
// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ]
var bool = ( x === out );
// returns trueThe function has the following parameters:
- x: input ndarray. Must have a numeric or "generic" data type.
- start: starting value. 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 ndarray, a start ndarray must be a zero-dimensional ndarray. - 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].
- When writing to a complex floating-point output ndarray, a real-valued
startvalue is treated as a complex number having a real component equaling the provided value and having an imaginary component equaling zero. - The
startargument is 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 unitspace = require( '@stdlib/blas/ext/unitspace' );
// Create a vector of starting values:
var start = unitspace( [ 5 ], 1 );
// Create a grid:
var out = unitspace( [ 5, 5 ], start );
console.log( ndarray2array( out ) );
// Generate values over multiple dimensions:
out = unitspace( [ 5, 5 ], 1, {
'dims': [ 0, 1 ]
});
console.log( ndarray2array( out ) );
// Generate values over multiple dimensions in column-major order:
out = unitspace( [ 5, 5 ], 1, {
'dims': [ 0, 1 ],
'order': 'column-major'
});
console.log( ndarray2array( out ) );