Skip to content

Commit 595918c

Browse files
Orthodox-64kgryte
andauthored
feat: add stats/strided/midrange
PR-URL: #9298 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent f8eacb1 commit 595918c

File tree

15 files changed

+1800
-0
lines changed

15 files changed

+1800
-0
lines changed
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# midrange
22+
23+
> Calculate the [mid-range][mid-range] of a strided array.
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 midrange = require( '@stdlib/stats/strided/midrange' );
39+
```
40+
41+
#### midrange( N, x, strideX )
42+
43+
Computes the [mid-range][mid-range] of a strided array.
44+
45+
```javascript
46+
var x = [ 1.0, -2.0, 2.0 ];
47+
48+
var v = midrange( x.length, x, 1 );
49+
// returns 0.0
50+
```
51+
52+
The function has the following parameters:
53+
54+
- **N**: number of indexed elements.
55+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
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 compute the [mid-range][mid-range] of every other element in `x`,
59+
60+
```javascript
61+
var x = [ 1.0, 2.0, 2.0, -7.0, -2.0, 3.0, 4.0, 2.0 ];
62+
63+
var v = midrange( 4, x, 2 );
64+
// returns 1.0
65+
```
66+
67+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
68+
69+
<!-- eslint-disable stdlib/capitalized-comments -->
70+
71+
```javascript
72+
var Float64Array = require( '@stdlib/array/float64' );
73+
74+
var x0 = new Float64Array( [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ] );
75+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
76+
77+
var v = midrange( 4, x1, 2 );
78+
// returns 1.0
79+
```
80+
81+
#### midrange.ndarray( N, x, strideX, offsetX )
82+
83+
Computes the [mid-range][mid-range] of a strided array using alternative indexing semantics.
84+
85+
```javascript
86+
var x = [ 1.0, -2.0, 2.0 ];
87+
88+
var v = midrange.ndarray( x.length, x, 1, 0 );
89+
// returns 0.0
90+
```
91+
92+
The function has the following additional parameters:
93+
94+
- **offsetX**: starting index for `x`.
95+
96+
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 [mid-range][mid-range] for every other element in `x` starting from the second element
97+
98+
```javascript
99+
var x = [ 2.0, 1.0, 2.0, -2.0, -2.0, 2.0, 3.0, 4.0 ];
100+
101+
var v = midrange.ndarray( 4, x, 2, 1 );
102+
// returns 1.0
103+
```
104+
105+
</section>
106+
107+
<!-- /.usage -->
108+
109+
<section class="notes">
110+
111+
## Notes
112+
113+
- If `N <= 0`, both functions return `NaN`.
114+
- Depending on the environment, the typed versions ([`dmidrange`][@stdlib/stats/strided/dmidrange], [`smidrange`][@stdlib/stats/strided/smidrange], etc.) are likely to be significantly more performant.
115+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
116+
117+
</section>
118+
119+
<!-- /.notes -->
120+
121+
<section class="examples">
122+
123+
## Examples
124+
125+
<!-- eslint no-undef: "error" -->
126+
127+
```javascript
128+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
129+
var midrange = require( '@stdlib/stats/strided/midrange' );
130+
131+
var x = discreteUniform( 10, -50, 50, {
132+
'dtype': 'float64'
133+
});
134+
console.log( x );
135+
136+
var v = midrange( x.length, x, 1 );
137+
console.log( v );
138+
```
139+
140+
</section>
141+
142+
<!-- /.examples -->
143+
144+
<!-- Section for related `stdlib` packages. -->
145+
146+
<section class="related">
147+
148+
</section>
149+
150+
<!-- /.related -->
151+
152+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
153+
154+
<section class="links">
155+
156+
[mid-range]: https://en.wikipedia.org/wiki/Mid-range
157+
158+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
159+
160+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
161+
162+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
163+
164+
[@stdlib/stats/strided/dmidrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/dmidrange
165+
166+
[@stdlib/stats/strided/smidrange]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/stats/strided/smidrange
167+
168+
</section>
169+
170+
<!-- /.links -->
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
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 midrange = require( './../lib/main.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
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, 10, options );
50+
return benchmark;
51+
52+
function benchmark( b ) {
53+
var v;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = midrange( x.length, x, 1 );
59+
if ( isnan( v ) ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( isnan( v ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
f = createBenchmark( len );
93+
bench( format( '%s:len=%d', pkg, len ), f );
94+
}
95+
}
96+
97+
main();
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
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 midrange = require( './../lib/ndarray.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
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, 10, options );
50+
return benchmark;
51+
52+
function benchmark( b ) {
53+
var v;
54+
var i;
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
v = midrange( x.length, x, 1, 0 );
59+
if ( isnan( v ) ) {
60+
b.fail( 'should not return NaN' );
61+
}
62+
}
63+
b.toc();
64+
if ( isnan( v ) ) {
65+
b.fail( 'should not return NaN' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
f = createBenchmark( len );
93+
bench( format( '%s:ndarray:len=%d', pkg, len ), f );
94+
}
95+
}
96+
97+
main();

0 commit comments

Comments
 (0)