Skip to content

Commit a75e344

Browse files
committed
feat: add stats/mskrange
--- 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 a75e344

13 files changed

Lines changed: 1718 additions & 0 deletions

File tree

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
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+
# mskrange
22+
23+
> Compute the [range][range] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions according to a mask.
24+
25+
<section class="intro">
26+
27+
The [**range**][range] is defined as the difference between the maximum and minimum values.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var mskrange = require( '@stdlib/stats/mskrange' );
39+
```
40+
41+
#### mskrange( x, mask\[, options] )
42+
43+
Computes the [range][range] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions according to a mask.
44+
45+
```javascript
46+
var Float64Array = require( '@stdlib/array/float64' );
47+
var Uint8Array = require( '@stdlib/array/uint8' );
48+
var array = require( '@stdlib/ndarray/array' );
49+
50+
var x = array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ), {
51+
'shape': [ 3, 2 ]
52+
});
53+
var mask = array( new Uint8Array( [ 0, 0, 1, 0, 0, 1 ] ), {
54+
'shape': [ 3, 2 ]
55+
});
56+
57+
var y = mskrange( x, mask );
58+
// returns <ndarray>[ 4.0 ]
59+
```
60+
61+
The function has the following parameters:
62+
63+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
64+
- **mask**: mask [ndarray][@stdlib/ndarray/ctor]. Must have a data type that can be interpreted as a mask.
65+
- **options**: function options (_optional_).
66+
67+
The function accepts the following options:
68+
69+
- **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].
70+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
71+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
72+
73+
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.
74+
75+
```javascript
76+
var Float64Array = require( '@stdlib/array/float64' );
77+
var Uint8Array = require( '@stdlib/array/uint8' );
78+
var array = require( '@stdlib/ndarray/array' );
79+
80+
var x = array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ), {
81+
'shape': [ 3, 2 ],
82+
'order': 'row-major'
83+
});
84+
var mask = array( new Uint8Array( [ 0, 0, 1, 1, 0, 0 ] ), {
85+
'shape': [ 3, 2 ],
86+
'order': 'row-major'
87+
});
88+
89+
var y = mskrange( x, mask, {
90+
'dims': [ 0 ]
91+
});
92+
// returns <ndarray>[ 4.0, 4.0 ]
93+
```
94+
95+
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`.
96+
97+
```javascript
98+
var Float64Array = require( '@stdlib/array/float64' );
99+
var Uint8Array = require( '@stdlib/array/uint8' );
100+
var array = require( '@stdlib/ndarray/array' );
101+
102+
var x = array( new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ), {
103+
'shape': [ 3, 2 ],
104+
'order': 'row-major'
105+
});
106+
var mask = array( new Uint8Array( [ 0, 0, 1, 1, 0, 0 ] ), {
107+
'shape': [ 3, 2 ],
108+
'order': 'row-major'
109+
});
110+
111+
var y = mskrange( x, mask, {
112+
'dims': [ 0 ],
113+
'keepdims': true
114+
});
115+
// returns <ndarray>[ [ 4.0, 4.0 ] ]
116+
```
117+
118+
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.
119+
120+
```javascript
121+
var Float64Array = require( '@stdlib/array/float64' );
122+
var Uint8Array = require( '@stdlib/array/uint8' );
123+
var getDType = require( '@stdlib/ndarray/dtype' );
124+
var array = require( '@stdlib/ndarray/array' );
125+
126+
var x = array( new Float64Array( [ 1.0, 2.0, 3.0 ] ), {
127+
'dtype': 'generic'
128+
});
129+
var mask = array( new Uint8Array( [ 0, 0, 0 ] ), {
130+
'dtype': 'uint8'
131+
});
132+
133+
var y = mskrange( x, mask, {
134+
'dtype': 'float64'
135+
});
136+
// returns <ndarray>[ 2.0 ]
137+
138+
var dt = String( getDType( y ) );
139+
// returns 'float64'
140+
```
141+
142+
#### mskrange.assign( x, mask, out\[, options] )
143+
144+
Computes the [range][range] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions according to a mask and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
145+
146+
```javascript
147+
var Float64Array = require( '@stdlib/array/float64' );
148+
var Uint8Array = require( '@stdlib/array/uint8' );
149+
var array = require( '@stdlib/ndarray/array' );
150+
var zeros = require( '@stdlib/ndarray/zeros' );
151+
152+
var x = array( new Float64Array( [ 1.0, 2.0, 3.0 ] ) );
153+
var mask = array( new Uint8Array( [ 0, 0, 0 ] ) );
154+
var y = zeros( [] );
155+
156+
var out = mskrange.assign( x, mask, y );
157+
// returns <ndarray>[ 2.0 ]
158+
159+
var bool = ( out === y );
160+
// returns true
161+
```
162+
163+
The method has the following parameters:
164+
165+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
166+
- **mask**: mask [ndarray][@stdlib/ndarray/ctor]. Must have a data type that can be interpreted as a mask.
167+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
168+
- **options**: function options (_optional_).
169+
170+
The method accepts the following options:
171+
172+
- **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].
173+
174+
</section>
175+
176+
<!-- /.usage -->
177+
178+
<section class="notes">
179+
180+
## Notes
181+
182+
- 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].
183+
- 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 the same [data type][@stdlib/ndarray/dtypes] as the input [ndarray][@stdlib/ndarray/ctor] (or a promoted data type if the input data types differ). For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes].
184+
185+
</section>
186+
187+
<!-- /.notes -->
188+
189+
<section class="examples">
190+
191+
## Examples
192+
193+
<!-- eslint no-undef: "error" -->
194+
195+
```javascript
196+
var uniform = require( '@stdlib/random/uniform' );
197+
var bernoulli = require( '@stdlib/random/bernoulli' );
198+
var getDType = require( '@stdlib/ndarray/dtype' );
199+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
200+
var mskrange = require( '@stdlib/stats/mskrange' );
201+
202+
// Generate an array of random numbers:
203+
var x = uniform( [ 5, 5 ], 0.0, 20.0 );
204+
console.log( 'x:' );
205+
console.log( ndarray2array( x ) );
206+
207+
// Generate a random mask:
208+
var mask = bernoulli( [ 5, 5 ], 0.2, {
209+
'dtype': 'uint8'
210+
});
211+
console.log( 'mask:' );
212+
console.log( ndarray2array( mask ) );
213+
214+
// Perform a reduction:
215+
var y = mskrange( x, mask, {
216+
'dims': [ 0 ]
217+
});
218+
219+
// Resolve the output array data type:
220+
var dt = getDType( y );
221+
console.log( 'Output dtype: %s', dt );
222+
223+
// Print the results:
224+
console.log( 'y:' );
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+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
253+
254+
</section>
255+
256+
<!-- /.links -->
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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 bernoulli = require( '@stdlib/random/array/bernoulli' );
29+
var zeros = require( '@stdlib/array/zeros' );
30+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
31+
var pkg = require( './../package.json' ).name;
32+
var mskrange = require( './../lib' );
33+
34+
35+
// VARIABLES //
36+
37+
var options = {
38+
'dtype': 'float64'
39+
};
40+
41+
42+
// FUNCTIONS //
43+
44+
/**
45+
* Creates a benchmark function.
46+
*
47+
* @private
48+
* @param {PositiveInteger} len - array length
49+
* @returns {Function} benchmark function
50+
*/
51+
function createBenchmark( len ) {
52+
var mask;
53+
var out;
54+
var x;
55+
56+
x = uniform( len, -50.0, 50.0, options );
57+
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
58+
59+
mask = bernoulli( len, 0.2, {
60+
'dtype': 'uint8'
61+
});
62+
mask = new ndarray( 'uint8', mask, [ len ], [ 1 ], 0, 'row-major' );
63+
64+
out = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' );
65+
66+
return benchmark;
67+
68+
/**
69+
* Benchmark function.
70+
*
71+
* @private
72+
* @param {Benchmark} b - benchmark instance
73+
*/
74+
function benchmark( b ) {
75+
var o;
76+
var i;
77+
78+
b.tic();
79+
for ( i = 0; i < b.iterations; i++ ) {
80+
o = mskrange.assign( x, mask, out );
81+
if ( typeof o !== 'object' ) {
82+
b.fail( 'should return an ndarray' );
83+
}
84+
}
85+
b.toc();
86+
if ( isnan( o.get() ) ) {
87+
b.fail( 'should not return NaN' );
88+
}
89+
b.pass( 'benchmark finished' );
90+
b.end();
91+
}
92+
}
93+
94+
95+
// MAIN //
96+
97+
/**
98+
* Main execution sequence.
99+
*
100+
* @private
101+
*/
102+
function main() {
103+
var len;
104+
var min;
105+
var max;
106+
var f;
107+
var i;
108+
109+
min = 1; // 10^min
110+
max = 6; // 10^max
111+
112+
for ( i = min; i <= max; i++ ) {
113+
len = pow( 10, i );
114+
f = createBenchmark( len );
115+
bench( format( '%s:assign:dtype=%s,len=%d', pkg, options.dtype, len ), f );
116+
}
117+
}
118+
119+
main();

0 commit comments

Comments
 (0)