Skip to content

Commit 39a137b

Browse files
committed
feat: add stats-max-by
--- 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 9a2b61d commit 39a137b

File tree

10 files changed

+937
-0
lines changed

10 files changed

+937
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
> Compute the maximum value of a one-dimensional ndarray via a callback function.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var maxBy = require( '@stdlib/stats/base/ndarray/max-by' );
37+
```
38+
39+
#### maxBy( arrays, clbk\[, thisArg ] )
40+
41+
Computes the maximum value of a one-dimensional ndarray via a callback function.
42+
43+
```javascript
44+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
45+
46+
function clbk( value ) {
47+
return value * 2.0;
48+
}
49+
50+
var xbuf = [ 1.0, 3.0, 4.0, 2.0 ];
51+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
52+
53+
var v = maxBy( [ x ], clbk );
54+
// returns 8.0
55+
```
56+
57+
The function has the following parameters:
58+
59+
- **arrays**: array-like object containing a one-dimensional input ndarray.
60+
- **clbk**: callback function.
61+
- **thisArg**: callback execution context (_optional_).
62+
63+
The invoked callback is provided four arguments:
64+
65+
- **value**: array element.
66+
- **aidx**: array index.
67+
- **sidx**: strided index (`offset + aidx*stride`).
68+
- **array**: input array/collection.
69+
70+
To set the callback execution context, provide a `thisArg`.
71+
72+
```javascript
73+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
74+
75+
function clbk( value ) {
76+
this.count += 1;
77+
return value * 2.0;
78+
}
79+
80+
var xbuf = [ 1.0, 3.0, 4.0, 2.0 ];
81+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
82+
var ctx = {
83+
'count': 0
84+
};
85+
86+
var v = maxBy( [ x ], clbk, ctx );
87+
// returns 8.0
88+
89+
var count = ctx.count;
90+
// returns 4
91+
```
92+
93+
</section>
94+
95+
<!-- /.usage -->
96+
97+
<section class="notes">
98+
99+
## Notes
100+
101+
- If provided an empty one-dimensional ndarray, the function returns `NaN`.
102+
103+
</section>
104+
105+
<!-- /.notes -->
106+
107+
<section class="examples">
108+
109+
## Examples
110+
111+
<!-- eslint no-undef: "error" -->
112+
113+
```javascript
114+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
115+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
116+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
117+
var maxBy = require( '@stdlib/stats/base/ndarray/max-by' );
118+
119+
function clbk( value ) {
120+
return value * 2.0;
121+
}
122+
123+
var xbuf = discreteUniform( 10, -50, 50, {
124+
'dtype': 'generic'
125+
});
126+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
127+
console.log( ndarray2array( x ) );
128+
129+
var v = maxBy( [ x ] );
130+
console.log( v );
131+
```
132+
133+
</section>
134+
135+
<!-- /.examples -->
136+
137+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
138+
139+
<section class="related">
140+
141+
</section>
142+
143+
<!-- /.related -->
144+
145+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
146+
147+
<section class="links">
148+
149+
</section>
150+
151+
<!-- /.links -->
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var pkg = require( './../package.json' ).name;
29+
var maxBy = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Callback function.
43+
*
44+
* @private
45+
* @param {number} value - array element
46+
* @returns {number} callback result
47+
*/
48+
function clbk( value ) {
49+
return value * 2.0;
50+
}
51+
52+
/**
53+
* Creates a benchmark function.
54+
*
55+
* @private
56+
* @param {PositiveInteger} len - array length
57+
* @returns {Function} benchmark function
58+
*/
59+
function createBenchmark( len ) {
60+
var xbuf;
61+
var x;
62+
63+
xbuf = uniform( len, -10.0, 10.0, options );
64+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
65+
66+
return benchmark;
67+
68+
function benchmark( b ) {
69+
var v;
70+
var i;
71+
72+
b.tic();
73+
for ( i = 0; i < b.iterations; i++ ) {
74+
v = maxBy( [ x ], clbk );
75+
if ( isnan( v ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
}
79+
b.toc();
80+
if ( isnan( v ) ) {
81+
b.fail( 'should not return NaN' );
82+
}
83+
b.pass( 'benchmark finished' );
84+
b.end();
85+
}
86+
}
87+
88+
89+
// MAIN //
90+
91+
/**
92+
* Main execution sequence.
93+
*
94+
* @private
95+
*/
96+
function main() {
97+
var len;
98+
var min;
99+
var max;
100+
var f;
101+
var i;
102+
103+
min = 1; // 10^min
104+
max = 6; // 10^max
105+
106+
for ( i = min; i <= max; i++ ) {
107+
len = pow( 10, i );
108+
f = createBenchmark( len );
109+
bench( pkg+':len='+len, f );
110+
}
111+
}
112+
113+
main();
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( arrays, clbk[, thisArg] )
3+
Computes the maximum value of a one-dimensional ndarray via a callback
4+
function.
5+
6+
If provided an empty ndarray, the function returns `NaN`.
7+
8+
Parameters
9+
----------
10+
arrays: ArrayLikeObject<ndarray>
11+
Array-like object containing a one-dimensional input ndarray.
12+
13+
clbk: Function
14+
Callback function.
15+
16+
thisArg: any (optional)
17+
Callback execution context.
18+
19+
Returns
20+
-------
21+
out: number
22+
Maximum value.
23+
24+
Examples
25+
--------
26+
> var xbuf = [ 1.0, -2.0, 2.0 ];
27+
> var dt = 'generic';
28+
> var sh = [ xbuf.length ];
29+
> var sx = [ 1 ];
30+
> var ox = 0;
31+
> var ord = 'row-major';
32+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
33+
> function f ( v ) { return v * 2.0; };
34+
> {{alias}}( [ x ], f )
35+
4.0
36+
37+
See Also
38+
--------
39+

0 commit comments

Comments
 (0)