Skip to content

Commit 3ff776d

Browse files
committed
feat: add stats/strided/snanmeanors
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 cae3e6f commit 3ff776d

34 files changed

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

0 commit comments

Comments
 (0)