|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2022 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 | +> Generate a linearly spaced numeric array whose elements increment by 1. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var unitspace = require( '@stdlib/array/base/unitspace' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### unitspace( start, stop, increment ) |
| 34 | + |
| 35 | +Generates a linearly spaced numeric `array` whose elements increment by `1`. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var arr = unitspace( 0, 6 ); |
| 39 | +// returns [ 0, 1, 2, 3, 4, 5 ] |
| 40 | +``` |
| 41 | + |
| 42 | +</section> |
| 43 | + |
| 44 | +<!-- /.usage --> |
| 45 | + |
| 46 | +<section class="notes"> |
| 47 | + |
| 48 | +### Notes |
| 49 | + |
| 50 | +- The output `array` is guaranteed to include the `start` value but does **not** include the `stop` value. Beware that values subsequent to the `start` value are subject to floating-point errors. Hence, |
| 51 | + |
| 52 | + ```javascript |
| 53 | + var arr = unitspace( -0.7, 1 ); |
| 54 | + // returns [ -0.7, ~0.3 ] |
| 55 | + ``` |
| 56 | + |
| 57 | + where `arr[1]` is only guaranteed to be approximately equal to `0.3`. |
| 58 | + |
| 59 | + If you desire more control over element precision, consider using [roundn][@stdlib/math/base/special/roundn]: |
| 60 | + |
| 61 | + ```javascript |
| 62 | + var roundn = require( '@stdlib/math/base/special/roundn' ); |
| 63 | + |
| 64 | + // Create an array subject to floating-point errors: |
| 65 | + var arr = unitspace( -10.7, 11.7 ); |
| 66 | +
|
| 67 | + // Round each value to the nearest hundredth: |
| 68 | + var i; |
| 69 | + for ( i = 0; i < arr.length; i++ ) { |
| 70 | + arr[ i ] = roundn( arr[ i ], -2 ); |
| 71 | + } |
| 72 | +
|
| 73 | + console.log( arr.join( '\n' ) ); |
| 74 | + ``` |
| 75 | + |
| 76 | +</section> |
| 77 | + |
| 78 | +<!-- /.notes --> |
| 79 | + |
| 80 | +<section class="examples"> |
| 81 | + |
| 82 | +## Examples |
| 83 | + |
| 84 | +<!-- eslint no-undef: "error" --> |
| 85 | + |
| 86 | +```javascript |
| 87 | +var sort2hp = require( '@stdlib/blas/ext/base/gsort2hp' ); |
| 88 | +var filledBy = require( '@stdlib/array/base/filled-by' ); |
| 89 | +var randu = require( '@stdlib/random/base/randu' ); |
| 90 | +var unitspace = require( '@stdlib/array/base/unitspace' ); |
| 91 | +
|
| 92 | +// Generate an array of random numbers: |
| 93 | +var x = filledBy( 10, randu ); |
| 94 | +
|
| 95 | +// Generate an array of indices: |
| 96 | +var idx = unitspace( 0, x.length ); |
| 97 | +
|
| 98 | +// Create a temporary array to avoid mutation: |
| 99 | +var tmp = x.slice(); |
| 100 | +
|
| 101 | +// Sort the index array according to the sort order of `x`: |
| 102 | +sort2hp( x.length, 1, tmp, 1, idx, 1 ); |
| 103 | +
|
| 104 | +console.log( x ); |
| 105 | +console.log( idx ); |
| 106 | +``` |
| 107 | + |
| 108 | +</section> |
| 109 | + |
| 110 | +<!-- /.examples --> |
| 111 | + |
| 112 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 113 | + |
| 114 | +<section class="related"> |
| 115 | + |
| 116 | +</section> |
| 117 | + |
| 118 | +<!-- /.related --> |
| 119 | + |
| 120 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 121 | + |
| 122 | +<section class="links"> |
| 123 | + |
| 124 | +[@stdlib/math/base/special/roundn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/roundn |
| 125 | + |
| 126 | +</section> |
| 127 | + |
| 128 | +<!-- /.links --> |
0 commit comments