Skip to content

Commit 609ca76

Browse files
committed
feat: add stats/strided/max-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 c584077 commit 609ca76

File tree

15 files changed

+2206
-0
lines changed

15 files changed

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