Skip to content

Commit cb3fa68

Browse files
feat: add stats/array/nanmin
PR-URL: #6971 Reviewed-by: Athan Reines <[email protected]> Co-authored-by: stdlib-bot <[email protected]>
1 parent 18f3021 commit cb3fa68

File tree

10 files changed

+745
-0
lines changed

10 files changed

+745
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
# nanmin
22+
23+
> Calculate the minimum value of an array, 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 nanmin = require( '@stdlib/stats/array/nanmin' );
37+
```
38+
39+
#### nanmin( x )
40+
41+
Computes the minimum value of an array, ignoring `NaN` values.
42+
43+
```javascript
44+
var x = [ 1.0, -2.0, NaN, 2.0 ];
45+
46+
var v = nanmin( x );
47+
// returns -2.0
48+
```
49+
50+
The function has the following parameters:
51+
52+
- **x**: input array.
53+
54+
</section>
55+
56+
<!-- /.usage -->
57+
58+
<section class="notes">
59+
60+
## Notes
61+
62+
- If provided an empty array, the function returns `NaN`.
63+
- 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]).
64+
65+
</section>
66+
67+
<!-- /.notes -->
68+
69+
<section class="examples">
70+
71+
## Examples
72+
73+
<!-- eslint no-undef: "error" -->
74+
75+
```javascript
76+
var uniform = require( '@stdlib/random/base/uniform' );
77+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
78+
var filledarrayBy = require( '@stdlib/array/filled-by' );
79+
var nanmin = require( '@stdlib/stats/array/nanmin' );
80+
81+
function rand() {
82+
if ( bernoulli( 0.8 ) < 1 ) {
83+
return NaN;
84+
}
85+
return uniform( -50.0, 50.0 );
86+
}
87+
88+
var x = filledarrayBy( 10, 'float64', rand );
89+
console.log( x );
90+
91+
var v = nanmin( x );
92+
console.log( v );
93+
```
94+
95+
</section>
96+
97+
<!-- /.examples -->
98+
99+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
100+
101+
<section class="related">
102+
103+
</section>
104+
105+
<!-- /.related -->
106+
107+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
108+
109+
<section class="links">
110+
111+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
112+
113+
</section>
114+
115+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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 nanmin = require( './../lib' );
31+
32+
33+
// FUNCTIONS //
34+
35+
/**
36+
* Returns a random number.
37+
*
38+
* @private
39+
* @returns {number} random number
40+
*/
41+
function rand() {
42+
if ( bernoulli( 0.8 ) < 1 ) {
43+
return NaN;
44+
}
45+
return uniform( -10.0, 10.0 );
46+
}
47+
48+
/**
49+
* Creates a benchmark function.
50+
*
51+
* @private
52+
* @param {PositiveInteger} len - array length
53+
* @returns {Function} benchmark function
54+
*/
55+
function createBenchmark( len ) {
56+
var x = filledarrayBy( len, 'generic', rand );
57+
return benchmark;
58+
59+
function benchmark( b ) {
60+
var v;
61+
var i;
62+
63+
b.tic();
64+
for ( i = 0; i < b.iterations; i++ ) {
65+
v = nanmin( x );
66+
if ( isnan( v ) ) {
67+
b.fail( 'should not return NaN' );
68+
}
69+
}
70+
b.toc();
71+
if ( isnan( v ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
b.pass( 'benchmark finished' );
75+
b.end();
76+
}
77+
}
78+
79+
80+
// MAIN //
81+
82+
/**
83+
* Main execution sequence.
84+
*
85+
* @private
86+
*/
87+
function main() {
88+
var len;
89+
var min;
90+
var max;
91+
var f;
92+
var i;
93+
94+
min = 1; // 10^min
95+
max = 6; // 10^max
96+
97+
for ( i = min; i <= max; i++ ) {
98+
len = pow( 10, i );
99+
f = createBenchmark( len );
100+
bench( pkg+':len='+len, f );
101+
}
102+
}
103+
104+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
{{alias}}( x )
3+
Computes the minimum value of an array, ignoring `NaN` values.
4+
5+
If provided an empty array, the function returns `NaN`.
6+
7+
Parameters
8+
----------
9+
x: Array<number>|TypedArray
10+
Input array.
11+
12+
Returns
13+
-------
14+
out: number
15+
Minimum value.
16+
17+
Examples
18+
--------
19+
> var x = [ 1.0, -2.0, NaN, 2.0 ];
20+
> {{alias}}( x )
21+
-2.0
22+
23+
See Also
24+
--------
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { NumericArray, Collection, AccessorArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Input array.
27+
*/
28+
type InputArray = NumericArray | Collection<number> | AccessorArrayLike<number>;
29+
30+
/**
31+
* Computes the minimum value of an array, ignoring `NaN` values.
32+
*
33+
* @param x - input array
34+
* @returns minimum value
35+
*
36+
* @example
37+
* var x = [ 1.0, -2.0, NaN, 2.0 ];
38+
*
39+
* var v = nanmin( x );
40+
* // returns -2.0
41+
*/
42+
declare function nanmin( x: InputArray ): number;
43+
44+
45+
// EXPORTS //
46+
47+
export = nanmin;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
import AccessorArray = require( '@stdlib/array/base/accessor' );
20+
import nanmin = require( './index' );
21+
22+
23+
// TESTS //
24+
25+
// The function returns a number...
26+
{
27+
const x = new Float64Array( 10 );
28+
29+
nanmin( x ); // $ExpectType number
30+
nanmin( new AccessorArray( x ) ); // $ExpectType number
31+
}
32+
33+
// The compiler throws an error if the function is provided a first argument which is not a numeric array...
34+
{
35+
nanmin( 10 ); // $ExpectError
36+
nanmin( '10' ); // $ExpectError
37+
nanmin( true ); // $ExpectError
38+
nanmin( false ); // $ExpectError
39+
nanmin( null ); // $ExpectError
40+
nanmin( undefined ); // $ExpectError
41+
nanmin( {} ); // $ExpectError
42+
nanmin( ( x: number ): number => x ); // $ExpectError
43+
}
44+
45+
// The compiler throws an error if the function is provided an unsupported number of arguments...
46+
{
47+
const x = new Float64Array( 10 );
48+
49+
nanmin(); // $ExpectError
50+
nanmin( x, {} ); // $ExpectError
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
var uniform = require( '@stdlib/random/base/uniform' );
22+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
23+
var filledarrayBy = require( '@stdlib/array/filled-by' );
24+
var nanmin = require( './../lib' );
25+
26+
function rand() {
27+
if ( bernoulli( 0.8 ) < 1 ) {
28+
return NaN;
29+
}
30+
return uniform( -50.0, 50.0 );
31+
}
32+
33+
var x = filledarrayBy( 10, 'float64', rand );
34+
console.log( x );
35+
36+
var v = nanmin( x );
37+
console.log( v );

0 commit comments

Comments
 (0)