|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# linspace |
| 22 | + |
| 23 | +> Return a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced values over a specified interval along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var linspace = require( '@stdlib/blas/ext/linspace' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### linspace( shape, start, stop\[, endpoint]\[, options] ) |
| 34 | + |
| 35 | +Returns a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced values over a specified interval along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 39 | + |
| 40 | +var x = linspace( [ 4 ], 1.0, 4.0 ); |
| 41 | +// returns <ndarray> |
| 42 | + |
| 43 | +var arr = ndarray2array( x ); |
| 44 | +// returns [ 1.0, 2.0, 3.0, 4.0 ] |
| 45 | +``` |
| 46 | + |
| 47 | +The function has the following parameters: |
| 48 | + |
| 49 | +- **shape**: array shape. |
| 50 | +- **start**: start of interval. 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]. |
| 51 | +- **stop**: end of interval. 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 stop [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 stop [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 52 | +- **endpoint**: specifies whether to include the end of the interval when writing values to the output [ndarray][@stdlib/ndarray/ctor] (_optional_). May be either a boolean or an [ndarray][@stdlib/ndarray/ctor] having a boolean 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]`, an endpoint [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, an endpoint [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. Default: `true`. |
| 53 | +- **options**: function options (_optional_). |
| 54 | + |
| 55 | +The function accepts the following options: |
| 56 | + |
| 57 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function generates linearly spaced values along the last dimension. Default: `[-1]`. |
| 58 | +- **dtype**: output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. If both `start` and `stop` are real-valued, the output array data type may be any floating-point data type or 'generic'. However, if either `start` or `stop` are complex-valued, the output array type must be a complex floating-point data type or 'generic'. If provided, `start` and `stop` are cast to the specified [data type][@stdlib/ndarray/dtypes]. If not provided and 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, if not provided, the default output array data type is `'complex128'`. |
| 59 | +- **order**: specifies whether an [ndarray][@stdlib/ndarray/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style). If `start`, `stop`, and `endpoint` are scalar values, the default order is `'row-major'`. If `start`, `stop`, and/or `endpoint` arrays have the same memory layout, the default order is the same layout. Otherwise, the default order is `'row-major'`. |
| 60 | +- **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`. |
| 61 | +- **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 constructor recycles modes using modulo arithmetic. Default: `[ options.mode ]`. |
| 62 | + |
| 63 | +By default, the function always includes the end of the interval in the list of values written to an output [ndarray][@stdlib/ndarray/ctor]. To exclude the end of the interval, provide an `endpoint` argument. |
| 64 | + |
| 65 | +```javascript |
| 66 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 67 | + |
| 68 | +var x = linspace( [ 4 ], 1.0, 5.0, false ); |
| 69 | +// returns <ndarray> |
| 70 | + |
| 71 | +var arr = ndarray2array( x ); |
| 72 | +// returns [ 1.0, 2.0, 3.0, 4.0 ] |
| 73 | +``` |
| 74 | + |
| 75 | +When provided scalar or zero-dimensional [ndarray][@stdlib/ndarray/ctor] `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][@stdlib/ndarray/ctor] arguments. |
| 76 | + |
| 77 | +```javascript |
| 78 | +var array = require( '@stdlib/ndarray/array' ); |
| 79 | +var BooleanArray = require( '@stdlib/array/bool' ); |
| 80 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 81 | + |
| 82 | +var start = array( [ 1.0, 5.0 ] ); |
| 83 | +var end = array( [ 3.0, 8.0 ] ); |
| 84 | +var endpoint = array( new BooleanArray( [ true, false ] ) ); |
| 85 | + |
| 86 | +var x = linspace( [ 2, 3 ], start, end, endpoint ); |
| 87 | +// returns <ndarray> |
| 88 | + |
| 89 | +var arr = ndarray2array( x ); |
| 90 | +// returns [ [ 1.0, 2.0, 3.0 ], [ 5.0, 6.0, 7.0 ] ] |
| 91 | +``` |
| 92 | + |
| 93 | +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. |
| 94 | + |
| 95 | +```javascript |
| 96 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 97 | + |
| 98 | +var x = linspace( [ 2, 2 ], 1.0, 4.0, { |
| 99 | + 'dims': [ 0, 1 ] |
| 100 | +}); |
| 101 | +// returns <ndarray> |
| 102 | + |
| 103 | +var arr = ndarray2array( x ); |
| 104 | +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] |
| 105 | +``` |
| 106 | + |
| 107 | +To specify the output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes], provide a `dtype` option. |
| 108 | + |
| 109 | +```javascript |
| 110 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 111 | + |
| 112 | +var x = linspace( [ 4 ], 1.0, 4.0, { |
| 113 | + 'dtype': 'float32' |
| 114 | +}); |
| 115 | +// returns <ndarray> |
| 116 | + |
| 117 | +var arr = ndarray2array( x ); |
| 118 | +// returns [ 1.0, 2.0, 3.0, 4.0 ] |
| 119 | +``` |
| 120 | + |
| 121 | +#### linspace.assign( out, start, stop\[, endpoint]\[, options] ) |
| 122 | + |
| 123 | +Fills an [ndarray][@stdlib/ndarray/ctor] with linearly spaced values over a specified interval along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 124 | + |
| 125 | +```javascript |
| 126 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 127 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 128 | + |
| 129 | +var x = zeros( [ 4 ] ); |
| 130 | +// returns <ndarray> |
| 131 | + |
| 132 | +var out = linspace.assign( x, 1.0, 4.0 ); |
| 133 | +// returns <ndarray> |
| 134 | + |
| 135 | +var arr = ndarray2array( out ); |
| 136 | +// returns [ 1.0, 2.0, 3.0, 4.0 ] |
| 137 | + |
| 138 | +var bool = ( x === out ); |
| 139 | +// returns true |
| 140 | +``` |
| 141 | + |
| 142 | +The function has the following parameters: |
| 143 | + |
| 144 | +- **out**: output [ndarray][@stdlib/ndarray/ctor]. Must have a floating-point or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 145 | +- **start**: start of interval. 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 output [ndarray][@stdlib/ndarray/ctor], a start [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 146 | +- **stop**: end of interval. 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 stop [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 output [ndarray][@stdlib/ndarray/ctor], a stop [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. |
| 147 | +- **endpoint**: specifies whether to include the end of the interval when writing values to the output [ndarray][@stdlib/ndarray/ctor] (_optional_). May be either a boolean or an [ndarray][@stdlib/ndarray/ctor] having a boolean 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]`, an endpoint [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 output [ndarray][@stdlib/ndarray/ctor], an endpoint [ndarray][@stdlib/ndarray/ctor] must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. Default: `true`. |
| 148 | +- **options**: function options (_optional_). |
| 149 | + |
| 150 | +The function accepts the following options: |
| 151 | + |
| 152 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function generates linearly spaced values along the last dimension. Default: `[-1]`. |
| 153 | + |
| 154 | +</section> |
| 155 | + |
| 156 | +<!-- /.usage --> |
| 157 | + |
| 158 | +<section class="notes"> |
| 159 | + |
| 160 | +## Notes |
| 161 | + |
| 162 | +- Let `M` be the number of linearly spaced values to be written along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. The spacing between values is thus given by |
| 163 | + |
| 164 | + ```text |
| 165 | + Δ = (stop-start)/(M-1) |
| 166 | + ``` |
| 167 | +
|
| 168 | +- If an output [ndarray][@stdlib/ndarray/ctor] 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][@stdlib/ndarray/ctor] only includes the end of the interval, but not the start of the interval. |
| 169 | +
|
| 170 | +- Otherwise, when an output [ndarray][@stdlib/ndarray/ctor] 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][@stdlib/ndarray/ctor] only includes the start of the interval, but not the end of the interval. |
| 171 | +
|
| 172 | +- For a real-valued output [ndarray][@stdlib/ndarray/ctor], if the start of the interval is less than end of the interval, the set of values written to an output [ndarray][@stdlib/ndarray/ctor] 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. |
| 173 | +
|
| 174 | +- When an output [ndarray][@stdlib/ndarray/ctor] 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][@stdlib/ndarray/ctor] 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. |
| 175 | +
|
| 176 | +- When writing to a complex floating-point output [ndarray][@stdlib/ndarray/ctor], 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. |
| 177 | +
|
| 178 | +- When generating linearly spaced complex floating-point numbers, the real and imaginary components are generated separately. |
| 179 | +
|
| 180 | +- Both `start` and `stop` are cast to the data type of the output [ndarray][@stdlib/ndarray/ctor]. |
| 181 | +
|
| 182 | +- 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. |
| 183 | +
|
| 184 | +</section> |
| 185 | +
|
| 186 | +<!-- /.notes --> |
| 187 | +
|
| 188 | +<section class="examples"> |
| 189 | +
|
| 190 | +## Examples |
| 191 | +
|
| 192 | +<!-- eslint no-undef: "error" --> |
| 193 | +
|
| 194 | +```javascript |
| 195 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 196 | +var linspace = require( '@stdlib/blas/ext/linspace' ); |
| 197 | +
|
| 198 | +// Create two vectors defining interval bounds: |
| 199 | +var start = linspace( [ 5 ], 1, 5, true ); |
| 200 | +var end = linspace( [ 5 ], 5, 9, true ); |
| 201 | +
|
| 202 | +// Create a grid: |
| 203 | +var out = linspace( [ 5, 5 ], start, end, true ); |
| 204 | +console.log( ndarray2array( out ) ); |
| 205 | +
|
| 206 | +// Generate linearly spaced values over multiple dimensions: |
| 207 | +out = linspace( [ 5, 5 ], 1, 25, true, { |
| 208 | + 'dims': [ 0, 1 ] |
| 209 | +}); |
| 210 | +console.log( ndarray2array( out ) ); |
| 211 | +
|
| 212 | +// Generate linearly spaced values over multiple dimensions in column-major order: |
| 213 | +out = linspace( [ 5, 5 ], 1, 25, true, { |
| 214 | + 'dims': [ 0, 1 ], |
| 215 | + 'order': 'column-major' |
| 216 | +}); |
| 217 | +console.log( ndarray2array( out ) ); |
| 218 | +``` |
| 219 | + |
| 220 | +</section> |
| 221 | + |
| 222 | +<!-- /.examples --> |
| 223 | + |
| 224 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 225 | + |
| 226 | +<section class="related"> |
| 227 | + |
| 228 | +</section> |
| 229 | + |
| 230 | +<!-- /.related --> |
| 231 | + |
| 232 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 233 | + |
| 234 | +<section class="links"> |
| 235 | + |
| 236 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 237 | + |
| 238 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 239 | + |
| 240 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 241 | + |
| 242 | +</section> |
| 243 | + |
| 244 | +<!-- /.links --> |
0 commit comments