Skip to content

Commit c9d512a

Browse files
feat: add stats/strided/srangeabs
PR-URL: #9850 Co-authored-by: stdlib-bot <noreply@stdlib.io> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 9bf5e56 commit c9d512a

33 files changed

+3579
-0
lines changed
Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
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+
# srangeabs
22+
23+
> Calculate the [range][range] of absolute values of a single-precision floating-point strided array.
24+
25+
<section class="intro">
26+
27+
The [**range**][range] is defined as the difference between the maximum and minimum values.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var srangeabs = require( '@stdlib/stats/strided/srangeabs' );
39+
```
40+
41+
#### srangeabs( N, x, strideX )
42+
43+
Computes the [range][range] of absolute values of a single-precision floating-point strided array `x`.
44+
45+
```javascript
46+
var Float32Array = require( '@stdlib/array/float32' );
47+
48+
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
49+
50+
var v = srangeabs( x.length, x, 1 );
51+
// returns 1.0
52+
```
53+
54+
The function has the following parameters:
55+
56+
- **N**: number of indexed elements.
57+
- **x**: input [`Float32Array`][@stdlib/array/float32].
58+
- **strideX**: index increment for `x`.
59+
60+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to compute the [range][range] of absolute values of every other element in `x`,
61+
62+
```javascript
63+
var Float32Array = require( '@stdlib/array/float32' );
64+
65+
var x = new Float32Array( [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ] );
66+
67+
var v = srangeabs( 4, x, 2 );
68+
// returns 3.0
69+
```
70+
71+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
72+
73+
<!-- eslint-disable stdlib/capitalized-comments -->
74+
75+
```javascript
76+
var Float32Array = require( '@stdlib/array/float32' );
77+
78+
var x0 = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
79+
var x1 = new Float32Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
80+
81+
var v = srangeabs( 4, x1, 2 );
82+
// returns 3.0
83+
```
84+
85+
#### srangeabs.ndarray( N, x, strideX, offsetX )
86+
87+
Computes the [range][range] of absolute values of a single-precision floating-point strided array using alternative indexing semantics.
88+
89+
```javascript
90+
var Float32Array = require( '@stdlib/array/float32' );
91+
92+
var x = new Float32Array( [ 1.0, -2.0, 2.0 ] );
93+
94+
var v = srangeabs.ndarray( x.length, x, 1, 0 );
95+
// returns 1.0
96+
```
97+
98+
The function has the following additional parameters:
99+
100+
- **offsetX**: starting index for `x`.
101+
102+
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 [range][range] of absolute values for every other element in `x` starting from the second element
103+
104+
```javascript
105+
var Float32Array = require( '@stdlib/array/float32' );
106+
107+
var x = new Float32Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
108+
109+
var v = srangeabs.ndarray( 4, x, 2, 1 );
110+
// returns 3.0
111+
```
112+
113+
</section>
114+
115+
<!-- /.usage -->
116+
117+
<section class="notes">
118+
119+
## Notes
120+
121+
- If `N <= 0`, both functions return `NaN`.
122+
123+
</section>
124+
125+
<!-- /.notes -->
126+
127+
<section class="examples">
128+
129+
## Examples
130+
131+
<!-- eslint no-undef: "error" -->
132+
133+
```javascript
134+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
135+
var srangeabs = require( '@stdlib/stats/strided/srangeabs' );
136+
137+
var x = discreteUniform( 10, -50, 50, {
138+
'dtype': 'float32'
139+
});
140+
console.log( x );
141+
142+
var v = srangeabs( x.length, x, 1 );
143+
console.log( v );
144+
```
145+
146+
</section>
147+
148+
<!-- /.examples -->
149+
150+
<!-- C interface documentation. -->
151+
152+
* * *
153+
154+
<section class="c">
155+
156+
## C APIs
157+
158+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
159+
160+
<section class="intro">
161+
162+
</section>
163+
164+
<!-- /.intro -->
165+
166+
<!-- C usage documentation. -->
167+
168+
<section class="usage">
169+
170+
### Usage
171+
172+
```c
173+
#include "stdlib/stats/strided/srangeabs.h"
174+
```
175+
176+
#### stdlib_strided_srangeabs( N, \*X, strideX )
177+
178+
Computes the [range][range] of absolute values of a single-precision floating-point strided array `x`.
179+
180+
```c
181+
const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f };
182+
183+
float v = stdlib_strided_srangeabs( 4, x, 1 );
184+
// returns 3.0f
185+
```
186+
187+
The function accepts the following arguments:
188+
189+
- **N**: `[in] CBLAS_INT` number of indexed elements.
190+
- **X**: `[in] float*` input array.
191+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
192+
193+
```c
194+
float stdlib_strided_srangeabs( const CBLAS_INT N, const float *X, const CBLAS_INT strideX );
195+
```
196+
197+
#### stdlib_strided_srangeabs_ndarray( N, \*X, strideX, offsetX )
198+
199+
Computes the [range][range] of absolute values of a single-precision floating-point strided array using alternative indexing semantics.
200+
201+
```c
202+
const float x[] = { 1.0f, -2.0f, 3.0f, -4.0f };
203+
204+
float v = stdlib_strided_srangeabs_ndarray( 4, x, 1, 0 );
205+
// returns 3.0f
206+
```
207+
208+
The function accepts the following arguments:
209+
210+
- **N**: `[in] CBLAS_INT` number of indexed elements.
211+
- **X**: `[in] float*` input array.
212+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
213+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
214+
215+
```c
216+
float stdlib_strided_srangeabs_ndarray( const CBLAS_INT N, const float *X, const CBLAS_INT strideX, const CBLAS_INT offsetX );
217+
```
218+
219+
</section>
220+
221+
<!-- /.usage -->
222+
223+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
224+
225+
<section class="notes">
226+
227+
</section>
228+
229+
<!-- /.notes -->
230+
231+
<!-- C API usage examples. -->
232+
233+
<section class="examples">
234+
235+
### Examples
236+
237+
```c
238+
#include "stdlib/stats/strided/srangeabs.h"
239+
#include <stdio.h>
240+
241+
int main( void ) {
242+
// Create a strided array:
243+
const float x[] = { 1.0f, -2.0f, -3.0f, 4.0f, -5.0f, -6.0f, 7.0f, 8.0f };
244+
245+
// Specify the number of elements:
246+
const int N = 4;
247+
248+
// Specify the stride length:
249+
const int strideX = 2;
250+
251+
// Compute the range:
252+
float v = stdlib_strided_srangeabs( N, x, strideX );
253+
254+
// Print the result:
255+
printf( "range: %f\n", v );
256+
}
257+
```
258+
259+
</section>
260+
261+
<!-- /.examples -->
262+
263+
</section>
264+
265+
<!-- /.c -->
266+
267+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
268+
269+
<section class="related">
270+
271+
</section>
272+
273+
<!-- /.related -->
274+
275+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
276+
277+
<section class="links">
278+
279+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
280+
281+
[@stdlib/array/float32]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float32
282+
283+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
284+
285+
<!-- <related-links> -->
286+
287+
<!-- </related-links> -->
288+
289+
</section>
290+
291+
<!-- /.links -->
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var srangeabs = require( './../lib/srangeabs.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'float32'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Creates a benchmark function.
43+
*
44+
* @private
45+
* @param {PositiveInteger} len - array length
46+
* @returns {Function} benchmark function
47+
*/
48+
function createBenchmark( len ) {
49+
var x = uniform( len, -10.0, 10.0, options );
50+
return benchmark;
51+
52+
/**
53+
* Benchmark function.
54+
*
55+
* @private
56+
* @param {Benchmark} b - benchmark instance
57+
*/
58+
function benchmark( b ) {
59+
var v;
60+
var i;
61+
62+
b.tic();
63+
for ( i = 0; i < b.iterations; i++ ) {
64+
v = srangeabs( x.length, x, 1 );
65+
if ( isnanf( v ) ) {
66+
b.fail( 'should not return NaN' );
67+
}
68+
}
69+
b.toc();
70+
if ( isnanf( v ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
}
76+
}
77+
78+
79+
// MAIN //
80+
81+
/**
82+
* Main execution sequence.
83+
*
84+
* @private
85+
*/
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( format( '%s:len=%d', pkg, len ), f );
100+
}
101+
}
102+
103+
main();

0 commit comments

Comments
 (0)