Skip to content

Commit dab1ccc

Browse files
committed
feat: add js and C implementation for stats/strided/snanmeankbn
1 parent a57b8ad commit dab1ccc

34 files changed

+3633
-0
lines changed
Lines changed: 328 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,328 @@
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+
# snanmeankbn
22+
23+
> Calculate the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array ignoring NaN values and using an improved Kahan-Babuska 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@60af5c459896bb85413697508c87c6a069a60e57/lib/node_modules/@stdlib/stats/strided/snanmeankbn/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 snanmeankbn = require( '@stdlib/stats/strided/snanmeankbn' );
52+
```
53+
54+
#### snanmeankbn( N, x, strideX )
55+
56+
Calculate the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array ignoring NaN values and using an improved Kahan-Babuska algorithm.
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 = snanmeankbn( 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.
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 Float32Array = require( '@stdlib/array/float32' );
77+
78+
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0, NaN ] );
79+
80+
var v = snanmeankbn( 4, 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 Float32Array = require( '@stdlib/array/float32' );
90+
91+
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
92+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
93+
94+
var v = snanmeankbn( 5, x1, 2 );
95+
// returns 1.25
96+
```
97+
98+
#### snanmeankbn.ndarray( N, x, strideX, offsetX )
99+
100+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array ignoring NaN values and using an improved Kahan-Babuska algorithm with alternative indexing semantics.
101+
102+
```javascript
103+
var Float32Array = require( '@stdlib/array/float32' );
104+
105+
var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
106+
107+
var v = snanmeankbn.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 in `x` starting from the second element
116+
117+
<!-- eslint-disable max-len -->
118+
119+
```javascript
120+
var Float32Array = require( '@stdlib/array/float32' );
121+
122+
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0, NaN, NaN ] );
123+
124+
var v = snanmeankbn.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+
139+
</section>
140+
141+
<!-- /.notes -->
142+
143+
<section class="examples">
144+
145+
## Examples
146+
147+
<!-- eslint no-undef: "error" -->
148+
149+
```javascript
150+
var uniform = require( '@stdlib/random/base/uniform' );
151+
var filledarrayBy = require( '@stdlib/array/filled-by' );
152+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
153+
var snanmeankbn = require( '@stdlib/stats/strided/snanmeankbn' );
154+
155+
function rand() {
156+
if ( bernoulli( 0.8 ) < 1 ) {
157+
return NaN;
158+
}
159+
return uniform( -50.0, 50.0 );
160+
}
161+
162+
var x = filledarrayBy( 10, 'float32', rand );
163+
console.log( x );
164+
165+
var v = snanmeankbn( x.length, x, 1 );
166+
console.log( v );
167+
```
168+
169+
</section>
170+
171+
<!-- /.examples -->
172+
173+
<!-- C interface documentation. -->
174+
175+
* * *
176+
177+
<section class="c">
178+
179+
## C APIs
180+
181+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
182+
183+
<section class="intro">
184+
185+
</section>
186+
187+
<!-- /.intro -->
188+
189+
<!-- C usage documentation. -->
190+
191+
<section class="usage">
192+
193+
### Usage
194+
195+
```c
196+
#include "stdlib/stats/strided/snanmeankbn.h"
197+
```
198+
199+
#### stdlib_strided_snanmeankbn( N, \*X, strideX )
200+
201+
Calculate the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array ignoring NaN values and using an improved Kahan-Babuska algorithm.
202+
203+
```c
204+
const float x[] = { 1.0f, 2.0f, 3.0f, 0.0f/0.0f };
205+
206+
float v = stdlib_strided_snanmeankbn( 4, x, 1 );
207+
// returns 2.0f
208+
```
209+
210+
The function accepts the following arguments:
211+
212+
- **N**: `[in] CBLAS_INT` number of indexed elements.
213+
- **X**: `[in] float*` input array.
214+
- **strideX**: `[in] CBLAS_INT` stride length.
215+
216+
```c
217+
float stdlib_strided_snanmeankbn( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
218+
```
219+
220+
#### stdlib_strided_snanmeankbn_ndarray( N, \*X, strideX, offsetX )
221+
222+
Computes the [arithmetic mean][arithmetic-mean] of a single-precision floating-point strided array ignoring NaN values and using an improved Kahan-Babuska algorithm with alternative indexing semantics.
223+
224+
```c
225+
const float x[] = { 1.0f, 2.0f, 3.0f, 0.0f/0.0f };
226+
227+
float v = stdlib_strided_snanmeankbn_ndarray( 4, x, 1, 0 );
228+
// returns 2.0f
229+
```
230+
231+
The function accepts the following arguments:
232+
233+
- **N**: `[in] CBLAS_INT` number of indexed elements.
234+
- **X**: `[in] float*` input array.
235+
- **strideX**: `[in] CBLAS_INT` stride length.
236+
- **offsetX**: `[in] CBLAS_INT` starting index.
237+
238+
```c
239+
float stdlib_strided_snanmeankbn_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
240+
```
241+
242+
</section>
243+
244+
<!-- /.usage -->
245+
246+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
247+
248+
<section class="notes">
249+
250+
</section>
251+
252+
<!-- /.notes -->
253+
254+
<!-- C API usage examples. -->
255+
256+
<section class="examples">
257+
258+
### Examples
259+
260+
```c
261+
#include "stdlib/stats/strided/snanmeankbn.h"
262+
#include <stdio.h>
263+
264+
int main( void ) {
265+
// Create a strided array:
266+
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 };
267+
268+
// Specify the number of elements:
269+
const int N = 6;
270+
271+
// Specify the stride length:
272+
const int strideX = 2;
273+
274+
// Compute the arithmetic mean:
275+
float v = stdlib_strided_snanmeankbn( N, x, strideX );
276+
277+
// Print the result:
278+
printf( "mean: %f\n", v );
279+
}
280+
```
281+
282+
</section>
283+
284+
<!-- /.examples -->
285+
286+
</section>
287+
288+
<!-- /.c -->
289+
290+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
291+
292+
<section class="related">
293+
294+
* * *
295+
296+
## See Also
297+
298+
- <span class="package-name">[`@stdlib/stats/strided/dmeankbn`][@stdlib/stats/strided/dmeankbn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a double-precision floating-point strided array using an improved Kahan–Babuška algorithm.</span>
299+
- <span class="package-name">[`@stdlib/stats/strided/smeankbn`][@stdlib/stats/strided/smeankbn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a single-precision floating-point strided array using an improved Kahan–Babuška algorithm.</span>
300+
- <span class="package-name">[`@stdlib/stats/strided/meankbn`][@stdlib/stats/strided/meankbn]</span><span class="delimiter">: </span><span class="description">calculate the arithmetic mean of a strided array using an improved Kahan–Babuška algorithm.</span>
301+
302+
</section>
303+
304+
<!-- /.related -->
305+
306+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
307+
308+
<section class="links">
309+
310+
[arithmetic-mean]: https://en.wikipedia.org/wiki/Arithmetic_mean
311+
312+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
313+
314+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
315+
316+
<!-- <related-links> -->
317+
318+
[@stdlib/stats/strided/dmeankbn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmeankbn
319+
320+
[@stdlib/stats/strided/smeankbn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smeankbn
321+
322+
[@stdlib/stats/strided/meankbn]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/meankbn
323+
324+
<!-- </related-links> -->
325+
326+
</section>
327+
328+
<!-- /.links -->

0 commit comments

Comments
 (0)