Skip to content

Commit 6986d92

Browse files
authored
feat: add stats/base/ndarray/midrange-by
PR-URL: #9512 Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent b0c05c9 commit 6986d92

10 files changed

Lines changed: 991 additions & 0 deletions

File tree

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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 [mid-range][mid-range] of a one-dimensional ndarray via a callback function.
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 midrangeBy = require( '@stdlib/stats/base/ndarray/midrange-by' );
39+
```
40+
41+
#### midrangeBy( arrays, clbk\[, thisArg ] )
42+
43+
Computes the [mid-range][mid-range] of a one-dimensional ndarray via a callback function.
44+
45+
```javascript
46+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
47+
48+
function clbk( value ) {
49+
return value * 2.0;
50+
}
51+
52+
var xbuf = [ 1.0, 3.0, 4.0, 2.0 ];
53+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
54+
55+
var v = midrangeBy( [ x ], clbk );
56+
// returns 5.0
57+
```
58+
59+
The function has the following parameters:
60+
61+
- **arrays**: array-like object containing a one-dimensional input ndarray.
62+
- **clbk**: callback function.
63+
- **thisArg**: callback execution context (_optional_).
64+
65+
The invoked callback is provided three arguments:
66+
67+
- **value**: current array element.
68+
- **idx**: current array element index.
69+
- **array**: input ndarray.
70+
71+
To set the callback execution context, provide a `thisArg`.
72+
73+
```javascript
74+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
75+
76+
function clbk( value ) {
77+
this.count += 1;
78+
return value * 2.0;
79+
}
80+
81+
var xbuf = [ 1.0, 3.0, 4.0, 2.0 ];
82+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
83+
var ctx = {
84+
'count': 0
85+
};
86+
87+
var v = midrangeBy( [ x ], clbk, ctx );
88+
// returns 5.0
89+
90+
var count = ctx.count;
91+
// returns 4
92+
```
93+
94+
</section>
95+
96+
<!-- /.usage -->
97+
98+
<section class="notes">
99+
100+
## Notes
101+
102+
- If provided an empty one-dimensional ndarray, the function returns `NaN`.
103+
- A provided callback function should return a numeric value.
104+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
105+
106+
</section>
107+
108+
<!-- /.notes -->
109+
110+
<section class="examples">
111+
112+
## Examples
113+
114+
<!-- eslint no-undef: "error" -->
115+
116+
```javascript
117+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
118+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
119+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
120+
var midrangeBy = require( '@stdlib/stats/base/ndarray/midrange-by' );
121+
122+
function clbk( value ) {
123+
return value * 2.0;
124+
}
125+
126+
var xbuf = discreteUniform( 10, -50, 50, {
127+
'dtype': 'generic'
128+
});
129+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
130+
console.log( ndarray2array( x ) );
131+
132+
var v = midrangeBy( [ x ], clbk );
133+
console.log( v );
134+
```
135+
136+
</section>
137+
138+
<!-- /.examples -->
139+
140+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
141+
142+
<section class="related">
143+
144+
</section>
145+
146+
<!-- /.related -->
147+
148+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
149+
150+
<section class="links">
151+
152+
[mid-range]: https://en.wikipedia.org/wiki/Mid-range
153+
154+
</section>
155+
156+
<!-- /.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 ndarray = require( '@stdlib/ndarray/base/ctor' );
29+
var pkg = require( './../package.json' ).name;
30+
var midrangeBy = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Callback function.
44+
*
45+
* @private
46+
* @param {number} value - array element
47+
* @returns {number} callback result
48+
*/
49+
function clbk( value ) {
50+
return value * 2.0;
51+
}
52+
53+
/**
54+
* Creates a benchmark function.
55+
*
56+
* @private
57+
* @param {PositiveInteger} len - array length
58+
* @returns {Function} benchmark function
59+
*/
60+
function createBenchmark( len ) {
61+
var xbuf;
62+
var x;
63+
64+
xbuf = uniform( len, -10.0, 10.0, options );
65+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
66+
67+
return benchmark;
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 ], clbk );
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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
{{alias}}( arrays, clbk[, thisArg] )
3+
Computes the mid-range of a one-dimensional ndarray via a callback function.
4+
5+
If provided an empty ndarray, 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 ndarray.
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+
arrays: ArrayLikeObject<ndarray>
21+
Array-like object containing a one-dimensional input ndarray.
22+
23+
clbk: Function
24+
Callback function.
25+
26+
thisArg: any (optional)
27+
Callback execution context.
28+
29+
Returns
30+
-------
31+
out: number
32+
Mid-range.
33+
34+
Examples
35+
--------
36+
> var xbuf = [ 1.0, -2.0, 2.0 ];
37+
> var dt = 'generic';
38+
> var sh = [ xbuf.length ];
39+
> var sx = [ 1 ];
40+
> var ox = 0;
41+
> var ord = 'row-major';
42+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
43+
> function f ( v ) { return v * 2.0; };
44+
> {{alias}}( [ x ], f )
45+
0.0
46+
47+
See Also
48+
--------
49+

0 commit comments

Comments
 (0)