|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2026 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# unitspace |
| 22 | + |
| 23 | +> Return a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced numeric elements which increment by `1` starting from a specified value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var unitspace = require( '@stdlib/blas/ext/unitspace' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### unitspace( shape, start\[, options] ) |
| 34 | + |
| 35 | +Returns a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced numeric elements which increment by `1` starting from a specified value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var x = unitspace( [ 4 ], 1.0 ); |
| 39 | +// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ] |
| 40 | +``` |
| 41 | + |
| 42 | +The function has the following parameters: |
| 43 | + |
| 44 | +- **shape**: array shape. |
| 45 | +- **start**: starting value. May be either a number, a complex number, or an [ndarray][@stdlib/ndarray/ctor] having a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] 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][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input shape, a start [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 46 | +- **options**: function options (_optional_). |
| 47 | + |
| 48 | +The function accepts the following options: |
| 49 | + |
| 50 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function generates linearly spaced values along the last dimension. Default: `[-1]`. |
| 51 | +- **dtype**: output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Must be a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. 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`. |
| 52 | +- **order**: specifies whether an [ndarray][@stdlib/ndarray/ctor] 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][@stdlib/ndarray/ctor], the default order is the same as the memory layout of `start`. |
| 53 | +- **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`. |
| 54 | +- **submode**: a mode array which specifies for each dimension how to handle subscripts which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). If provided fewer modes than dimensions, the function recycles modes using modulo arithmetic. Default: `[ options.mode ]`. |
| 55 | + |
| 56 | +When provided a scalar or zero-dimensional [ndarray][@stdlib/ndarray/ctor] `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][@stdlib/ndarray/ctor] argument. |
| 57 | + |
| 58 | +```javascript |
| 59 | +var array = require( '@stdlib/ndarray/array' ); |
| 60 | + |
| 61 | +var start = array( [ 1.0, 5.0 ] ); |
| 62 | +// returns <ndarray>[ 1.0, 5.0 ] |
| 63 | + |
| 64 | +var x = unitspace( [ 2, 3 ], start ); |
| 65 | +// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 5.0, 6.0, 7.0 ] ] |
| 66 | +``` |
| 67 | + |
| 68 | +By default, the function generates linearly spaced values along the last dimension of an output [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option. |
| 69 | + |
| 70 | +```javascript |
| 71 | +var x = unitspace( [ 2, 2 ], 1.0, { |
| 72 | + 'dims': [ 0, 1 ] |
| 73 | +}); |
| 74 | +// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] |
| 75 | +``` |
| 76 | + |
| 77 | +To specify the output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes], provide a `dtype` option. |
| 78 | + |
| 79 | +```javascript |
| 80 | +var x = unitspace( [ 4 ], 1.0, { |
| 81 | + 'dtype': 'float32' |
| 82 | +}); |
| 83 | +// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ] |
| 84 | +``` |
| 85 | + |
| 86 | +#### unitspace.assign( x, start\[, options] ) |
| 87 | + |
| 88 | +Fills an [ndarray][@stdlib/ndarray/ctor] with linearly spaced numeric elements which increment by `1` starting from a specified value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 89 | + |
| 90 | +```javascript |
| 91 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 92 | + |
| 93 | +var x = zeros( [ 4 ] ); |
| 94 | +// returns <ndarray>[ 0.0, 0.0, 0.0, 0.0 ] |
| 95 | + |
| 96 | +var out = unitspace.assign( x, 1.0 ); |
| 97 | +// returns <ndarray>[ 1.0, 2.0, 3.0, 4.0 ] |
| 98 | + |
| 99 | +var bool = ( x === out ); |
| 100 | +// returns true |
| 101 | +``` |
| 102 | + |
| 103 | +The function has the following parameters: |
| 104 | + |
| 105 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 106 | +- **start**: starting value. May be either a number, a complex number, or an [ndarray][@stdlib/ndarray/ctor] having a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] 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][@stdlib/ndarray/ctor] must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], a start [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 107 | +- **options**: function options (_optional_). |
| 108 | + |
| 109 | +The function accepts the following options: |
| 110 | + |
| 111 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function generates linearly spaced values along the last dimension. Default: `[-1]`. |
| 112 | + |
| 113 | +</section> |
| 114 | + |
| 115 | +<!-- /.usage --> |
| 116 | + |
| 117 | +<section class="notes"> |
| 118 | + |
| 119 | +## Notes |
| 120 | + |
| 121 | +- When writing to a complex floating-point output [ndarray][@stdlib/ndarray/ctor], 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. |
| 122 | +- The `start` argument is cast to the data type of the output [ndarray][@stdlib/ndarray/ctor]. |
| 123 | +- The function iterates over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of an output [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional output [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an output [ndarray][@stdlib/ndarray/ctor] to contiguous memory before filling with linearly spaced values. |
| 124 | + |
| 125 | +</section> |
| 126 | + |
| 127 | +<!-- /.notes --> |
| 128 | + |
| 129 | +<section class="examples"> |
| 130 | + |
| 131 | +## Examples |
| 132 | + |
| 133 | +<!-- eslint no-undef: "error" --> |
| 134 | + |
| 135 | +```javascript |
| 136 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 137 | +var unitspace = require( '@stdlib/blas/ext/unitspace' ); |
| 138 | + |
| 139 | +// Create a vector of starting values: |
| 140 | +var start = unitspace( [ 5 ], 1 ); |
| 141 | + |
| 142 | +// Create a grid: |
| 143 | +var out = unitspace( [ 5, 5 ], start ); |
| 144 | +console.log( ndarray2array( out ) ); |
| 145 | + |
| 146 | +// Generate values over multiple dimensions: |
| 147 | +out = unitspace( [ 5, 5 ], 1, { |
| 148 | + 'dims': [ 0, 1 ] |
| 149 | +}); |
| 150 | +console.log( ndarray2array( out ) ); |
| 151 | + |
| 152 | +// Generate values over multiple dimensions in column-major order: |
| 153 | +out = unitspace( [ 5, 5 ], 1, { |
| 154 | + 'dims': [ 0, 1 ], |
| 155 | + 'order': 'column-major' |
| 156 | +}); |
| 157 | +console.log( ndarray2array( out ) ); |
| 158 | +``` |
| 159 | + |
| 160 | +</section> |
| 161 | + |
| 162 | +<!-- /.examples --> |
| 163 | + |
| 164 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 165 | + |
| 166 | +<section class="related"> |
| 167 | + |
| 168 | +</section> |
| 169 | + |
| 170 | +<!-- /.related --> |
| 171 | + |
| 172 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 173 | + |
| 174 | +<section class="links"> |
| 175 | + |
| 176 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 177 | + |
| 178 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 179 | + |
| 180 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 181 | + |
| 182 | +</section> |
| 183 | + |
| 184 | +<!-- /.links --> |
0 commit comments