Skip to content

Commit c034496

Browse files
committed
Auto-generated commit
1 parent a59f89a commit c034496

File tree

14 files changed

+2677
-0
lines changed

14 files changed

+2677
-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+
- [`2c30c8f`](https://github.com/stdlib-js/stdlib/commit/2c30c8fccde4aa82d60552050b8a876ffac9bc6d) - add `stats/midrange` [(#9333)](https://github.com/stdlib-js/stdlib/pull/9333)
1314
- [`f04a31d`](https://github.com/stdlib-js/stdlib/commit/f04a31de84e7201d3cb54cc39a19eee84f49b407) - add `stats/base/ndarray/stdevch` [(#9327)](https://github.com/stdlib-js/stdlib/pull/9327)
1415
- [`2447f62`](https://github.com/stdlib-js/stdlib/commit/2447f62ca5393d6bfcfb487915efb58797867c10) - add `stats/base/ndarray/sstdev` [(#9341)](https://github.com/stdlib-js/stdlib/pull/9341)
1516
- [`ffee9eb`](https://github.com/stdlib-js/stdlib/commit/ffee9eb167e2f3b1750163bf9f9937f7e9db48d3) - add `stats/strided/mskmidrange` [(#9331)](https://github.com/stdlib-js/stdlib/pull/9331)
@@ -3531,6 +3532,7 @@ A total of 559 issues were closed in this release:
35313532

35323533
<details>
35333534

3535+
- [`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)_
35343536
- [`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)_
35353537
- [`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)_
35363538
- [`d94f831`](https://github.com/stdlib-js/stdlib/commit/d94f8313eb8c0a08d2963a938c96a5f22a5f666e) - **test:** fix message _(by Athan Reines)_

midrange/README.md

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
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+
# midrange
22+
23+
> Compute the [mid-range][mid-range] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
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 midrange = require( '@stdlib/stats/midrange' );
39+
```
40+
41+
#### midrange( x\[, options] )
42+
43+
Computes the [mid-range][mid-range] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions.
44+
45+
```javascript
46+
var array = require( '@stdlib/ndarray/array' );
47+
48+
var x = array( [ -1.0, 2.0, -3.0 ] );
49+
50+
var y = midrange( x );
51+
// returns <ndarray>[ -0.5 ]
52+
```
53+
54+
The function has the following parameters:
55+
56+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or "generic" [data type][@stdlib/ndarray/dtypes].
57+
- **options**: function options (_optional_).
58+
59+
The function accepts the following options:
60+
61+
- **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].
62+
- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. Must be a real-valued floating-point or "generic" [data type][@stdlib/ndarray/dtypes].
63+
- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`.
64+
65+
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.
66+
67+
```javascript
68+
var array = require( '@stdlib/ndarray/array' );
69+
70+
var x = array( [ -1.0, 2.0, -3.0, 4.0 ], {
71+
'shape': [ 2, 2 ],
72+
'order': 'row-major'
73+
});
74+
// returns <ndarray>[ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ]
75+
76+
var y = midrange( x, {
77+
'dims': [ 0 ]
78+
});
79+
// returns <ndarray>[ -2.0, 3.0 ]
80+
81+
y = midrange( x, {
82+
'dims': [ 1 ]
83+
});
84+
// returns <ndarray>[ 0.5, 0.5 ]
85+
86+
y = midrange( x, {
87+
'dims': [ 0, 1 ]
88+
});
89+
// returns <ndarray>[ 0.5 ]
90+
```
91+
92+
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`.
93+
94+
```javascript
95+
var array = require( '@stdlib/ndarray/array' );
96+
97+
var x = array( [ -1.0, 2.0, -3.0, 4.0 ], {
98+
'shape': [ 2, 2 ],
99+
'order': 'row-major'
100+
});
101+
// returns <ndarray>[ [ -1.0, 2.0 ], [ -3.0, 4.0 ] ]
102+
103+
var y = midrange( x, {
104+
'dims': [ 0 ],
105+
'keepdims': true
106+
});
107+
// returns <ndarray>[ [ -2.0, 3.0 ] ]
108+
109+
y = midrange( x, {
110+
'dims': [ 1 ],
111+
'keepdims': true
112+
});
113+
// returns <ndarray>[ [ 0.5 ], [ 0.5 ] ]
114+
115+
y = midrange( x, {
116+
'dims': [ 0, 1 ],
117+
'keepdims': true
118+
});
119+
// returns <ndarray>[ [ 0.5 ] ]
120+
```
121+
122+
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.
123+
124+
```javascript
125+
var getDType = require( '@stdlib/ndarray/dtype' );
126+
var array = require( '@stdlib/ndarray/array' );
127+
128+
var x = array( [ -1.0, 2.0, -3.0 ], {
129+
'dtype': 'generic'
130+
});
131+
132+
var y = midrange( x, {
133+
'dtype': 'float64'
134+
});
135+
// returns <ndarray>[ -0.5 ]
136+
137+
var dt = String( getDType( y ) );
138+
// returns 'float64'
139+
```
140+
141+
#### midrange.assign( x, out\[, options] )
142+
143+
Computes the [mid-range][mid-range] along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor].
144+
145+
```javascript
146+
var array = require( '@stdlib/ndarray/array' );
147+
var zeros = require( '@stdlib/ndarray/zeros' );
148+
149+
var x = array( [ -1.0, 2.0, -3.0 ] );
150+
var y = zeros( [] );
151+
152+
var out = midrange.assign( x, y );
153+
// returns <ndarray>[ -0.5 ]
154+
155+
var bool = ( out === y );
156+
// returns true
157+
```
158+
159+
The method has the following parameters:
160+
161+
- **x**: input [ndarray][@stdlib/ndarray/ctor]. Must have a real-valued or generic [data type][@stdlib/ndarray/dtypes].
162+
- **out**: output [ndarray][@stdlib/ndarray/ctor].
163+
- **options**: function options (_optional_).
164+
165+
The method accepts the following options:
166+
167+
- **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].
168+
169+
</section>
170+
171+
<!-- /.usage -->
172+
173+
<section class="notes">
174+
175+
## Notes
176+
177+
- 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].
178+
- 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 floating-point 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].
179+
180+
</section>
181+
182+
<!-- /.notes -->
183+
184+
<section class="examples">
185+
186+
## Examples
187+
188+
<!-- eslint no-undef: "error" -->
189+
190+
```javascript
191+
var uniform = require( '@stdlib/random/uniform' );
192+
var getDType = require( '@stdlib/ndarray/dtype' );
193+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
194+
var midrange = require( '@stdlib/stats/midrange' );
195+
196+
// Generate an array of random numbers:
197+
var x = uniform( [ 5, 5 ], 0.0, 20.0 );
198+
console.log( ndarray2array( x ) );
199+
200+
// Perform a reduction:
201+
var y = midrange( x, {
202+
'dims': [ 0 ]
203+
});
204+
205+
// Resolve the output array data type:
206+
var dt = getDType( y );
207+
console.log( dt );
208+
209+
// Print the results:
210+
console.log( ndarray2array( y ) );
211+
```
212+
213+
</section>
214+
215+
<!-- /.examples -->
216+
217+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
218+
219+
<section class="related">
220+
221+
</section>
222+
223+
<!-- /.related -->
224+
225+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
226+
227+
<section class="links">
228+
229+
[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/ndarray-ctor
230+
231+
[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/ndarray-dtypes
232+
233+
[@stdlib/ndarray/output-dtype-policies]: https://github.com/stdlib-js/ndarray-output-dtype-policies
234+
235+
[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/ndarray-base-broadcast-shapes
236+
237+
[mid-range]: https://en.wikipedia.org/wiki/Mid-range
238+
239+
</section>
240+
241+
<!-- /.links -->
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var pow = require( '@stdlib/math/base/special/pow' );
26+
var format = require( '@stdlib/string/format' );
27+
var uniform = require( '@stdlib/random/array/uniform' );
28+
var zeros = require( '@stdlib/array/zeros' );
29+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
30+
var pkg = require( './../package.json' ).name;
31+
var midrange = require( './../lib' );
32+
33+
34+
// VARIABLES //
35+
36+
var options = {
37+
'dtype': 'float64'
38+
};
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {PositiveInteger} len - array length
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len ) {
51+
var out;
52+
var x;
53+
54+
x = uniform( len, -50.0, 50.0, options );
55+
x = new ndarray( options.dtype, x, [ len ], [ 1 ], 0, 'row-major' );
56+
57+
out = new ndarray( options.dtype, zeros( 1, options.dtype ), [], [ 0 ], 0, 'row-major' );
58+
59+
return benchmark;
60+
61+
/**
62+
* Benchmark function.
63+
*
64+
* @private
65+
* @param {Benchmark} b - benchmark instance
66+
*/
67+
function benchmark( b ) {
68+
var o;
69+
var i;
70+
71+
b.tic();
72+
for ( i = 0; i < b.iterations; i++ ) {
73+
o = midrange.assign( x, out );
74+
if ( typeof o !== 'object' ) {
75+
b.fail( 'should return an ndarray' );
76+
}
77+
}
78+
b.toc();
79+
if ( isnan( o.get() ) ) {
80+
b.fail( 'should not return NaN' );
81+
}
82+
b.pass( 'benchmark finished' );
83+
b.end();
84+
}
85+
}
86+
87+
88+
// MAIN //
89+
90+
/**
91+
* Main execution sequence.
92+
*
93+
* @private
94+
*/
95+
function main() {
96+
var len;
97+
var min;
98+
var max;
99+
var f;
100+
var i;
101+
102+
min = 1; // 10^min
103+
max = 6; // 10^max
104+
105+
for ( i = min; i <= max; i++ ) {
106+
len = pow( 10, i );
107+
f = createBenchmark( len );
108+
bench( format( '%s:assign:dtype=%s,len=%d', pkg, options.dtype, len ), f );
109+
}
110+
}
111+
112+
main();

0 commit comments

Comments
 (0)