Skip to content

Commit 20722e9

Browse files
authored
feat: add stats/array/mskmin
PR-URL: #6963 Reviewed-by: Athan Reines <[email protected]>
1 parent 37476e8 commit 20722e9

File tree

10 files changed

+928
-0
lines changed

10 files changed

+928
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
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+
# mskmin
22+
23+
> Calculate the minimum value of an array according to a mask.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var mskmin = require( '@stdlib/stats/array/mskmin' );
37+
```
38+
39+
#### mskmin( x, mask )
40+
41+
Computes the minimum value of an array according to a mask.
42+
43+
```javascript
44+
var x = [ 1.0, -2.0, -4.0, 2.0 ];
45+
var mask = [ 0, 0, 1, 0 ];
46+
47+
var v = mskmin( x, mask );
48+
// returns -2.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/array/uniform' );
79+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
80+
var mskmin = require( '@stdlib/stats/array/mskmin' );
81+
82+
var x = uniform( 10, -50.0, 50.0, {
83+
'dtype': 'float64'
84+
});
85+
console.log( x );
86+
87+
var mask = bernoulli( x.length, 0.2, {
88+
'dtype': 'uint8'
89+
});
90+
console.log( mask );
91+
92+
var v = mskmin( x, mask );
93+
console.log( v );
94+
```
95+
96+
</section>
97+
98+
<!-- /.examples -->
99+
100+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
101+
102+
<section class="related">
103+
104+
</section>
105+
106+
<!-- /.related -->
107+
108+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
109+
110+
<section class="links">
111+
112+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
113+
114+
</section>
115+
116+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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/array/uniform' );
25+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var pkg = require( './../package.json' ).name;
29+
var mskmin = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var mask = bernoulli( len, 0.2, options );
50+
var x = uniform( len, -10, 10, options );
51+
return benchmark;
52+
53+
function benchmark( b ) {
54+
var v;
55+
var i;
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
v = mskmin( x, mask );
60+
if ( isnan( v ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
}
64+
b.toc();
65+
if ( isnan( v ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
}
71+
}
72+
73+
74+
// MAIN //
75+
76+
/**
77+
* Main execution sequence.
78+
*
79+
* @private
80+
*/
81+
function main() {
82+
var len;
83+
var min;
84+
var max;
85+
var f;
86+
var i;
87+
88+
min = 1; // 10^min
89+
max = 6; // 10^max
90+
91+
for ( i = min; i <= max; i++ ) {
92+
len = pow( 10, i );
93+
f = createBenchmark( len );
94+
bench( pkg+':len='+len, f );
95+
}
96+
}
97+
98+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
{{alias}}( x, mask )
3+
Computes the minimum value of an array according to a mask.
4+
5+
If a `mask` array element is `0`, the corresponding element in `x` is
6+
considered valid and included in computation.
7+
8+
If a `mask` array element is `1`, the corresponding element in `x` is
9+
considered invalid/missing and excluded from computation.
10+
11+
If provided an empty array, the function returns `NaN`.
12+
13+
Parameters
14+
----------
15+
x: Array<number>|TypedArray
16+
Input array.
17+
18+
mask: Array<number>|TypedArray
19+
Mask array.
20+
21+
Returns
22+
-------
23+
out: number
24+
Minimum value.
25+
26+
Examples
27+
--------
28+
> var x = [ 1.0, -2.0, -4.0, 2.0 ];
29+
> var msk = [ 0, 0, 1, 0 ];
30+
> {{alias}}( x, msk )
31+
-2.0
32+
33+
See Also
34+
--------
35+
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 minimum value of an array according to a mask.
32+
*
33+
* @param x - input array
34+
* @param mask - mask array
35+
* @returns minimum value
36+
*
37+
* @example
38+
* var x = [ 1.0, -2.0, -4.0, 2.0 ];
39+
* var mask = [ 0, 0, 1, 0 ];
40+
*
41+
* var v = mskmin( x, mask );
42+
* // returns -2.0
43+
*/
44+
declare function mskmin( x: InputArray, mask: InputArray ): number;
45+
46+
47+
// EXPORTS //
48+
49+
export = mskmin;
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 mskmin = 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+
mskmin( x, mask ); // $ExpectType number
31+
mskmin( 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+
mskmin( 10, mask ); // $ExpectError
39+
mskmin( '10', mask ); // $ExpectError
40+
mskmin( true, mask ); // $ExpectError
41+
mskmin( false, mask ); // $ExpectError
42+
mskmin( null, mask ); // $ExpectError
43+
mskmin( undefined, mask ); // $ExpectError
44+
mskmin( {}, mask ); // $ExpectError
45+
mskmin( ( 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+
mskmin( x, 10 ); // $ExpectError
53+
mskmin( x, '10' ); // $ExpectError
54+
mskmin( x, true ); // $ExpectError
55+
mskmin( x, false ); // $ExpectError
56+
mskmin( x, null ); // $ExpectError
57+
mskmin( x, undefined ); // $ExpectError
58+
mskmin( x, {} ); // $ExpectError
59+
mskmin( 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+
mskmin(); // $ExpectError
68+
mskmin( x ); // $ExpectError
69+
mskmin( x, mask, {} ); // $ExpectError
70+
}

0 commit comments

Comments
 (0)