Skip to content

Commit 2a8cda9

Browse files
feat: add stats/array/midrange-by
PR-URL: #10263 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com>
1 parent 6e63e9c commit 2a8cda9

File tree

10 files changed

+999
-0
lines changed

10 files changed

+999
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
# midrangeBy
22+
23+
> Calculate the [midrange][midrange] of an array via a callback function.
24+
25+
<section class="intro">
26+
27+
The [**midrange**][midrange] is defined as the arithmetic mean of the maximum and minimum values.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var midrangeBy = require( '@stdlib/stats/array/midrange-by' );
39+
```
40+
41+
#### midrangeBy( x, clbk\[, thisArg] )
42+
43+
Computes the [midrange][midrange] of an array via a callback function.
44+
45+
```javascript
46+
function accessor( v ) {
47+
return v * 2.0;
48+
}
49+
50+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
51+
52+
var v = midrangeBy( x, accessor );
53+
// returns -1.0
54+
```
55+
56+
The function has the following parameters:
57+
58+
- **x**: input array.
59+
- **clbk**: callback function.
60+
- **thisArg**: execution context (_optional_).
61+
62+
The invoked callback is provided three arguments:
63+
64+
- **value**: current array element.
65+
- **index**: current array index.
66+
- **array**: input array.
67+
68+
To set the callback execution context, provide a `thisArg`.
69+
70+
```javascript
71+
function accessor( v ) {
72+
this.count += 1;
73+
return v * 2.0;
74+
}
75+
76+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
77+
78+
var context = {
79+
'count': 0
80+
};
81+
82+
var v = midrangeBy( x, accessor, context );
83+
// returns -1.0
84+
85+
var cnt = context.count;
86+
// returns 8
87+
```
88+
89+
</section>
90+
91+
<!-- /.usage -->
92+
93+
<section class="notes">
94+
95+
## Notes
96+
97+
- If provided an empty array, the function returns `NaN`.
98+
- A provided callback function should return a numeric value.
99+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
100+
- The function supports array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
101+
102+
</section>
103+
104+
<!-- /.notes -->
105+
106+
<section class="examples">
107+
108+
## Examples
109+
110+
<!-- eslint no-undef: "error" -->
111+
112+
```javascript
113+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
114+
var midrangeBy = require( '@stdlib/stats/array/midrange-by' );
115+
116+
function accessor( v ) {
117+
return v * 2.0;
118+
}
119+
120+
var x = discreteUniform( 10, -50, 50, {
121+
'dtype': 'float64'
122+
});
123+
console.log( x );
124+
125+
var v = midrangeBy( x, accessor );
126+
console.log( v );
127+
```
128+
129+
</section>
130+
131+
<!-- /.examples -->
132+
133+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
134+
135+
<section class="related">
136+
137+
</section>
138+
139+
<!-- /.related -->
140+
141+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
142+
143+
<section class="links">
144+
145+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
146+
147+
[midrange]: https://en.wikipedia.org/wiki/Mid-range
148+
149+
</section>
150+
151+
<!-- /.links -->
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 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 midrangeBy = require( './../lib' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Accessor function.
43+
*
44+
* @private
45+
* @param {number} value - array element
46+
* @returns {number} accessed value
47+
*/
48+
function accessor( value ) {
49+
return value * 2.0;
50+
}
51+
52+
/**
53+
* Creates a benchmark function.
54+
*
55+
* @private
56+
* @param {PositiveInteger} len - array length
57+
* @returns {Function} benchmark function
58+
*/
59+
function createBenchmark( len ) {
60+
var x = uniform( len, -10, 10, options );
61+
return benchmark;
62+
63+
/**
64+
* Benchmark function.
65+
*
66+
* @private
67+
* @param {Benchmark} b - benchmark instance
68+
*/
69+
function benchmark( b ) {
70+
var v;
71+
var i;
72+
73+
b.tic();
74+
for ( i = 0; i < b.iterations; i++ ) {
75+
v = midrangeBy( x, accessor );
76+
if ( isnan( v ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
}
80+
b.toc();
81+
if ( isnan( v ) ) {
82+
b.fail( 'should not return NaN' );
83+
}
84+
b.pass( 'benchmark finished' );
85+
b.end();
86+
}
87+
}
88+
89+
90+
// MAIN //
91+
92+
/**
93+
* Main execution sequence.
94+
*
95+
* @private
96+
*/
97+
function main() {
98+
var len;
99+
var min;
100+
var max;
101+
var f;
102+
var i;
103+
104+
min = 1; // 10^min
105+
max = 6; // 10^max
106+
107+
for ( i = min; i <= max; i++ ) {
108+
len = pow( 10, i );
109+
f = createBenchmark( len );
110+
bench( format( '%s:len=%d', pkg, len ), f );
111+
}
112+
}
113+
114+
main();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
{{alias}}( x, clbk[, thisArg] )
3+
Computes the midrange of an array via a callback function.
4+
5+
If provided an empty array, the function returns `NaN`.
6+
7+
The callback function is provided three arguments:
8+
9+
- value: current array element.
10+
- index: current array index.
11+
- array: the input array.
12+
13+
The callback function should return a numeric value.
14+
15+
If the callback function does not return any value (or equivalently,
16+
explicitly returns `undefined`), the value is ignored.
17+
18+
Parameters
19+
----------
20+
x: Array|TypedArray
21+
Input array.
22+
23+
clbk: Function
24+
Callback function.
25+
26+
thisArg: any (optional)
27+
Execution context.
28+
29+
Returns
30+
-------
31+
out: number
32+
Midrange.
33+
34+
Examples
35+
--------
36+
> function accessor( v ) { return v * 2.0; };
37+
> var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, -1.0, -3.0 ];
38+
> {{alias}}( x, accessor )
39+
-1.0
40+
41+
See Also
42+
--------
43+

0 commit comments

Comments
 (0)