Skip to content

Commit 937d30e

Browse files
gururaj1512kgrytestdlib-bot
authored
feat: add stats/array/nanmax-by
PR-URL: #7204 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent eb4a2bd commit 937d30e

File tree

10 files changed

+1012
-0
lines changed

10 files changed

+1012
-0
lines changed
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
# nanmaxBy
22+
23+
> Calculate the maximum value of an array via a callback function, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var nanmaxBy = require( '@stdlib/stats/array/nanmax-by' );
37+
```
38+
39+
#### nanmaxBy( x, clbk\[, thisArg] )
40+
41+
Computes the maximum value of an array via a callback function, ignoring `NaN` values.
42+
43+
```javascript
44+
function accessor( v ) {
45+
return v * 2.0;
46+
}
47+
48+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0, NaN ];
49+
50+
var v = nanmaxBy( x, accessor );
51+
// returns 8.0
52+
```
53+
54+
The function has the following parameters:
55+
56+
- **x**: input array.
57+
- **clbk**: callback function.
58+
- **thisArg**: execution context (_optional_).
59+
60+
The invoked callback is provided three arguments:
61+
62+
- **value**: current array element.
63+
- **index**: current array index.
64+
- **array**: input array.
65+
66+
To set the callback execution context, provide a `thisArg`.
67+
68+
```javascript
69+
function accessor( v ) {
70+
this.count += 1;
71+
return v * 2.0;
72+
}
73+
74+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, 0.0, -1.0, -3.0, NaN ];
75+
76+
var context = {
77+
'count': 0
78+
};
79+
80+
var v = nanmaxBy( x, accessor, context );
81+
// returns 8.0
82+
83+
var cnt = context.count;
84+
// returns 10
85+
```
86+
87+
</section>
88+
89+
<!-- /.usage -->
90+
91+
<section class="notes">
92+
93+
## Notes
94+
95+
- If provided an empty array, the function returns `NaN`.
96+
- A provided callback function should return a numeric value.
97+
- If a provided callback function returns `NaN`, the value is **ignored**.
98+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
99+
- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
100+
101+
</section>
102+
103+
<!-- /.notes -->
104+
105+
<section class="examples">
106+
107+
## Examples
108+
109+
<!-- eslint no-undef: "error" -->
110+
111+
```javascript
112+
var uniform = require( '@stdlib/random/base/uniform' );
113+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
114+
var filledarrayBy = require( '@stdlib/array/filled-by' );
115+
var nanmaxBy = require( '@stdlib/stats/array/nanmax-by' );
116+
117+
function rand() {
118+
if ( bernoulli( 0.8 ) < 1 ) {
119+
return NaN;
120+
}
121+
return uniform( -50.0, 50.0 );
122+
}
123+
124+
function accessor( v ) {
125+
return v * 2.0;
126+
}
127+
128+
var x = filledarrayBy( 10, 'float64', rand );
129+
console.log( x );
130+
131+
var v = nanmaxBy( x, accessor );
132+
console.log( v );
133+
```
134+
135+
</section>
136+
137+
<!-- /.examples -->
138+
139+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
140+
141+
<section class="related">
142+
143+
</section>
144+
145+
<!-- /.related -->
146+
147+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
148+
149+
<section class="links">
150+
151+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
152+
153+
</section>
154+
155+
<!-- /.links -->
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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/base/uniform' );
25+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var pkg = require( './../package.json' ).name;
30+
var nanmaxBy = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Accessor function.
37+
*
38+
* @private
39+
* @param {number} value - array element
40+
* @returns {number} accessed value
41+
*/
42+
function accessor( value ) {
43+
return value * 2.0;
44+
}
45+
46+
/**
47+
* Returns a random number.
48+
*
49+
* @private
50+
* @returns {number} random number
51+
*/
52+
function rand() {
53+
if ( bernoulli( 0.8 ) < 1 ) {
54+
return NaN;
55+
}
56+
return uniform( -10.0, 10.0 );
57+
}
58+
59+
/**
60+
* Creates a benchmark function.
61+
*
62+
* @private
63+
* @param {PositiveInteger} len - array length
64+
* @returns {Function} benchmark function
65+
*/
66+
function createBenchmark( len ) {
67+
var x = filledarrayBy( len, 'generic', rand );
68+
return benchmark;
69+
70+
function benchmark( b ) {
71+
var v;
72+
var i;
73+
74+
b.tic();
75+
for ( i = 0; i < b.iterations; i++ ) {
76+
v = nanmaxBy( x, accessor );
77+
if ( isnan( v ) ) {
78+
b.fail( 'should not return NaN' );
79+
}
80+
}
81+
b.toc();
82+
if ( isnan( v ) ) {
83+
b.fail( 'should not return NaN' );
84+
}
85+
b.pass( 'benchmark finished' );
86+
b.end();
87+
}
88+
}
89+
90+
91+
// MAIN //
92+
93+
/**
94+
* Main execution sequence.
95+
*
96+
* @private
97+
*/
98+
function main() {
99+
var len;
100+
var min;
101+
var max;
102+
var f;
103+
var i;
104+
105+
min = 1; // 10^min
106+
max = 6; // 10^max
107+
108+
for ( i = min; i <= max; i++ ) {
109+
len = pow( 10, i );
110+
f = createBenchmark( len );
111+
bench( pkg+':len='+len, f );
112+
}
113+
}
114+
115+
main();
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
2+
{{alias}}( x, clbk[, thisArg] )
3+
Computes the maximum value of an array via a callback function, ignoring
4+
`NaN` values.
5+
6+
If provided an empty array, the function returns `NaN`.
7+
8+
The callback function is provided three arguments:
9+
10+
- value: current array element.
11+
- index: current array index.
12+
- array: the input array.
13+
14+
The callback function should return a numeric value.
15+
16+
If the callback function returns `NaN`, the value is ignored.
17+
18+
If the callback function does not return any value (or equivalently,
19+
explicitly returns `undefined`), the value is ignored.
20+
21+
Parameters
22+
----------
23+
x: Array|TypedArray
24+
Input array.
25+
26+
clbk: Function
27+
Callback function.
28+
29+
thisArg: any (optional)
30+
Execution context.
31+
32+
Returns
33+
-------
34+
out: number
35+
Maximum value.
36+
37+
Examples
38+
--------
39+
> function accessor( v ) { return v * 2.0; };
40+
> var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, NaN, -1.0, -3.0 ];
41+
> {{alias}}( x, accessor )
42+
8.0
43+
44+
See Also
45+
--------
46+

0 commit comments

Comments
 (0)