Skip to content

Commit 2be3696

Browse files
committed
feat: add js and C implementation for stats/strided/dnanmeankbn2
--- 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 abe64bf commit 2be3696

34 files changed

+3584
-0
lines changed
Lines changed: 335 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,335 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# dnanmeankbn2
22+
23+
> Calculate the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values using a second-order iterative Kahan–Babuška algorithm.
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@4bf02936365dcd2e9d3cbd3d606450bc9646d382/lib/node_modules/@stdlib/stats/strided/dnanmeankbn2/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 dnanmeankbn2 = require( '@stdlib/stats/strided/dnanmeankbn2' );
52+
```
53+
54+
#### dnanmeankbn2( N, x, strideX )
55+
56+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array `x`, ignoring `NaN` values using a second-order iterative Kahan–Babuška algorithm.
57+
58+
```javascript
59+
var Float64Array = require( '@stdlib/array/float64' );
60+
61+
var x = new Float64Array( [ 1.0, -2.0, 2.0 ] );
62+
63+
var v = dnanmeankbn2( 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+
```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+
var v = dnanmeankbn2( 4, x, 2 );
80+
// returns 1.25
81+
```
82+
83+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
84+
85+
<!-- eslint-disable stdlib/capitalized-comments -->
86+
87+
<!-- eslint-disable max-len -->
88+
89+
```javascript
90+
var Float64Array = require( '@stdlib/array/float64' );
91+
92+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
93+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
94+
95+
var v = dnanmeankbn2( 5, x1, 2 );
96+
// returns 1.25
97+
```
98+
99+
#### dnanmeankbn2.ndarray( N, x, strideX, offsetX )
100+
101+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
102+
103+
```javascript
104+
var Float64Array = require( '@stdlib/array/float64' );
105+
106+
var x = new Float64Array( [ 1.0, -2.0, NaN, 2.0 ] );
107+
108+
var v = dnanmeankbn2.ndarray( x.length, x, 1, 0 );
109+
// returns ~0.33333
110+
```
111+
112+
The function has the following additional parameters:
113+
114+
- **offsetX**: starting index for `x`.
115+
116+
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
117+
118+
<!-- eslint-disable max-len -->
119+
120+
```javascript
121+
var Float64Array = require( '@stdlib/array/float64' );
122+
123+
var x = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
124+
125+
var v = dnanmeankbn2.ndarray( 5, x, 2, 1 );
126+
// returns 1.25
127+
```
128+
129+
</section>
130+
131+
<!-- /.usage -->
132+
133+
<section class="notes">
134+
135+
## Notes
136+
137+
- If `N <= 0`, both functions return `NaN`.
138+
139+
</section>
140+
141+
<!-- /.notes -->
142+
143+
<section class="examples">
144+
145+
## Examples
146+
147+
<!-- eslint no-undef: "error" -->
148+
149+
```javascript
150+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
151+
var dnanmeankbn2 = require( '@stdlib/stats/strided/dnanmeankbn2' );
152+
153+
var x = discreteUniform( 10, -50, 50, {
154+
'dtype': 'float64'
155+
});
156+
console.log( x );
157+
158+
var v = dnanmeankbn2( x.length, x, 1 );
159+
console.log( v );
160+
```
161+
162+
</section>
163+
164+
<!-- /.examples -->
165+
166+
<!-- C interface documentation. -->
167+
168+
* * *
169+
170+
<section class="c">
171+
172+
## C APIs
173+
174+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
175+
176+
<section class="intro">
177+
178+
</section>
179+
180+
<!-- /.intro -->
181+
182+
<!-- C usage documentation. -->
183+
184+
<section class="usage">
185+
186+
### Usage
187+
188+
```c
189+
#include "stdlib/stats/strided/dnanmeankbn2.h"
190+
```
191+
192+
#### stdlib_strided_dnanmeankbn2( N, \*X, strideX )
193+
194+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values using a second-order iterative Kahan–Babuška algorithm.
195+
196+
```c
197+
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 };
198+
199+
double v = stdlib_strided_dnanmeankbn2( 6, x, 2 );
200+
// returns ~4.6667
201+
```
202+
203+
The function accepts the following arguments:
204+
205+
- **N**: `[in] CBLAS_INT` number of indexed elements.
206+
- **X**: `[in] double*` input array.
207+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
208+
209+
```c
210+
double stdlib_strided_dnanmeankbn2( const CBLAS_INT N, const double *X, const CBLAS_INT strideX );
211+
```
212+
213+
#### stdlib_strided_dnanmeankbn2_ndarray( N, \*X, strideX, offsetX )
214+
215+
Computes the [arithmetic mean][arithmetic-mean] of a double-precision floating-point strided array, ignoring `NaN` values using a second-order iterative Kahan–Babuška algorithm and alternative indexing semantics.
216+
217+
```c
218+
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 };
219+
220+
double v = stdlib_strided_dnanmeankbn2_ndarray( 6, x, 2, 0 );
221+
// returns ~4.6667
222+
```
223+
224+
The function accepts the following arguments:
225+
226+
- **N**: `[in] CBLAS_INT` number of indexed elements.
227+
- **X**: `[in] double*` input array.
228+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
229+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
230+
231+
```c
232+
double stdlib_strided_dnanmeankbn2_ndarray( const CBLAS_INT N, const double *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/dnanmeankbn2.h"
255+
#include <stdio.h>
256+
257+
int main( void ) {
258+
// Create a strided array:
259+
const double x[] = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0 };
260+
261+
// Specify the number of elements:
262+
const int N = 4;
263+
264+
// Specify the stride length:
265+
const int strideX = 2;
266+
267+
// Compute the arithmetic mean:
268+
double v = stdlib_strided_dnanmeankbn2( N, x, strideX );
269+
270+
// Print the result:
271+
printf( "mean: %lf\n", v );
272+
}
273+
```
274+
275+
</section>
276+
277+
<!-- /.examples -->
278+
279+
</section>
280+
281+
<!-- /.c -->
282+
283+
* * *
284+
285+
<section class="references">
286+
287+
## References
288+
289+
- Klein, Andreas. 2005. "A Generalized Kahan-Babuška-Summation-Algorithm." _Computing_ 76 (3): 279–93. doi:[10.1007/s00607-005-0139-x][@klein:2005a].
290+
291+
</section>
292+
293+
<!-- /.references -->
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/strided/dmean`][@stdlib/stats/strided/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/strided/meankbn2`][@stdlib/stats/strided/meankbn2]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a strided array using a second-order iterative Kahan–Babuška algorithm.</span>
305+
- <span class="package-name">[`@stdlib/stats/strided/smeankbn2`][@stdlib/stats/strided/smeankbn2]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array using a second-order iterative Kahan–Babuška algorithm.</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+
[@klein:2005a]: https://doi.org/10.1007/s00607-005-0139-x
322+
323+
<!-- <related-links> -->
324+
325+
[@stdlib/stats/strided/dmean]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmean
326+
327+
[@stdlib/stats/strided/meankbn2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/meankbn2
328+
329+
[@stdlib/stats/strided/smeankbn2]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smeankbn2
330+
331+
<!-- </related-links> -->
332+
333+
</section>
334+
335+
<!-- /.links -->

0 commit comments

Comments
 (0)