Skip to content

Commit de01991

Browse files
committed
Auto-generated commit
1 parent 9f2f3c2 commit de01991

File tree

13 files changed

+1034
-7
lines changed

13 files changed

+1034
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`aa31c82`](https://github.com/stdlib-js/stdlib/commit/aa31c82f3d919244c5393ae7a8b7331355eb2e17) - add `stats/base/ndarray/nanmidrange-by` [(#9501)](https://github.com/stdlib-js/stdlib/pull/9501)
1314
- [`6986d92`](https://github.com/stdlib-js/stdlib/commit/6986d92dd402cf96c394abfea5340be84184d2c1) - add `stats/base/ndarray/midrange-by` [(#9512)](https://github.com/stdlib-js/stdlib/pull/9512)
1415
- [`c216b80`](https://github.com/stdlib-js/stdlib/commit/c216b80baf3efe0d21387a280368c44122328b76) - add `stats/base/ndarray/smskmidrange` [(#9519)](https://github.com/stdlib-js/stdlib/pull/9519)
1516
- [`d681872`](https://github.com/stdlib-js/stdlib/commit/d681872a9b87321f4f66852f5aabfeec19e195fc) - add `stats/base/ndarray/mskmidrange` [(#9511)](https://github.com/stdlib-js/stdlib/pull/9511)
@@ -3553,6 +3554,8 @@ A total of 562 issues were closed in this release:
35533554

35543555
<details>
35553556

3557+
- [`948e04b`](https://github.com/stdlib-js/stdlib/commit/948e04bf760415e316dbf802d78d82478ca72b2c) - **docs:** update examples _(by Athan Reines)_
3558+
- [`aa31c82`](https://github.com/stdlib-js/stdlib/commit/aa31c82f3d919244c5393ae7a8b7331355eb2e17) - **feat:** add `stats/base/ndarray/nanmidrange-by` [(#9501)](https://github.com/stdlib-js/stdlib/pull/9501) _(by Sachin Pangal, Athan Reines)_
35563559
- [`6986d92`](https://github.com/stdlib-js/stdlib/commit/6986d92dd402cf96c394abfea5340be84184d2c1) - **feat:** add `stats/base/ndarray/midrange-by` [(#9512)](https://github.com/stdlib-js/stdlib/pull/9512) _(by Sachin Pangal)_
35573560
- [`3af0a04`](https://github.com/stdlib-js/stdlib/commit/3af0a041f6ae95c251cbfa2acae82b30e3b4579b) - **fix:** return `NaN` when the number of indexed elements is zero in `stats/strided/distances/dcosine-similarity` [(#9497)](https://github.com/stdlib-js/stdlib/pull/9497) _(by Nakul Krishnakumar)_
35583561
- [`c216b80`](https://github.com/stdlib-js/stdlib/commit/c216b80baf3efe0d21387a280368c44122328b76) - **feat:** add `stats/base/ndarray/smskmidrange` [(#9519)](https://github.com/stdlib-js/stdlib/pull/9519) _(by Sachin Pangal, Athan Reines)_
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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 [mid-range][mid-range] of a one-dimensional ndarray via a callback function, ignoring `NaN` values.
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 nanmidrangeBy = require( '@stdlib/stats/base/ndarray/nanmidrange-by' );
39+
```
40+
41+
#### nanmidrangeBy( arrays, clbk\[, thisArg ] )
42+
43+
Calculates the [mid-range][mid-range] of a one-dimensional ndarray via a callback function, ignoring `NaN` values.
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, -2.0, NaN, 2.0 ];
53+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
54+
55+
var v = nanmidrangeBy( [ x ], clbk );
56+
// returns 0.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, -2.0, NaN, 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 = nanmidrangeBy( [ x ], clbk, ctx );
88+
// returns 0.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 returns `NaN`, the value is ignored.
105+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
106+
107+
</section>
108+
109+
<!-- /.notes -->
110+
111+
<section class="examples">
112+
113+
## Examples
114+
115+
<!-- eslint no-undef: "error" -->
116+
117+
```javascript
118+
var uniform = require( '@stdlib/random/base/uniform' );
119+
var filledarrayBy = require( '@stdlib/array/filled-by' );
120+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
121+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
122+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
123+
var nanmidrangeBy = require( '@stdlib/stats/base/ndarray/nanmidrange-by' );
124+
125+
function clbk( value ) {
126+
return value * 2.0;
127+
}
128+
129+
function rand() {
130+
if ( bernoulli( 0.8 ) < 1 ) {
131+
return NaN;
132+
}
133+
return uniform( -50.0, 50.0 );
134+
}
135+
136+
var xbuf = filledarrayBy( 10, 'generic', rand );
137+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
138+
console.log( ndarray2array( x ) );
139+
140+
var v = nanmidrangeBy( [ x ], clbk );
141+
console.log( v );
142+
```
143+
144+
</section>
145+
146+
<!-- /.examples -->
147+
148+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
149+
150+
<section class="related">
151+
152+
</section>
153+
154+
<!-- /.related -->
155+
156+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
157+
158+
<section class="links">
159+
160+
[mid-range]: https://en.wikipedia.org/wiki/Mid-range
161+
162+
</section>
163+
164+
<!-- /.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 bernoulli = require( '@stdlib/random/base/bernoulli' );
26+
var filledarrayBy = require( '@stdlib/array/filled-by' );
27+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
28+
var pow = require( '@stdlib/math/base/special/pow' );
29+
var format = require( '@stdlib/string/format' );
30+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
31+
var pkg = require( './../package.json' ).name;
32+
var nanmidrangeBy = require( './../lib' );
33+
34+
35+
// FUNCTIONS //
36+
37+
/**
38+
* Callback function.
39+
*
40+
* @private
41+
* @param {number} value - array element
42+
* @returns {number} callback result
43+
*/
44+
function clbk( value ) {
45+
return value * 2.0;
46+
}
47+
48+
/**
49+
* Returns a random number.
50+
*
51+
* @private
52+
* @returns {number} random number or `NaN`
53+
*/
54+
function rand() {
55+
if ( bernoulli( 0.8 ) < 1 ) {
56+
return NaN;
57+
}
58+
return uniform( -10.0, 10.0 );
59+
}
60+
61+
/**
62+
* Creates a benchmark function.
63+
*
64+
* @private
65+
* @param {PositiveInteger} len - array length
66+
* @returns {Function} benchmark function
67+
*/
68+
function createBenchmark( len ) {
69+
var xbuf;
70+
var x;
71+
72+
xbuf = filledarrayBy( len, 'generic', rand );
73+
x = new ndarray( 'generic', xbuf, [ len ], [ 1 ], 0, 'row-major' );
74+
75+
return benchmark;
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 ], clbk );
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: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
{{alias}}( arrays, clbk[, thisArg] )
3+
Computes the mid-range of a one-dimensional ndarray via a callback function,
4+
ignoring `NaN` values.
5+
6+
If provided an empty ndarray, 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 ndarray.
13+
14+
The callback function should return a numeric value.
15+
16+
If the callback function does not return any value (or equivalently,
17+
explicitly returns `undefined`), the value is ignored.
18+
19+
If the callback function returns `NaN`, the value is ignored.
20+
21+
Parameters
22+
----------
23+
arrays: ArrayLikeObject<ndarray>
24+
Array-like object containing a one-dimensional input ndarray.
25+
26+
clbk: Function
27+
Callback function.
28+
29+
thisArg: any (optional)
30+
Callback execution context.
31+
32+
Returns
33+
-------
34+
out: number
35+
Mid-range.
36+
37+
Examples
38+
--------
39+
> var xbuf = [ 1.0, -2.0, NaN, 2.0 ];
40+
> var dt = 'generic';
41+
> var sh = [ xbuf.length ];
42+
> var sx = [ 1 ];
43+
> var ox = 0;
44+
> var ord = 'row-major';
45+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
46+
> function f ( v ) { return v * 2.0; };
47+
> {{alias}}( [ x ], f )
48+
0.0
49+
50+
See Also
51+
--------
52+

0 commit comments

Comments
 (0)