Skip to content

Commit ecd50e4

Browse files
committed
Add base array utility to create a linearly spaced numeric array whose elements increment by 1
1 parent 2ff8052 commit ecd50e4

File tree

11 files changed

+637
-0
lines changed

11 files changed

+637
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var isArray = require( '@stdlib/assert/is-array' );
25+
var pkg = require( './../package.json' ).name;
26+
var unitspace = require( './../lib' );
27+
28+
29+
// MAIN //
30+
31+
bench( pkg, function benchmark( b ) {
32+
var i;
33+
var v;
34+
35+
b.tic();
36+
for ( i = 0; i < b.iterations; i++ ) {
37+
v = unitspace( i, i+100 );
38+
if ( typeof v !== 'object' ) {
39+
b.fail( 'should return an array' );
40+
}
41+
}
42+
b.toc();
43+
if ( !isArray( v ) ) {
44+
b.fail( 'should return an array' );
45+
}
46+
b.pass( 'benchmark finished' );
47+
b.end();
48+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var pkg = require( './../package.json' ).name;
27+
var unitspace = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
/**
33+
* Creates a benchmark function.
34+
*
35+
* @private
36+
* @param {PositiveInteger} len - array length
37+
* @returns {Function} benchmark function
38+
*/
39+
function createBenchmark( len ) {
40+
return benchmark;
41+
42+
/**
43+
* Benchmark function.
44+
*
45+
* @private
46+
* @param {Benchmark} b - benchmark instance
47+
*/
48+
function benchmark( b ) {
49+
var v;
50+
var i;
51+
52+
b.tic();
53+
for ( i = 0; i < b.iterations; i++ ) {
54+
v = unitspace( i, i+len );
55+
if ( typeof v !== 'object' ) {
56+
b.fail( 'should return an array' );
57+
}
58+
}
59+
b.toc();
60+
if ( !isArray( v ) ) {
61+
b.fail( 'should return an array' );
62+
}
63+
b.pass( 'benchmark finished' );
64+
b.end();
65+
}
66+
}
67+
68+
69+
// MAIN //
70+
71+
/**
72+
* Main execution sequence.
73+
*
74+
* @private
75+
*/
76+
function main() {
77+
var len;
78+
var min;
79+
var max;
80+
var f;
81+
var i;
82+
83+
min = 1; // 10^min
84+
max = 6; // 10^max
85+
86+
for ( i = min; i <= max; i++ ) {
87+
len = pow( 10, i );
88+
f = createBenchmark( len );
89+
bench( pkg+':len='+len, f );
90+
}
91+
}
92+
93+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( start, stop )
3+
Generates a linearly spaced numeric array whose elements increment by 1.
4+
5+
The output array is guaranteed to include the `start` value but does not
6+
include the `stop` value.
7+
8+
Parameters
9+
----------
10+
start: number
11+
First array value.
12+
13+
stop: number
14+
Array element bound.
15+
16+
Returns
17+
-------
18+
out: Array
19+
Linearly spaced numeric array.
20+
21+
Examples
22+
--------
23+
> var arr = {{alias}}( 0, 6 )
24+
[ 0, 1, 2, 3, 4, 5 ]
25+
26+
See Also
27+
--------
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 2.0
20+
21+
/**
22+
* Generates a linearly spaced numeric array whose elements increment by 1.
23+
*
24+
* @param x1 - first array value
25+
* @param x2 - array element bound
26+
* @returns linearly spaced numeric array
27+
*
28+
* @example
29+
* var arr = unitspace( 0, 6 );
30+
* // returns [ 0, 1, 2, 3, 4, 5 ]
31+
*/
32+
declare function unitspace( x1: number, x2: number ): Array<number>;
33+
34+
35+
// EXPORTS //
36+
37+
export = unitspace;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
import unitspace = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an array of numbers...
25+
{
26+
unitspace( 0, 11 ); // $ExpectType number[]
27+
}
28+
29+
// The compiler throws an error if the function is provided values other than two numbers for the first two parameters...
30+
{
31+
unitspace( true, 10 ); // $ExpectError
32+
unitspace( false, 10 ); // $ExpectError
33+
unitspace( '5', 10 ); // $ExpectError
34+
unitspace( [], 10 ); // $ExpectError
35+
unitspace( {}, 20 ); // $ExpectError
36+
unitspace( ( x: number ): number => x, 20 ); // $ExpectError
37+
38+
unitspace( 9, true ); // $ExpectError
39+
unitspace( 9, false ); // $ExpectError
40+
unitspace( 5, '5' ); // $ExpectError
41+
unitspace( 8, [] ); // $ExpectError
42+
unitspace( 9, {} ); // $ExpectError
43+
unitspace( 8, ( x: number ): number => x ); // $ExpectError
44+
}
45+
46+
// The compiler throws an error if the function is provided an unsupported number of arguments...
47+
{
48+
unitspace(); // $ExpectError
49+
unitspace( 3 ); // $ExpectError
50+
unitspace( 3, 4, 1 ); // $ExpectError
51+
}

0 commit comments

Comments
 (0)