Skip to content

Commit fde3f00

Browse files
committed
feat: add stats/strided/dnanmeanpw
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: missing_dependencies - task: lint_c_examples status: missing_dependencies - task: lint_c_benchmarks status: missing_dependencies - 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 dcb628f commit fde3f00

34 files changed

+3734
-0
lines changed
Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
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+
# dnanmeanpw
22+
23+
> Calculate the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values and using pairwise 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@6905c1fbb86fb9d9c958da1e70ae7132a1245ba0/lib/node_modules/@stdlib/stats/strided/dnanmeanpw/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 dnanmeanpw = require( '@stdlib/stats/strided/dnanmeanpw' );
52+
```
53+
54+
#### dnanmeanpw( 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 pairwise 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 = dnanmeanpw( 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.
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:
74+
75+
```javascript
76+
var Float64Array = require( '@stdlib/array/float64' );
77+
78+
var x = new Float64Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN ] );
79+
80+
var v = dnanmeanpw( 5, x, 2 );
81+
// returns 1.25
82+
```
83+
84+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
85+
86+
<!-- eslint-disable stdlib/capitalized-comments, max-len -->
87+
88+
```javascript
89+
var Float64Array = require( '@stdlib/array/float64' );
90+
91+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
92+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
93+
94+
var v = dnanmeanpw( 5, x1, 2 );
95+
// returns 1.25
96+
```
97+
98+
#### dnanmeanpw.ndarray( N, x, strideX, offsetX )
99+
100+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values and using pairwise summation and alternative indexing semantics.
101+
102+
```javascript
103+
var Float64Array = require( '@stdlib/array/float64' );
104+
105+
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
106+
107+
var v = dnanmeanpw.ndarray( x.length, x, 1, 0 );
108+
// returns ~0.33333
109+
```
110+
111+
The function has the following additional parameters:
112+
113+
- **offsetX**: starting index.
114+
115+
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 starting from the second element:
116+
117+
<!-- eslint-disable max-len -->
118+
119+
```javascript
120+
var Float64Array = require( '@stdlib/array/float64' );
121+
122+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
123+
124+
var v = dnanmeanpw.ndarray( 5, x, 2, 1 );
125+
// returns 1.25
126+
```
127+
128+
</section>
129+
130+
<!-- /.usage -->
131+
132+
<section class="notes">
133+
134+
## Notes
135+
136+
- If `N <= 0`, both functions return `NaN`.
137+
- If every indexed element is `NaN`, both functions return `NaN`.
138+
- In general, pairwise summation is more numerically stable than ordinary recursive summation (i.e., "simple" summation), with slightly worse performance. While not the most numerically stable summation technique (e.g., compensated summation techniques such as the Kahan–Babuška-Neumaier algorithm are generally more numerically stable), pairwise summation strikes a reasonable balance between numerical stability and performance. If either numerical stability or performance is more desirable for your use case, consider alternative summation techniques.
139+
140+
</section>
141+
142+
<!-- /.notes -->
143+
144+
<section class="examples">
145+
146+
## Examples
147+
148+
<!-- eslint no-undef: "error" -->
149+
150+
```javascript
151+
var uniform = require( '@stdlib/random/base/uniform' );
152+
var filledarrayBy = require( '@stdlib/array/filled-by' );
153+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
154+
var dnanmeanpw = require( '@stdlib/stats/strided/dnanmeanpw' );
155+
156+
function rand() {
157+
if ( bernoulli( 0.8 ) < 1 ) {
158+
return NaN;
159+
}
160+
return uniform( -50.0, 50.0 );
161+
}
162+
163+
var x = filledarrayBy( 10, 'float64', rand );
164+
console.log( x );
165+
166+
var v = dnanmeanpw( x.length, x, 1 );
167+
console.log( v );
168+
```
169+
170+
</section>
171+
172+
<!-- /.examples -->
173+
174+
<!-- C interface documentation. -->
175+
176+
* * *
177+
178+
<section class="c">
179+
180+
## C APIs
181+
182+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
183+
184+
<section class="intro">
185+
186+
</section>
187+
188+
<!-- /.intro -->
189+
190+
<!-- C usage documentation. -->
191+
192+
<section class="usage">
193+
194+
### Usage
195+
196+
```c
197+
#include "stdlib/stats/strided/dnanmeanpw.h"
198+
```
199+
200+
#### stdlib_strided_dnanmeanpw( N, \*X, strideX )
201+
202+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values and using pairwise summation.
203+
204+
```c
205+
const double x[] = { 1.0, -2.0, 0.0/0.0, 2.0 };
206+
207+
double v = stdlib_strided_dnanmeanpw( 4, x, 1 );
208+
// returns ~0.3333
209+
```
210+
211+
The function accepts the following arguments:
212+
213+
- **N**: `[in] CBLAS_INT` number of indexed elements.
214+
- **X**: `[in] double*` input array.
215+
- **strideX**: `[in] CBLAS_INT` stride length.
216+
217+
```c
218+
double stdlib_strided_dnanmeanpw( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
219+
```
220+
221+
#### stdlib_strided_dnanmeanpw_ndarray( N, \*X, strideX, offsetX )
222+
223+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values and using pairwise summation and alternative indexing semantics.
224+
225+
```c
226+
const double x[] = { 1.0, -2.0, 0.0/0.0, 2.0 };
227+
228+
double v = stdlib_strided_dnanmeanpw_ndarray( 4, x, 1, 0 );
229+
// returns ~0.3333
230+
```
231+
232+
The function accepts the following arguments:
233+
234+
- **N**: `[in] CBLAS_INT` number of indexed elements.
235+
- **X**: `[in] double*` input array.
236+
- **strideX**: `[in] CBLAS_INT` stride length.
237+
- **offsetX**: `[in] CBLAS_INT` starting index.
238+
239+
```c
240+
double stdlib_strided_dnanmeanpw_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
241+
```
242+
243+
</section>
244+
245+
<!-- /.usage -->
246+
247+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
248+
249+
<section class="notes">
250+
251+
</section>
252+
253+
<!-- /.notes -->
254+
255+
<!-- C API usage examples. -->
256+
257+
<section class="examples">
258+
259+
### Examples
260+
261+
```c
262+
#include "stdlib/stats/strided/dnanmeanpw.h"
263+
#include <stdio.h>
264+
265+
int main( void ) {
266+
// Create a strided array:
267+
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 };
268+
269+
// Specify the number of elements:
270+
const int N = 6;
271+
272+
// Specify the stride length:
273+
const int strideX = 2;
274+
275+
// Compute the arithmetic mean:
276+
double v = stdlib_strided_dnanmeanpw( N, x, strideX );
277+
278+
// Print the result:
279+
printf( "mean: %lf\n", v );
280+
}
281+
```
282+
283+
</section>
284+
285+
<!-- /.examples -->
286+
287+
</section>
288+
289+
<!-- /.c -->
290+
291+
* * *
292+
293+
<section class="references">
294+
295+
## References
296+
297+
- Higham, Nicholas J. 1993. "The Accuracy of Floating Point Summation." _SIAM Journal on Scientific Computing_ 14 (4): 783–99. doi:[10.1137/0914050][@higham:1993a].
298+
299+
</section>
300+
301+
<!-- /.references -->
302+
303+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
304+
305+
<section class="related">
306+
307+
* * *
308+
309+
## See Also
310+
311+
- <span class="package-name">[`@stdlib/stats/strided/dmeanpw`][@stdlib/stats/strided/dmeanpw]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array using pairwise summation.</span>
312+
- <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>
313+
314+
</section>
315+
316+
<!-- /.related -->
317+
318+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
319+
320+
<section class="links">
321+
322+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
323+
324+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
325+
326+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
327+
328+
[@higham:1993a]: https://doi.org/10.1137/0914050
329+
330+
<!-- <related-links> -->
331+
332+
[@stdlib/stats/strided/dmeanpw]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmeanpw
333+
334+
[@stdlib/stats/strided/dnanmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dnanmean
335+
336+
<!-- </related-links> -->
337+
338+
</section>
339+
340+
<!-- /.links -->

0 commit comments

Comments
 (0)