Skip to content

Commit 36f7a1a

Browse files
committed
feat: add stats/strided/dnanmean
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 0f30934 commit 36f7a1a

34 files changed

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

0 commit comments

Comments
 (0)