Skip to content

Commit fc39350

Browse files
committed
feat: add stats/strided/snancurange
1 parent 93c9868 commit fc39350

33 files changed

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

0 commit comments

Comments
 (0)