Skip to content

Commit 80483a6

Browse files
committed
Auto-generated commit
1 parent c034496 commit 80483a6

File tree

16 files changed

+2227
-1
lines changed

16 files changed

+2227
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
<section class="release" id="unreleased">
66

7-
## Unreleased (2025-12-24)
7+
## Unreleased (2025-12-25)
88

99
<section class="features">
1010

1111
### Features
1212

13+
- [`65784f7`](https://github.com/stdlib-js/stdlib/commit/65784f785748107e1fc5d84ad75c9778422e8442) - add `stats/strided/midrange-by` [(#9328)](https://github.com/stdlib-js/stdlib/pull/9328)
1314
- [`2c30c8f`](https://github.com/stdlib-js/stdlib/commit/2c30c8fccde4aa82d60552050b8a876ffac9bc6d) - add `stats/midrange` [(#9333)](https://github.com/stdlib-js/stdlib/pull/9333)
1415
- [`f04a31d`](https://github.com/stdlib-js/stdlib/commit/f04a31de84e7201d3cb54cc39a19eee84f49b407) - add `stats/base/ndarray/stdevch` [(#9327)](https://github.com/stdlib-js/stdlib/pull/9327)
1516
- [`2447f62`](https://github.com/stdlib-js/stdlib/commit/2447f62ca5393d6bfcfb487915efb58797867c10) - add `stats/base/ndarray/sstdev` [(#9341)](https://github.com/stdlib-js/stdlib/pull/9341)
@@ -3532,6 +3533,7 @@ A total of 559 issues were closed in this release:
35323533

35333534
<details>
35343535

3536+
- [`65784f7`](https://github.com/stdlib-js/stdlib/commit/65784f785748107e1fc5d84ad75c9778422e8442) - **feat:** add `stats/strided/midrange-by` [(#9328)](https://github.com/stdlib-js/stdlib/pull/9328) _(by Sachin Pangal, Athan Reines)_
35353537
- [`2c30c8f`](https://github.com/stdlib-js/stdlib/commit/2c30c8fccde4aa82d60552050b8a876ffac9bc6d) - **feat:** add `stats/midrange` [(#9333)](https://github.com/stdlib-js/stdlib/pull/9333) _(by Sachin Pangal, Athan Reines, stdlib-bot)_
35363538
- [`f04a31d`](https://github.com/stdlib-js/stdlib/commit/f04a31de84e7201d3cb54cc39a19eee84f49b407) - **feat:** add `stats/base/ndarray/stdevch` [(#9327)](https://github.com/stdlib-js/stdlib/pull/9327) _(by Pratik, Athan Reines)_
35373539
- [`2447f62`](https://github.com/stdlib-js/stdlib/commit/2447f62ca5393d6bfcfb487915efb58797867c10) - **feat:** add `stats/base/ndarray/sstdev` [(#9341)](https://github.com/stdlib-js/stdlib/pull/9341) _(by Kaustubh Patange, Athan Reines)_

strided/midrange-by/README.md

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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+
# midrangeBy
22+
23+
> Calculate the [mid-range][mid-range] of a strided array via a callback function.
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 midrangeBy = require( '@stdlib/stats/strided/midrange-by' );
39+
```
40+
41+
#### midrangeBy( N, x, strideX, clbk\[, thisArg] )
42+
43+
Computes the [mid-range][mid-range] of a strided array via a callback function.
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, 0.0, -1.0, -3.0 ];
51+
52+
var v = midrangeBy( x.length, x, 1, accessor );
53+
// returns -1.0
54+
```
55+
56+
The function has the following parameters:
57+
58+
- **N**: number of indexed elements.
59+
- **x**: input [`Array`][mdn-array], [`typed array`][mdn-typed-array], or an array-like object (excluding strings and functions).
60+
- **strideX**: stride length.
61+
- **clbk**: callback function.
62+
- **thisArg**: execution context (_optional_).
63+
64+
The invoked callback is provided four arguments:
65+
66+
- **value**: array element.
67+
- **aidx**: array index.
68+
- **sidx**: strided index (`offset + aidx*stride`).
69+
- **array**: input array/collection.
70+
71+
To set the callback execution context, provide a `thisArg`.
72+
73+
```javascript
74+
function accessor( v ) {
75+
this.count += 1;
76+
return v * 2.0;
77+
}
78+
79+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
80+
81+
var context = {
82+
'count': 0
83+
};
84+
85+
var v = midrangeBy( x.length, x, 1, accessor, context );
86+
// returns -1.0
87+
88+
var cnt = context.count;
89+
// returns 8
90+
```
91+
92+
The `N` and stride parameters determine which elements in the strided array are accessed at runtime. For example, to access every other element
93+
94+
```javascript
95+
function accessor( v ) {
96+
return v * 2.0;
97+
}
98+
99+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
100+
101+
var v = midrangeBy( 4, x, 2, accessor );
102+
// returns 2.0
103+
```
104+
105+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
106+
107+
```javascript
108+
var Float64Array = require( '@stdlib/array/float64' );
109+
110+
function accessor( v ) {
111+
return v * 2.0;
112+
}
113+
114+
// Initial array...
115+
var x0 = new Float64Array( [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ] );
116+
117+
// Create an offset view...
118+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
119+
120+
// Access every other element...
121+
var v = midrangeBy( 3, x1, 2, accessor );
122+
// returns -8.0
123+
```
124+
125+
#### midrangeBy.ndarray( N, x, strideX, offsetX, clbk\[, thisArg] )
126+
127+
Computes the [mid-range][mid-range] of a strided array via a callback function and using alternative indexing semantics.
128+
129+
```javascript
130+
function accessor( v ) {
131+
return v * 2.0;
132+
}
133+
134+
var x = [ -2.0, 1.0, 3.0, -5.0, 4.0, 0.0, -1.0, -3.0 ];
135+
136+
var v = midrangeBy.ndarray( x.length, x, 1, 0, accessor );
137+
// returns -1.0
138+
```
139+
140+
The function has the following additional parameters:
141+
142+
- **offsetX**: starting index.
143+
144+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example, to access only the last three elements of `x`
145+
146+
```javascript
147+
function accessor( v ) {
148+
return v * 2.0;
149+
}
150+
151+
var x = [ 1.0, -2.0, 3.0, -4.0, 5.0, -6.0 ];
152+
153+
var v = midrangeBy.ndarray( 3, x, 1, x.length-3, accessor );
154+
// returns -1.0
155+
```
156+
157+
</section>
158+
159+
<!-- /.usage -->
160+
161+
<section class="notes">
162+
163+
## Notes
164+
165+
- If `N <= 0`, both functions return `NaN`.
166+
- A provided callback function should return a numeric value.
167+
- If a provided callback function does not return any value (or equivalently, explicitly returns `undefined`), the value is **ignored**.
168+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
169+
- When possible, prefer using [`dmidrange`][@stdlib/stats/strided/dmidrange], [`smidrange`][@stdlib/stats/strided/smidrange], and/or [`midrange`][@stdlib/stats/strided/midrange], as, depending on the environment, these interfaces are likely to be significantly more performant.
170+
171+
</section>
172+
173+
<!-- /.notes -->
174+
175+
<section class="examples">
176+
177+
## Examples
178+
179+
<!-- eslint no-undef: "error" -->
180+
181+
```javascript
182+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
183+
var midrangeBy = require( '@stdlib/stats/strided/midrange-by' );
184+
185+
function accessor( v ) {
186+
return v * 2.0;
187+
}
188+
189+
var x = discreteUniform( 10, -50, 50, {
190+
'dtype': 'float64'
191+
});
192+
console.log( x );
193+
194+
var v = midrangeBy( x.length, x, 1, accessor );
195+
console.log( v );
196+
```
197+
198+
</section>
199+
200+
<!-- /.examples -->
201+
202+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
203+
204+
<section class="related">
205+
206+
</section>
207+
208+
<!-- /.related -->
209+
210+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
211+
212+
<section class="links">
213+
214+
[mid-range]: https://en.wikipedia.org/wiki/Mid-range
215+
216+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
217+
218+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
219+
220+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/array-base-accessor
221+
222+
[@stdlib/stats/strided/midrange]: https://github.com/stdlib-js/stats/tree/main/strided/midrange
223+
224+
[@stdlib/stats/strided/dmidrange]: https://github.com/stdlib-js/stats/tree/main/strided/dmidrange
225+
226+
[@stdlib/stats/strided/smidrange]: https://github.com/stdlib-js/stats/tree/main/strided/smidrange
227+
228+
<!-- <related-links> -->
229+
230+
<!-- </related-links> -->
231+
232+
</section>
233+
234+
<!-- /.links -->
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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/array/uniform' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var format = require( '@stdlib/string/format' );
28+
var pkg = require( './../package.json' ).name;
29+
var midrangeBy = require( './../lib/main.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var options = {
35+
'dtype': 'generic'
36+
};
37+
38+
39+
// FUNCTIONS //
40+
41+
/**
42+
* Accessor function.
43+
*
44+
* @private
45+
* @param {number} value - array element
46+
* @returns {number} accessed value
47+
*/
48+
function accessor( value ) {
49+
return value * 2.0;
50+
}
51+
52+
/**
53+
* Create a benchmark function.
54+
*
55+
* @private
56+
* @param {PositiveInteger} len - array length
57+
* @returns {Function} benchmark function
58+
*/
59+
function createBenchmark( len ) {
60+
var x = uniform( len, -10, 10, options );
61+
return benchmark;
62+
63+
function benchmark( b ) {
64+
var v;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
v = midrangeBy( x.length, x, 1, accessor );
70+
if ( isnan( v ) ) {
71+
b.fail( 'should not return NaN' );
72+
}
73+
}
74+
b.toc();
75+
if ( isnan( v ) ) {
76+
b.fail( 'should not return NaN' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
function main() {
87+
var len;
88+
var min;
89+
var max;
90+
var f;
91+
var i;
92+
93+
min = 1; // 10^min
94+
max = 6; // 10^max
95+
96+
for ( i = min; i <= max; i++ ) {
97+
len = pow( 10, i );
98+
f = createBenchmark( len );
99+
bench( format( '%s:len=%d', pkg, len ), f );
100+
}
101+
}
102+
103+
main();

0 commit comments

Comments
 (0)