Skip to content

Commit 5c941d3

Browse files
Sachinn-64kgryte
andauthored
feat: add stats/base/ndarray/nanmin-by
PR-URL: #8681 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent ce61923 commit 5c941d3

File tree

10 files changed

+1053
-0
lines changed

10 files changed

+1053
-0
lines changed
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
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+
# nanminBy
22+
23+
> Compute the minimum 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 nanminBy = require( '@stdlib/stats/base/ndarray/nanmin-by' );
37+
```
38+
39+
#### nanminBy( arrays, clbk\[, thisArg ] )
40+
41+
Computes the minimum 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, -2.0, NaN, 2.0 ];
51+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
52+
53+
var v = nanminBy( [ x ], clbk );
54+
// returns -4.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, -2.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 = nanminBy( [ x ], clbk, ctx );
86+
// returns -4.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 filledarrayBy = require( '@stdlib/array/filled-by' );
117+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
118+
var uniform = require( '@stdlib/random/base/uniform' );
119+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
120+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
121+
var nanminBy = require( '@stdlib/stats/base/ndarray/nanmin-by' );
122+
123+
function rand() {
124+
if ( bernoulli( 0.2 ) > 0 ) {
125+
return NaN;
126+
}
127+
return uniform( -50.0, 50.0 );
128+
}
129+
130+
function clbk( value ) {
131+
return value * 2.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 = nanminBy( [ x ], clbk );
139+
console.log( v );
140+
```
141+
142+
</section>
143+
144+
<!-- /.examples -->
145+
146+
<section class="references">
147+
148+
</section>
149+
150+
<!-- /.references -->
151+
152+
<section class="related">
153+
154+
</section>
155+
156+
<!-- /.related -->
157+
158+
<section class="links">
159+
160+
</section>
161+
162+
<!-- /.links -->
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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 ndarray = require( '@stdlib/ndarray/base/ctor' );
30+
var pkg = require( './../package.json' ).name;
31+
var nanminBy = require( './../lib' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Callback function.
38+
*
39+
* @private
40+
* @param {number} value - array element
41+
* @returns {number} callback result
42+
*/
43+
function clbk( value ) {
44+
return value * 2.0;
45+
}
46+
47+
/**
48+
* Returns a random number.
49+
*
50+
* @private
51+
* @returns {number} random number or `NaN`
52+
*/
53+
function rand() {
54+
if ( bernoulli( 0.8 ) < 1 ) {
55+
return NaN;
56+
}
57+
return uniform( -10.0, 10.0 );
58+
}
59+
60+
/**
61+
* Creates a benchmark function.
62+
*
63+
* @private
64+
* @param {PositiveInteger} len - array length
65+
* @returns {Function} benchmark function
66+
*/
67+
function createBenchmark( len ) {
68+
var xbuf;
69+
var x;
70+
71+
xbuf = filledarrayBy( len, 'generic', rand );
72+
x = new ndarray( 'generic', xbuf, [ len ], [ 1 ], 0, 'row-major' );
73+
74+
return benchmark;
75+
76+
function benchmark( b ) {
77+
var v;
78+
var i;
79+
80+
b.tic();
81+
for ( i = 0; i < b.iterations; i++ ) {
82+
v = nanminBy( [ x ], clbk );
83+
if ( isnan( v ) ) {
84+
b.fail( 'should not return NaN' );
85+
}
86+
}
87+
b.toc();
88+
if ( isnan( v ) ) {
89+
b.fail( 'should not return NaN' );
90+
}
91+
b.pass( 'benchmark finished' );
92+
b.end();
93+
}
94+
}
95+
96+
97+
// MAIN //
98+
99+
/**
100+
* Main execution sequence.
101+
*
102+
* @private
103+
*/
104+
function main() {
105+
var len;
106+
var min;
107+
var max;
108+
var f;
109+
var i;
110+
111+
min = 1; // 10^min
112+
max = 6; // 10^max
113+
114+
for ( i = min; i <= max; i++ ) {
115+
len = pow( 10, i );
116+
f = createBenchmark( len );
117+
bench( pkg+':len='+len, f );
118+
}
119+
}
120+
121+
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 minimum 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+
Minimum value.
36+
37+
Examples
38+
--------
39+
> var xbuf = [ 1.0, -2.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)