Skip to content

Commit fd5cf0f

Browse files
docs(ndarray-dmidrange): update readme, examples, and doctest output for midrange implementation
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 82bee78 commit fd5cf0f

File tree

11 files changed

+816
-0
lines changed

11 files changed

+816
-0
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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+
# dmidrange
22+
23+
> Compute the [midrange][midrange] of a one-dimensional double-precision floating-point ndarray, ignoring `NaN` values .
24+
25+
<section class="intro">
26+
27+
The [midrange][midrange] is defined as
28+
29+
<!-- <equation class="equation" label="eq:midrange" align="center" raw="\mathrm{midrange} = \frac{\max(x) + \min(x)}{2}" alt="Equation for the midrange."> -->
30+
31+
```math
32+
\mathrm{midrange} = \frac{\max(x) + \min(x)}{2}
33+
```
34+
35+
<!-- <div class="equation" align="center" data-raw-text="\mathrm{midrange} = \frac{\max(x) + \min(x)}{2}" data-equation="eq:midrange"> <img src="img/equation_midrange.svg" alt="Equation for the midrange."> <br> </div> -->
36+
37+
<!-- </equation> -->
38+
39+
</section>
40+
41+
<!-- /.intro -->
42+
43+
<section class="usage">
44+
45+
## Usage
46+
47+
```javascript
48+
var dmidrange = require( '@stdlib/stats/base/ndarray/dmidrange' );
49+
```
50+
51+
#### dmidrange( arrays )
52+
53+
Computes the [midrange][midrange] of a one-dimensional double-precision floating-point ndarray, ignoring `NaN` values.
54+
55+
```javascript
56+
var Float64Array = require( '@stdlib/array/float64' );
57+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
58+
59+
var xbuf = new Float64Array( [ 1.0, -2.0, 2.0, 2.0 ] );
60+
var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
61+
62+
var v = dmidrange( [ x ] );
63+
// returns 0.0
64+
```
65+
66+
The function has the following parameters:
67+
68+
- **arrays**: array-like object containing a one-dimensional input ndarray.
69+
70+
</section>
71+
72+
<!-- /.usage -->
73+
74+
<section class="notes">
75+
76+
## Notes
77+
78+
- If provided an empty one-dimensional ndarray, the function returns `NaN`.
79+
- The midrange is a simple measure of central tendency and is sensitive to outliers because it depends only on extremes..
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<section class="examples">
86+
87+
## Examples
88+
89+
<!-- eslint no-undef: "error" -->
90+
91+
```javascript
92+
var uniform = require( '@stdlib/random/base/uniform' );
93+
var filledarrayBy = require( '@stdlib/array/filled-by' );
94+
var bernoulli = require( '@stdlib/random/base/bernoulli' );
95+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
96+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
97+
var dmidrange = require( '@stdlib/stats/base/ndarray/dmidrange' );
98+
99+
function rand() {
100+
if ( bernoulli( 0.8 ) === 1 ) {
101+
return NaN;
102+
}
103+
return uniform( -50.0, 50.0 );
104+
}
105+
106+
var xbuf = filledarrayBy( 10, 'float64', rand );
107+
var x = new ndarray( 'float64', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
108+
console.log( ndarray2array( x ) );
109+
110+
var v = dmidrange( [ x ] );
111+
console.log( v );
112+
```
113+
114+
</section>
115+
116+
<!-- /.examples -->
117+
118+
<section class="references">
119+
120+
</section>
121+
122+
<!-- /.references -->
123+
124+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
125+
126+
<section class="related">
127+
128+
</section>
129+
130+
<!-- /.related -->
131+
132+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
133+
134+
<section class="links">
135+
136+
[midrange]: https://en.wikipedia.org/wiki/Mid-range
137+
138+
</section>
139+
140+
<!-- /.links -->
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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 dmidrange = require( './../lib' );
32+
33+
34+
// FUNCTIONS //
35+
36+
/**
37+
* Returns a random number (80% chance of returning a `NaN`).
38+
*
39+
* @private
40+
* @returns {number} random number or `NaN`
41+
*/
42+
function rand() {
43+
if ( bernoulli( 0.8 ) === 1 ) {
44+
return NaN;
45+
}
46+
return uniform( -10.0, 10.0 );
47+
}
48+
49+
/**
50+
* Creates a benchmark function.
51+
*
52+
* @private
53+
* @param {PositiveInteger} len - array length
54+
* @returns {Function} benchmark function
55+
*/
56+
function createBenchmark( len ) {
57+
var xbuf;
58+
var x;
59+
60+
xbuf = filledarrayBy( len, 'float64', rand );
61+
x = new ndarray( 'float64', xbuf, [ len ], [ 1 ], 0, 'row-major' );
62+
63+
return benchmark;
64+
65+
function benchmark( b ) {
66+
var v;
67+
var i;
68+
69+
b.tic();
70+
for ( i = 0; i < b.iterations; i++ ) {
71+
v = dmidrange( [ x ] );
72+
}
73+
b.toc();
74+
if ( isnan( v ) ) {
75+
b.fail( 'should not return NaN' );
76+
}
77+
b.pass( 'benchmark finished' );
78+
b.end();
79+
}
80+
}
81+
82+
83+
// MAIN //
84+
85+
/**
86+
* Main execution sequence.
87+
*
88+
* @private
89+
*/
90+
function main() {
91+
var len;
92+
var min;
93+
var max;
94+
var f;
95+
var i;
96+
97+
min = 1; // 10^min
98+
max = 6; // 10^max
99+
100+
for ( i = min; i <= max; i++ ) {
101+
len = pow( 10, i );
102+
f = createBenchmark( len );
103+
bench( pkg+':len='+len, f );
104+
}
105+
}
106+
107+
main();
Lines changed: 42 additions & 0 deletions
Loading
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( arrays )
3+
Computes the midrange of a one-dimensional double-precision floating-
4+
point ndarray, ignoring `NaN` values.
5+
6+
If provided an empty ndarray, the function returns `NaN`.
7+
8+
Parameters
9+
----------
10+
arrays: ArrayLikeObject<ndarray>
11+
Array-like object containing a one-dimensional input ndarray.
12+
13+
Returns
14+
-------
15+
out: number
16+
Midrange value.
17+
18+
Examples
19+
--------
20+
> var xbuf = new {{alias:@stdlib/array/float64}}( [ 1.0, -2.0, 2.0, 4.0 ] );
21+
> var dt = 'float64';
22+
> var sh = [ xbuf.length ];
23+
> var sx = [ 1 ];
24+
> var ox = 0;
25+
> var ord = 'row-major';
26+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
27+
> {{alias}}( [ x ] )
28+
1.0
29+
30+
See Also
31+
--------
32+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { float64ndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Computes the midrange value of a one-dimensional double-precision floating-point ndarray, ignoring `NaN` values.
27+
*
28+
* @param arrays - array-like object containing an input ndarray
29+
* @returns midrange value
30+
*
31+
* @example
32+
* var Float64Array = require( '@stdlib/array/float64' );
33+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
34+
*
35+
* var xbuf = new Float64Array( [ 1.0, -2.0, 2.0, 4.0 ] );
36+
* var x = new ndarray( 'float64', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
37+
*
38+
* var v = dmidrange( [ x ] );
39+
* // returns 1.0
40+
*/
41+
declare function dmidrange( arrays: [ float64ndarray ] ): number;
42+
43+
44+
// EXPORTS //
45+
46+
export = dmidrange;

0 commit comments

Comments
 (0)