Skip to content

Commit 0734cda

Browse files
committed
Auto-generated commit
1 parent d94e6ac commit 0734cda

File tree

11 files changed

+1074
-0
lines changed

11 files changed

+1074
-0
lines changed

CHANGELOG.md

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

1111
### Features
1212

13+
- [`9fd602c`](https://github.com/stdlib-js/stdlib/commit/9fd602c0962f9c7f2bcb3783b1b65aa9b8c517bc) - add `stats/base/ndarray/nanrange-by` [(#8737)](https://github.com/stdlib-js/stdlib/pull/8737)
1314
- [`1708e83`](https://github.com/stdlib-js/stdlib/commit/1708e83cf99c01a7b8294b82d993bf52cf9bd311) - add `stats/base/ndarray/nanmskmin` [(#8734)](https://github.com/stdlib-js/stdlib/pull/8734)
1415
- [`8e2937d`](https://github.com/stdlib-js/stdlib/commit/8e2937d8b29d34d294c62b64257070312e7d336e) - add `stats/base/ndarray/nanmskmax` [(#8732)](https://github.com/stdlib-js/stdlib/pull/8732)
1516
- [`b74955d`](https://github.com/stdlib-js/stdlib/commit/b74955d44608b40c744e1854739fc957758df872) - add `stats/range-by` [(#8723)](https://github.com/stdlib-js/stdlib/pull/8723)
@@ -3496,6 +3497,7 @@ A total of 557 issues were closed in this release:
34963497

34973498
<details>
34983499

3500+
- [`9fd602c`](https://github.com/stdlib-js/stdlib/commit/9fd602c0962f9c7f2bcb3783b1b65aa9b8c517bc) - **feat:** add `stats/base/ndarray/nanrange-by` [(#8737)](https://github.com/stdlib-js/stdlib/pull/8737) _(by Sachin Pangal)_
34993501
- [`1708e83`](https://github.com/stdlib-js/stdlib/commit/1708e83cf99c01a7b8294b82d993bf52cf9bd311) - **feat:** add `stats/base/ndarray/nanmskmin` [(#8734)](https://github.com/stdlib-js/stdlib/pull/8734) _(by Kaustubh Patange, Athan Reines)_
35003502
- [`8e2937d`](https://github.com/stdlib-js/stdlib/commit/8e2937d8b29d34d294c62b64257070312e7d336e) - **feat:** add `stats/base/ndarray/nanmskmax` [(#8732)](https://github.com/stdlib-js/stdlib/pull/8732) _(by Kaustubh Patange)_
35013503
- [`b74955d`](https://github.com/stdlib-js/stdlib/commit/b74955d44608b40c744e1854739fc957758df872) - **feat:** add `stats/range-by` [(#8723)](https://github.com/stdlib-js/stdlib/pull/8723) _(by Sachin Pangal, Athan Reines)_

base/ndarray/nanrange-by/README.md

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) 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+
# nanrangeBy
22+
23+
> Calculate the [range][range] of a one-dimensional ndarray via a callback function, ignoring `NaN` values.
24+
25+
<section class="intro">
26+
27+
The [**range**][range] is defined as the difference between the maximum and minimum values.
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<section class="usage">
34+
35+
## Usage
36+
37+
```javascript
38+
var nanrangeBy = require( '@stdlib/stats/base/ndarray/nanrange-by' );
39+
```
40+
41+
#### nanrangeBy( arrays, clbk\[, thisArg ] )
42+
43+
Computes the [range][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 = nanrangeBy( [ x ], clbk );
56+
// returns 8.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 = nanrangeBy( [ x ], clbk, ctx );
88+
// returns 8.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 nanrangeBy = require( '@stdlib/stats/base/ndarray/nanrange-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 = nanrangeBy( [ 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+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
161+
162+
</section>
163+
164+
<!-- /.links -->
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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/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 ndarray = require( '@stdlib/ndarray/base/ctor' );
30+
var pkg = require( './../package.json' ).name;
31+
var nanrangeBy = require( './../lib' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Callback function.
38+
*
39+
* @private
40+
* @param {number} value - array element
41+
* @returns {number} callback result
42+
*/
43+
function clbk( value ) {
44+
return value * 2.0;
45+
}
46+
47+
/**
48+
* Returns a random number.
49+
*
50+
* @private
51+
* @returns {number} random number or `NaN`
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 xbuf;
69+
var x;
70+
71+
xbuf = filledarrayBy( len, 'generic', rand );
72+
x = new ndarray( 'generic', xbuf, [ len ], [ 1 ], 0, 'row-major' );
73+
74+
return benchmark;
75+
76+
function benchmark( b ) {
77+
var v;
78+
var i;
79+
80+
b.tic();
81+
for ( i = 0; i < b.iterations; i++ ) {
82+
v = nanrangeBy( [ x ], clbk );
83+
if ( isnan( v ) ) {
84+
b.fail( 'should not return NaN' );
85+
}
86+
}
87+
b.toc();
88+
if ( isnan( v ) ) {
89+
b.fail( 'should not return NaN' );
90+
}
91+
b.pass( 'benchmark finished' );
92+
b.end();
93+
}
94+
}
95+
96+
97+
// MAIN //
98+
99+
/**
100+
* Main execution sequence.
101+
*
102+
* @private
103+
*/
104+
function main() {
105+
var len;
106+
var min;
107+
var max;
108+
var f;
109+
var i;
110+
111+
min = 1; // 10^min
112+
max = 6; // 10^max
113+
114+
for ( i = min; i <= max; i++ ) {
115+
len = pow( 10, i );
116+
f = createBenchmark( len );
117+
bench( pkg+':len='+len, f );
118+
}
119+
}
120+
121+
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 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+
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+
8.0
49+
50+
See Also
51+
--------
52+

0 commit comments

Comments
 (0)