Skip to content

Commit bf87433

Browse files
committed
feat: add stats/strided/dmskrange
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: passed - task: lint_c_examples status: passed - task: lint_c_benchmarks status: passed - 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 9b62a75 commit bf87433

33 files changed

+3997
-0
lines changed
Lines changed: 356 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,356 @@
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+
# dmskrange
22+
23+
> Calculate the [range][range] of a double-precision floating-point strided array 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 dmskrange = require( '@stdlib/stats/strided/dmskrange' );
39+
```
40+
41+
#### dmskrange( N, x, strideX, mask, strideMask )
42+
43+
Computes the [range][range] of a double-precision floating-point strided array according to a mask.
44+
45+
```javascript
46+
var Float64Array = require( '@stdlib/array/float64' );
47+
var Uint8Array = require( '@stdlib/array/uint8' );
48+
49+
var x = new Float64Array( [ 1.0, -2.0, 4.0, 2.0 ] );
50+
var mask = new Uint8Array( [ 0, 0, 1, 0 ] );
51+
52+
var v = dmskrange( x.length, x, 1, mask, 1 );
53+
// returns 4.0
54+
```
55+
56+
The function has the following parameters:
57+
58+
- **N**: number of indexed elements.
59+
- **x**: input [`Float64Array`][@stdlib/array/float64].
60+
- **strideX**: index increment for `x`.
61+
- **mask**: mask [`Uint8Array`][@stdlib/array/uint8]. 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.
62+
- **strideMask**: index increment for `mask`.
63+
64+
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`,
65+
66+
```javascript
67+
var Float64Array = require( '@stdlib/array/float64' );
68+
var Uint8Array = require( '@stdlib/array/uint8' );
69+
70+
var x = new Float64Array( [ 1.0, 2.0, -7.0, -2.0, 4.0, 3.0, 5.0, 6.0 ] );
71+
var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
72+
73+
var v = dmskrange( 4, x, 2, mask, 2 );
74+
// returns 11.0
75+
```
76+
77+
Note that indexing is relative to the first index. To introduce offsets, use [`typed array`][mdn-typed-array] views.
78+
79+
<!-- eslint-disable stdlib/capitalized-comments -->
80+
81+
```javascript
82+
var Float64Array = require( '@stdlib/array/float64' );
83+
var Uint8Array = require( '@stdlib/array/uint8' );
84+
85+
var x0 = new Float64Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ] );
86+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
87+
88+
var mask0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
89+
var mask1 = new Uint8Array( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
90+
var v = dmskrange( 4, x1, 2, mask1, 2 );
91+
// returns 6.0
92+
```
93+
94+
#### dmskrange.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask )
95+
96+
Computes the [range][range] of a double-precision floating-point strided array according to a `mask` and using alternative indexing semantics.
97+
98+
```javascript
99+
var Float64Array = require( '@stdlib/array/float64' );
100+
var Uint8Array = require( '@stdlib/array/uint8' );
101+
102+
var x = new Float64Array( [ 1.0, -2.0, 4.0, 2.0 ] );
103+
var mask = new Uint8Array( [ 0, 0, 1, 0 ] );
104+
105+
var v = dmskrange.ndarray( x.length, x, 1, 0, mask, 1, 0 );
106+
// returns 4.0
107+
```
108+
109+
The function has the following additional parameters:
110+
111+
- **offsetX**: starting index for `x`.
112+
- **offsetMask**: starting index for `mask`.
113+
114+
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 [range][range] for every other element in `x` starting from the second element
115+
116+
```javascript
117+
var Float64Array = require( '@stdlib/array/float64' );
118+
var Uint8Array = require( '@stdlib/array/uint8' );
119+
120+
var x = new Float64Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ] );
121+
var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
122+
123+
var v = dmskrange.ndarray( 4, x, 2, 1, mask, 2, 1 );
124+
// returns 6.0
125+
```
126+
127+
</section>
128+
129+
<!-- /.usage -->
130+
131+
<section class="notes">
132+
133+
## Notes
134+
135+
- If `N <= 0`, both functions return `NaN`.
136+
137+
</section>
138+
139+
<!-- /.notes -->
140+
141+
<section class="examples">
142+
143+
## Examples
144+
145+
<!-- eslint no-undef: "error" -->
146+
147+
```javascript
148+
var uniform = require( '@stdlib/random/array/uniform' );
149+
var bernoulli = require( '@stdlib/random/array/bernoulli' );
150+
var dmskrange = require( '@stdlib/stats/strided/dmskrange' );
151+
152+
var uniformOptions = {
153+
'dtype': 'float64'
154+
};
155+
var bernoulliOptions = {
156+
'dtype': 'uint8'
157+
};
158+
159+
var x = uniform( 10, -50.0, 50.0, uniformOptions );
160+
var mask = bernoulli( x.length, 0.2, bernoulliOptions );
161+
console.log( x );
162+
console.log( mask );
163+
164+
var v = dmskrange( x.length, x, 1, mask, 1 );
165+
console.log( v );
166+
```
167+
168+
</section>
169+
170+
<!-- /.examples -->
171+
172+
<!-- C interface documentation. -->
173+
174+
* * *
175+
176+
<section class="c">
177+
178+
## C APIs
179+
180+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
181+
182+
<section class="intro">
183+
184+
</section>
185+
186+
<!-- /.intro -->
187+
188+
<!-- C usage documentation. -->
189+
190+
<section class="usage">
191+
192+
### Usage
193+
194+
```c
195+
#include "stdlib/stats/strided/dmskrange.h"
196+
```
197+
198+
#### stdlib_strided_dmskrange( N, \*X, strideX, \*Mask, strideMask )
199+
200+
Computes the [range][range] of a double-precision floating-point strided array according to a mask.
201+
202+
```c
203+
#include <stdint.h>
204+
205+
const double x[] = { 1.0, -2.0, 2.0 };
206+
const uint8_t mask[] = { 0, 1, 0 };
207+
208+
double v = stdlib_strided_dmskrange( 3, x, 1, mask, 1 );
209+
// returns 1.0
210+
```
211+
212+
The function accepts the following arguments:
213+
214+
- **N**: `[in] CBLAS_INT` number of indexed elements.
215+
- **X**: `[in] double*` input array.
216+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
217+
- **Mask**: `[in] uint8_t*` mask 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.
218+
- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`.
219+
220+
```c
221+
double stdlib_strided_dmskrange( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const uint8_t *Mask, const CBLAS_INT strideMask );
222+
```
223+
224+
<!-- lint disable maximum-heading-length -->
225+
226+
#### stdlib_strided_dmskrange_ndarray( N, \*X, strideX, offsetX, \*Mask, strideMask, offsetMask )
227+
228+
Computes the [range][range] of a double-precision floating-point strided array according to a mask and using alternative indexing semantics.
229+
230+
```c
231+
#include <stdint.h>
232+
233+
const double x[] = { 1.0, -2.0, 2.0 };
234+
const uint8_t mask[] = { 0, 1, 0 };
235+
236+
double v = stdlib_strided_dmskrange( 3, x, 1, 0, mask, 1, 0 );
237+
// returns 1.0
238+
```
239+
240+
The function accepts the following arguments:
241+
242+
- **N**: `[in] CBLAS_INT` number of indexed elements.
243+
- **X**: `[in] double*` input array.
244+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
245+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
246+
- **Mask**: `[in] uint8_t*` mask 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.
247+
- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`.
248+
- **offsetMask**: `[in] CBLAS_INT` starting index for `Mask`.
249+
250+
```c
251+
double stdlib_strided_dmskrange_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const uint8_t *Mask, const CBLAS_INT strideMask, const CBLAS_INT offsetMask );
252+
```
253+
254+
</section>
255+
256+
<!-- /.usage -->
257+
258+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
259+
260+
<section class="notes">
261+
262+
</section>
263+
264+
<!-- /.notes -->
265+
266+
<!-- C API usage examples. -->
267+
268+
<section class="examples">
269+
270+
### Examples
271+
272+
```c
273+
#include "stdlib/stats/strided/dmskrange.h"
274+
#include <stdint.h>
275+
#include <stdio.h>
276+
277+
int main( void ) {
278+
// Create a strided array:
279+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };
280+
281+
// Create a mask array:
282+
const uint8_t mask[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 };
283+
284+
// Specify the number of elements:
285+
const int N = 5;
286+
287+
// Specify the stride lengths:
288+
const int strideX = 2;
289+
const int strideMask = 2;
290+
291+
// Compute the range:
292+
double v = stdlib_strided_dmskrange( N, x, strideX, mask, strideMask );
293+
294+
// Print the result:
295+
printf( "range: %lf\n", v );
296+
}
297+
```
298+
299+
</section>
300+
301+
<!-- /.examples -->
302+
303+
</section>
304+
305+
<!-- /.c -->
306+
307+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
308+
309+
<section class="related">
310+
311+
* * *
312+
313+
## See Also
314+
315+
- <span class="package-name">[`@stdlib/stats/strided/dmskmax`][@stdlib/stats/strided/dmskmax]</span><span class="delimiter">: </span><span class="description">calculate the maximum value of a double-precision floating-point strided array according to a mask.</span>
316+
- <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>
317+
- <span class="package-name">[`@stdlib/stats/strided/dnanrange`][@stdlib/stats/strided/dnanrange]</span><span class="delimiter">: </span><span class="description">calculate the range of a double-precision floating-point strided array, ignoring NaN values.</span>
318+
- <span class="package-name">[`@stdlib/stats/strided/drange`][@stdlib/stats/strided/drange]</span><span class="delimiter">: </span><span class="description">calculate the range of a double-precision floating-point strided array.</span>
319+
- <span class="package-name">[`@stdlib/stats/base/mskrange`][@stdlib/stats/base/mskrange]</span><span class="delimiter">: </span><span class="description">calculate the range of a strided array according to a mask.</span>
320+
- <span class="package-name">[`@stdlib/stats/strided/smskrange`][@stdlib/stats/strided/smskrange]</span><span class="delimiter">: </span><span class="description">calculate the range of a single-precision floating-point strided array according to a mask.</span>
321+
322+
</section>
323+
324+
<!-- /.related -->
325+
326+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
327+
328+
<section class="links">
329+
330+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
331+
332+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
333+
334+
[@stdlib/array/uint8]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint8
335+
336+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
337+
338+
<!-- <related-links> -->
339+
340+
[@stdlib/stats/strided/dmskmax]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmskmax
341+
342+
[@stdlib/stats/strided/dmskmin]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmskmin
343+
344+
[@stdlib/stats/strided/dnanrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanrange
345+
346+
[@stdlib/stats/strided/drange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/drange
347+
348+
[@stdlib/stats/base/mskrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/mskrange
349+
350+
[@stdlib/stats/strided/smskrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smskrange
351+
352+
<!-- </related-links> -->
353+
354+
</section>
355+
356+
<!-- /.links -->

0 commit comments

Comments
 (0)