Skip to content

Commit 1c56b53

Browse files
gururaj1512kgryte
andauthored
feat: add stats/array/nanmskrange
PR-URL: #7514 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 803f30a commit 1c56b53

File tree

10 files changed

+954
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)