Skip to content

Commit 8ecbdfa

Browse files
committed
feat: add stats/strided/range-by
Ref: #4797 --- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent a742112 commit 8ecbdfa

File tree

15 files changed

+2224
-0
lines changed

15 files changed

+2224
-0
lines changed
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
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+
# rangeBy
22+
23+
> Calculate the [range][range] of a strided array via a callback function.
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 rangeBy = require( '@stdlib/stats/strided/range-by' );
39+
```
40+
41+
#### rangeBy( N, x, strideX, clbk\[, thisArg] )
42+
43+
Computes the [range][range] of a strided array via a callback function.
44+
45+
```javascript
46+
function accessor( v ) {
47+
return v * 2.0;
48+
}
49+
50+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
51+
52+
var v = rangeBy( x.length, x, 1, accessor );
53+
// returns 18.0
54+
```
55+
56+
The function has the following parameters:
57+
58+
- **N**: number of indexed elements.
59+
- **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions).
60+
- **strideX**: stride length.
61+
- **clbk**: callback function.
62+
- **thisArg**: execution context (_optional_).
63+
64+
The invoked callback is provided four arguments:
65+
66+
- **value**: array element.
67+
- **aidx**: array index.
68+
- **sidx**: strided index (`offset + aidx*stride`).
69+
- **array**: input array/collection.
70+
71+
To set the callback execution context, provide a `thisArg`.
72+
73+
```javascript
74+
function accessor( v ) {
75+
this.count += 1;
76+
return v * 2.0;
77+
}
78+
79+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
80+
81+
var context = {
82+
'count': 0
83+
};
84+
85+
var v = rangeBy( x.length, x, 1, accessor, context );
86+
// returns 18.0
87+
88+
var cnt = context.count;
89+
// returns 8
90+
```
91+
92+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element
93+
94+
```javascript
95+
function accessor( v ) {
96+
return v * 2.0;
97+
}
98+
99+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
100+
101+
var v = rangeBy( 4, x, 2, accessor );
102+
// returns 12.0
103+
```
104+
105+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
106+
107+
```javascript
108+
var Float64Array = require( '@stdlib/array/float64' );
109+
110+
function accessor( v ) {
111+
return v * 2.0;
112+
}
113+
114+
// Initial array...
115+
var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
116+
117+
// Create an offset view...
118+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
119+
120+
// Access every other element...
121+
var v = rangeBy( 3, x1, 2, accessor );
122+
// returns 8.0
123+
```
124+
125+
#### rangeBy.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] )
126+
127+
Computes the [range][range] of a strided array via a callback function and using alternative indexing semantics.
128+
129+
```javascript
130+
function accessor( v ) {
131+
return v * 2.0;
132+
}
133+
134+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
135+
136+
var v = rangeBy.ndarray( x.length, x, 1, 0, accessor );
137+
// returns 18.0
138+
```
139+
140+
The function has the following additional parameters:
141+
142+
- **offsetX**: starting index.
143+
144+
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 access only the last three elements of `x`
145+
146+
```javascript
147+
function accessor( v ) {
148+
return v * 2.0;
149+
}
150+
151+
var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
152+
153+
var v = rangeBy.ndarray( 3, x, 1, x.length-3, accessor );
154+
// returns 22.0
155+
```
156+
157+
</section>
158+
159+
<!-- /.usage -->
160+
161+
<section class="notes">
162+
163+
## Notes
164+
165+
- If `N <= 0`, both functions return `NaN`.
166+
- A provided callback function should return a numeric value.
167+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
168+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
169+
- When possible, prefer using [`drange`][@stdlib/stats/strided/drange], [`srange`][@stdlib/stats/strided/srange], and/or [`range`][@stdlib/stats/base/range], as, depending on the environment, these interfaces are likely to be significantly more performant.
170+
171+
</section>
172+
173+
<!-- /.notes -->
174+
175+
<section class="examples">
176+
177+
## Examples
178+
179+
<!-- eslint no-undef: "error" -->
180+
181+
```javascript
182+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
183+
var rangeBy = require( '@stdlib/stats/strided/range-by' );
184+
185+
function accessor( v ) {
186+
return v * 2.0;
187+
}
188+
189+
var x = discreteUniform( 10, -50, 50, {
190+
'dtype': 'float64'
191+
});
192+
console.log( x );
193+
194+
var v = rangeBy( x.length, x, 1, accessor );
195+
console.log( v );
196+
```
197+
198+
</section>
199+
200+
<!-- /.examples -->
201+
202+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
203+
204+
<section class="related">
205+
206+
* * *
207+
208+
## See Also
209+
210+
- <span class="package-name">[`@stdlib/stats/strided/drange`][@stdlib/stats/strided/drange]</span><span class="delimiter">: </span><span class="description">calculate the range of a double-precision floating-point strided array.</span>
211+
- <span class="package-name">[`@stdlib/stats/strided/max-by`][@stdlib/stats/strided/max-by]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a strided array via a callback function.</span>
212+
- <span class="package-name">[`@stdlib/stats/strided/min-by`][@stdlib/stats/strided/min-by]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a strided array via a callback function.</span>
213+
- <span class="package-name">[`@stdlib/stats/strided/nanrange-by`][@stdlib/stats/strided/nanrange-by]</span><span class="delimiter">: </span><span class="description">calculate the range of a strided array via a callback function, ignoring NaN values.</span>
214+
- <span class="package-name">[`@stdlib/stats/base/range`][@stdlib/stats/base/range]</span><span class="delimiter">: </span><span class="description">calculate the range of a strided array.</span>
215+
- <span class="package-name">[`@stdlib/stats/strided/srange`][@stdlib/stats/strided/srange]</span><span class="delimiter">: </span><span class="description">calculate the range of a single-precision floating-point strided array.</span>
216+
217+
</section>
218+
219+
<!-- /.related -->
220+
221+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
222+
223+
<section class="links">
224+
225+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
226+
227+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
228+
229+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
230+
231+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
232+
233+
<!-- <related-links> -->
234+
235+
[@stdlib/stats/strided/drange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/drange
236+
237+
[@stdlib/stats/strided/max-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/max-by
238+
239+
[@stdlib/stats/strided/min-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/min-by
240+
241+
[@stdlib/stats/strided/nanrange-by]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/nanrange-by
242+
243+
[@stdlib/stats/base/range]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/range
244+
245+
[@stdlib/stats/strided/srange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/srange
246+
247+
<!-- </related-links> -->
248+
249+
</section>
250+
251+
<!-- /.links -->
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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 uniform = require( '@stdlib/random/array/uniform' );
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 rangeBy = require( './../lib/main.js' );
29+
30+
31+
// VARIABLES //
32+
33+
var options = {
34+
'dtype': 'generic'
35+
};
36+
37+
38+
// FUNCTIONS //
39+
40+
/**
41+
* Accessor function.
42+
*
43+
* @private
44+
* @param {number} value - array element
45+
* @returns {number} accessed value
46+
*/
47+
function accessor( value ) {
48+
return value * 2.0;
49+
}
50+
51+
/**
52+
* Create a benchmark function.
53+
*
54+
* @private
55+
* @param {PositiveInteger} len - array length
56+
* @returns {Function} benchmark function
57+
*/
58+
function createBenchmark( len ) {
59+
var x = uniform( len, -10, 10, options );
60+
return benchmark;
61+
62+
function benchmark( b ) {
63+
var y;
64+
var i;
65+
66+
b.tic();
67+
for ( i = 0; i < b.iterations; i++ ) {
68+
y = rangeBy( x.length, x, 1, accessor );
69+
if ( isnan( y ) ) {
70+
b.fail( 'should not return NaN' );
71+
}
72+
}
73+
b.toc();
74+
if ( isnan( y ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( pkg+':len='+len, f );
99+
}
100+
}
101+
102+
main();

0 commit comments

Comments
 (0)