Skip to content

Commit 10b28fd

Browse files
Om-A-oscPlaneshifterSachinn-64
authored
feat: add stats/array/nanmidrange-by
PR-URL: stdlib-js#10273 Co-authored-by: Philipp Burckhardt <pburckhardt@outlook.com> Co-authored-by: Sachin Pangal <151670745+Sachinn-64@users.noreply.github.com> Reviewed-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Philipp Burckhardt <pburckhardt@outlook.com> Signed-off-by: Sachin Pangal <151670745+Sachinn-64@users.noreply.github.com>
1 parent 5360fda commit 10b28fd

File tree

10 files changed

+1027
-0
lines changed

10 files changed

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

0 commit comments

Comments
 (0)