Skip to content

Commit 68037ff

Browse files
feat: add stats/base/ndarray/nanmax-by
PR-URL: #8640 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 2e44271 commit 68037ff

File tree

10 files changed

+1015
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)