Skip to content

Commit c6ec41b

Browse files
committed
feat: add stats/mskmin
--- 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 086d2d6 commit c6ec41b

13 files changed

Lines changed: 1473 additions & 0 deletions

File tree

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
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+
# mskmin
22+
23+
> Compute the minimum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions according to a mask.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var mskmin = require( '@stdlib/stats/mskmin' );
31+
```
32+
33+
#### mskmin( x, mask\[, options] )
34+
35+
Computes the minimum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions according to a mask.
36+
37+
```javascript
38+
var array = require( '@stdlib/ndarray/array' );
39+
40+
var x = array( [ -1.0, 2.0, -3.0 ] );
41+
var mask = array( [ 0.0, 0.0, 0.0 ] );
42+
43+
var y = mskmin( x, mask );
44+
// returns <ndarray>[ -3.0 ]
45+
```
46+
47+
The function has the following parameters:
48+
49+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
50+
- **mask**: mask [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
51+
- **options**: function options (_optional_).
52+
53+
The function accepts the following options:
54+
55+
- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
56+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
57+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
58+
59+
By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option.
60+
61+
```javascript
62+
var array = require( '@stdlib/ndarray/array' );
63+
64+
var x = array( [ -1.0, 2.0, -3.0, 4.0 ], {
65+
'shape': [ 2, 2 ],
66+
'order': 'row-major'
67+
});
68+
// returns <ndarray>[ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ]
69+
70+
var mask = array( [ 0.0, 0.0, 0.0, 0.0 ], {
71+
'shape': [ 2, 2 ],
72+
'order': 'row-major'
73+
});
74+
// returns <ndarray>[ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]
75+
76+
var y = mskmin( x, mask, {
77+
'dims': [ 0 ]
78+
});
79+
// returns <ndarray>[ -3.0, 2.0 ]
80+
81+
y = mskmin( x, mask, {
82+
'dims': [ 1 ]
83+
});
84+
// returns <ndarray>[ -1.0, -3.0 ]
85+
86+
y = mskmin( x, mask, {
87+
'dims': [ 0, 1 ]
88+
});
89+
// returns <ndarray>[ -3.0 ]
90+
```
91+
92+
By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`.
93+
94+
```javascript
95+
var array = require( '@stdlib/ndarray/array' );
96+
97+
var x = array( [ -1.0, 2.0, -3.0, 4.0 ], {
98+
'shape': [ 2, 2 ],
99+
'order': 'row-major'
100+
});
101+
// returns <ndarray>[ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ]
102+
103+
var mask = array( [ 0.0, 0.0, 0.0, 0.0 ], {
104+
'shape': [ 2, 2 ],
105+
'order': 'row-major'
106+
});
107+
// returns <ndarray>[ [ 0.0, 0.0 ], [ 0.0, 0.0 ] ]
108+
109+
var y = mskmin( x, mask, {
110+
'dims': [ 0 ],
111+
'keepdims': true
112+
});
113+
// returns <ndarray>[ [ -3.0, 2.0 ] ]
114+
115+
y = mskmin( x, mask, {
116+
'dims': [ 1 ],
117+
'keepdims': true
118+
});
119+
// returns <ndarray>[ [ -1.0 ], [ -3.0 ] ]
120+
121+
y = mskmin( x, mask, {
122+
'dims': [ 0, 1 ],
123+
'keepdims': true
124+
});
125+
// returns <ndarray>[ [ -3.0 ] ]
126+
```
127+
128+
By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option.
129+
130+
```javascript
131+
var getDType = require( '@stdlib/ndarray/dtype' );
132+
var array = require( '@stdlib/ndarray/array' );
133+
134+
var x = array( [ -1.0, 2.0, -3.0 ], {
135+
'dtype': 'generic'
136+
});
137+
var mask = array( [ 0.0, 0.0, 0.0 ], {
138+
'dtype': 'generic'
139+
});
140+
141+
var y = mskmin( x, mask, {
142+
'dtype': 'float64'
143+
});
144+
// returns <ndarray>
145+
146+
var dt = String( getDType( y ) );
147+
// returns 'float64'
148+
```
149+
150+
#### mskmin.assign( x, mask, out\[, options] )
151+
152+
Computes the minimum value along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
153+
154+
```javascript
155+
var array = require( '@stdlib/ndarray/array' );
156+
var zeros = require( '@stdlib/ndarray/zeros' );
157+
158+
var x = array( [ -1.0, 2.0, -3.0 ] );
159+
var mask = array( [ 0.0, 0.0, 0.0 ] );
160+
var y = zeros( [] );
161+
162+
var out = mskmin.assign( x, mask, y );
163+
// returns <ndarray>[ -3.0 ]
164+
165+
var bool = ( out === y );
166+
// returns true
167+
```
168+
169+
The method has the following parameters:
170+
171+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
172+
- **mask**: mask [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
173+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
174+
- **options**: function options (_optional_).
175+
176+
The method accepts the following options:
177+
178+
- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
179+
180+
</section>
181+
182+
<!-- /.usage -->
183+
184+
<section class="notes">
185+
186+
## Notes
187+
188+
- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor].
189+
- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes].
190+
191+
</section>
192+
193+
<!-- /.notes -->
194+
195+
<section class="examples">
196+
197+
## Examples
198+
199+
<!-- eslint no-undef: "error" -->
200+
201+
```javascript
202+
var uniform = require( '@stdlib/random/uniform' );
203+
var zeros = require( '@stdlib/ndarray/zeros' );
204+
var getDType = require( '@stdlib/ndarray/dtype' );
205+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
206+
var mskmin = require( '@stdlib/stats/mskmin' );
207+
208+
// Generate an array of random numbers:
209+
var x = uniform( [ 5, 5 ], 0.0, 20.0 );
210+
var mask = zeros( [ 5, 5 ], {
211+
'dtype': 'float64'
212+
});
213+
console.log( ndarray2array( x ) );
214+
215+
// Perform a reduction:
216+
var y = mskmin( x, mask, {
217+
'dims': [ 0 ]
218+
});
219+
220+
// Resolve the output array data type:
221+
var dt = getDType( y );
222+
console.log( dt );
223+
224+
// Print the results:
225+
console.log( ndarray2array( y ) );
226+
```
227+
228+
</section>
229+
230+
<!-- /.examples -->
231+
232+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
233+
234+
<section class="related">
235+
236+
</section>
237+
238+
<!-- /.related -->
239+
240+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
241+
242+
<section class="links">
243+
244+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
245+
246+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
247+
248+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
249+
250+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
251+
252+
</section>
253+
254+
<!-- /.links -->
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var uniform = require( '@stdlib/random/array/uniform' );
27+
var format = require( '@stdlib/string/format' );
28+
var zeros = require( '@stdlib/array/zeros' );
29+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
30+
var pkg = require( './../package.json' ).name;
31+
var mskmin = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var options = {
37+
'dtype': 'float64'
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 out;
53+
var x;
54+
55+
x = uniform( len, -50.0, 50.0, options );
56+
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
57+
58+
mask = new ndarray( options.dtype, zeros( len, options.dtype ), [ len ], [ 1 ], 0, 'row-major' );
59+
out = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' );
60+
61+
return benchmark;
62+
63+
/**
64+
* Benchmark function.
65+
*
66+
* @private
67+
* @param {Benchmark} b - benchmark instance
68+
*/
69+
function benchmark( b ) {
70+
var o;
71+
var i;
72+
73+
b.tic();
74+
for ( i = 0; i < b.iterations; i++ ) {
75+
o = mskmin.assign( x, mask, out );
76+
if ( typeof o !== 'object' ) {
77+
b.fail( 'should return an ndarray' );
78+
}
79+
}
80+
b.toc();
81+
if ( isnan( o.get() ) ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
b.pass( 'benchmark finished' );
85+
b.end();
86+
}
87+
}
88+
89+
90+
// MAIN //
91+
92+
/**
93+
* Main execution sequence.
94+
*
95+
* @private
96+
*/
97+
function main() {
98+
var len;
99+
var min;
100+
var max;
101+
var f;
102+
var i;
103+
104+
min = 1; // 10^min
105+
max = 6; // 10^max
106+
107+
for ( i = min; i <= max; i++ ) {
108+
len = pow( 10, i );
109+
f = createBenchmark( len );
110+
bench( format( '%s:assign:dtype=%s,len=%d', pkg, options.dtype, len ), f );
111+
}
112+
}
113+
114+
main();

0 commit comments

Comments
 (0)