Skip to content

Commit fb7c43e

Browse files
Sachinn-64kgrytestdlib-bot
authored
feat: add stats/strided/smskmidrange
PR-URL: #9492 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Co-authored-by: stdlib-bot <noreply@stdlib.io>
1 parent 03605c2 commit fb7c43e

33 files changed

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

0 commit comments

Comments
 (0)