Skip to content

Commit b74955d

Browse files
Orthodox-64kgryte
andauthored
feat: add stats/range-by
PR-URL: #8723 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent 2192360 commit b74955d

File tree

13 files changed

+3967
-0
lines changed

13 files changed

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

0 commit comments

Comments
 (0)