Skip to content

Commit 562065d

Browse files
Sachinn-64Planeshifterkgryte
authored
feat: add stats/strided/snanmskmidrange
PR-URL: #9821 Reviewed-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Athan Reines <kgryte@gmail.com>
1 parent 8f8998f commit 562065d

33 files changed

+4129
-0
lines changed
Lines changed: 338 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,338 @@
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+
<!-- lint disable maximum-heading-length -->
22+
23+
# snanmskmidrange
24+
25+
> Calculate the [mid-range][mid-range] of a single-precision floating-point strided array according to a mask, ignoring `NaN` values.
26+
27+
<section class="intro">
28+
29+
The [**mid-range**][mid-range], or **mid-extreme**, is the arithmetic mean of the maximum and minimum values in a data set.
30+
31+
</section>
32+
33+
<!-- /.intro -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var snanmskmidrange = require( '@stdlib/stats/strided/snanmskmidrange' );
41+
```
42+
43+
#### snanmskmidrange( N, x, strideX, mask, strideMask )
44+
45+
Computes the [mid-range][mid-range] of a single-precision floating-point strided array according to a `mask`, ignoring `NaN` values.
46+
47+
```javascript
48+
var Float32Array = require( '@stdlib/array/float32' );
49+
var Uint8Array = require( '@stdlib/array/uint8' );
50+
51+
var x = new Float32Array( [ 1.0, -2.0, 4.0, 2.0, NaN ] );
52+
var mask = new Uint8Array( [ 0, 0, 1, 0, 0 ] );
53+
54+
var v = snanmskmidrange( x.length, x, 1, mask, 1 );
55+
// returns 0.0
56+
```
57+
58+
The function has the following parameters:
59+
60+
- **N**: number of indexed elements.
61+
- **x**: input [`Float32Array`][@stdlib/array/float32].
62+
- **strideX**: stride length for `x`.
63+
- **mask**: mask [`Uint8Array`][@stdlib/array/uint8]. If a `mask` array element is `0`, the corresponding element in `x` is considered valid and **included** in computation. If a `mask` array element is `1`, the corresponding element in `x` is considered invalid/missing and **excluded** from computation.
64+
- **strideMask**: stride length for `mask`.
65+
66+
The `N` and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the [mid-range][mid-range] of every other element in `x`,
67+
68+
```javascript
69+
var Float32Array = require( '@stdlib/array/float32' );
70+
var Uint8Array = require( '@stdlib/array/uint8' );
71+
72+
var x = new Float32Array( [ 1.0, 2.0, -7.0, -2.0, 4.0, 3.0, 5.0, 6.0 ] );
73+
var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
74+
75+
var v = snanmskmidrange( 4, x, 2, mask, 2 );
76+
// returns -1.5
77+
```
78+
79+
Note that indexing is relative to the first index. To introduce offsets, use [`typed array`][mdn-typed-array] views.
80+
81+
<!-- eslint-disable stdlib/capitalized-comments -->
82+
83+
```javascript
84+
var Float32Array = require( '@stdlib/array/float32' );
85+
var Uint8Array = require( '@stdlib/array/uint8' );
86+
87+
var x0 = new Float32Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ] );
88+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
89+
90+
var mask0 = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
91+
var mask1 = new Uint8Array( mask0.buffer, mask0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
92+
93+
var v = snanmskmidrange( 4, x1, 2, mask1, 2 );
94+
// returns 1.0
95+
```
96+
97+
#### snanmskmidrange.ndarray( N, x, strideX, offsetX, mask, strideMask, offsetMask )
98+
99+
Computes the [mid-range][mid-range] of a single-precision floating-point strided array according to a `mask`, ignoring `NaN` values and using alternative indexing semantics.
100+
101+
```javascript
102+
var Float32Array = require( '@stdlib/array/float32' );
103+
var Uint8Array = require( '@stdlib/array/uint8' );
104+
105+
var x = new Float32Array( [ 1.0, -2.0, 4.0, 2.0, NaN ] );
106+
var mask = new Uint8Array( [ 0, 0, 1, 0, 0 ] );
107+
108+
var v = snanmskmidrange.ndarray( x.length, x, 1, 0, mask, 1, 0 );
109+
// returns 0.0
110+
```
111+
112+
The function has the following additional parameters:
113+
114+
- **offsetX**: starting index for `x`.
115+
- **offsetMask**: starting index for `mask`.
116+
117+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting indices. For example, to calculate the [mid-range][mid-range] for every other element in `x` starting from the second element
118+
119+
```javascript
120+
var Float32Array = require( '@stdlib/array/float32' );
121+
var Uint8Array = require( '@stdlib/array/uint8' );
122+
123+
var x = new Float32Array( [ 2.0, 1.0, -2.0, -2.0, 3.0, 4.0, 5.0, 6.0 ] );
124+
var mask = new Uint8Array( [ 0, 0, 0, 0, 0, 0, 1, 1 ] );
125+
126+
var v = snanmskmidrange.ndarray( 4, x, 2, 1, mask, 2, 1 );
127+
// returns 1.0
128+
```
129+
130+
</section>
131+
132+
<!-- /.usage -->
133+
134+
<section class="notes">
135+
136+
## Notes
137+
138+
- If `N <= 0`, both functions return `NaN`.
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 bernoulli = require( '@stdlib/random/base/bernoulli' );
153+
var filledarrayBy = require( '@stdlib/array/filled-by' );
154+
var snanmskmidrange = require( '@stdlib/stats/strided/snanmskmidrange' );
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, 'float32', rand );
164+
console.log( x );
165+
166+
var mask = filledarrayBy( x.length, 'uint8', bernoulli.factory( 0.2 ) );
167+
console.log( mask );
168+
169+
var v = snanmskmidrange( x.length, x, 1, mask, 1 );
170+
console.log( v );
171+
```
172+
173+
</section>
174+
175+
<!-- /.examples -->
176+
177+
<!-- C interface documentation. -->
178+
179+
* * *
180+
181+
<section class="c">
182+
183+
## C APIs
184+
185+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
186+
187+
<section class="intro">
188+
189+
</section>
190+
191+
<!-- /.intro -->
192+
193+
<!-- C usage documentation. -->
194+
195+
<section class="usage">
196+
197+
### Usage
198+
199+
```c
200+
#include "stdlib/stats/strided/snanmskmidrange.h"
201+
```
202+
203+
#### stdlib_strided_snanmskmidrange( N, \*X, strideX, \*Mask, strideMask )
204+
205+
Computes the [mid-range][mid-range] of a single-precision floating-point strided array according to a `mask`, ignoring `NaN` values.
206+
207+
```c
208+
#include <stdint.h>
209+
210+
const float x[] = { 1.0f, -2.0f, 4.0f, 2.0f, 0.0f/0.0f };
211+
const uint8_t mask[] = { 0, 0, 1, 0, 0 };
212+
213+
float v = stdlib_strided_snanmskmidrange( 5, x, 1, mask, 1 );
214+
// returns 0.0f
215+
```
216+
217+
The function accepts the following arguments:
218+
219+
- **N**: `[in] CBLAS_INT` number of indexed elements.
220+
- **X**: `[in] float*` input array.
221+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
222+
- **Mask**: `[in] uint8_t*` mask array. If a `Mask` array element is `0`, the corresponding element in `X` is considered valid and included in computation. If a `Mask` array element is `1`, the corresponding element in `X` is considered invalid/missing and excluded from computation.
223+
- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`.
224+
225+
```c
226+
float stdlib_strided_snanmskmidrange( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const uint8_t *Mask, const CBLAS_INT strideMask );
227+
```
228+
229+
#### stdlib_strided_snanmskmidrange_ndarray( N, \*X, strideX, offsetX, \*Mask, strideMask, offsetMask )
230+
231+
Computes the [mid-range][mid-range] of a single-precision floating-point strided array according to a `mask`, ignoring `NaN` values and using alternative indexing semantics.
232+
233+
```c
234+
#include <stdint.h>
235+
236+
const float x[] = { 1.0f, -2.0f, 4.0f, 2.0f, 0.0f/0.0f };
237+
const uint8_t mask[] = { 0, 0, 1, 0, 0 };
238+
239+
float v = stdlib_strided_snanmskmidrange_ndarray( 5, x, 1, 0, mask, 1, 0 );
240+
// returns 0.0f
241+
```
242+
243+
The function accepts the following arguments:
244+
245+
- **N**: `[in] CBLAS_INT` number of indexed elements.
246+
- **X**: `[in] float*` input array.
247+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
248+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
249+
- **Mask**: `[in] uint8_t*` mask array. If a `Mask` array element is `0`, the corresponding element in `X` is considered valid and included in computation. If a `Mask` array element is `1`, the corresponding element in `X` is considered invalid/missing and excluded from computation.
250+
- **strideMask**: `[in] CBLAS_INT` stride length for `Mask`.
251+
- **offsetMask**: `[in] CBLAS_INT` starting index for `Mask`.
252+
253+
```c
254+
float stdlib_strided_snanmskmidrange_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, const uint8_t *Mask, const CBLAS_INT strideMask, const CBLAS_INT offsetMask );
255+
```
256+
257+
</section>
258+
259+
<!-- /.usage -->
260+
261+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
262+
263+
<section class="notes">
264+
265+
</section>
266+
267+
<!-- /.notes -->
268+
269+
<!-- C API usage examples. -->
270+
271+
<section class="examples">
272+
273+
### Examples
274+
275+
```c
276+
#include "stdlib/stats/strided/snanmskmidrange.h"
277+
#include <stdint.h>
278+
#include <stdio.h>
279+
280+
int main( void ) {
281+
// Create a strided array:
282+
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 };
283+
284+
// Create a mask array:
285+
const uint8_t mask[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1 };
286+
287+
// Specify the number of elements:
288+
const int N = 5;
289+
290+
// Specify the stride lengths:
291+
const int strideX = 2;
292+
const int strideMask = 2;
293+
294+
// Compute the mid-range:
295+
float v = stdlib_strided_snanmskmidrange( N, x, strideX, mask, strideMask );
296+
297+
// Print the result:
298+
printf( "mid-range: %f\n", v );
299+
}
300+
```
301+
302+
</section>
303+
304+
<!-- /.examples -->
305+
306+
</section>
307+
308+
<!-- /.c -->
309+
310+
<section class="references">
311+
312+
</section>
313+
314+
<!-- /.references -->
315+
316+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
317+
318+
<section class="related">
319+
320+
</section>
321+
322+
<!-- /.related -->
323+
324+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
325+
326+
<section class="links">
327+
328+
[mid-range]: https://en.wikipedia.org/wiki/Mid-range
329+
330+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
331+
332+
[@stdlib/array/uint8]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/uint8
333+
334+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
335+
336+
</section>
337+
338+
<!-- /.links -->

0 commit comments

Comments
 (0)