Skip to content

Commit 87f3303

Browse files
authored
feat: add stats/strided/snancount
PR-URL: #11438 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent cbcb5a5 commit 87f3303

33 files changed

+3522
-0
lines changed
Lines changed: 301 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,301 @@
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+
# snancount
22+
23+
> Calculate the number of non-`NaN` elements in a single-precision floating-point strided array.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var snancount = require( '@stdlib/stats/strided/snancount' );
37+
```
38+
39+
#### snancount( N, x, strideX )
40+
41+
Computes the number of non-`NaN` elements in a single-precision floating-point strided array.
42+
43+
```javascript
44+
var Float32Array = require( '@stdlib/array/float32' );
45+
46+
var x = new Float32Array( [ 1.0, -2.0, NaN, 2.0 ] );
47+
48+
var v = snancount( x.length, x, 1 );
49+
// returns 3
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **N**: number of indexed elements.
55+
- **x**: input [`Float32Array`][@stdlib/array/float32].
56+
- **strideX**: stride length for `x`.
57+
58+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to count every other element in `x`,
59+
60+
```javascript
61+
var Float32Array = require( '@stdlib/array/float32' );
62+
63+
var x = new Float32Array( [ 1.0, 2.0, NaN, -2.0, 4.0, 3.0, NaN, NaN ] );
64+
65+
var v = snancount( 4, x, 2 );
66+
// returns 2
67+
```
68+
69+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
70+
71+
<!-- eslint-disable stdlib/capitalized-comments -->
72+
73+
```javascript
74+
var Float32Array = require( '@stdlib/array/float32' );
75+
76+
var x0 = new Float32Array( [ 2.0, 1.0, NaN, -2.0, 3.0, 4.0, NaN, NaN ] );
77+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
78+
79+
var v = snancount( 4, x1, 2 );
80+
// returns 3
81+
```
82+
83+
#### snancount.ndarray( N, x, strideX, offsetX )
84+
85+
Computes the number of non-`NaN` elements in a single-precision floating-point strided array using alternative indexing semantics.
86+
87+
```javascript
88+
var Float32Array = require( '@stdlib/array/float32' );
89+
90+
var x = new Float32Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, NaN, NaN ] );
91+
92+
var v = snancount.ndarray( 4, x, 2, 1 );
93+
// returns 3
94+
```
95+
96+
The function has the following additional parameter:
97+
98+
- **offsetX**: starting index for `x`.
99+
100+
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 count every other element in `x` starting from the second element,
101+
102+
```javascript
103+
var Float32Array = require( '@stdlib/array/float32' );
104+
105+
var x = new Float32Array( [ 2.0, 1.0, NaN, -2.0, 3.0, 4.0, NaN, NaN ] );
106+
107+
var v = snancount.ndarray( 4, x, 2, 1 );
108+
// returns 3
109+
```
110+
111+
</section>
112+
113+
<!-- /.usage -->
114+
115+
<section class="notes">
116+
117+
## Notes
118+
119+
- If `N <= 0`, both functions return `0`.
120+
121+
</section>
122+
123+
<!-- /.notes -->
124+
125+
<section class="examples">
126+
127+
## Examples
128+
129+
<!-- eslint no-undef: "error" -->
130+
131+
```javascript
132+
var uniform = require( '@stdlib/random/base/uniform' );
133+
var filledarrayBy = require( '@stdlib/array/filled-by' );
134+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
135+
var snancount = require( '@stdlib/stats/strided/snancount' );
136+
137+
function rand() {
138+
if ( bernoulli( 0.8 ) < 1 ) {
139+
return NaN;
140+
}
141+
return uniform( -50.0, 50.0 );
142+
}
143+
144+
var x = filledarrayBy( 10, 'float32', rand );
145+
console.log( x );
146+
147+
var v = snancount( x.length, x, 1 );
148+
console.log( v );
149+
```
150+
151+
</section>
152+
153+
<!-- /.examples -->
154+
155+
<!-- C interface documentation. -->
156+
157+
* * *
158+
159+
<section class="c">
160+
161+
## C APIs
162+
163+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
164+
165+
<section class="intro">
166+
167+
</section>
168+
169+
<!-- /.intro -->
170+
171+
<!-- C usage documentation. -->
172+
173+
<section class="usage">
174+
175+
### Usage
176+
177+
```c
178+
#include "stdlib/stats/strided/snancount.h"
179+
```
180+
181+
#### stdlib_strided_snancount( N, \*X, strideX )
182+
183+
Computes the number of non-`NaN` elements in a single-precision floating-point strided array.
184+
185+
```c
186+
const float x[] = { 1.0f, 2.0f, 0.0f / 0.0f, 4.0f, 5.0f, 6.0f, 0.0f / 0.0f, 8.0f };
187+
188+
int v = stdlib_strided_snancount( 4, x, 2 );
189+
// returns 2
190+
```
191+
192+
The function accepts the following arguments:
193+
194+
- **N**: `[in] CBLAS_INT` number of indexed elements.
195+
- **X**: `[in] float*` input array.
196+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
197+
198+
```c
199+
CBLAS_INT stdlib_strided_snancount( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
200+
```
201+
202+
#### stdlib_strided_snancount_ndarray( N, \*X, strideX, offsetX )
203+
204+
Computes the number of non-`NaN` elements in a single-precision floating-point strided array using alternative indexing semantics.
205+
206+
```c
207+
const float x[] = { 2.0f, 1.0f, 0.0f / 0.0f, -2.0f, 3.0f, 4.0f, 0.0f / 0.0f, 0.0f / 0.0f };
208+
209+
int v = stdlib_strided_snancount_ndarray( 4, x, 2, 1 );
210+
// returns 3
211+
```
212+
213+
The function accepts the following arguments:
214+
215+
- **N**: `[in] CBLAS_INT` number of indexed elements.
216+
- **X**: `[in] float*` input array.
217+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
218+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
219+
220+
```c
221+
CBLAS_INT stdlib_strided_snancount_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
222+
```
223+
224+
</section>
225+
226+
<!-- /.usage -->
227+
228+
229+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
230+
231+
232+
<section class="notes">
233+
234+
### Notes
235+
236+
- If `N <= 0`, both functions return `0`.
237+
238+
</section>
239+
240+
<!-- /.notes -->
241+
242+
<!-- C API usage examples. -->
243+
244+
<section class="examples">
245+
246+
### Examples
247+
248+
```c
249+
#include "stdlib/stats/strided/snancount.h"
250+
#include <stdio.h>
251+
252+
int main( void ) {
253+
// Create a strided array:
254+
const float x[] = { 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 0.0f/0.0f, 0.0f/0.0f };
255+
256+
// Specify the number of elements:
257+
const int N = 5;
258+
259+
// Specify the stride length:
260+
const int strideX = 2;
261+
262+
// Compute the number of non-NaN elements:
263+
int v = stdlib_strided_snancount( N, x, strideX );
264+
265+
// Print the result:
266+
printf( "count: %d\n", v );
267+
}
268+
```
269+
270+
</section>
271+
272+
<!-- /.examples -->
273+
274+
</section>
275+
276+
<!-- /.c -->
277+
278+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
279+
280+
<section class="related">
281+
282+
</section>
283+
284+
<!-- /.related -->
285+
286+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
287+
288+
<section class="links">
289+
290+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
291+
292+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
293+
294+
<!-- <related-links> -->
295+
296+
297+
<!-- </related-links> -->
298+
299+
</section>
300+
301+
<!-- /.links -->

0 commit comments

Comments
 (0)