Skip to content

Commit 31b0640

Browse files
committed
feat: stats/base/ndarray/snanmskminabs
1 parent f5199d6 commit 31b0640

File tree

10 files changed

+865
-0
lines changed

10 files changed

+865
-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) 2026 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+
# snanmskminabs
22+
23+
> Calculate the minimum absolute value of a one-dimensional single-precision floating-point ndarray 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 snanmskminabs = require( '@stdlib/stats/base/ndarray/snanmskminabs' );
37+
```
38+
39+
#### snanmskminabs( arrays )
40+
41+
Computes the minimum absolute value of a one-dimensional single-precision floating-point ndarray according to a mask, ignoring `NaN` values.
42+
43+
```javascript
44+
var Float32Array = require( '@stdlib/array/float32' );
45+
var Uint8Array = require( '@stdlib/array/uint8' );
46+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
47+
48+
var xbuf = new Float32Array( [ 1.0, -2.0, 4.0, 2.0, NaN ] );
49+
var x = new ndarray( 'float32', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
50+
51+
var maskbuf = new Uint8Array( [ 0, 0, 1, 0, 0 ] );
52+
var mask = new ndarray( 'uint8', maskbuf, [ 5 ], [ 1 ], 0, 'row-major' );
53+
54+
var v = snanmskminabs( [ x, mask ] );
55+
// returns 1.0
56+
```
57+
58+
The function has the following parameters:
59+
60+
- **arrays**: array-like object containing a one-dimensional input ndarray and a one-dimensional mask ndarray.
61+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<section class="notes">
67+
68+
## Notes
69+
70+
- 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.
71+
- If provided an empty ndarray or a mask with all elements set to `1`, the function returns `NaN`.
72+
73+
</section>
74+
75+
<!-- /.notes -->
76+
77+
<section class="examples">
78+
79+
## Examples
80+
81+
<!-- eslint no-undef: "error" -->
82+
83+
```javascript
84+
var uniform = require( '@stdlib/random/array/uniform' );
85+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
86+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
87+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
88+
var snanmskminabs = require( '@stdlib/stats/base/ndarray/snanmskminabs' );
89+
90+
var xbuf = uniform( 10, -50.0, 50.0, {
91+
'dtype': 'float32'
92+
});
93+
var x = new ndarray( 'float32', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
94+
console.log( ndarray2array( x ) );
95+
96+
var maskbuf = bernoulli( xbuf.length, 0.2, {
97+
'dtype': 'uint8'
98+
});
99+
var mask = new ndarray( 'uint8', maskbuf, [ maskbuf.length ], [ 1 ], 0, 'row-major' );
100+
console.log( ndarray2array( mask ) );
101+
102+
var v = snanmskminabs( [ x, mask ] );
103+
console.log( v );
104+
```
105+
106+
</section>
107+
108+
<!-- /.examples -->
109+
110+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
111+
112+
<section class="related">
113+
114+
</section>
115+
116+
<!-- /.related -->
117+
118+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
119+
120+
<section class="links">
121+
122+
</section>
123+
124+
<!-- /.links -->
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 isnanf = require( '@stdlib/math/base/assert/is-nanf' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var format = require( '@stdlib/string/format' );
30+
var pkg = require( './../package.json' ).name;
31+
var snanmskminabs = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var options = {
37+
'dtype': 'float32'
38+
};
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {PositiveInteger} len - array length
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len ) {
51+
var mask;
52+
var mbuf;
53+
var xbuf;
54+
var x;
55+
56+
xbuf = uniform( len, -100.0, 100.0, options );
57+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
58+
59+
mbuf = bernoulli( len, 0.2, {
60+
'dtype': 'uint8'
61+
});
62+
mask = new ndarray( 'uint8', mbuf, [ len ], [ 1 ], 0, 'row-major' );
63+
64+
return benchmark;
65+
66+
/**
67+
* Benchmark function.
68+
*
69+
* @private
70+
* @param {Benchmark} b - benchmark instance
71+
*/
72+
function benchmark( b ) {
73+
var v;
74+
var i;
75+
76+
b.tic();
77+
for ( i = 0; i < b.iterations; i++ ) {
78+
xbuf[ i%len ] = i;
79+
v = snanmskminabs( [ x, mask ] );
80+
if ( isnanf( v ) ) {
81+
b.fail( 'should not return NaN' );
82+
}
83+
}
84+
b.toc();
85+
if ( isnanf( v ) ) {
86+
b.fail( 'should not return NaN' );
87+
}
88+
b.pass( 'benchmark finished' );
89+
b.end();
90+
}
91+
}
92+
93+
94+
// MAIN //
95+
96+
/**
97+
* Main execution sequence.
98+
*
99+
* @private
100+
*/
101+
function main() {
102+
var len;
103+
var min;
104+
var max;
105+
var f;
106+
var i;
107+
108+
min = 1; // 10^min
109+
max = 6; // 10^max
110+
111+
for ( i = min; i <= max; i++ ) {
112+
len = pow( 10, i );
113+
f = createBenchmark( len );
114+
bench( format( '%s:len=%d', pkg, len ), f );
115+
}
116+
}
117+
118+
main();
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
{{alias}}( arrays )
3+
Calculates the minimum absolute value of a one-dimensional single-precision
4+
floating-point ndarray according to a mask, ignoring `NaN` values.
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+
Minimum absolute value.
18+
19+
Examples
20+
--------
21+
> var Float32Array = require( '@stdlib/array/float32' );
22+
> var Uint8Array = require( '@stdlib/array/uint8' );
23+
> var xbuf = new Float32Array( [ 1.0, -2.0, 4.0, 2.0, NaN ] );
24+
> var dt = 'float32';
25+
> var sh = [ xbuf.length ];
26+
> var sx = [ 1 ];
27+
> var ox = 0;
28+
> var ord = 'row-major';
29+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
30+
> var mbuf = new Uint8Array( [ 0, 0, 1, 0, 0 ] );
31+
> var mdt = 'uint8';
32+
> var mask = new {{alias:@stdlib/ndarray/ctor}}( mdt, mbuf, sh, sx, ox, ord );
33+
> {{alias}}( [ x, mask ] )
34+
1.0
35+
36+
See Also
37+
--------
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 { float32ndarray, uint8ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Computes the minimum absolute value of a one-dimensional single-precision floating-point ndarray according to a mask, ignoring `NaN` values.
27+
*
28+
* @param arrays - array-like object containing an input ndarray and a mask ndarray
29+
* @returns minimum absolute value
30+
*
31+
* @example
32+
* var Float32Array = require( '@stdlib/array/float32' );
33+
* var Uint8Array = require( '@stdlib/array/uint8' );
34+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
35+
*
36+
* var xbuf = new Float32Array( [ 1.0, -2.0, 4.0, 2.0, NaN ] );
37+
* var x = new ndarray( 'float32', xbuf, [ 5 ], [ 1 ], 0, 'row-major' );
38+
*
39+
* var maskbuf = new Uint8Array( [ 0, 0, 1, 0, 0 ] );
40+
* var mask = new ndarray( 'uint8', maskbuf, [ 5 ], [ 1 ], 0, 'row-major' );
41+
*
42+
* var v = snanmskminabs( [ x, mask ] );
43+
* // returns 1.0
44+
*/
45+
declare function snanmskminabs( arrays: [ float32ndarray, uint8ndarray ] ): number;
46+
47+
48+
// EXPORTS //
49+
50+
export = snanmskminabs;
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) 2026 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 snanmskminabs = require( './index' );
23+
24+
25+
// TESTS //
26+
27+
// The function returns a number...
28+
{
29+
const x = zeros( [ 10 ], {
30+
'dtype': 'float32'
31+
});
32+
const mask = zeros( [ 10 ], {
33+
'dtype': 'uint8'
34+
});
35+
36+
snanmskminabs( [ 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+
snanmskminabs( '10' ); // $ExpectError
42+
snanmskminabs( 10 ); // $ExpectError
43+
snanmskminabs( true ); // $ExpectError
44+
snanmskminabs( false ); // $ExpectError
45+
snanmskminabs( null ); // $ExpectError
46+
snanmskminabs( undefined ); // $ExpectError
47+
snanmskminabs( [] ); // $ExpectError
48+
snanmskminabs( {} ); // $ExpectError
49+
snanmskminabs( ( 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': 'float32'
56+
});
57+
const mask = zeros( [ 10 ], {
58+
'dtype': 'uint8'
59+
});
60+
61+
snanmskminabs(); // $ExpectError
62+
snanmskminabs( [ x, mask ], {} ); // $ExpectError
63+
}

0 commit comments

Comments
 (0)