Skip to content

Commit 47a304a

Browse files
committed
feat: add stats/strided/mskmin
Ref: #4797 --- 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 f77221f commit 47a304a

File tree

16 files changed

+2201
-0
lines changed

16 files changed

+2201
-0
lines changed
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 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+
> Calculate the minimum value of a strided array 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 mskmin = require( '@stdlib/stats/strided/mskmin' );
37+
```
38+
39+
#### mskmin( N, x, strideX, mask, strideMask )
40+
41+
Computes the minimum value of a strided array `x` according to a `mask`.
42+
43+
```javascript
44+
var x = [ 1.0, -2.0, -4.0, 2.0 ];
45+
var mask = [ 0, 0, 1, 0 ];
46+
47+
var v = mskmin( x.length, x, 1, mask, 1 );
48+
// returns -2.0
49+
```
50+
51+
The function has the following parameters:
52+
53+
- **N**: number of indexed elements.
54+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
55+
- **strideX**: stride length for `x`.
56+
- **mask**: mask [`Array`][mdn-array] or [`typed array`][mdn-typed-array]. If a `mask` array element is `0`, the corresponding element in `x` is considered valid and **included** in computation. If a `mask` array element is `1`, the corresponding element in `x` is considered invalid/missing and **excluded** from computation.
57+
- **strideMask**: stride length for `mask`.
58+
59+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the minimum value of every other element in `x`,
60+
61+
```javascript
62+
var x = [ 1.0, 2.0, -7.0, -2.0, 4.0, 3.0, -5.0, -6.0 ];
63+
var mask = [ 0, 0, 0, 0, 0, 0, 1, 1 ];
64+
65+
var v = mskmin( 4, x, 2, mask, 2 );
66+
// returns -7.0
67+
```
68+
69+
Note that indexing is relative to the first index. To introduce offsets, use [`typed array`][mdn-typed-array] views.
70+
71+
<!-- eslint-disable stdlib/capitalized-comments -->
72+
73+
```javascript
74+
var Float64Array = require( '@stdlib/array/float64' );
75+
var Uint8Array = require( '@stdlib/array/uint8' );
76+
77+
var x0 = new Float64Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ] );
78+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
79+
80+
var mask0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
81+
var mask1 = new Uint8Array( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
82+
83+
var v = mskmin( 4, x1, 2, mask1, 2 );
84+
// returns -2.0
85+
```
86+
87+
#### mskmin.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask )
88+
89+
Computes the minimum value of a strided array according to a `mask` and using alternative indexing semantics.
90+
91+
```javascript
92+
var x = [ 1.0, -2.0, -4.0, 2.0 ];
93+
var mask = [ 0, 0, 1, 0 ];
94+
95+
var v = mskmin.ndarray( x.length, x, 1, 0, mask, 1, 0 );
96+
// returns -2.0
97+
```
98+
99+
The function has the following additional parameters:
100+
101+
- **offsetX**: starting index for `x`.
102+
- **offsetMask**: starting index for `mask`.
103+
104+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example, to calculate the minimum value for every other value in `x` starting from the second value
105+
106+
```javascript
107+
var x = [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, -5.0, -6.0 ];
108+
var mask = [ 0, 0, 0, 0, 0, 0, 1, 1 ];
109+
110+
var v = mskmin.ndarray( 4, x, 2, 1, mask, 2, 1 );
111+
// returns -2.0
112+
```
113+
114+
</section>
115+
116+
<!-- /.usage -->
117+
118+
<section class="notes">
119+
120+
## Notes
121+
122+
- If `N <= 0`, both functions return `NaN`.
123+
- Depending on the environment, the typed versions ([`dmskmin`][@stdlib/stats/strided/dmskmin], [`smskmin`][@stdlib/stats/strided/smskmin], etc.) are likely to be significantly more performant.
124+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
125+
126+
</section>
127+
128+
<!-- /.notes -->
129+
130+
<section class="examples">
131+
132+
## Examples
133+
134+
<!-- eslint no-undef: "error" -->
135+
136+
```javascript
137+
var uniform = require( '@stdlib/random/array/uniform' );
138+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
139+
var mskmin = require( '@stdlib/stats/strided/mskmin' );
140+
141+
var x = uniform( 10, -50.0, 50.0, {
142+
'dtype': 'float64'
143+
});
144+
console.log( x );
145+
146+
var mask = bernoulli( x.length, 0.2, {
147+
'dtype': 'uint8'
148+
});
149+
console.log( mask );
150+
151+
var v = mskmin( x.length, x, 1, mask, 1 );
152+
console.log( v );
153+
```
154+
155+
</section>
156+
157+
<!-- /.examples -->
158+
159+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
160+
161+
<section class="related">
162+
163+
* * *
164+
165+
## See Also
166+
167+
- <span class="package-name">[`@stdlib/stats/strided/dmskmin`][@stdlib/stats/strided/dmskmin]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a double-precision floating-point strided array according to a mask.</span>
168+
- <span class="package-name">[`@stdlib/stats/strided/min`][@stdlib/stats/strided/min]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a strided array.</span>
169+
- <span class="package-name">[`@stdlib/stats/strided/mskmax`][@stdlib/stats/strided/mskmax]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a strided array according to a mask.</span>
170+
- <span class="package-name">[`@stdlib/stats/base/nanmin`][@stdlib/stats/base/nanmin]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a strided array, ignoring NaN values.</span>
171+
- <span class="package-name">[`@stdlib/stats/strided/smskmin`][@stdlib/stats/strided/smskmin]</span><span class="delimiter">: </span><span class="description">calculate the minimum value of a single-precision floating-point strided array according to a mask.</span>
172+
173+
</section>
174+
175+
<!-- /.related -->
176+
177+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
178+
179+
<section class="links">
180+
181+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
182+
183+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
184+
185+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
186+
187+
<!-- <related-links> -->
188+
189+
[@stdlib/stats/strided/dmskmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmskmin
190+
191+
[@stdlib/stats/strided/min]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/min
192+
193+
[@stdlib/stats/strided/mskmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/mskmax
194+
195+
[@stdlib/stats/base/nanmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmin
196+
197+
[@stdlib/stats/strided/smskmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smskmin
198+
199+
<!-- </related-links> -->
200+
201+
</section>
202+
203+
<!-- /.links -->
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 pkg = require( './../package.json' ).name;
29+
var mskmin = require( './../lib/mskmin.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var mask = bernoulli( len, 0.2, options );
50+
var x = uniform( len, -10.0, 10.0, options );
51+
return benchmark;
52+
53+
function benchmark( b ) {
54+
var v;
55+
var i;
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
v = mskmin( x.length, x, 1, mask, 1 );
60+
if ( isnan( v ) ) {
61+
b.fail( 'should not return NaN' );
62+
}
63+
}
64+
b.toc();
65+
if ( isnan( v ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
}
71+
}
72+
73+
74+
// MAIN //
75+
76+
/**
77+
* Main execution sequence.
78+
*
79+
* @private
80+
*/
81+
function main() {
82+
var len;
83+
var min;
84+
var max;
85+
var f;
86+
var i;
87+
88+
min = 1; // 10^min
89+
max = 6; // 10^max
90+
91+
for ( i = min; i <= max; i++ ) {
92+
len = pow( 10, i );
93+
f = createBenchmark( len );
94+
bench( pkg+':len='+len, f );
95+
}
96+
}
97+
98+
main();

0 commit comments

Comments
 (0)