Skip to content

stdlib-js/blas-ext-unitspace

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!

unitspace

NPM version Build Status Coverage Status

Return a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions.

Installation

npm install @stdlib/blas-ext-unitspace

Alternatively,

  • To load the package in a website via a script tag without installation and bundlers, use the ES Module available on the esm branch (see README).
  • If you are using Deno, visit the deno branch (see README for usage intructions).
  • For use in Observable, or in browser/node environments, use the Universal Module Definition (UMD) build available on the umd branch (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.

Usage

var unitspace = require( '@stdlib/blas-ext-unitspace' );

unitspace( shape, start[, options] )

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] and options.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, start is 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 of start.
  • order: specifies whether an ndarray is 'row-major' (C-style) or 'column-major' (Fortran-style). If start is a scalar value, the default order is 'row-major'. If start is an ndarray, the default order is the same as the memory layout of start.
  • 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 ]

unitspace.assign( x, start[, options] )

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 true

The 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] and options.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].

Notes

  • When writing to a complex floating-point output ndarray, a real-valued start value is treated as a complex number having a real component equaling the provided value and having an imaginary component equaling zero.
  • The start argument 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.

Examples

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 ) );

Notice

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.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2026. The Stdlib Authors.

About

Return a new ndarray filled with linearly spaced numeric elements which increment by 1 starting from a specified value along one or more ndarray dimensions.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors