Skip to content

Commit 436565a

Browse files
Orthodox-64kgryte
andauthored
feat: add stats/base/ndarray/mskmax
PR-URL: #8448 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent b10256d commit 436565a

File tree

10 files changed

+841
-0
lines changed

10 files changed

+841
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
# mskmax
22+
23+
> Calculate the maximum value of a one-dimensional ndarray 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 mskmax = require( '@stdlib/stats/base/ndarray/mskmax' );
37+
```
38+
39+
#### mskmax( arrays )
40+
41+
Computes the maximum value of a one-dimensional ndarray according to a mask.
42+
43+
```javascript
44+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
45+
46+
var xbuf = [ 1.0, -2.0, 4.0, 2.0 ];
47+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
48+
49+
var maskbuf = [ 0, 0, 1, 0 ];
50+
var mask = new ndarray( 'generic', maskbuf, [ 4 ], [ 1 ], 0, 'row-major' );
51+
52+
var v = mskmax( [ x, mask ] );
53+
// returns 2.0
54+
```
55+
56+
The function has the following parameters:
57+
58+
- **arrays**: array-like object containing an input ndarray and a mask ndarray.
59+
60+
If a `mask` array element is `0`, the corresponding element in the input ndarray is considered valid and **included** in computation. If a `mask` array element is `1`, the corresponding element in the input ndarray is considered invalid/missing and **excluded** from computation.
61+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<section class="notes">
67+
68+
## Notes
69+
70+
- If provided an empty ndarray or a mask with all elements set to `1`, the function returns `NaN`.
71+
72+
</section>
73+
74+
<!-- /.notes -->
75+
76+
<section class="examples">
77+
78+
## Examples
79+
80+
<!-- eslint no-undef: "error" -->
81+
82+
```javascript
83+
var uniform = require( '@stdlib/random/array/uniform' );
84+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
85+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
86+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
87+
var mskmax = require( '@stdlib/stats/base/ndarray/mskmax' );
88+
89+
var xbuf = uniform( 10, -50.0, 50.0, {
90+
'dtype': 'float64'
91+
});
92+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
93+
console.log( ndarray2array( x ) );
94+
95+
var maskbuf = bernoulli( xbuf.length, 0.2, {
96+
'dtype': 'uint8'
97+
});
98+
var mask = new ndarray( 'generic', maskbuf, [ maskbuf.length ], [ 1 ], 0, 'row-major' );
99+
console.log( ndarray2array( mask ) );
100+
101+
var v = mskmax( [ x, mask ] );
102+
console.log( v );
103+
```
104+
105+
</section>
106+
107+
<!-- /.examples -->
108+
109+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
110+
111+
<section class="related">
112+
113+
</section>
114+
115+
<!-- /.related -->
116+
117+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
118+
119+
<section class="links">
120+
121+
</section>
122+
123+
<!-- /.links -->
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var pkg = require( './../package.json' ).name;
30+
var mskmax = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var mask;
51+
var mbuf;
52+
var xbuf;
53+
var x;
54+
55+
xbuf = uniform( len, -100.0, 100.0, options );
56+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
57+
58+
mbuf = bernoulli( len, 0.2, options );
59+
mask = new ndarray( options.dtype, mbuf, [ len ], [ 1 ], 0, 'row-major' );
60+
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+
xbuf[ i%len ] = i;
70+
v = mskmax( [ x, mask ] );
71+
if ( isnan( v ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
}
75+
b.toc();
76+
if ( isnan( v ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var len;
94+
var min;
95+
var max;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 6; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
len = pow( 10, i );
104+
f = createBenchmark( len );
105+
bench( pkg+':len='+len, f );
106+
}
107+
}
108+
109+
main();
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
{{alias}}( arrays )
3+
Calculates the maximum value of a one-dimensional ndarray according to a
4+
mask.
5+
6+
If provided an empty ndarray or a mask with all elements set to `1`, the
7+
function returns `NaN`.
8+
9+
Parameters
10+
----------
11+
arrays: ArrayLikeObject<ndarray>
12+
Array-like object containing an input ndarray and a mask ndarray.
13+
14+
Returns
15+
-------
16+
out: number
17+
Maximum value.
18+
19+
Examples
20+
--------
21+
> var xbuf = [ 1.0, -2.0, 4.0, 2.0 ];
22+
> var dt = 'generic';
23+
> var sh = [ xbuf.length ];
24+
> var sx = [ 1 ];
25+
> var ox = 0;
26+
> var ord = 'row-major';
27+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
28+
> var mbuf = [ 0, 0, 1, 0 ];
29+
> var mask = new {{alias:@stdlib/ndarray/ctor}}( dt, mbuf, sh, sx, ox, ord );
30+
> {{alias}}( [ x, mask ] )
31+
2.0
32+
33+
See Also
34+
--------
35+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 { ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Calculates the maximum value of a one-dimensional ndarray according to a mask.
27+
*
28+
* @param arrays - array-like object containing an input ndarray and a mask ndarray
29+
* @returns maximum value
30+
*
31+
* @example
32+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
33+
*
34+
* var xbuf = [ 1.0, -2.0, 4.0, 2.0 ];
35+
* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
36+
*
37+
* var mbuf = [ 0, 0, 1, 0 ];
38+
* var mask = new ndarray( 'generic', mbuf, [ 4 ], [ 1 ], 0, 'row-major' );
39+
*
40+
* var v = mskmax( [ x, mask ] );
41+
* // returns 2.0
42+
*/
43+
declare function mskmax<T extends ndarray = ndarray>( arrays: [ T, T ] ): number;
44+
45+
46+
// EXPORTS //
47+
48+
export = mskmax;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import mskmax = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns a number...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'generic'
31+
});
32+
const mask = zeros( [ 10 ], {
33+
'dtype': 'generic'
34+
});
35+
36+
mskmax( [ x, mask ] ); // $ExpectType number
37+
}
38+
39+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
40+
{
41+
mskmax( '10' ); // $ExpectError
42+
mskmax( 10 ); // $ExpectError
43+
mskmax( true ); // $ExpectError
44+
mskmax( false ); // $ExpectError
45+
mskmax( null ); // $ExpectError
46+
mskmax( undefined ); // $ExpectError
47+
mskmax( [] ); // $ExpectError
48+
mskmax( {} ); // $ExpectError
49+
mskmax( ( x: number ): number => x ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided an unsupported number of arguments...
53+
{
54+
const x = zeros( [ 10 ], {
55+
'dtype': 'generic'
56+
});
57+
const mask = zeros( [ 10 ], {
58+
'dtype': 'generic'
59+
});
60+
61+
mskmax(); // $ExpectError
62+
mskmax( [ x, mask ], {} ); // $ExpectError
63+
}

0 commit comments

Comments
 (0)