Skip to content

Commit 7ecae0f

Browse files
committed
feat: add stats/strided/snanrange
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 876f392 commit 7ecae0f

33 files changed

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

0 commit comments

Comments
 (0)