Skip to content

Commit f03d6c7

Browse files
feat: adding dmskmin for ndarray
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent e80fff8 commit f03d6c7

File tree

10 files changed

+857
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)