Skip to content

Commit 769ab89

Browse files
Orthodox-64kgryte
andauthored
feat: add stats/nanrange-by
PR-URL: #9006 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 2067ee5 commit 769ab89

File tree

13 files changed

+3966
-0
lines changed

13 files changed

+3966
-0
lines changed
Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
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+
> Compute the [**range**][range] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions according to 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/nanrange-by' );
39+
```
40+
41+
#### nanrangeBy( x\[, options], clbk\[, thisArg] )
42+
43+
Computes the [**range**][range] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions according to a callback function, ignoring `NaN` values.
44+
45+
```javascript
46+
var array = require( '@stdlib/ndarray/array' );
47+
48+
var x = array( [ -1.0, 2.0, NaN ] );
49+
50+
function clbk( v ) {
51+
return v * 2.0;
52+
}
53+
54+
var y = nanrangeBy( x, clbk );
55+
// returns <ndarray>[ 6.0 ]
56+
```
57+
58+
The function has the following parameters:
59+
60+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
61+
- **options**: function options (_optional_).
62+
- **clbk**: callback function.
63+
- **thisArg**: callback function execution context (_optional_).
64+
65+
The invoked callback is provided three arguments:
66+
67+
- **value**: current array element.
68+
- **index**: current array element index.
69+
- **array**: input ndarray.
70+
71+
To set the callback execution context, provide a `thisArg`.
72+
73+
<!-- eslint-disable no-invalid-this -->
74+
75+
```javascript
76+
var array = require( '@stdlib/ndarray/array' );
77+
78+
var x = array( [ -1.0, 2.0, NaN ] );
79+
80+
function clbk( v ) {
81+
this.count += 1;
82+
return v * 2.0;
83+
}
84+
85+
var ctx = {
86+
'count': 0
87+
};
88+
var y = nanrangeBy( x, clbk, ctx );
89+
// returns <ndarray>[ 6.0 ]
90+
91+
var count = ctx.count;
92+
// returns 3
93+
```
94+
95+
The function accepts the following options:
96+
97+
- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
98+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
99+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
100+
101+
By default, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform a reduction over specific dimensions, provide a `dims` option.
102+
103+
```javascript
104+
var array = require( '@stdlib/ndarray/array' );
105+
106+
function clbk( v ) {
107+
return v * 100.0;
108+
}
109+
110+
var x = array( [ -1.0, 2.0, -3.0, 4.0 ], {
111+
'shape': [ 2, 2 ],
112+
'order': 'row-major'
113+
});
114+
// returns <ndarray>[ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ]
115+
116+
var opts = {
117+
'dims': [ 0 ]
118+
};
119+
var y = nanrangeBy( x, opts, clbk );
120+
// returns <ndarray>[ 200.0, 200.0 ]
121+
122+
opts = {
123+
'dims': [ 1 ]
124+
};
125+
y = nanrangeBy( x, opts, clbk );
126+
// returns <ndarray>[ 300.0, 700.0 ]
127+
128+
opts = {
129+
'dims': [ 0, 1 ]
130+
};
131+
y = nanrangeBy( x, opts, clbk );
132+
// returns <ndarray>[ 700.0 ]
133+
```
134+
135+
By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`.
136+
137+
```javascript
138+
var array = require( '@stdlib/ndarray/array' );
139+
140+
function clbk( v ) {
141+
return v * 100.0;
142+
}
143+
144+
var x = array( [ -1.0, 2.0, -3.0, 4.0 ], {
145+
'shape': [ 2, 2 ],
146+
'order': 'row-major'
147+
});
148+
// returns <ndarray>[ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ]
149+
150+
var opts = {
151+
'dims': [ 0 ],
152+
'keepdims': true
153+
};
154+
var y = nanrangeBy( x, opts, clbk );
155+
// returns <ndarray>[ [ 200.0, 200.0 ] ]
156+
157+
opts = {
158+
'dims': [ 1 ],
159+
'keepdims': true
160+
};
161+
y = nanrangeBy( x, opts, clbk );
162+
// returns <ndarray>[ [ 300.0 ], [ 700.0 ] ]
163+
164+
opts = {
165+
'dims': [ 0, 1 ],
166+
'keepdims': true
167+
};
168+
y = nanrangeBy( x, opts, clbk );
169+
// returns <ndarray>[ [ 700.0 ] ]
170+
```
171+
172+
By default, the function returns an [ndarray][@stdlib/ndarray/ctor] having a [data type][@stdlib/ndarray/dtypes] determined by the function's output data type [policy][@stdlib/ndarray/output-dtype-policies]. To override the default behavior, set the `dtype` option.
173+
174+
```javascript
175+
var getDType = require( '@stdlib/ndarray/dtype' );
176+
var array = require( '@stdlib/ndarray/array' );
177+
178+
function clbk( v ) {
179+
return v * 100.0;
180+
}
181+
182+
var x = array( [ -1.0, 2.0, -3.0 ], {
183+
'dtype': 'generic'
184+
});
185+
186+
var opts = {
187+
'dtype': 'float64'
188+
};
189+
var y = nanrangeBy( x, opts, clbk );
190+
// returns <ndarray>[ 500.0 ]
191+
192+
var dt = String( getDType( y ) );
193+
// returns 'float64'
194+
```
195+
196+
#### nanrangeBy.assign( x, out\[, options], clbk\[, thisArg] )
197+
198+
Computes the [**range**][range] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions according to a callback function, ignoring `NaN` values, and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
199+
200+
```javascript
201+
var array = require( '@stdlib/ndarray/array' );
202+
var zeros = require( '@stdlib/ndarray/zeros' );
203+
204+
function clbk( v ) {
205+
return v * 100.0;
206+
}
207+
208+
var x = array( [ -1.0, 2.0, NaN ] );
209+
var y = zeros( [] );
210+
211+
var out = nanrangeBy.assign( x, y, clbk );
212+
// returns <ndarray>[ 300.0 ]
213+
214+
var bool = ( out === y );
215+
// returns true
216+
```
217+
218+
The method has the following parameters:
219+
220+
- **x**: input [ndarray][@stdlib/ndarray/ctor].
221+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
222+
- **options**: function options (_optional_).
223+
- **clbk**: callback function.
224+
- **thisArg**: callback execution context (_optional_).
225+
226+
The method accepts the following options:
227+
228+
- **dims**: list of dimensions over which to perform a reduction. If not provided, the function performs a reduction over all elements in a provided input [ndarray][@stdlib/ndarray/ctor].
229+
230+
</section>
231+
232+
<!-- /.usage -->
233+
234+
<section class="notes">
235+
236+
## Notes
237+
238+
- A provided callback function should return a numeric value.
239+
- If a provided callback function returns `NaN`, the value is ignored.
240+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
241+
- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor].
242+
- The output data type [policy][@stdlib/ndarray/output-dtype-policies] only applies to the main function and specifies that, by default, the function must return an [ndarray][@stdlib/ndarray/ctor] having a real-valued or "generic" [data type][@stdlib/ndarray/dtypes]. For the `assign` method, the output [ndarray][@stdlib/ndarray/ctor] is allowed to have any supported output [data type][@stdlib/ndarray/dtypes].
243+
244+
</section>
245+
246+
<!-- /.notes -->
247+
248+
<section class="examples">
249+
250+
## Examples
251+
252+
<!-- eslint no-undef: "error" -->
253+
254+
```javascript
255+
var filledarrayBy = require( '@stdlib/array/filled-by' );
256+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
257+
var getDType = require( '@stdlib/ndarray/dtype' );
258+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
259+
var ndarray = require( '@stdlib/ndarray/ctor' );
260+
var nanrangeBy = require( '@stdlib/stats/nanrange-by' );
261+
262+
// Define a function for generating an object having a random value:
263+
function random() {
264+
return {
265+
'value': discreteUniform( 0, 20 )
266+
};
267+
}
268+
269+
// Generate an array of random objects:
270+
var xbuf = filledarrayBy( 25, 'generic', random );
271+
272+
// Wrap in an ndarray:
273+
var x = new ndarray( 'generic', xbuf, [ 5, 5 ], [ 5, 1 ], 0, 'row-major' );
274+
console.log( ndarray2array( x ) );
275+
276+
// Define an accessor function:
277+
function accessor( v ) {
278+
return v.value * 100;
279+
}
280+
281+
// Perform a reduction:
282+
var opts = {
283+
'dims': [ 0 ]
284+
};
285+
var y = nanrangeBy( x, opts, accessor );
286+
287+
// Resolve the output array data type:
288+
var dt = getDType( y );
289+
console.log( dt );
290+
291+
// Print the results:
292+
console.log( ndarray2array( y ) );
293+
```
294+
295+
</section>
296+
297+
<!-- /.examples -->
298+
299+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
300+
301+
<section class="related">
302+
303+
</section>
304+
305+
<!-- /.related -->
306+
307+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
308+
309+
<section class="links">
310+
311+
[range]: https://en.wikipedia.org/wiki/Range_%28statistics%29
312+
313+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor
314+
315+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes
316+
317+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/output-dtype-policies
318+
319+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes
320+
321+
</section>
322+
323+
<!-- /.links -->

0 commit comments

Comments
 (0)