10000 feat: add `stats/strided/nanmskrange` · stdlib-js/stdlib@d62f174 · GitHub
[go: up one dir, main page]

Skip to content

Commit d62f174

Browse files
committed
feat: add stats/strided/nanmskrange
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 f1e8702 commit d62f174

File tree

16 files changed

+2294
-0
lines changed

16 files changed

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

0 commit comments

Comments
 (0)
0