Skip to content

Commit 4e780e0

Browse files
committed
feat: add stats/strided/dnanmeanors
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 f5459a7 commit 4e780e0

34 files changed

+3600
-0
lines changed
Lines changed: 337 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,337 @@
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+
# dnanmeanors
22+
23+
> Calculate the [arithmetic mean][arithmetic-mean] of a double-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@086f7b0f8efb88e4bc7ec5a9417e4ea89cd3f27c/lib/node_modules/@stdlib/stats/strided/dnanmeanors/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 dnanmeanors = require( '@stdlib/stats/strided/dnanmeanors' );
52+
```
53+
54+
#### dnanmeanors( N, x, strideX )
55+
56+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array `x`, ignoring `NaN` values and using ordinary recursive summation.
57+
58+
```javascript
59+
var Float64Array = require( '@stdlib/array/float64' );
60+
61+
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
62+
63+
var v = dnanmeanors( 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 [`Float64Array`][@stdlib/array/float64].
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 Float64Array = require( '@stdlib/array/float64' );
79+
80+
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN, NaN ] );
81+
82+
var v = dnanmeanors( 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 Float64Array = require( '@stdlib/array/float64' );
92+
93+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
94+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
95+
96+
var v = dnanmeanors( 5, x1, 2 );
97+
// returns 1.25
98+
```
99+
100+
#### dnanmeanors.ndarray( N, x, strideX, offsetX )
101+
102+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
103+
104+
```javascript
105+
var Float64Array = require( '@stdlib/array/float64' );
106+
107+
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
108+
109+
var v = dnanmeanors.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 Float64Array = require( '@stdlib/array/float64' );
123+
124+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
125+
126+
var v = dnanmeanors.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 randu = require( '@stdlib/random/base/randu' );
154+
var round = require( '@stdlib/math/base/special/round' );
155+
var Float64Array = require( '@stdlib/array/float64' );
156+
var dnanmeanors = require( '@stdlib/stats/strided/dnanmeanors' );
157+
158+
var x;
159+
var i;
160+
161+
x = new Float64Array( 10 );
162+
for ( i = 0; i < x.length; i++ ) {
163+
if ( randu() < 0.2 ) {
164+
x[ i ] = NaN;
165+
} else {
166+
x[ i ] = round( (randu()*100.0) - 50.0 );
167+
}
168+
}
169+
console.log( x );
170+
171+
var v = dnanmeanors( x.length, x, 1 );
172+
console.log( v );
173+
```
174+
175+
</section>
176+
177+
<!-- /.examples -->
178+
179+
<!-- C interface documentation. -->
180+
181+
* * *
182+
183+
<section class="c">
184+
185+
## C APIs
186+
187+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
188+
189+
<section class="intro">
190+
191+
</section>
192+
193+
<!-- /.intro -->
194+
195+
<!-- C usage documentation. -->
196+
197+
<section class="usage">
198+
199+
### Usage
200+
201+
```c
202+
#include "stdlib/stats/strided/dnanmeanors.h"
203+
```
204+
205+
#### stdlib_strided_dnanmeanors( N, \*X, strideX )
206+
207+
Computes the arithmetic mean of a double-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation.
208+
209+
```c
210+
const double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 };
211+
212+
double v = stdlib_strided_dnanmeanors( 6, x, 2 );
213+
// returns ~4.6667
214+
```
215+
216+
The function accepts the following arguments:
217+
218+
- **N**: `[in] CBLAS_INT` number of indexed elements.
219+
- **X**: `[in] double*` input array.
220+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
221+
222+
```c
223+
double stdlib_strided_dnanmeanors( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
224+
```
225+
226+
#### stdlib_strided_dnanmeanors_ndarray( N, \*X, strideX, offsetX )
227+
228+
Computes the arithmetic mean of a double-precision floating-point strided array, ignoring `NaN` values and using ordinary recursive summation and alternative indexing semantics.
229+
230+
```c
231+
const double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 };
232+
233+
double v = stdlib_strided_dnanmeanors_ndarray( 6, x, 2, 0 );
234+
// returns ~4.6667
235+
```
236+
237+
The function accepts the following arguments:
238+
239+
- **N**: `[in] CBLAS_INT` number of indexed elements.
240+
- **X**: `[in] double*` input array.
241+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
242+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
243+
244+
```c
245+
double stdlib_strided_dnanmeanors_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
246+
```
247+
248+
</section>
249+
250+
<!-- /.usage -->
251+
252+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
253+
254+
<section class="notes">
255+
256+
</section>
257+
258+
<!-- /.notes -->
259+
260+
<!-- C API usage examples. -->
261+
262+
<section class="examples">
263+
264+
### Examples
265+
266+
```c
267+
#include "stdlib/stats/strided/dnanmeanors.h"
268+
#include <stdio.h>
269+
270+
int main( void ) {
271+
// Create a strided array:
272+
const double x[] = { 1.0, 2.0, 0.0/0.0, 3.0, 0.0/0.0, 4.0, 5.0, 6.0, 0.0/0.0, 7.0, 8.0, 0.0/0.0 };
273+
274+
// Specify the number of elements:
275+
const int N = 6;
276+
277+
// Specify the stride length:
278+
const int strideX = 2;
279+
280+
// Compute the arithmetic mean:
281+
double v = stdlib_strided_dnanmeanors( N, x, strideX );
282+
283+
// Print the result:
284+
printf( "mean: %lf\n", v );
285+
}
286+
```
287+
288+
</section>
289+
290+
<!-- /.examples -->
291+
292+
</section>
293+
294+
<!-- /.c -->
295+
296+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
297+
298+
<section class="related">
299+
300+
* * *
301+
302+
## See Also
303+
304+
- <span class="package-name">[`@stdlib/stats/strided/dmeanors`][@stdlib/stats/strided/dmeanors]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array using ordinary recursive summation.</span>
305+
- <span class="package-name">[`@stdlib/stats/strided/dnanmean`][@stdlib/stats/strided/dnanmean]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array, ignoring NaN values.</span>
306+
- <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>
307+
- <span class="package-name">[`@stdlib/stats/base/snanmeanors`][@stdlib/stats/base/snanmeanors]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array, ignoring NaN values and using ordinary recursive summation.</span>
308+
309+
</section>
310+
311+
<!-- /.related -->
312+
313+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
314+
315+
<section class="links">
316+
317+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
318+
319+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
320+
321+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
322+
323+
<!-- <related-links> -->
324+
325+
[@stdlib/stats/strided/dmeanors]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmeanors
326+
327+
[@stdlib/stats/strided/dnanmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanmean
328+
329+
[@stdlib/stats/base/nanmeanors]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/nanmeanors
330+
331+
[@stdlib/stats/base/snanmeanors]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/base/snanmeanors
332+
333+
<!-- </related-links> -->
334+
335+
</section>
336+
337+
<!-- /.links -->

0 commit comments

Comments
 (0)