|
| 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 | +# oneTo |
| 22 | + |
| 23 | +> Return a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced numeric elements which increment by `1` starting from one along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var oneTo = require( '@stdlib/blas/ext/one-to' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### oneTo( shape\[, options] ) |
| 34 | + |
| 35 | +Returns a new [ndarray][@stdlib/ndarray/ctor] filled with linearly spaced numeric elements which increment by `1` starting from one along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var x = oneTo( [ 2, 3 ] ); |
| 39 | +// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 1.0, 2.0, 3.0 ] ] |
| 40 | +``` |
| 41 | + |
| 42 | +The function has the following parameters: |
| 43 | + |
| 44 | +- **shape**: array shape. |
| 45 | +- **options**: function options (_optional_). |
| 46 | + |
| 47 | +The function accepts the following options: |
| 48 | + |
| 49 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function generates values along the last dimension. Default: `[-1]`. |
| 50 | +- **dtype**: output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes]. Must be a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. Default: `'float64'`. |
| 51 | +- **order**: specifies whether an [ndarray][@stdlib/ndarray/ctor] is `'row-major'` (C-style) or `'column-major'` (Fortran-style). Default: `'row-major'`. |
| 52 | +- **mode**: specifies how to handle indices which exceed array dimensions (see [`ndarray`][@stdlib/ndarray/ctor]). Default: `'throw'`. |
| 53 | +- **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 ]`. |
| 54 | + |
| 55 | +By default, the function generates values along the last dimension of an output [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option. |
| 56 | + |
| 57 | +```javascript |
| 58 | +var x = oneTo( [ 2, 2 ], { |
| 59 | + 'dims': [ 0, 1 ] |
| 60 | +}); |
| 61 | +// returns <ndarray>[ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] |
| 62 | +``` |
| 63 | + |
| 64 | +To specify the output [ndarray][@stdlib/ndarray/ctor] [data type][@stdlib/ndarray/dtypes], provide a `dtype` option. |
| 65 | + |
| 66 | +```javascript |
| 67 | +var x = oneTo( [ 3 ], { |
| 68 | + 'dtype': 'float32' |
| 69 | +}); |
| 70 | +// returns <ndarray>[ 1.0, 2.0, 3.0 ] |
| 71 | +``` |
| 72 | + |
| 73 | +#### oneTo.assign( x\[, options] ) |
| 74 | + |
| 75 | +Fills an [ndarray][@stdlib/ndarray/ctor] with linearly spaced numeric elements which increment by `1` starting from one along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 76 | + |
| 77 | +```javascript |
| 78 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 79 | + |
| 80 | +var x = zeros( [ 2, 3 ] ); |
| 81 | +// returns <ndarray>[ [ 0.0, 0.0, 0.0 ], [ 0.0, 0.0, 0.0 ] ] |
| 82 | + |
| 83 | +var out = oneTo.assign( x ); |
| 84 | +// returns <ndarray>[ [ 1.0, 2.0, 3.0 ], [ 1.0, 2.0, 3.0 ] ] |
| 85 | + |
| 86 | +var bool = ( x === out ); |
| 87 | +// returns true |
| 88 | +``` |
| 89 | + |
| 90 | +The function has the following parameters: |
| 91 | + |
| 92 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a numeric or "generic" [data type][@stdlib/ndarray/dtypes]. |
| 93 | +- **options**: function options (_optional_). |
| 94 | + |
| 95 | +The function accepts the following options: |
| 96 | + |
| 97 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function generates values along the last dimension. Default: `[-1]`. |
| 98 | + |
| 99 | +</section> |
| 100 | + |
| 101 | +<!-- /.usage --> |
| 102 | + |
| 103 | +<section class="notes"> |
| 104 | + |
| 105 | +## Notes |
| 106 | + |
| 107 | +- The function iterates over [ndarray][@stdlib/ndarray/ctor] elements according to the memory layout of the input [ndarray][@stdlib/ndarray/ctor]. Accordingly, performance degradation is possible when operating over multiple dimensions of a large non-contiguous multi-dimensional input [ndarray][@stdlib/ndarray/ctor]. In such scenarios, one may want to copy an input [ndarray][@stdlib/ndarray/ctor] to contiguous memory before performing the operation. |
| 108 | + |
| 109 | +</section> |
| 110 | + |
| 111 | +<!-- /.notes --> |
| 112 | + |
| 113 | +<section class="examples"> |
| 114 | + |
| 115 | +## Examples |
| 116 | + |
| 117 | +<!-- eslint no-undef: "error" --> |
| 118 | + |
| 119 | +```javascript |
| 120 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 121 | +var oneTo = require( '@stdlib/blas/ext/one-to' ); |
| 122 | + |
| 123 | +// Create a new ndarray: |
| 124 | +var x = oneTo( [ 5, 5 ], { |
| 125 | + 'dtype': 'generic' |
| 126 | +}); |
| 127 | +console.log( ndarray2array( x ) ); |
| 128 | + |
| 129 | +// Generate values over a specific dimension: |
| 130 | +x = oneTo( [ 5, 5 ], { |
| 131 | + 'dtype': 'generic', |
| 132 | + 'dims': [ 0 ] |
| 133 | +}); |
| 134 | +console.log( ndarray2array( x ) ); |
| 135 | + |
| 136 | +// Generate values over all dimensions: |
| 137 | +x = oneTo( [ 5, 5 ], { |
| 138 | + 'dtype': 'generic', |
| 139 | + 'dims': [ 0, 1 ] |
| 140 | +}); |
| 141 | +console.log( ndarray2array( x ) ); |
| 142 | +``` |
| 143 | + |
| 144 | +</section> |
| 145 | + |
| 146 | +<!-- /.examples --> |
| 147 | + |
| 148 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 149 | + |
| 150 | +<section class="related"> |
| 151 | + |
| 152 | +</section> |
| 153 | + |
| 154 | +<!-- /.related --> |
| 155 | + |
| 156 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 157 | + |
| 158 | +<section class="links"> |
| 159 | + |
| 160 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 161 | + |
| 162 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 163 | + |
| 164 | +</section> |
| 165 | + |
| 166 | +<!-- /.links --> |
0 commit comments