Skip to content

Commit a47ff30

Browse files
committed
Add strided interface to compute a range
1 parent cf48bc9 commit a47ff30

File tree

15 files changed

+1477
-0
lines changed

15 files changed

+1477
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 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+
# range
22+
23+
> Calculate the [range][range] of a strided array.
24+
25+
<section class="intro>">
26+
27+
The [**range**][range] is defined as the difference between the maximum and minimum values.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var range = require( '@stdlib/stats/base/range' );
39+
```
40+
41+
#### range( N, x, stride )
42+
43+
Computes the [range][range] of a strided array `x`.
44+
45+
```javascript
46+
var x = [ 1.0, -2.0, 2.0 ];
47+
var N = x.length;
48+
49+
var v = range( N, x, 1 );
50+
// returns 4.0
51+
```
52+
53+
The function accepts the following parameters:
54+
55+
- **N**: number of indexed elements.
56+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
57+
- **stride**: index increment for `x`.
58+
59+
The `N` and `stride` parameters determine which elements in `x` are accessed at runtime. For example, to compute the [range][range] of every other element in `x`,
60+
61+
```javascript
62+
var floor = require( '@stdlib/math/base/special/floor' );
63+
64+
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
65+
var N = floor( x.length / 2 );
66+
67+
var v = range( N, x, 2 );
68+
// returns 6.0
69+
```
70+
71+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
72+
73+
<!-- eslint-disable stdlib/capitalized-comments -->
74+
75+
```javascript
76+
var Float64Array = require( '@stdlib/array/float64' );
77+
var floor = require( '@stdlib/math/base/special/floor' );
78+
79+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
80+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
81+
82+
var N = floor( x0.length / 2 );
83+
84+
var v = range( N, x1, 2 );
85+
// returns 6.0
86+
```
87+
88+
#### range.ndarray( N, x, stride, offset )
89+
90+
Computes the [range][range] of a strided array using alternative indexing semantics.
91+
92+
```javascript
93+
var x = [ 1.0, -2.0, 2.0 ];
94+
var N = x.length;
95+
96+
var v = range.ndarray( N, x, 1, 0 );
97+
// returns 4.0
98+
```
99+
100+
The function accepts the following additional parameters:
101+
102+
- **offset**: starting index for `x`.
103+
104+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying `buffer`, the `offset` parameter supports indexing semantics based on a starting index. For example, to calculate the [range][range] for every other value in `x` starting from the second value
105+
106+
```javascript
107+
var floor = require( '@stdlib/math/base/special/floor' );
108+
109+
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
110+
var N = floor( x.length / 2 );
111+
112+
var v = range.ndarray( N, x, 2, 1 );
113+
// returns 6.0
114+
```
115+
116+
</section>
117+
118+
<!-- /.usage -->
119+
120+
<section class="notes">
121+
122+
## Notes
123+
124+
- If `N <= 0`, both functions return `NaN`.
125+
- Depending on the environment, the typed versions ([`drange`][@stdlib/stats/base/drange], [`srange`][@stdlib/stats/base/srange], etc.) are likely to be significantly more performant.
126+
127+
</section>
128+
129+
<!-- /.notes -->
130+
131+
<section class="examples">
132+
133+
## Examples
134+
135+
<!-- eslint no-undef: "error" -->
136+
137+
```javascript
138+
var randu = require( '@stdlib/random/base/randu' );
139+
var round = require( '@stdlib/math/base/special/round' );
140+
var Float64Array = require( '@stdlib/array/float64' );
141+
var range = require( '@stdlib/stats/base/range' );
142+
143+
var x;
144+
var i;
145+
146+
x = new Float64Array( 10 );
147+
for ( i = 0; i < x.length; i++ ) {
148+
x[ i ] = round( (randu()*100.0) - 50.0 );
149+
}
150+
console.log( x );
151+
152+
var v = range( x.length, x, 1 );
153+
console.log( v );
154+
```
155+
156+
</section>
157+
158+
<!-- /.examples -->
159+
160+
<section class="links">
161+
162+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
163+
164+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
165+
166+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
167+
168+
[@stdlib/stats/base/drange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/drange
169+
170+
[@stdlib/stats/base/srange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/srange
171+
172+
</section>
173+
174+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var range = require( './../lib/range.js' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x;
42+
var i;
43+
44+
x = [];
45+
for ( i = 0; i < len; i++ ) {
46+
x.push( ( randu()*20.0 ) - 10.0 );
47+
}
48+
return benchmark;
49+
50+
function benchmark( b ) {
51+
var v;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
v = range( x.length, x, 1 );
57+
if ( isnan( v ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( v ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
f = createBenchmark( len );
91+
bench( pkg+':len='+len, f );
92+
}
93+
}
94+
95+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 randu = require( '@stdlib/random/base/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
var range = require( './../lib/ndarray.js' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var x;
42+
var i;
43+
44+
x = [];
45+
for ( i = 0; i < len; i++ ) {
46+
x.push( ( randu()*20.0 ) - 10.0 );
47+
}
48+
return benchmark;
49+
50+
function benchmark( b ) {
51+
var v;
52+
var i;
53+
54+
b.tic();
55+
for ( i = 0; i < b.iterations; i++ ) {
56+
v = range( x.length, x, 1, 0 );
57+
if ( isnan( v ) ) {
58+
b.fail( 'should not return NaN' );
59+
}
60+
}
61+
b.toc();
62+
if ( isnan( v ) ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
b.pass( 'benchmark finished' );
66+
b.end();
67+
}
68+
}
69+
70+
71+
// MAIN //
72+
73+
/**
74+
* Main execution sequence.
75+
*
76+
* @private
77+
*/
78+
function main() {
79+
var len;
80+
var min;
81+
var max;
82+
var f;
83+
var i;
84+
85+
min = 1; // 10^min
86+
max = 6; // 10^max
87+
88+
for ( i = min; i <= max; i++ ) {
89+
len = pow( 10, i );
90+
f = createBenchmark( len );
91+
bench( pkg+':ndarray:len='+len, f );
92+
}
93+
}
94+
95+
main();

0 commit comments

Comments
 (0)